1 // i386.cc -- i386 target support for gold. 2 3 // Copyright 2006, 2007, 2008, 2009, 2010, 2011, 2012 4 // Free Software Foundation, Inc. 5 // Written by Ian Lance Taylor <iant@google.com>. 6 7 // This file is part of gold. 8 9 // This program is free software; you can redistribute it and/or modify 10 // it under the terms of the GNU General Public License as published by 11 // the Free Software Foundation; either version 3 of the License, or 12 // (at your option) any later version. 13 14 // This program is distributed in the hope that it will be useful, 15 // but WITHOUT ANY WARRANTY; without even the implied warranty of 16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 // GNU General Public License for more details. 18 19 // You should have received a copy of the GNU General Public License 20 // along with this program; if not, write to the Free Software 21 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, 22 // MA 02110-1301, USA. 23 24 #include "gold.h" 25 26 #include <cstring> 27 28 #include "elfcpp.h" 29 #include "dwarf.h" 30 #include "parameters.h" 31 #include "reloc.h" 32 #include "i386.h" 33 #include "object.h" 34 #include "symtab.h" 35 #include "layout.h" 36 #include "output.h" 37 #include "copy-relocs.h" 38 #include "target.h" 39 #include "target-reloc.h" 40 #include "target-select.h" 41 #include "tls.h" 42 #include "freebsd.h" 43 #include "nacl.h" 44 #include "gc.h" 45 46 namespace 47 { 48 49 using namespace gold; 50 51 // A class to handle the PLT data. 52 // This is an abstract base class that handles most of the linker details 53 // but does not know the actual contents of PLT entries. The derived 54 // classes below fill in those details. 55 56 class Output_data_plt_i386 : public Output_section_data 57 { 58 public: 59 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section; 60 61 Output_data_plt_i386(Layout*, uint64_t addralign, 62 Output_data_space*, Output_data_space*); 63 64 // Add an entry to the PLT. 65 void 66 add_entry(Symbol_table*, Layout*, Symbol* gsym); 67 68 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. 69 unsigned int 70 add_local_ifunc_entry(Symbol_table*, Layout*, 71 Sized_relobj_file<32, false>* relobj, 72 unsigned int local_sym_index); 73 74 // Return the .rel.plt section data. 75 Reloc_section* 76 rel_plt() const 77 { return this->rel_; } 78 79 // Return where the TLS_DESC relocations should go. 80 Reloc_section* 81 rel_tls_desc(Layout*); 82 83 // Return where the IRELATIVE relocations should go. 84 Reloc_section* 85 rel_irelative(Symbol_table*, Layout*); 86 87 // Return whether we created a section for IRELATIVE relocations. 88 bool 89 has_irelative_section() const 90 { return this->irelative_rel_ != NULL; } 91 92 // Return the number of PLT entries. 93 unsigned int 94 entry_count() const 95 { return this->count_ + this->irelative_count_; } 96 97 // Return the offset of the first non-reserved PLT entry. 98 unsigned int 99 first_plt_entry_offset() 100 { return this->get_plt_entry_size(); } 101 102 // Return the size of a PLT entry. 103 unsigned int 104 get_plt_entry_size() const 105 { return this->do_get_plt_entry_size(); } 106 107 // Return the PLT address to use for a global symbol. 108 uint64_t 109 address_for_global(const Symbol*); 110 111 // Return the PLT address to use for a local symbol. 112 uint64_t 113 address_for_local(const Relobj*, unsigned int symndx); 114 115 // Add .eh_frame information for the PLT. 116 void 117 add_eh_frame(Layout* layout) 118 { this->do_add_eh_frame(layout); } 119 120 protected: 121 // Fill the first PLT entry, given the pointer to the PLT section data 122 // and the runtime address of the GOT. 123 void 124 fill_first_plt_entry(unsigned char* pov, 125 elfcpp::Elf_types<32>::Elf_Addr got_address) 126 { this->do_fill_first_plt_entry(pov, got_address); } 127 128 // Fill a normal PLT entry, given the pointer to the entry's data in the 129 // section, the runtime address of the GOT, the offset into the GOT of 130 // the corresponding slot, the offset into the relocation section of the 131 // corresponding reloc, and the offset of this entry within the whole 132 // PLT. Return the offset from this PLT entry's runtime address that 133 // should be used to compute the initial value of the GOT slot. 134 unsigned int 135 fill_plt_entry(unsigned char* pov, 136 elfcpp::Elf_types<32>::Elf_Addr got_address, 137 unsigned int got_offset, 138 unsigned int plt_offset, 139 unsigned int plt_rel_offset) 140 { 141 return this->do_fill_plt_entry(pov, got_address, got_offset, 142 plt_offset, plt_rel_offset); 143 } 144 145 virtual unsigned int 146 do_get_plt_entry_size() const = 0; 147 148 virtual void 149 do_fill_first_plt_entry(unsigned char* pov, 150 elfcpp::Elf_types<32>::Elf_Addr got_address) = 0; 151 152 virtual unsigned int 153 do_fill_plt_entry(unsigned char* pov, 154 elfcpp::Elf_types<32>::Elf_Addr got_address, 155 unsigned int got_offset, 156 unsigned int plt_offset, 157 unsigned int plt_rel_offset) = 0; 158 159 virtual void 160 do_add_eh_frame(Layout*) = 0; 161 162 void 163 do_adjust_output_section(Output_section* os); 164 165 // Write to a map file. 166 void 167 do_print_to_mapfile(Mapfile* mapfile) const 168 { mapfile->print_output_data(this, _("** PLT")); } 169 170 // The .eh_frame unwind information for the PLT. 171 // The CIE is common across variants of the PLT format. 172 static const int plt_eh_frame_cie_size = 16; 173 static const unsigned char plt_eh_frame_cie[plt_eh_frame_cie_size]; 174 175 private: 176 // Set the final size. 177 void 178 set_final_data_size() 179 { 180 this->set_data_size((this->count_ + this->irelative_count_ + 1) 181 * this->get_plt_entry_size()); 182 } 183 184 // Write out the PLT data. 185 void 186 do_write(Output_file*); 187 188 // We keep a list of global STT_GNU_IFUNC symbols, each with its 189 // offset in the GOT. 190 struct Global_ifunc 191 { 192 Symbol* sym; 193 unsigned int got_offset; 194 }; 195 196 // We keep a list of local STT_GNU_IFUNC symbols, each with its 197 // offset in the GOT. 198 struct Local_ifunc 199 { 200 Sized_relobj_file<32, false>* object; 201 unsigned int local_sym_index; 202 unsigned int got_offset; 203 }; 204 205 // A pointer to the Layout class, so that we can find the .dynamic 206 // section when we write out the GOT PLT section. 207 Layout* layout_; 208 // The reloc section. 209 Reloc_section* rel_; 210 // The TLS_DESC relocations, if necessary. These must follow the 211 // regular PLT relocs. 212 Reloc_section* tls_desc_rel_; 213 // The IRELATIVE relocations, if necessary. These must follow the 214 // regular relocatoins and the TLS_DESC relocations. 215 Reloc_section* irelative_rel_; 216 // The .got.plt section. 217 Output_data_space* got_plt_; 218 // The part of the .got.plt section used for IRELATIVE relocs. 219 Output_data_space* got_irelative_; 220 // The number of PLT entries. 221 unsigned int count_; 222 // Number of PLT entries with R_386_IRELATIVE relocs. These follow 223 // the regular PLT entries. 224 unsigned int irelative_count_; 225 // Global STT_GNU_IFUNC symbols. 226 std::vector<Global_ifunc> global_ifuncs_; 227 // Local STT_GNU_IFUNC symbols. 228 std::vector<Local_ifunc> local_ifuncs_; 229 }; 230 231 // This is an abstract class for the standard PLT layout. 232 // The derived classes below handle the actual PLT contents 233 // for the executable (non-PIC) and shared-library (PIC) cases. 234 // The unwind information is uniform across those two, so it's here. 235 236 class Output_data_plt_i386_standard : public Output_data_plt_i386 237 { 238 public: 239 Output_data_plt_i386_standard(Layout* layout, 240 Output_data_space* got_plt, 241 Output_data_space* got_irelative) 242 : Output_data_plt_i386(layout, plt_entry_size, got_plt, got_irelative) 243 { } 244 245 protected: 246 virtual unsigned int 247 do_get_plt_entry_size() const 248 { return plt_entry_size; } 249 250 virtual void 251 do_add_eh_frame(Layout* layout) 252 { 253 layout->add_eh_frame_for_plt(this, plt_eh_frame_cie, plt_eh_frame_cie_size, 254 plt_eh_frame_fde, plt_eh_frame_fde_size); 255 } 256 257 // The size of an entry in the PLT. 258 static const int plt_entry_size = 16; 259 260 // The .eh_frame unwind information for the PLT. 261 static const int plt_eh_frame_fde_size = 32; 262 static const unsigned char plt_eh_frame_fde[plt_eh_frame_fde_size]; 263 }; 264 265 // Actually fill the PLT contents for an executable (non-PIC). 266 267 class Output_data_plt_i386_exec : public Output_data_plt_i386_standard 268 { 269 public: 270 Output_data_plt_i386_exec(Layout* layout, 271 Output_data_space* got_plt, 272 Output_data_space* got_irelative) 273 : Output_data_plt_i386_standard(layout, got_plt, got_irelative) 274 { } 275 276 protected: 277 virtual void 278 do_fill_first_plt_entry(unsigned char* pov, 279 elfcpp::Elf_types<32>::Elf_Addr got_address); 280 281 virtual unsigned int 282 do_fill_plt_entry(unsigned char* pov, 283 elfcpp::Elf_types<32>::Elf_Addr got_address, 284 unsigned int got_offset, 285 unsigned int plt_offset, 286 unsigned int plt_rel_offset); 287 288 private: 289 // The first entry in the PLT for an executable. 290 static const unsigned char first_plt_entry[plt_entry_size]; 291 292 // Other entries in the PLT for an executable. 293 static const unsigned char plt_entry[plt_entry_size]; 294 }; 295 296 // Actually fill the PLT contents for a shared library (PIC). 297 298 class Output_data_plt_i386_dyn : public Output_data_plt_i386_standard 299 { 300 public: 301 Output_data_plt_i386_dyn(Layout* layout, 302 Output_data_space* got_plt, 303 Output_data_space* got_irelative) 304 : Output_data_plt_i386_standard(layout, got_plt, got_irelative) 305 { } 306 307 protected: 308 virtual void 309 do_fill_first_plt_entry(unsigned char* pov, elfcpp::Elf_types<32>::Elf_Addr); 310 311 virtual unsigned int 312 do_fill_plt_entry(unsigned char* pov, 313 elfcpp::Elf_types<32>::Elf_Addr, 314 unsigned int got_offset, 315 unsigned int plt_offset, 316 unsigned int plt_rel_offset); 317 318 private: 319 // The first entry in the PLT for a shared object. 320 static const unsigned char first_plt_entry[plt_entry_size]; 321 322 // Other entries in the PLT for a shared object. 323 static const unsigned char plt_entry[plt_entry_size]; 324 }; 325 326 // The i386 target class. 327 // TLS info comes from 328 // http://people.redhat.com/drepper/tls.pdf 329 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt 330 331 class Target_i386 : public Sized_target<32, false> 332 { 333 public: 334 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section; 335 336 Target_i386(const Target::Target_info* info = &i386_info) 337 : Sized_target<32, false>(info), 338 got_(NULL), plt_(NULL), got_plt_(NULL), got_irelative_(NULL), 339 got_tlsdesc_(NULL), global_offset_table_(NULL), rel_dyn_(NULL), 340 rel_irelative_(NULL), copy_relocs_(elfcpp::R_386_COPY), dynbss_(NULL), 341 got_mod_index_offset_(-1U), tls_base_symbol_defined_(false) 342 { } 343 344 // Process the relocations to determine unreferenced sections for 345 // garbage collection. 346 void 347 gc_process_relocs(Symbol_table* symtab, 348 Layout* layout, 349 Sized_relobj_file<32, false>* object, 350 unsigned int data_shndx, 351 unsigned int sh_type, 352 const unsigned char* prelocs, 353 size_t reloc_count, 354 Output_section* output_section, 355 bool needs_special_offset_handling, 356 size_t local_symbol_count, 357 const unsigned char* plocal_symbols); 358 359 // Scan the relocations to look for symbol adjustments. 360 void 361 scan_relocs(Symbol_table* symtab, 362 Layout* layout, 363 Sized_relobj_file<32, false>* object, 364 unsigned int data_shndx, 365 unsigned int sh_type, 366 const unsigned char* prelocs, 367 size_t reloc_count, 368 Output_section* output_section, 369 bool needs_special_offset_handling, 370 size_t local_symbol_count, 371 const unsigned char* plocal_symbols); 372 373 // Finalize the sections. 374 void 375 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*); 376 377 // Return the value to use for a dynamic which requires special 378 // treatment. 379 uint64_t 380 do_dynsym_value(const Symbol*) const; 381 382 // Relocate a section. 383 void 384 relocate_section(const Relocate_info<32, false>*, 385 unsigned int sh_type, 386 const unsigned char* prelocs, 387 size_t reloc_count, 388 Output_section* output_section, 389 bool needs_special_offset_handling, 390 unsigned char* view, 391 elfcpp::Elf_types<32>::Elf_Addr view_address, 392 section_size_type view_size, 393 const Reloc_symbol_changes*); 394 395 // Scan the relocs during a relocatable link. 396 void 397 scan_relocatable_relocs(Symbol_table* symtab, 398 Layout* layout, 399 Sized_relobj_file<32, false>* object, 400 unsigned int data_shndx, 401 unsigned int sh_type, 402 const unsigned char* prelocs, 403 size_t reloc_count, 404 Output_section* output_section, 405 bool needs_special_offset_handling, 406 size_t local_symbol_count, 407 const unsigned char* plocal_symbols, 408 Relocatable_relocs*); 409 410 // Relocate a section during a relocatable link. 411 void 412 relocate_for_relocatable(const Relocate_info<32, false>*, 413 unsigned int sh_type, 414 const unsigned char* prelocs, 415 size_t reloc_count, 416 Output_section* output_section, 417 elfcpp::Elf_types<32>::Elf_Off 418 offset_in_output_section, 419 const Relocatable_relocs*, 420 unsigned char* view, 421 elfcpp::Elf_types<32>::Elf_Addr view_address, 422 section_size_type view_size, 423 unsigned char* reloc_view, 424 section_size_type reloc_view_size); 425 426 // Return a string used to fill a code section with nops. 427 std::string 428 do_code_fill(section_size_type length) const; 429 430 // Return whether SYM is defined by the ABI. 431 bool 432 do_is_defined_by_abi(const Symbol* sym) const 433 { return strcmp(sym->name(), "___tls_get_addr") == 0; } 434 435 // Return whether a symbol name implies a local label. The UnixWare 436 // 2.1 cc generates temporary symbols that start with .X, so we 437 // recognize them here. FIXME: do other SVR4 compilers also use .X?. 438 // If so, we should move the .X recognition into 439 // Target::do_is_local_label_name. 440 bool 441 do_is_local_label_name(const char* name) const 442 { 443 if (name[0] == '.' && name[1] == 'X') 444 return true; 445 return Target::do_is_local_label_name(name); 446 } 447 448 // Return the PLT address to use for a global symbol. 449 uint64_t 450 do_plt_address_for_global(const Symbol* gsym) const 451 { return this->plt_section()->address_for_global(gsym); } 452 453 uint64_t 454 do_plt_address_for_local(const Relobj* relobj, unsigned int symndx) const 455 { return this->plt_section()->address_for_local(relobj, symndx); } 456 457 // We can tell whether we take the address of a function. 458 inline bool 459 do_can_check_for_function_pointers() const 460 { return true; } 461 462 // Return the base for a DW_EH_PE_datarel encoding. 463 uint64_t 464 do_ehframe_datarel_base() const; 465 466 // Return whether SYM is call to a non-split function. 467 bool 468 do_is_call_to_non_split(const Symbol* sym, unsigned int) const; 469 470 // Adjust -fsplit-stack code which calls non-split-stack code. 471 void 472 do_calls_non_split(Relobj* object, unsigned int shndx, 473 section_offset_type fnoffset, section_size_type fnsize, 474 unsigned char* view, section_size_type view_size, 475 std::string* from, std::string* to) const; 476 477 // Return the size of the GOT section. 478 section_size_type 479 got_size() const 480 { 481 gold_assert(this->got_ != NULL); 482 return this->got_->data_size(); 483 } 484 485 // Return the number of entries in the GOT. 486 unsigned int 487 got_entry_count() const 488 { 489 if (this->got_ == NULL) 490 return 0; 491 return this->got_size() / 4; 492 } 493 494 // Return the number of entries in the PLT. 495 unsigned int 496 plt_entry_count() const; 497 498 // Return the offset of the first non-reserved PLT entry. 499 unsigned int 500 first_plt_entry_offset() const; 501 502 // Return the size of each PLT entry. 503 unsigned int 504 plt_entry_size() const; 505 506 protected: 507 // Instantiate the plt_ member. 508 // This chooses the right PLT flavor for an executable or a shared object. 509 Output_data_plt_i386* 510 make_data_plt(Layout* layout, 511 Output_data_space* got_plt, 512 Output_data_space* got_irelative, 513 bool dyn) 514 { return this->do_make_data_plt(layout, got_plt, got_irelative, dyn); } 515 516 virtual Output_data_plt_i386* 517 do_make_data_plt(Layout* layout, 518 Output_data_space* got_plt, 519 Output_data_space* got_irelative, 520 bool dyn) 521 { 522 if (dyn) 523 return new Output_data_plt_i386_dyn(layout, got_plt, got_irelative); 524 else 525 return new Output_data_plt_i386_exec(layout, got_plt, got_irelative); 526 } 527 528 private: 529 // The class which scans relocations. 530 struct Scan 531 { 532 static inline int 533 534 get_reference_flags(unsigned int r_type); 535 536 inline void 537 local(Symbol_table* symtab, Layout* layout, Target_i386* target, 538 Sized_relobj_file<32, false>* object, 539 unsigned int data_shndx, 540 Output_section* output_section, 541 const elfcpp::Rel<32, false>& reloc, unsigned int r_type, 542 const elfcpp::Sym<32, false>& lsym); 543 544 inline void 545 global(Symbol_table* symtab, Layout* layout, Target_i386* target, 546 Sized_relobj_file<32, false>* object, 547 unsigned int data_shndx, 548 Output_section* output_section, 549 const elfcpp::Rel<32, false>& reloc, unsigned int r_type, 550 Symbol* gsym); 551 552 inline bool 553 local_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout, 554 Target_i386* target, 555 Sized_relobj_file<32, false>* object, 556 unsigned int data_shndx, 557 Output_section* output_section, 558 const elfcpp::Rel<32, false>& reloc, 559 unsigned int r_type, 560 const elfcpp::Sym<32, false>& lsym); 561 562 inline bool 563 global_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout, 564 Target_i386* target, 565 Sized_relobj_file<32, false>* object, 566 unsigned int data_shndx, 567 Output_section* output_section, 568 const elfcpp::Rel<32, false>& reloc, 569 unsigned int r_type, 570 Symbol* gsym); 571 572 inline bool 573 possible_function_pointer_reloc(unsigned int r_type); 574 575 bool 576 reloc_needs_plt_for_ifunc(Sized_relobj_file<32, false>*, 577 unsigned int r_type); 578 579 static void 580 unsupported_reloc_local(Sized_relobj_file<32, false>*, unsigned int r_type); 581 582 static void 583 unsupported_reloc_global(Sized_relobj_file<32, false>*, unsigned int r_type, 584 Symbol*); 585 }; 586 587 // The class which implements relocation. 588 class Relocate 589 { 590 public: 591 Relocate() 592 : skip_call_tls_get_addr_(false), 593 local_dynamic_type_(LOCAL_DYNAMIC_NONE) 594 { } 595 596 ~Relocate() 597 { 598 if (this->skip_call_tls_get_addr_) 599 { 600 // FIXME: This needs to specify the location somehow. 601 gold_error(_("missing expected TLS relocation")); 602 } 603 } 604 605 // Return whether the static relocation needs to be applied. 606 inline bool 607 should_apply_static_reloc(const Sized_symbol<32>* gsym, 608 unsigned int r_type, 609 bool is_32bit, 610 Output_section* output_section); 611 612 // Do a relocation. Return false if the caller should not issue 613 // any warnings about this relocation. 614 inline bool 615 relocate(const Relocate_info<32, false>*, Target_i386*, Output_section*, 616 size_t relnum, const elfcpp::Rel<32, false>&, 617 unsigned int r_type, const Sized_symbol<32>*, 618 const Symbol_value<32>*, 619 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr, 620 section_size_type); 621 622 private: 623 // Do a TLS relocation. 624 inline void 625 relocate_tls(const Relocate_info<32, false>*, Target_i386* target, 626 size_t relnum, const elfcpp::Rel<32, false>&, 627 unsigned int r_type, const Sized_symbol<32>*, 628 const Symbol_value<32>*, 629 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr, 630 section_size_type); 631 632 // Do a TLS General-Dynamic to Initial-Exec transition. 633 inline void 634 tls_gd_to_ie(const Relocate_info<32, false>*, size_t relnum, 635 Output_segment* tls_segment, 636 const elfcpp::Rel<32, false>&, unsigned int r_type, 637 elfcpp::Elf_types<32>::Elf_Addr value, 638 unsigned char* view, 639 section_size_type view_size); 640 641 // Do a TLS General-Dynamic to Local-Exec transition. 642 inline void 643 tls_gd_to_le(const Relocate_info<32, false>*, size_t relnum, 644 Output_segment* tls_segment, 645 const elfcpp::Rel<32, false>&, unsigned int r_type, 646 elfcpp::Elf_types<32>::Elf_Addr value, 647 unsigned char* view, 648 section_size_type view_size); 649 650 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Initial-Exec 651 // transition. 652 inline void 653 tls_desc_gd_to_ie(const Relocate_info<32, false>*, size_t relnum, 654 Output_segment* tls_segment, 655 const elfcpp::Rel<32, false>&, unsigned int r_type, 656 elfcpp::Elf_types<32>::Elf_Addr value, 657 unsigned char* view, 658 section_size_type view_size); 659 660 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Local-Exec 661 // transition. 662 inline void 663 tls_desc_gd_to_le(const Relocate_info<32, false>*, size_t relnum, 664 Output_segment* tls_segment, 665 const elfcpp::Rel<32, false>&, unsigned int r_type, 666 elfcpp::Elf_types<32>::Elf_Addr value, 667 unsigned char* view, 668 section_size_type view_size); 669 670 // Do a TLS Local-Dynamic to Local-Exec transition. 671 inline void 672 tls_ld_to_le(const Relocate_info<32, false>*, size_t relnum, 673 Output_segment* tls_segment, 674 const elfcpp::Rel<32, false>&, unsigned int r_type, 675 elfcpp::Elf_types<32>::Elf_Addr value, 676 unsigned char* view, 677 section_size_type view_size); 678 679 // Do a TLS Initial-Exec to Local-Exec transition. 680 static inline void 681 tls_ie_to_le(const Relocate_info<32, false>*, size_t relnum, 682 Output_segment* tls_segment, 683 const elfcpp::Rel<32, false>&, unsigned int r_type, 684 elfcpp::Elf_types<32>::Elf_Addr value, 685 unsigned char* view, 686 section_size_type view_size); 687 688 // We need to keep track of which type of local dynamic relocation 689 // we have seen, so that we can optimize R_386_TLS_LDO_32 correctly. 690 enum Local_dynamic_type 691 { 692 LOCAL_DYNAMIC_NONE, 693 LOCAL_DYNAMIC_SUN, 694 LOCAL_DYNAMIC_GNU 695 }; 696 697 // This is set if we should skip the next reloc, which should be a 698 // PLT32 reloc against ___tls_get_addr. 699 bool skip_call_tls_get_addr_; 700 // The type of local dynamic relocation we have seen in the section 701 // being relocated, if any. 702 Local_dynamic_type local_dynamic_type_; 703 }; 704 705 // A class which returns the size required for a relocation type, 706 // used while scanning relocs during a relocatable link. 707 class Relocatable_size_for_reloc 708 { 709 public: 710 unsigned int 711 get_size_for_reloc(unsigned int, Relobj*); 712 }; 713 714 // Adjust TLS relocation type based on the options and whether this 715 // is a local symbol. 716 static tls::Tls_optimization 717 optimize_tls_reloc(bool is_final, int r_type); 718 719 // Get the GOT section, creating it if necessary. 720 Output_data_got<32, false>* 721 got_section(Symbol_table*, Layout*); 722 723 // Get the GOT PLT section. 724 Output_data_space* 725 got_plt_section() const 726 { 727 gold_assert(this->got_plt_ != NULL); 728 return this->got_plt_; 729 } 730 731 // Get the GOT section for TLSDESC entries. 732 Output_data_got<32, false>* 733 got_tlsdesc_section() const 734 { 735 gold_assert(this->got_tlsdesc_ != NULL); 736 return this->got_tlsdesc_; 737 } 738 739 // Create the PLT section. 740 void 741 make_plt_section(Symbol_table* symtab, Layout* layout); 742 743 // Create a PLT entry for a global symbol. 744 void 745 make_plt_entry(Symbol_table*, Layout*, Symbol*); 746 747 // Create a PLT entry for a local STT_GNU_IFUNC symbol. 748 void 749 make_local_ifunc_plt_entry(Symbol_table*, Layout*, 750 Sized_relobj_file<32, false>* relobj, 751 unsigned int local_sym_index); 752 753 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment. 754 void 755 define_tls_base_symbol(Symbol_table*, Layout*); 756 757 // Create a GOT entry for the TLS module index. 758 unsigned int 759 got_mod_index_entry(Symbol_table* symtab, Layout* layout, 760 Sized_relobj_file<32, false>* object); 761 762 // Get the PLT section. 763 Output_data_plt_i386* 764 plt_section() const 765 { 766 gold_assert(this->plt_ != NULL); 767 return this->plt_; 768 } 769 770 // Get the dynamic reloc section, creating it if necessary. 771 Reloc_section* 772 rel_dyn_section(Layout*); 773 774 // Get the section to use for TLS_DESC relocations. 775 Reloc_section* 776 rel_tls_desc_section(Layout*) const; 777 778 // Get the section to use for IRELATIVE relocations. 779 Reloc_section* 780 rel_irelative_section(Layout*); 781 782 // Add a potential copy relocation. 783 void 784 copy_reloc(Symbol_table* symtab, Layout* layout, 785 Sized_relobj_file<32, false>* object, 786 unsigned int shndx, Output_section* output_section, 787 Symbol* sym, const elfcpp::Rel<32, false>& reloc) 788 { 789 this->copy_relocs_.copy_reloc(symtab, layout, 790 symtab->get_sized_symbol<32>(sym), 791 object, shndx, output_section, reloc, 792 this->rel_dyn_section(layout)); 793 } 794 795 // Information about this specific target which we pass to the 796 // general Target structure. 797 static const Target::Target_info i386_info; 798 799 // The types of GOT entries needed for this platform. 800 // These values are exposed to the ABI in an incremental link. 801 // Do not renumber existing values without changing the version 802 // number of the .gnu_incremental_inputs section. 803 enum Got_type 804 { 805 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol 806 GOT_TYPE_TLS_NOFFSET = 1, // GOT entry for negative TLS offset 807 GOT_TYPE_TLS_OFFSET = 2, // GOT entry for positive TLS offset 808 GOT_TYPE_TLS_PAIR = 3, // GOT entry for TLS module/offset pair 809 GOT_TYPE_TLS_DESC = 4 // GOT entry for TLS_DESC pair 810 }; 811 812 // The GOT section. 813 Output_data_got<32, false>* got_; 814 // The PLT section. 815 Output_data_plt_i386* plt_; 816 // The GOT PLT section. 817 Output_data_space* got_plt_; 818 // The GOT section for IRELATIVE relocations. 819 Output_data_space* got_irelative_; 820 // The GOT section for TLSDESC relocations. 821 Output_data_got<32, false>* got_tlsdesc_; 822 // The _GLOBAL_OFFSET_TABLE_ symbol. 823 Symbol* global_offset_table_; 824 // The dynamic reloc section. 825 Reloc_section* rel_dyn_; 826 // The section to use for IRELATIVE relocs. 827 Reloc_section* rel_irelative_; 828 // Relocs saved to avoid a COPY reloc. 829 Copy_relocs<elfcpp::SHT_REL, 32, false> copy_relocs_; 830 // Space for variables copied with a COPY reloc. 831 Output_data_space* dynbss_; 832 // Offset of the GOT entry for the TLS module index. 833 unsigned int got_mod_index_offset_; 834 // True if the _TLS_MODULE_BASE_ symbol has been defined. 835 bool tls_base_symbol_defined_; 836 }; 837 838 const Target::Target_info Target_i386::i386_info = 839 { 840 32, // size 841 false, // is_big_endian 842 elfcpp::EM_386, // machine_code 843 false, // has_make_symbol 844 false, // has_resolve 845 true, // has_code_fill 846 true, // is_default_stack_executable 847 true, // can_icf_inline_merge_sections 848 '\0', // wrap_char 849 "/usr/lib/libc.so.1", // dynamic_linker 850 0x08048000, // default_text_segment_address 851 0x1000, // abi_pagesize (overridable by -z max-page-size) 852 0x1000, // common_pagesize (overridable by -z common-page-size) 853 false, // isolate_execinstr 854 0, // rosegment_gap 855 elfcpp::SHN_UNDEF, // small_common_shndx 856 elfcpp::SHN_UNDEF, // large_common_shndx 857 0, // small_common_section_flags 858 0, // large_common_section_flags 859 NULL, // attributes_section 860 NULL // attributes_vendor 861 }; 862 863 // Get the GOT section, creating it if necessary. 864 865 Output_data_got<32, false>* 866 Target_i386::got_section(Symbol_table* symtab, Layout* layout) 867 { 868 if (this->got_ == NULL) 869 { 870 gold_assert(symtab != NULL && layout != NULL); 871 872 this->got_ = new Output_data_got<32, false>(); 873 874 // When using -z now, we can treat .got.plt as a relro section. 875 // Without -z now, it is modified after program startup by lazy 876 // PLT relocations. 877 bool is_got_plt_relro = parameters->options().now(); 878 Output_section_order got_order = (is_got_plt_relro 879 ? ORDER_RELRO 880 : ORDER_RELRO_LAST); 881 Output_section_order got_plt_order = (is_got_plt_relro 882 ? ORDER_RELRO 883 : ORDER_NON_RELRO_FIRST); 884 885 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS, 886 (elfcpp::SHF_ALLOC 887 | elfcpp::SHF_WRITE), 888 this->got_, got_order, true); 889 890 this->got_plt_ = new Output_data_space(4, "** GOT PLT"); 891 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS, 892 (elfcpp::SHF_ALLOC 893 | elfcpp::SHF_WRITE), 894 this->got_plt_, got_plt_order, 895 is_got_plt_relro); 896 897 // The first three entries are reserved. 898 this->got_plt_->set_current_data_size(3 * 4); 899 900 if (!is_got_plt_relro) 901 { 902 // Those bytes can go into the relro segment. 903 layout->increase_relro(3 * 4); 904 } 905 906 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT. 907 this->global_offset_table_ = 908 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL, 909 Symbol_table::PREDEFINED, 910 this->got_plt_, 911 0, 0, elfcpp::STT_OBJECT, 912 elfcpp::STB_LOCAL, 913 elfcpp::STV_HIDDEN, 0, 914 false, false); 915 916 // If there are any IRELATIVE relocations, they get GOT entries 917 // in .got.plt after the jump slot relocations. 918 this->got_irelative_ = new Output_data_space(4, "** GOT IRELATIVE PLT"); 919 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS, 920 (elfcpp::SHF_ALLOC 921 | elfcpp::SHF_WRITE), 922 this->got_irelative_, 923 got_plt_order, is_got_plt_relro); 924 925 // If there are any TLSDESC relocations, they get GOT entries in 926 // .got.plt after the jump slot entries. 927 this->got_tlsdesc_ = new Output_data_got<32, false>(); 928 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS, 929 (elfcpp::SHF_ALLOC 930 | elfcpp::SHF_WRITE), 931 this->got_tlsdesc_, 932 got_plt_order, is_got_plt_relro); 933 } 934 935 return this->got_; 936 } 937 938 // Get the dynamic reloc section, creating it if necessary. 939 940 Target_i386::Reloc_section* 941 Target_i386::rel_dyn_section(Layout* layout) 942 { 943 if (this->rel_dyn_ == NULL) 944 { 945 gold_assert(layout != NULL); 946 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc()); 947 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL, 948 elfcpp::SHF_ALLOC, this->rel_dyn_, 949 ORDER_DYNAMIC_RELOCS, false); 950 } 951 return this->rel_dyn_; 952 } 953 954 // Get the section to use for IRELATIVE relocs, creating it if 955 // necessary. These go in .rel.dyn, but only after all other dynamic 956 // relocations. They need to follow the other dynamic relocations so 957 // that they can refer to global variables initialized by those 958 // relocs. 959 960 Target_i386::Reloc_section* 961 Target_i386::rel_irelative_section(Layout* layout) 962 { 963 if (this->rel_irelative_ == NULL) 964 { 965 // Make sure we have already create the dynamic reloc section. 966 this->rel_dyn_section(layout); 967 this->rel_irelative_ = new Reloc_section(false); 968 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL, 969 elfcpp::SHF_ALLOC, this->rel_irelative_, 970 ORDER_DYNAMIC_RELOCS, false); 971 gold_assert(this->rel_dyn_->output_section() 972 == this->rel_irelative_->output_section()); 973 } 974 return this->rel_irelative_; 975 } 976 977 // Create the PLT section. The ordinary .got section is an argument, 978 // since we need to refer to the start. We also create our own .got 979 // section just for PLT entries. 980 981 Output_data_plt_i386::Output_data_plt_i386(Layout* layout, 982 uint64_t addralign, 983 Output_data_space* got_plt, 984 Output_data_space* got_irelative) 985 : Output_section_data(addralign), 986 layout_(layout), tls_desc_rel_(NULL), 987 irelative_rel_(NULL), got_plt_(got_plt), got_irelative_(got_irelative), 988 count_(0), irelative_count_(0), global_ifuncs_(), local_ifuncs_() 989 { 990 this->rel_ = new Reloc_section(false); 991 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL, 992 elfcpp::SHF_ALLOC, this->rel_, 993 ORDER_DYNAMIC_PLT_RELOCS, false); 994 } 995 996 void 997 Output_data_plt_i386::do_adjust_output_section(Output_section* os) 998 { 999 // UnixWare sets the entsize of .plt to 4, and so does the old GNU 1000 // linker, and so do we. 1001 os->set_entsize(4); 1002 } 1003 1004 // Add an entry to the PLT. 1005 1006 void 1007 Output_data_plt_i386::add_entry(Symbol_table* symtab, Layout* layout, 1008 Symbol* gsym) 1009 { 1010 gold_assert(!gsym->has_plt_offset()); 1011 1012 // Every PLT entry needs a reloc. 1013 if (gsym->type() == elfcpp::STT_GNU_IFUNC 1014 && gsym->can_use_relative_reloc(false)) 1015 { 1016 gsym->set_plt_offset(this->irelative_count_ * this->get_plt_entry_size()); 1017 ++this->irelative_count_; 1018 section_offset_type got_offset = 1019 this->got_irelative_->current_data_size(); 1020 this->got_irelative_->set_current_data_size(got_offset + 4); 1021 Reloc_section* rel = this->rel_irelative(symtab, layout); 1022 rel->add_symbolless_global_addend(gsym, elfcpp::R_386_IRELATIVE, 1023 this->got_irelative_, got_offset); 1024 struct Global_ifunc gi; 1025 gi.sym = gsym; 1026 gi.got_offset = got_offset; 1027 this->global_ifuncs_.push_back(gi); 1028 } 1029 else 1030 { 1031 // When setting the PLT offset we skip the initial reserved PLT 1032 // entry. 1033 gsym->set_plt_offset((this->count_ + 1) * this->get_plt_entry_size()); 1034 1035 ++this->count_; 1036 1037 section_offset_type got_offset = this->got_plt_->current_data_size(); 1038 1039 // Every PLT entry needs a GOT entry which points back to the 1040 // PLT entry (this will be changed by the dynamic linker, 1041 // normally lazily when the function is called). 1042 this->got_plt_->set_current_data_size(got_offset + 4); 1043 1044 gsym->set_needs_dynsym_entry(); 1045 this->rel_->add_global(gsym, elfcpp::R_386_JUMP_SLOT, this->got_plt_, 1046 got_offset); 1047 } 1048 1049 // Note that we don't need to save the symbol. The contents of the 1050 // PLT are independent of which symbols are used. The symbols only 1051 // appear in the relocations. 1052 } 1053 1054 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return 1055 // the PLT offset. 1056 1057 unsigned int 1058 Output_data_plt_i386::add_local_ifunc_entry( 1059 Symbol_table* symtab, 1060 Layout* layout, 1061 Sized_relobj_file<32, false>* relobj, 1062 unsigned int local_sym_index) 1063 { 1064 unsigned int plt_offset = this->irelative_count_ * this->get_plt_entry_size(); 1065 ++this->irelative_count_; 1066 1067 section_offset_type got_offset = this->got_irelative_->current_data_size(); 1068 1069 // Every PLT entry needs a GOT entry which points back to the PLT 1070 // entry. 1071 this->got_irelative_->set_current_data_size(got_offset + 4); 1072 1073 // Every PLT entry needs a reloc. 1074 Reloc_section* rel = this->rel_irelative(symtab, layout); 1075 rel->add_symbolless_local_addend(relobj, local_sym_index, 1076 elfcpp::R_386_IRELATIVE, 1077 this->got_irelative_, got_offset); 1078 1079 struct Local_ifunc li; 1080 li.object = relobj; 1081 li.local_sym_index = local_sym_index; 1082 li.got_offset = got_offset; 1083 this->local_ifuncs_.push_back(li); 1084 1085 return plt_offset; 1086 } 1087 1088 // Return where the TLS_DESC relocations should go, creating it if 1089 // necessary. These follow the JUMP_SLOT relocations. 1090 1091 Output_data_plt_i386::Reloc_section* 1092 Output_data_plt_i386::rel_tls_desc(Layout* layout) 1093 { 1094 if (this->tls_desc_rel_ == NULL) 1095 { 1096 this->tls_desc_rel_ = new Reloc_section(false); 1097 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL, 1098 elfcpp::SHF_ALLOC, this->tls_desc_rel_, 1099 ORDER_DYNAMIC_PLT_RELOCS, false); 1100 gold_assert(this->tls_desc_rel_->output_section() 1101 == this->rel_->output_section()); 1102 } 1103 return this->tls_desc_rel_; 1104 } 1105 1106 // Return where the IRELATIVE relocations should go in the PLT. These 1107 // follow the JUMP_SLOT and TLS_DESC relocations. 1108 1109 Output_data_plt_i386::Reloc_section* 1110 Output_data_plt_i386::rel_irelative(Symbol_table* symtab, Layout* layout) 1111 { 1112 if (this->irelative_rel_ == NULL) 1113 { 1114 // Make sure we have a place for the TLS_DESC relocations, in 1115 // case we see any later on. 1116 this->rel_tls_desc(layout); 1117 this->irelative_rel_ = new Reloc_section(false); 1118 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL, 1119 elfcpp::SHF_ALLOC, this->irelative_rel_, 1120 ORDER_DYNAMIC_PLT_RELOCS, false); 1121 gold_assert(this->irelative_rel_->output_section() 1122 == this->rel_->output_section()); 1123 1124 if (parameters->doing_static_link()) 1125 { 1126 // A statically linked executable will only have a .rel.plt 1127 // section to hold R_386_IRELATIVE relocs for STT_GNU_IFUNC 1128 // symbols. The library will use these symbols to locate 1129 // the IRELATIVE relocs at program startup time. 1130 symtab->define_in_output_data("__rel_iplt_start", NULL, 1131 Symbol_table::PREDEFINED, 1132 this->irelative_rel_, 0, 0, 1133 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL, 1134 elfcpp::STV_HIDDEN, 0, false, true); 1135 symtab->define_in_output_data("__rel_iplt_end", NULL, 1136 Symbol_table::PREDEFINED, 1137 this->irelative_rel_, 0, 0, 1138 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL, 1139 elfcpp::STV_HIDDEN, 0, true, true); 1140 } 1141 } 1142 return this->irelative_rel_; 1143 } 1144 1145 // Return the PLT address to use for a global symbol. 1146 1147 uint64_t 1148 Output_data_plt_i386::address_for_global(const Symbol* gsym) 1149 { 1150 uint64_t offset = 0; 1151 if (gsym->type() == elfcpp::STT_GNU_IFUNC 1152 && gsym->can_use_relative_reloc(false)) 1153 offset = (this->count_ + 1) * this->get_plt_entry_size(); 1154 return this->address() + offset; 1155 } 1156 1157 // Return the PLT address to use for a local symbol. These are always 1158 // IRELATIVE relocs. 1159 1160 uint64_t 1161 Output_data_plt_i386::address_for_local(const Relobj*, unsigned int) 1162 { 1163 return this->address() + (this->count_ + 1) * this->get_plt_entry_size(); 1164 } 1165 1166 // The first entry in the PLT for an executable. 1167 1168 const unsigned char Output_data_plt_i386_exec::first_plt_entry[plt_entry_size] = 1169 { 1170 0xff, 0x35, // pushl contents of memory address 1171 0, 0, 0, 0, // replaced with address of .got + 4 1172 0xff, 0x25, // jmp indirect 1173 0, 0, 0, 0, // replaced with address of .got + 8 1174 0, 0, 0, 0 // unused 1175 }; 1176 1177 void 1178 Output_data_plt_i386_exec::do_fill_first_plt_entry( 1179 unsigned char* pov, 1180 elfcpp::Elf_types<32>::Elf_Addr got_address) 1181 { 1182 memcpy(pov, first_plt_entry, plt_entry_size); 1183 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_address + 4); 1184 elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 8); 1185 } 1186 1187 // The first entry in the PLT for a shared object. 1188 1189 const unsigned char Output_data_plt_i386_dyn::first_plt_entry[plt_entry_size] = 1190 { 1191 0xff, 0xb3, 4, 0, 0, 0, // pushl 4(%ebx) 1192 0xff, 0xa3, 8, 0, 0, 0, // jmp *8(%ebx) 1193 0, 0, 0, 0 // unused 1194 }; 1195 1196 void 1197 Output_data_plt_i386_dyn::do_fill_first_plt_entry( 1198 unsigned char* pov, 1199 elfcpp::Elf_types<32>::Elf_Addr) 1200 { 1201 memcpy(pov, first_plt_entry, plt_entry_size); 1202 } 1203 1204 // Subsequent entries in the PLT for an executable. 1205 1206 const unsigned char Output_data_plt_i386_exec::plt_entry[plt_entry_size] = 1207 { 1208 0xff, 0x25, // jmp indirect 1209 0, 0, 0, 0, // replaced with address of symbol in .got 1210 0x68, // pushl immediate 1211 0, 0, 0, 0, // replaced with offset into relocation table 1212 0xe9, // jmp relative 1213 0, 0, 0, 0 // replaced with offset to start of .plt 1214 }; 1215 1216 unsigned int 1217 Output_data_plt_i386_exec::do_fill_plt_entry( 1218 unsigned char* pov, 1219 elfcpp::Elf_types<32>::Elf_Addr got_address, 1220 unsigned int got_offset, 1221 unsigned int plt_offset, 1222 unsigned int plt_rel_offset) 1223 { 1224 memcpy(pov, plt_entry, plt_entry_size); 1225 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, 1226 got_address + got_offset); 1227 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_rel_offset); 1228 elfcpp::Swap<32, false>::writeval(pov + 12, - (plt_offset + 12 + 4)); 1229 return 6; 1230 } 1231 1232 // Subsequent entries in the PLT for a shared object. 1233 1234 const unsigned char Output_data_plt_i386_dyn::plt_entry[plt_entry_size] = 1235 { 1236 0xff, 0xa3, // jmp *offset(%ebx) 1237 0, 0, 0, 0, // replaced with offset of symbol in .got 1238 0x68, // pushl immediate 1239 0, 0, 0, 0, // replaced with offset into relocation table 1240 0xe9, // jmp relative 1241 0, 0, 0, 0 // replaced with offset to start of .plt 1242 }; 1243 1244 unsigned int 1245 Output_data_plt_i386_dyn::do_fill_plt_entry(unsigned char* pov, 1246 elfcpp::Elf_types<32>::Elf_Addr, 1247 unsigned int got_offset, 1248 unsigned int plt_offset, 1249 unsigned int plt_rel_offset) 1250 { 1251 memcpy(pov, plt_entry, plt_entry_size); 1252 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_offset); 1253 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_rel_offset); 1254 elfcpp::Swap<32, false>::writeval(pov + 12, - (plt_offset + 12 + 4)); 1255 return 6; 1256 } 1257 1258 // The .eh_frame unwind information for the PLT. 1259 1260 const unsigned char 1261 Output_data_plt_i386::plt_eh_frame_cie[plt_eh_frame_cie_size] = 1262 { 1263 1, // CIE version. 1264 'z', // Augmentation: augmentation size included. 1265 'R', // Augmentation: FDE encoding included. 1266 '\0', // End of augmentation string. 1267 1, // Code alignment factor. 1268 0x7c, // Data alignment factor. 1269 8, // Return address column. 1270 1, // Augmentation size. 1271 (elfcpp::DW_EH_PE_pcrel // FDE encoding. 1272 | elfcpp::DW_EH_PE_sdata4), 1273 elfcpp::DW_CFA_def_cfa, 4, 4, // DW_CFA_def_cfa: r4 (esp) ofs 4. 1274 elfcpp::DW_CFA_offset + 8, 1, // DW_CFA_offset: r8 (eip) at cfa-4. 1275 elfcpp::DW_CFA_nop, // Align to 16 bytes. 1276 elfcpp::DW_CFA_nop 1277 }; 1278 1279 const unsigned char 1280 Output_data_plt_i386_standard::plt_eh_frame_fde[plt_eh_frame_fde_size] = 1281 { 1282 0, 0, 0, 0, // Replaced with offset to .plt. 1283 0, 0, 0, 0, // Replaced with size of .plt. 1284 0, // Augmentation size. 1285 elfcpp::DW_CFA_def_cfa_offset, 8, // DW_CFA_def_cfa_offset: 8. 1286 elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6. 1287 elfcpp::DW_CFA_def_cfa_offset, 12, // DW_CFA_def_cfa_offset: 12. 1288 elfcpp::DW_CFA_advance_loc + 10, // Advance 10 to __PLT__ + 16. 1289 elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression. 1290 11, // Block length. 1291 elfcpp::DW_OP_breg4, 4, // Push %esp + 4. 1292 elfcpp::DW_OP_breg8, 0, // Push %eip. 1293 elfcpp::DW_OP_lit15, // Push 0xf. 1294 elfcpp::DW_OP_and, // & (%eip & 0xf). 1295 elfcpp::DW_OP_lit11, // Push 0xb. 1296 elfcpp::DW_OP_ge, // >= ((%eip & 0xf) >= 0xb) 1297 elfcpp::DW_OP_lit2, // Push 2. 1298 elfcpp::DW_OP_shl, // << (((%eip & 0xf) >= 0xb) << 2) 1299 elfcpp::DW_OP_plus, // + ((((%eip&0xf)>=0xb)<<2)+%esp+4 1300 elfcpp::DW_CFA_nop, // Align to 32 bytes. 1301 elfcpp::DW_CFA_nop, 1302 elfcpp::DW_CFA_nop, 1303 elfcpp::DW_CFA_nop 1304 }; 1305 1306 // Write out the PLT. This uses the hand-coded instructions above, 1307 // and adjusts them as needed. This is all specified by the i386 ELF 1308 // Processor Supplement. 1309 1310 void 1311 Output_data_plt_i386::do_write(Output_file* of) 1312 { 1313 const off_t offset = this->offset(); 1314 const section_size_type oview_size = 1315 convert_to_section_size_type(this->data_size()); 1316 unsigned char* const oview = of->get_output_view(offset, oview_size); 1317 1318 const off_t got_file_offset = this->got_plt_->offset(); 1319 gold_assert(parameters->incremental_update() 1320 || (got_file_offset + this->got_plt_->data_size() 1321 == this->got_irelative_->offset())); 1322 const section_size_type got_size = 1323 convert_to_section_size_type(this->got_plt_->data_size() 1324 + this->got_irelative_->data_size()); 1325 unsigned char* const got_view = of->get_output_view(got_file_offset, 1326 got_size); 1327 1328 unsigned char* pov = oview; 1329 1330 elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address(); 1331 elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address(); 1332 1333 this->fill_first_plt_entry(pov, got_address); 1334 pov += this->get_plt_entry_size(); 1335 1336 unsigned char* got_pov = got_view; 1337 1338 // The first entry in the GOT is the address of the .dynamic section 1339 // aka the PT_DYNAMIC segment. The next two entries are reserved. 1340 // We saved space for them when we created the section in 1341 // Target_i386::got_section. 1342 Output_section* dynamic = this->layout_->dynamic_section(); 1343 uint32_t dynamic_addr = dynamic == NULL ? 0 : dynamic->address(); 1344 elfcpp::Swap<32, false>::writeval(got_pov, dynamic_addr); 1345 got_pov += 4; 1346 memset(got_pov, 0, 8); 1347 got_pov += 8; 1348 1349 const int rel_size = elfcpp::Elf_sizes<32>::rel_size; 1350 1351 unsigned int plt_offset = this->get_plt_entry_size(); 1352 unsigned int plt_rel_offset = 0; 1353 unsigned int got_offset = 12; 1354 const unsigned int count = this->count_ + this->irelative_count_; 1355 for (unsigned int i = 0; 1356 i < count; 1357 ++i, 1358 pov += this->get_plt_entry_size(), 1359 got_pov += 4, 1360 plt_offset += this->get_plt_entry_size(), 1361 plt_rel_offset += rel_size, 1362 got_offset += 4) 1363 { 1364 // Set and adjust the PLT entry itself. 1365 unsigned int lazy_offset = this->fill_plt_entry(pov, 1366 got_address, 1367 got_offset, 1368 plt_offset, 1369 plt_rel_offset); 1370 1371 // Set the entry in the GOT. 1372 elfcpp::Swap<32, false>::writeval(got_pov, 1373 plt_address + plt_offset + lazy_offset); 1374 } 1375 1376 // If any STT_GNU_IFUNC symbols have PLT entries, we need to change 1377 // the GOT to point to the actual symbol value, rather than point to 1378 // the PLT entry. That will let the dynamic linker call the right 1379 // function when resolving IRELATIVE relocations. 1380 unsigned char* got_irelative_view = got_view + this->got_plt_->data_size(); 1381 for (std::vector<Global_ifunc>::const_iterator p = 1382 this->global_ifuncs_.begin(); 1383 p != this->global_ifuncs_.end(); 1384 ++p) 1385 { 1386 const Sized_symbol<32>* ssym = 1387 static_cast<const Sized_symbol<32>*>(p->sym); 1388 elfcpp::Swap<32, false>::writeval(got_irelative_view + p->got_offset, 1389 ssym->value()); 1390 } 1391 1392 for (std::vector<Local_ifunc>::const_iterator p = 1393 this->local_ifuncs_.begin(); 1394 p != this->local_ifuncs_.end(); 1395 ++p) 1396 { 1397 const Symbol_value<32>* psymval = 1398 p->object->local_symbol(p->local_sym_index); 1399 elfcpp::Swap<32, false>::writeval(got_irelative_view + p->got_offset, 1400 psymval->value(p->object, 0)); 1401 } 1402 1403 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size); 1404 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size); 1405 1406 of->write_output_view(offset, oview_size, oview); 1407 of->write_output_view(got_file_offset, got_size, got_view); 1408 } 1409 1410 // Create the PLT section. 1411 1412 void 1413 Target_i386::make_plt_section(Symbol_table* symtab, Layout* layout) 1414 { 1415 if (this->plt_ == NULL) 1416 { 1417 // Create the GOT sections first. 1418 this->got_section(symtab, layout); 1419 1420 const bool dyn = parameters->options().output_is_position_independent(); 1421 this->plt_ = this->make_data_plt(layout, 1422 this->got_plt_, 1423 this->got_irelative_, 1424 dyn); 1425 1426 // Add unwind information if requested. 1427 if (parameters->options().ld_generated_unwind_info()) 1428 this->plt_->add_eh_frame(layout); 1429 1430 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS, 1431 (elfcpp::SHF_ALLOC 1432 | elfcpp::SHF_EXECINSTR), 1433 this->plt_, ORDER_PLT, false); 1434 1435 // Make the sh_info field of .rel.plt point to .plt. 1436 Output_section* rel_plt_os = this->plt_->rel_plt()->output_section(); 1437 rel_plt_os->set_info_section(this->plt_->output_section()); 1438 } 1439 } 1440 1441 // Create a PLT entry for a global symbol. 1442 1443 void 1444 Target_i386::make_plt_entry(Symbol_table* symtab, Layout* layout, Symbol* gsym) 1445 { 1446 if (gsym->has_plt_offset()) 1447 return; 1448 if (this->plt_ == NULL) 1449 this->make_plt_section(symtab, layout); 1450 this->plt_->add_entry(symtab, layout, gsym); 1451 } 1452 1453 // Make a PLT entry for a local STT_GNU_IFUNC symbol. 1454 1455 void 1456 Target_i386::make_local_ifunc_plt_entry(Symbol_table* symtab, Layout* layout, 1457 Sized_relobj_file<32, false>* relobj, 1458 unsigned int local_sym_index) 1459 { 1460 if (relobj->local_has_plt_offset(local_sym_index)) 1461 return; 1462 if (this->plt_ == NULL) 1463 this->make_plt_section(symtab, layout); 1464 unsigned int plt_offset = this->plt_->add_local_ifunc_entry(symtab, layout, 1465 relobj, 1466 local_sym_index); 1467 relobj->set_local_plt_offset(local_sym_index, plt_offset); 1468 } 1469 1470 // Return the number of entries in the PLT. 1471 1472 unsigned int 1473 Target_i386::plt_entry_count() const 1474 { 1475 if (this->plt_ == NULL) 1476 return 0; 1477 return this->plt_->entry_count(); 1478 } 1479 1480 // Return the offset of the first non-reserved PLT entry. 1481 1482 unsigned int 1483 Target_i386::first_plt_entry_offset() const 1484 { 1485 return this->plt_->first_plt_entry_offset(); 1486 } 1487 1488 // Return the size of each PLT entry. 1489 1490 unsigned int 1491 Target_i386::plt_entry_size() const 1492 { 1493 return this->plt_->get_plt_entry_size(); 1494 } 1495 1496 // Get the section to use for TLS_DESC relocations. 1497 1498 Target_i386::Reloc_section* 1499 Target_i386::rel_tls_desc_section(Layout* layout) const 1500 { 1501 return this->plt_section()->rel_tls_desc(layout); 1502 } 1503 1504 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment. 1505 1506 void 1507 Target_i386::define_tls_base_symbol(Symbol_table* symtab, Layout* layout) 1508 { 1509 if (this->tls_base_symbol_defined_) 1510 return; 1511 1512 Output_segment* tls_segment = layout->tls_segment(); 1513 if (tls_segment != NULL) 1514 { 1515 bool is_exec = parameters->options().output_is_executable(); 1516 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL, 1517 Symbol_table::PREDEFINED, 1518 tls_segment, 0, 0, 1519 elfcpp::STT_TLS, 1520 elfcpp::STB_LOCAL, 1521 elfcpp::STV_HIDDEN, 0, 1522 (is_exec 1523 ? Symbol::SEGMENT_END 1524 : Symbol::SEGMENT_START), 1525 true); 1526 } 1527 this->tls_base_symbol_defined_ = true; 1528 } 1529 1530 // Create a GOT entry for the TLS module index. 1531 1532 unsigned int 1533 Target_i386::got_mod_index_entry(Symbol_table* symtab, Layout* layout, 1534 Sized_relobj_file<32, false>* object) 1535 { 1536 if (this->got_mod_index_offset_ == -1U) 1537 { 1538 gold_assert(symtab != NULL && layout != NULL && object != NULL); 1539 Reloc_section* rel_dyn = this->rel_dyn_section(layout); 1540 Output_data_got<32, false>* got = this->got_section(symtab, layout); 1541 unsigned int got_offset = got->add_constant(0); 1542 rel_dyn->add_local(object, 0, elfcpp::R_386_TLS_DTPMOD32, got, 1543 got_offset); 1544 got->add_constant(0); 1545 this->got_mod_index_offset_ = got_offset; 1546 } 1547 return this->got_mod_index_offset_; 1548 } 1549 1550 // Optimize the TLS relocation type based on what we know about the 1551 // symbol. IS_FINAL is true if the final address of this symbol is 1552 // known at link time. 1553 1554 tls::Tls_optimization 1555 Target_i386::optimize_tls_reloc(bool is_final, int r_type) 1556 { 1557 // If we are generating a shared library, then we can't do anything 1558 // in the linker. 1559 if (parameters->options().shared()) 1560 return tls::TLSOPT_NONE; 1561 1562 switch (r_type) 1563 { 1564 case elfcpp::R_386_TLS_GD: 1565 case elfcpp::R_386_TLS_GOTDESC: 1566 case elfcpp::R_386_TLS_DESC_CALL: 1567 // These are General-Dynamic which permits fully general TLS 1568 // access. Since we know that we are generating an executable, 1569 // we can convert this to Initial-Exec. If we also know that 1570 // this is a local symbol, we can further switch to Local-Exec. 1571 if (is_final) 1572 return tls::TLSOPT_TO_LE; 1573 return tls::TLSOPT_TO_IE; 1574 1575 case elfcpp::R_386_TLS_LDM: 1576 // This is Local-Dynamic, which refers to a local symbol in the 1577 // dynamic TLS block. Since we know that we generating an 1578 // executable, we can switch to Local-Exec. 1579 return tls::TLSOPT_TO_LE; 1580 1581 case elfcpp::R_386_TLS_LDO_32: 1582 // Another type of Local-Dynamic relocation. 1583 return tls::TLSOPT_TO_LE; 1584 1585 case elfcpp::R_386_TLS_IE: 1586 case elfcpp::R_386_TLS_GOTIE: 1587 case elfcpp::R_386_TLS_IE_32: 1588 // These are Initial-Exec relocs which get the thread offset 1589 // from the GOT. If we know that we are linking against the 1590 // local symbol, we can switch to Local-Exec, which links the 1591 // thread offset into the instruction. 1592 if (is_final) 1593 return tls::TLSOPT_TO_LE; 1594 return tls::TLSOPT_NONE; 1595 1596 case elfcpp::R_386_TLS_LE: 1597 case elfcpp::R_386_TLS_LE_32: 1598 // When we already have Local-Exec, there is nothing further we 1599 // can do. 1600 return tls::TLSOPT_NONE; 1601 1602 default: 1603 gold_unreachable(); 1604 } 1605 } 1606 1607 // Get the Reference_flags for a particular relocation. 1608 1609 int 1610 Target_i386::Scan::get_reference_flags(unsigned int r_type) 1611 { 1612 switch (r_type) 1613 { 1614 case elfcpp::R_386_NONE: 1615 case elfcpp::R_386_GNU_VTINHERIT: 1616 case elfcpp::R_386_GNU_VTENTRY: 1617 case elfcpp::R_386_GOTPC: 1618 // No symbol reference. 1619 return 0; 1620 1621 case elfcpp::R_386_32: 1622 case elfcpp::R_386_16: 1623 case elfcpp::R_386_8: 1624 return Symbol::ABSOLUTE_REF; 1625 1626 case elfcpp::R_386_PC32: 1627 case elfcpp::R_386_PC16: 1628 case elfcpp::R_386_PC8: 1629 case elfcpp::R_386_GOTOFF: 1630 return Symbol::RELATIVE_REF; 1631 1632 case elfcpp::R_386_PLT32: 1633 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF; 1634 1635 case elfcpp::R_386_GOT32: 1636 // Absolute in GOT. 1637 return Symbol::ABSOLUTE_REF; 1638 1639 case elfcpp::R_386_TLS_GD: // Global-dynamic 1640 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url) 1641 case elfcpp::R_386_TLS_DESC_CALL: 1642 case elfcpp::R_386_TLS_LDM: // Local-dynamic 1643 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic 1644 case elfcpp::R_386_TLS_IE: // Initial-exec 1645 case elfcpp::R_386_TLS_IE_32: 1646 case elfcpp::R_386_TLS_GOTIE: 1647 case elfcpp::R_386_TLS_LE: // Local-exec 1648 case elfcpp::R_386_TLS_LE_32: 1649 return Symbol::TLS_REF; 1650 1651 case elfcpp::R_386_COPY: 1652 case elfcpp::R_386_GLOB_DAT: 1653 case elfcpp::R_386_JUMP_SLOT: 1654 case elfcpp::R_386_RELATIVE: 1655 case elfcpp::R_386_IRELATIVE: 1656 case elfcpp::R_386_TLS_TPOFF: 1657 case elfcpp::R_386_TLS_DTPMOD32: 1658 case elfcpp::R_386_TLS_DTPOFF32: 1659 case elfcpp::R_386_TLS_TPOFF32: 1660 case elfcpp::R_386_TLS_DESC: 1661 case elfcpp::R_386_32PLT: 1662 case elfcpp::R_386_TLS_GD_32: 1663 case elfcpp::R_386_TLS_GD_PUSH: 1664 case elfcpp::R_386_TLS_GD_CALL: 1665 case elfcpp::R_386_TLS_GD_POP: 1666 case elfcpp::R_386_TLS_LDM_32: 1667 case elfcpp::R_386_TLS_LDM_PUSH: 1668 case elfcpp::R_386_TLS_LDM_CALL: 1669 case elfcpp::R_386_TLS_LDM_POP: 1670 case elfcpp::R_386_USED_BY_INTEL_200: 1671 default: 1672 // Not expected. We will give an error later. 1673 return 0; 1674 } 1675 } 1676 1677 // Report an unsupported relocation against a local symbol. 1678 1679 void 1680 Target_i386::Scan::unsupported_reloc_local(Sized_relobj_file<32, false>* object, 1681 unsigned int r_type) 1682 { 1683 gold_error(_("%s: unsupported reloc %u against local symbol"), 1684 object->name().c_str(), r_type); 1685 } 1686 1687 // Return whether we need to make a PLT entry for a relocation of a 1688 // given type against a STT_GNU_IFUNC symbol. 1689 1690 bool 1691 Target_i386::Scan::reloc_needs_plt_for_ifunc( 1692 Sized_relobj_file<32, false>* object, 1693 unsigned int r_type) 1694 { 1695 int flags = Scan::get_reference_flags(r_type); 1696 if (flags & Symbol::TLS_REF) 1697 gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"), 1698 object->name().c_str(), r_type); 1699 return flags != 0; 1700 } 1701 1702 // Scan a relocation for a local symbol. 1703 1704 inline void 1705 Target_i386::Scan::local(Symbol_table* symtab, 1706 Layout* layout, 1707 Target_i386* target, 1708 Sized_relobj_file<32, false>* object, 1709 unsigned int data_shndx, 1710 Output_section* output_section, 1711 const elfcpp::Rel<32, false>& reloc, 1712 unsigned int r_type, 1713 const elfcpp::Sym<32, false>& lsym) 1714 { 1715 // A local STT_GNU_IFUNC symbol may require a PLT entry. 1716 if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC 1717 && this->reloc_needs_plt_for_ifunc(object, r_type)) 1718 { 1719 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info()); 1720 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym); 1721 } 1722 1723 switch (r_type) 1724 { 1725 case elfcpp::R_386_NONE: 1726 case elfcpp::R_386_GNU_VTINHERIT: 1727 case elfcpp::R_386_GNU_VTENTRY: 1728 break; 1729 1730 case elfcpp::R_386_32: 1731 // If building a shared library (or a position-independent 1732 // executable), we need to create a dynamic relocation for 1733 // this location. The relocation applied at link time will 1734 // apply the link-time value, so we flag the location with 1735 // an R_386_RELATIVE relocation so the dynamic loader can 1736 // relocate it easily. 1737 if (parameters->options().output_is_position_independent()) 1738 { 1739 Reloc_section* rel_dyn = target->rel_dyn_section(layout); 1740 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info()); 1741 rel_dyn->add_local_relative(object, r_sym, elfcpp::R_386_RELATIVE, 1742 output_section, data_shndx, 1743 reloc.get_r_offset()); 1744 } 1745 break; 1746 1747 case elfcpp::R_386_16: 1748 case elfcpp::R_386_8: 1749 // If building a shared library (or a position-independent 1750 // executable), we need to create a dynamic relocation for 1751 // this location. Because the addend needs to remain in the 1752 // data section, we need to be careful not to apply this 1753 // relocation statically. 1754 if (parameters->options().output_is_position_independent()) 1755 { 1756 Reloc_section* rel_dyn = target->rel_dyn_section(layout); 1757 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info()); 1758 if (lsym.get_st_type() != elfcpp::STT_SECTION) 1759 rel_dyn->add_local(object, r_sym, r_type, output_section, 1760 data_shndx, reloc.get_r_offset()); 1761 else 1762 { 1763 gold_assert(lsym.get_st_value() == 0); 1764 unsigned int shndx = lsym.get_st_shndx(); 1765 bool is_ordinary; 1766 shndx = object->adjust_sym_shndx(r_sym, shndx, 1767 &is_ordinary); 1768 if (!is_ordinary) 1769 object->error(_("section symbol %u has bad shndx %u"), 1770 r_sym, shndx); 1771 else 1772 rel_dyn->add_local_section(object, shndx, 1773 r_type, output_section, 1774 data_shndx, reloc.get_r_offset()); 1775 } 1776 } 1777 break; 1778 1779 case elfcpp::R_386_PC32: 1780 case elfcpp::R_386_PC16: 1781 case elfcpp::R_386_PC8: 1782 break; 1783 1784 case elfcpp::R_386_PLT32: 1785 // Since we know this is a local symbol, we can handle this as a 1786 // PC32 reloc. 1787 break; 1788 1789 case elfcpp::R_386_GOTOFF: 1790 case elfcpp::R_386_GOTPC: 1791 // We need a GOT section. 1792 target->got_section(symtab, layout); 1793 break; 1794 1795 case elfcpp::R_386_GOT32: 1796 { 1797 // The symbol requires a GOT entry. 1798 Output_data_got<32, false>* got = target->got_section(symtab, layout); 1799 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info()); 1800 1801 // For a STT_GNU_IFUNC symbol we want the PLT offset. That 1802 // lets function pointers compare correctly with shared 1803 // libraries. Otherwise we would need an IRELATIVE reloc. 1804 bool is_new; 1805 if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC) 1806 is_new = got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD); 1807 else 1808 is_new = got->add_local(object, r_sym, GOT_TYPE_STANDARD); 1809 if (is_new) 1810 { 1811 // If we are generating a shared object, we need to add a 1812 // dynamic RELATIVE relocation for this symbol's GOT entry. 1813 if (parameters->options().output_is_position_independent()) 1814 { 1815 Reloc_section* rel_dyn = target->rel_dyn_section(layout); 1816 unsigned int got_offset = 1817 object->local_got_offset(r_sym, GOT_TYPE_STANDARD); 1818 rel_dyn->add_local_relative(object, r_sym, 1819 elfcpp::R_386_RELATIVE, 1820 got, got_offset); 1821 } 1822 } 1823 } 1824 break; 1825 1826 // These are relocations which should only be seen by the 1827 // dynamic linker, and should never be seen here. 1828 case elfcpp::R_386_COPY: 1829 case elfcpp::R_386_GLOB_DAT: 1830 case elfcpp::R_386_JUMP_SLOT: 1831 case elfcpp::R_386_RELATIVE: 1832 case elfcpp::R_386_IRELATIVE: 1833 case elfcpp::R_386_TLS_TPOFF: 1834 case elfcpp::R_386_TLS_DTPMOD32: 1835 case elfcpp::R_386_TLS_DTPOFF32: 1836 case elfcpp::R_386_TLS_TPOFF32: 1837 case elfcpp::R_386_TLS_DESC: 1838 gold_error(_("%s: unexpected reloc %u in object file"), 1839 object->name().c_str(), r_type); 1840 break; 1841 1842 // These are initial TLS relocs, which are expected when 1843 // linking. 1844 case elfcpp::R_386_TLS_GD: // Global-dynamic 1845 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url) 1846 case elfcpp::R_386_TLS_DESC_CALL: 1847 case elfcpp::R_386_TLS_LDM: // Local-dynamic 1848 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic 1849 case elfcpp::R_386_TLS_IE: // Initial-exec 1850 case elfcpp::R_386_TLS_IE_32: 1851 case elfcpp::R_386_TLS_GOTIE: 1852 case elfcpp::R_386_TLS_LE: // Local-exec 1853 case elfcpp::R_386_TLS_LE_32: 1854 { 1855 bool output_is_shared = parameters->options().shared(); 1856 const tls::Tls_optimization optimized_type 1857 = Target_i386::optimize_tls_reloc(!output_is_shared, r_type); 1858 switch (r_type) 1859 { 1860 case elfcpp::R_386_TLS_GD: // Global-dynamic 1861 if (optimized_type == tls::TLSOPT_NONE) 1862 { 1863 // Create a pair of GOT entries for the module index and 1864 // dtv-relative offset. 1865 Output_data_got<32, false>* got 1866 = target->got_section(symtab, layout); 1867 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info()); 1868 unsigned int shndx = lsym.get_st_shndx(); 1869 bool is_ordinary; 1870 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary); 1871 if (!is_ordinary) 1872 object->error(_("local symbol %u has bad shndx %u"), 1873 r_sym, shndx); 1874 else 1875 got->add_local_pair_with_rel(object, r_sym, shndx, 1876 GOT_TYPE_TLS_PAIR, 1877 target->rel_dyn_section(layout), 1878 elfcpp::R_386_TLS_DTPMOD32, 0); 1879 } 1880 else if (optimized_type != tls::TLSOPT_TO_LE) 1881 unsupported_reloc_local(object, r_type); 1882 break; 1883 1884 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva) 1885 target->define_tls_base_symbol(symtab, layout); 1886 if (optimized_type == tls::TLSOPT_NONE) 1887 { 1888 // Create a double GOT entry with an R_386_TLS_DESC 1889 // reloc. The R_386_TLS_DESC reloc is resolved 1890 // lazily, so the GOT entry needs to be in an area in 1891 // .got.plt, not .got. Call got_section to make sure 1892 // the section has been created. 1893 target->got_section(symtab, layout); 1894 Output_data_got<32, false>* got = target->got_tlsdesc_section(); 1895 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info()); 1896 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC)) 1897 { 1898 unsigned int got_offset = got->add_constant(0); 1899 // The local symbol value is stored in the second 1900 // GOT entry. 1901 got->add_local(object, r_sym, GOT_TYPE_TLS_DESC); 1902 // That set the GOT offset of the local symbol to 1903 // point to the second entry, but we want it to 1904 // point to the first. 1905 object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC, 1906 got_offset); 1907 Reloc_section* rt = target->rel_tls_desc_section(layout); 1908 rt->add_absolute(elfcpp::R_386_TLS_DESC, got, got_offset); 1909 } 1910 } 1911 else if (optimized_type != tls::TLSOPT_TO_LE) 1912 unsupported_reloc_local(object, r_type); 1913 break; 1914 1915 case elfcpp::R_386_TLS_DESC_CALL: 1916 break; 1917 1918 case elfcpp::R_386_TLS_LDM: // Local-dynamic 1919 if (optimized_type == tls::TLSOPT_NONE) 1920 { 1921 // Create a GOT entry for the module index. 1922 target->got_mod_index_entry(symtab, layout, object); 1923 } 1924 else if (optimized_type != tls::TLSOPT_TO_LE) 1925 unsupported_reloc_local(object, r_type); 1926 break; 1927 1928 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic 1929 break; 1930 1931 case elfcpp::R_386_TLS_IE: // Initial-exec 1932 case elfcpp::R_386_TLS_IE_32: 1933 case elfcpp::R_386_TLS_GOTIE: 1934 layout->set_has_static_tls(); 1935 if (optimized_type == tls::TLSOPT_NONE) 1936 { 1937 // For the R_386_TLS_IE relocation, we need to create a 1938 // dynamic relocation when building a shared library. 1939 if (r_type == elfcpp::R_386_TLS_IE 1940 && parameters->options().shared()) 1941 { 1942 Reloc_section* rel_dyn = target->rel_dyn_section(layout); 1943 unsigned int r_sym 1944 = elfcpp::elf_r_sym<32>(reloc.get_r_info()); 1945 rel_dyn->add_local_relative(object, r_sym, 1946 elfcpp::R_386_RELATIVE, 1947 output_section, data_shndx, 1948 reloc.get_r_offset()); 1949 } 1950 // Create a GOT entry for the tp-relative offset. 1951 Output_data_got<32, false>* got 1952 = target->got_section(symtab, layout); 1953 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info()); 1954 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_IE_32 1955 ? elfcpp::R_386_TLS_TPOFF32 1956 : elfcpp::R_386_TLS_TPOFF); 1957 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32 1958 ? GOT_TYPE_TLS_OFFSET 1959 : GOT_TYPE_TLS_NOFFSET); 1960 got->add_local_with_rel(object, r_sym, got_type, 1961 target->rel_dyn_section(layout), 1962 dyn_r_type); 1963 } 1964 else if (optimized_type != tls::TLSOPT_TO_LE) 1965 unsupported_reloc_local(object, r_type); 1966 break; 1967 1968 case elfcpp::R_386_TLS_LE: // Local-exec 1969 case elfcpp::R_386_TLS_LE_32: 1970 layout->set_has_static_tls(); 1971 if (output_is_shared) 1972 { 1973 // We need to create a dynamic relocation. 1974 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION); 1975 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info()); 1976 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_LE_32 1977 ? elfcpp::R_386_TLS_TPOFF32 1978 : elfcpp::R_386_TLS_TPOFF); 1979 Reloc_section* rel_dyn = target->rel_dyn_section(layout); 1980 rel_dyn->add_local(object, r_sym, dyn_r_type, output_section, 1981 data_shndx, reloc.get_r_offset()); 1982 } 1983 break; 1984 1985 default: 1986 gold_unreachable(); 1987 } 1988 } 1989 break; 1990 1991 case elfcpp::R_386_32PLT: 1992 case elfcpp::R_386_TLS_GD_32: 1993 case elfcpp::R_386_TLS_GD_PUSH: 1994 case elfcpp::R_386_TLS_GD_CALL: 1995 case elfcpp::R_386_TLS_GD_POP: 1996 case elfcpp::R_386_TLS_LDM_32: 1997 case elfcpp::R_386_TLS_LDM_PUSH: 1998 case elfcpp::R_386_TLS_LDM_CALL: 1999 case elfcpp::R_386_TLS_LDM_POP: 2000 case elfcpp::R_386_USED_BY_INTEL_200: 2001 default: 2002 unsupported_reloc_local(object, r_type); 2003 break; 2004 } 2005 } 2006 2007 // Report an unsupported relocation against a global symbol. 2008 2009 void 2010 Target_i386::Scan::unsupported_reloc_global( 2011 Sized_relobj_file<32, false>* object, 2012 unsigned int r_type, 2013 Symbol* gsym) 2014 { 2015 gold_error(_("%s: unsupported reloc %u against global symbol %s"), 2016 object->name().c_str(), r_type, gsym->demangled_name().c_str()); 2017 } 2018 2019 inline bool 2020 Target_i386::Scan::possible_function_pointer_reloc(unsigned int r_type) 2021 { 2022 switch (r_type) 2023 { 2024 case elfcpp::R_386_32: 2025 case elfcpp::R_386_16: 2026 case elfcpp::R_386_8: 2027 case elfcpp::R_386_GOTOFF: 2028 case elfcpp::R_386_GOT32: 2029 { 2030 return true; 2031 } 2032 default: 2033 return false; 2034 } 2035 return false; 2036 } 2037 2038 inline bool 2039 Target_i386::Scan::local_reloc_may_be_function_pointer( 2040 Symbol_table* , 2041 Layout* , 2042 Target_i386* , 2043 Sized_relobj_file<32, false>* , 2044 unsigned int , 2045 Output_section* , 2046 const elfcpp::Rel<32, false>& , 2047 unsigned int r_type, 2048 const elfcpp::Sym<32, false>&) 2049 { 2050 return possible_function_pointer_reloc(r_type); 2051 } 2052 2053 inline bool 2054 Target_i386::Scan::global_reloc_may_be_function_pointer( 2055 Symbol_table* , 2056 Layout* , 2057 Target_i386* , 2058 Sized_relobj_file<32, false>* , 2059 unsigned int , 2060 Output_section* , 2061 const elfcpp::Rel<32, false>& , 2062 unsigned int r_type, 2063 Symbol*) 2064 { 2065 return possible_function_pointer_reloc(r_type); 2066 } 2067 2068 // Scan a relocation for a global symbol. 2069 2070 inline void 2071 Target_i386::Scan::global(Symbol_table* symtab, 2072 Layout* layout, 2073 Target_i386* target, 2074 Sized_relobj_file<32, false>* object, 2075 unsigned int data_shndx, 2076 Output_section* output_section, 2077 const elfcpp::Rel<32, false>& reloc, 2078 unsigned int r_type, 2079 Symbol* gsym) 2080 { 2081 // A STT_GNU_IFUNC symbol may require a PLT entry. 2082 if (gsym->type() == elfcpp::STT_GNU_IFUNC 2083 && this->reloc_needs_plt_for_ifunc(object, r_type)) 2084 target->make_plt_entry(symtab, layout, gsym); 2085 2086 switch (r_type) 2087 { 2088 case elfcpp::R_386_NONE: 2089 case elfcpp::R_386_GNU_VTINHERIT: 2090 case elfcpp::R_386_GNU_VTENTRY: 2091 break; 2092 2093 case elfcpp::R_386_32: 2094 case elfcpp::R_386_16: 2095 case elfcpp::R_386_8: 2096 { 2097 // Make a PLT entry if necessary. 2098 if (gsym->needs_plt_entry()) 2099 { 2100 target->make_plt_entry(symtab, layout, gsym); 2101 // Since this is not a PC-relative relocation, we may be 2102 // taking the address of a function. In that case we need to 2103 // set the entry in the dynamic symbol table to the address of 2104 // the PLT entry. 2105 if (gsym->is_from_dynobj() && !parameters->options().shared()) 2106 gsym->set_needs_dynsym_value(); 2107 } 2108 // Make a dynamic relocation if necessary. 2109 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type))) 2110 { 2111 if (gsym->may_need_copy_reloc()) 2112 { 2113 target->copy_reloc(symtab, layout, object, 2114 data_shndx, output_section, gsym, reloc); 2115 } 2116 else if (r_type == elfcpp::R_386_32 2117 && gsym->type() == elfcpp::STT_GNU_IFUNC 2118 && gsym->can_use_relative_reloc(false) 2119 && !gsym->is_from_dynobj() 2120 && !gsym->is_undefined() 2121 && !gsym->is_preemptible()) 2122 { 2123 // Use an IRELATIVE reloc for a locally defined 2124 // STT_GNU_IFUNC symbol. This makes a function 2125 // address in a PIE executable match the address in a 2126 // shared library that it links against. 2127 Reloc_section* rel_dyn = target->rel_irelative_section(layout); 2128 rel_dyn->add_symbolless_global_addend(gsym, 2129 elfcpp::R_386_IRELATIVE, 2130 output_section, 2131 object, data_shndx, 2132 reloc.get_r_offset()); 2133 } 2134 else if (r_type == elfcpp::R_386_32 2135 && gsym->can_use_relative_reloc(false)) 2136 { 2137 Reloc_section* rel_dyn = target->rel_dyn_section(layout); 2138 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE, 2139 output_section, object, 2140 data_shndx, reloc.get_r_offset()); 2141 } 2142 else 2143 { 2144 Reloc_section* rel_dyn = target->rel_dyn_section(layout); 2145 rel_dyn->add_global(gsym, r_type, output_section, object, 2146 data_shndx, reloc.get_r_offset()); 2147 } 2148 } 2149 } 2150 break; 2151 2152 case elfcpp::R_386_PC32: 2153 case elfcpp::R_386_PC16: 2154 case elfcpp::R_386_PC8: 2155 { 2156 // Make a PLT entry if necessary. 2157 if (gsym->needs_plt_entry()) 2158 { 2159 // These relocations are used for function calls only in 2160 // non-PIC code. For a 32-bit relocation in a shared library, 2161 // we'll need a text relocation anyway, so we can skip the 2162 // PLT entry and let the dynamic linker bind the call directly 2163 // to the target. For smaller relocations, we should use a 2164 // PLT entry to ensure that the call can reach. 2165 if (!parameters->options().shared() 2166 || r_type != elfcpp::R_386_PC32) 2167 target->make_plt_entry(symtab, layout, gsym); 2168 } 2169 // Make a dynamic relocation if necessary. 2170 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type))) 2171 { 2172 if (gsym->may_need_copy_reloc()) 2173 { 2174 target->copy_reloc(symtab, layout, object, 2175 data_shndx, output_section, gsym, reloc); 2176 } 2177 else 2178 { 2179 Reloc_section* rel_dyn = target->rel_dyn_section(layout); 2180 rel_dyn->add_global(gsym, r_type, output_section, object, 2181 data_shndx, reloc.get_r_offset()); 2182 } 2183 } 2184 } 2185 break; 2186 2187 case elfcpp::R_386_GOT32: 2188 { 2189 // The symbol requires a GOT entry. 2190 Output_data_got<32, false>* got = target->got_section(symtab, layout); 2191 if (gsym->final_value_is_known()) 2192 { 2193 // For a STT_GNU_IFUNC symbol we want the PLT address. 2194 if (gsym->type() == elfcpp::STT_GNU_IFUNC) 2195 got->add_global_plt(gsym, GOT_TYPE_STANDARD); 2196 else 2197 got->add_global(gsym, GOT_TYPE_STANDARD); 2198 } 2199 else 2200 { 2201 // If this symbol is not fully resolved, we need to add a 2202 // GOT entry with a dynamic relocation. 2203 Reloc_section* rel_dyn = target->rel_dyn_section(layout); 2204 2205 // Use a GLOB_DAT rather than a RELATIVE reloc if: 2206 // 2207 // 1) The symbol may be defined in some other module. 2208 // 2209 // 2) We are building a shared library and this is a 2210 // protected symbol; using GLOB_DAT means that the dynamic 2211 // linker can use the address of the PLT in the main 2212 // executable when appropriate so that function address 2213 // comparisons work. 2214 // 2215 // 3) This is a STT_GNU_IFUNC symbol in position dependent 2216 // code, again so that function address comparisons work. 2217 if (gsym->is_from_dynobj() 2218 || gsym->is_undefined() 2219 || gsym->is_preemptible() 2220 || (gsym->visibility() == elfcpp::STV_PROTECTED 2221 && parameters->options().shared()) 2222 || (gsym->type() == elfcpp::STT_GNU_IFUNC 2223 && parameters->options().output_is_position_independent())) 2224 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD, 2225 rel_dyn, elfcpp::R_386_GLOB_DAT); 2226 else 2227 { 2228 // For a STT_GNU_IFUNC symbol we want to write the PLT 2229 // offset into the GOT, so that function pointer 2230 // comparisons work correctly. 2231 bool is_new; 2232 if (gsym->type() != elfcpp::STT_GNU_IFUNC) 2233 is_new = got->add_global(gsym, GOT_TYPE_STANDARD); 2234 else 2235 { 2236 is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD); 2237 // Tell the dynamic linker to use the PLT address 2238 // when resolving relocations. 2239 if (gsym->is_from_dynobj() 2240 && !parameters->options().shared()) 2241 gsym->set_needs_dynsym_value(); 2242 } 2243 if (is_new) 2244 { 2245 unsigned int got_off = gsym->got_offset(GOT_TYPE_STANDARD); 2246 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE, 2247 got, got_off); 2248 } 2249 } 2250 } 2251 } 2252 break; 2253 2254 case elfcpp::R_386_PLT32: 2255 // If the symbol is fully resolved, this is just a PC32 reloc. 2256 // Otherwise we need a PLT entry. 2257 if (gsym->final_value_is_known()) 2258 break; 2259 // If building a shared library, we can also skip the PLT entry 2260 // if the symbol is defined in the output file and is protected 2261 // or hidden. 2262 if (gsym->is_defined() 2263 && !gsym->is_from_dynobj() 2264 && !gsym->is_preemptible()) 2265 break; 2266 target->make_plt_entry(symtab, layout, gsym); 2267 break; 2268 2269 case elfcpp::R_386_GOTOFF: 2270 case elfcpp::R_386_GOTPC: 2271 // We need a GOT section. 2272 target->got_section(symtab, layout); 2273 break; 2274 2275 // These are relocations which should only be seen by the 2276 // dynamic linker, and should never be seen here. 2277 case elfcpp::R_386_COPY: 2278 case elfcpp::R_386_GLOB_DAT: 2279 case elfcpp::R_386_JUMP_SLOT: 2280 case elfcpp::R_386_RELATIVE: 2281 case elfcpp::R_386_IRELATIVE: 2282 case elfcpp::R_386_TLS_TPOFF: 2283 case elfcpp::R_386_TLS_DTPMOD32: 2284 case elfcpp::R_386_TLS_DTPOFF32: 2285 case elfcpp::R_386_TLS_TPOFF32: 2286 case elfcpp::R_386_TLS_DESC: 2287 gold_error(_("%s: unexpected reloc %u in object file"), 2288 object->name().c_str(), r_type); 2289 break; 2290 2291 // These are initial tls relocs, which are expected when 2292 // linking. 2293 case elfcpp::R_386_TLS_GD: // Global-dynamic 2294 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url) 2295 case elfcpp::R_386_TLS_DESC_CALL: 2296 case elfcpp::R_386_TLS_LDM: // Local-dynamic 2297 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic 2298 case elfcpp::R_386_TLS_IE: // Initial-exec 2299 case elfcpp::R_386_TLS_IE_32: 2300 case elfcpp::R_386_TLS_GOTIE: 2301 case elfcpp::R_386_TLS_LE: // Local-exec 2302 case elfcpp::R_386_TLS_LE_32: 2303 { 2304 const bool is_final = gsym->final_value_is_known(); 2305 const tls::Tls_optimization optimized_type 2306 = Target_i386::optimize_tls_reloc(is_final, r_type); 2307 switch (r_type) 2308 { 2309 case elfcpp::R_386_TLS_GD: // Global-dynamic 2310 if (optimized_type == tls::TLSOPT_NONE) 2311 { 2312 // Create a pair of GOT entries for the module index and 2313 // dtv-relative offset. 2314 Output_data_got<32, false>* got 2315 = target->got_section(symtab, layout); 2316 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR, 2317 target->rel_dyn_section(layout), 2318 elfcpp::R_386_TLS_DTPMOD32, 2319 elfcpp::R_386_TLS_DTPOFF32); 2320 } 2321 else if (optimized_type == tls::TLSOPT_TO_IE) 2322 { 2323 // Create a GOT entry for the tp-relative offset. 2324 Output_data_got<32, false>* got 2325 = target->got_section(symtab, layout); 2326 got->add_global_with_rel(gsym, GOT_TYPE_TLS_NOFFSET, 2327 target->rel_dyn_section(layout), 2328 elfcpp::R_386_TLS_TPOFF); 2329 } 2330 else if (optimized_type != tls::TLSOPT_TO_LE) 2331 unsupported_reloc_global(object, r_type, gsym); 2332 break; 2333 2334 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (~oliva url) 2335 target->define_tls_base_symbol(symtab, layout); 2336 if (optimized_type == tls::TLSOPT_NONE) 2337 { 2338 // Create a double GOT entry with an R_386_TLS_DESC 2339 // reloc. The R_386_TLS_DESC reloc is resolved 2340 // lazily, so the GOT entry needs to be in an area in 2341 // .got.plt, not .got. Call got_section to make sure 2342 // the section has been created. 2343 target->got_section(symtab, layout); 2344 Output_data_got<32, false>* got = target->got_tlsdesc_section(); 2345 Reloc_section* rt = target->rel_tls_desc_section(layout); 2346 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_DESC, rt, 2347 elfcpp::R_386_TLS_DESC, 0); 2348 } 2349 else if (optimized_type == tls::TLSOPT_TO_IE) 2350 { 2351 // Create a GOT entry for the tp-relative offset. 2352 Output_data_got<32, false>* got 2353 = target->got_section(symtab, layout); 2354 got->add_global_with_rel(gsym, GOT_TYPE_TLS_NOFFSET, 2355 target->rel_dyn_section(layout), 2356 elfcpp::R_386_TLS_TPOFF); 2357 } 2358 else if (optimized_type != tls::TLSOPT_TO_LE) 2359 unsupported_reloc_global(object, r_type, gsym); 2360 break; 2361 2362 case elfcpp::R_386_TLS_DESC_CALL: 2363 break; 2364 2365 case elfcpp::R_386_TLS_LDM: // Local-dynamic 2366 if (optimized_type == tls::TLSOPT_NONE) 2367 { 2368 // Create a GOT entry for the module index. 2369 target->got_mod_index_entry(symtab, layout, object); 2370 } 2371 else if (optimized_type != tls::TLSOPT_TO_LE) 2372 unsupported_reloc_global(object, r_type, gsym); 2373 break; 2374 2375 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic 2376 break; 2377 2378 case elfcpp::R_386_TLS_IE: // Initial-exec 2379 case elfcpp::R_386_TLS_IE_32: 2380 case elfcpp::R_386_TLS_GOTIE: 2381 layout->set_has_static_tls(); 2382 if (optimized_type == tls::TLSOPT_NONE) 2383 { 2384 // For the R_386_TLS_IE relocation, we need to create a 2385 // dynamic relocation when building a shared library. 2386 if (r_type == elfcpp::R_386_TLS_IE 2387 && parameters->options().shared()) 2388 { 2389 Reloc_section* rel_dyn = target->rel_dyn_section(layout); 2390 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE, 2391 output_section, object, 2392 data_shndx, 2393 reloc.get_r_offset()); 2394 } 2395 // Create a GOT entry for the tp-relative offset. 2396 Output_data_got<32, false>* got 2397 = target->got_section(symtab, layout); 2398 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_IE_32 2399 ? elfcpp::R_386_TLS_TPOFF32 2400 : elfcpp::R_386_TLS_TPOFF); 2401 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32 2402 ? GOT_TYPE_TLS_OFFSET 2403 : GOT_TYPE_TLS_NOFFSET); 2404 got->add_global_with_rel(gsym, got_type, 2405 target->rel_dyn_section(layout), 2406 dyn_r_type); 2407 } 2408 else if (optimized_type != tls::TLSOPT_TO_LE) 2409 unsupported_reloc_global(object, r_type, gsym); 2410 break; 2411 2412 case elfcpp::R_386_TLS_LE: // Local-exec 2413 case elfcpp::R_386_TLS_LE_32: 2414 layout->set_has_static_tls(); 2415 if (parameters->options().shared()) 2416 { 2417 // We need to create a dynamic relocation. 2418 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_LE_32 2419 ? elfcpp::R_386_TLS_TPOFF32 2420 : elfcpp::R_386_TLS_TPOFF); 2421 Reloc_section* rel_dyn = target->rel_dyn_section(layout); 2422 rel_dyn->add_global(gsym, dyn_r_type, output_section, object, 2423 data_shndx, reloc.get_r_offset()); 2424 } 2425 break; 2426 2427 default: 2428 gold_unreachable(); 2429 } 2430 } 2431 break; 2432 2433 case elfcpp::R_386_32PLT: 2434 case elfcpp::R_386_TLS_GD_32: 2435 case elfcpp::R_386_TLS_GD_PUSH: 2436 case elfcpp::R_386_TLS_GD_CALL: 2437 case elfcpp::R_386_TLS_GD_POP: 2438 case elfcpp::R_386_TLS_LDM_32: 2439 case elfcpp::R_386_TLS_LDM_PUSH: 2440 case elfcpp::R_386_TLS_LDM_CALL: 2441 case elfcpp::R_386_TLS_LDM_POP: 2442 case elfcpp::R_386_USED_BY_INTEL_200: 2443 default: 2444 unsupported_reloc_global(object, r_type, gsym); 2445 break; 2446 } 2447 } 2448 2449 // Process relocations for gc. 2450 2451 void 2452 Target_i386::gc_process_relocs(Symbol_table* symtab, 2453 Layout* layout, 2454 Sized_relobj_file<32, false>* object, 2455 unsigned int data_shndx, 2456 unsigned int, 2457 const unsigned char* prelocs, 2458 size_t reloc_count, 2459 Output_section* output_section, 2460 bool needs_special_offset_handling, 2461 size_t local_symbol_count, 2462 const unsigned char* plocal_symbols) 2463 { 2464 gold::gc_process_relocs<32, false, Target_i386, elfcpp::SHT_REL, 2465 Target_i386::Scan, 2466 Target_i386::Relocatable_size_for_reloc>( 2467 symtab, 2468 layout, 2469 this, 2470 object, 2471 data_shndx, 2472 prelocs, 2473 reloc_count, 2474 output_section, 2475 needs_special_offset_handling, 2476 local_symbol_count, 2477 plocal_symbols); 2478 } 2479 2480 // Scan relocations for a section. 2481 2482 void 2483 Target_i386::scan_relocs(Symbol_table* symtab, 2484 Layout* layout, 2485 Sized_relobj_file<32, false>* object, 2486 unsigned int data_shndx, 2487 unsigned int sh_type, 2488 const unsigned char* prelocs, 2489 size_t reloc_count, 2490 Output_section* output_section, 2491 bool needs_special_offset_handling, 2492 size_t local_symbol_count, 2493 const unsigned char* plocal_symbols) 2494 { 2495 if (sh_type == elfcpp::SHT_RELA) 2496 { 2497 gold_error(_("%s: unsupported RELA reloc section"), 2498 object->name().c_str()); 2499 return; 2500 } 2501 2502 gold::scan_relocs<32, false, Target_i386, elfcpp::SHT_REL, 2503 Target_i386::Scan>( 2504 symtab, 2505 layout, 2506 this, 2507 object, 2508 data_shndx, 2509 prelocs, 2510 reloc_count, 2511 output_section, 2512 needs_special_offset_handling, 2513 local_symbol_count, 2514 plocal_symbols); 2515 } 2516 2517 // Finalize the sections. 2518 2519 void 2520 Target_i386::do_finalize_sections( 2521 Layout* layout, 2522 const Input_objects*, 2523 Symbol_table* symtab) 2524 { 2525 const Reloc_section* rel_plt = (this->plt_ == NULL 2526 ? NULL 2527 : this->plt_->rel_plt()); 2528 layout->add_target_dynamic_tags(true, this->got_plt_, rel_plt, 2529 this->rel_dyn_, true, false); 2530 2531 // Emit any relocs we saved in an attempt to avoid generating COPY 2532 // relocs. 2533 if (this->copy_relocs_.any_saved_relocs()) 2534 this->copy_relocs_.emit(this->rel_dyn_section(layout)); 2535 2536 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of 2537 // the .got.plt section. 2538 Symbol* sym = this->global_offset_table_; 2539 if (sym != NULL) 2540 { 2541 uint32_t data_size = this->got_plt_->current_data_size(); 2542 symtab->get_sized_symbol<32>(sym)->set_symsize(data_size); 2543 } 2544 2545 if (parameters->doing_static_link() 2546 && (this->plt_ == NULL || !this->plt_->has_irelative_section())) 2547 { 2548 // If linking statically, make sure that the __rel_iplt symbols 2549 // were defined if necessary, even if we didn't create a PLT. 2550 static const Define_symbol_in_segment syms[] = 2551 { 2552 { 2553 "__rel_iplt_start", // name 2554 elfcpp::PT_LOAD, // segment_type 2555 elfcpp::PF_W, // segment_flags_set 2556 elfcpp::PF(0), // segment_flags_clear 2557 0, // value 2558 0, // size 2559 elfcpp::STT_NOTYPE, // type 2560 elfcpp::STB_GLOBAL, // binding 2561 elfcpp::STV_HIDDEN, // visibility 2562 0, // nonvis 2563 Symbol::SEGMENT_START, // offset_from_base 2564 true // only_if_ref 2565 }, 2566 { 2567 "__rel_iplt_end", // name 2568 elfcpp::PT_LOAD, // segment_type 2569 elfcpp::PF_W, // segment_flags_set 2570 elfcpp::PF(0), // segment_flags_clear 2571 0, // value 2572 0, // size 2573 elfcpp::STT_NOTYPE, // type 2574 elfcpp::STB_GLOBAL, // binding 2575 elfcpp::STV_HIDDEN, // visibility 2576 0, // nonvis 2577 Symbol::SEGMENT_START, // offset_from_base 2578 true // only_if_ref 2579 } 2580 }; 2581 2582 symtab->define_symbols(layout, 2, syms, 2583 layout->script_options()->saw_sections_clause()); 2584 } 2585 } 2586 2587 // Return whether a direct absolute static relocation needs to be applied. 2588 // In cases where Scan::local() or Scan::global() has created 2589 // a dynamic relocation other than R_386_RELATIVE, the addend 2590 // of the relocation is carried in the data, and we must not 2591 // apply the static relocation. 2592 2593 inline bool 2594 Target_i386::Relocate::should_apply_static_reloc(const Sized_symbol<32>* gsym, 2595 unsigned int r_type, 2596 bool is_32bit, 2597 Output_section* output_section) 2598 { 2599 // If the output section is not allocated, then we didn't call 2600 // scan_relocs, we didn't create a dynamic reloc, and we must apply 2601 // the reloc here. 2602 if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0) 2603 return true; 2604 2605 int ref_flags = Scan::get_reference_flags(r_type); 2606 2607 // For local symbols, we will have created a non-RELATIVE dynamic 2608 // relocation only if (a) the output is position independent, 2609 // (b) the relocation is absolute (not pc- or segment-relative), and 2610 // (c) the relocation is not 32 bits wide. 2611 if (gsym == NULL) 2612 return !(parameters->options().output_is_position_independent() 2613 && (ref_flags & Symbol::ABSOLUTE_REF) 2614 && !is_32bit); 2615 2616 // For global symbols, we use the same helper routines used in the 2617 // scan pass. If we did not create a dynamic relocation, or if we 2618 // created a RELATIVE dynamic relocation, we should apply the static 2619 // relocation. 2620 bool has_dyn = gsym->needs_dynamic_reloc(ref_flags); 2621 bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF) 2622 && gsym->can_use_relative_reloc(ref_flags 2623 & Symbol::FUNCTION_CALL); 2624 return !has_dyn || is_rel; 2625 } 2626 2627 // Perform a relocation. 2628 2629 inline bool 2630 Target_i386::Relocate::relocate(const Relocate_info<32, false>* relinfo, 2631 Target_i386* target, 2632 Output_section* output_section, 2633 size_t relnum, 2634 const elfcpp::Rel<32, false>& rel, 2635 unsigned int r_type, 2636 const Sized_symbol<32>* gsym, 2637 const Symbol_value<32>* psymval, 2638 unsigned char* view, 2639 elfcpp::Elf_types<32>::Elf_Addr address, 2640 section_size_type view_size) 2641 { 2642 if (this->skip_call_tls_get_addr_) 2643 { 2644 if ((r_type != elfcpp::R_386_PLT32 2645 && r_type != elfcpp::R_386_PC32) 2646 || gsym == NULL 2647 || strcmp(gsym->name(), "___tls_get_addr") != 0) 2648 gold_error_at_location(relinfo, relnum, rel.get_r_offset(), 2649 _("missing expected TLS relocation")); 2650 else 2651 { 2652 this->skip_call_tls_get_addr_ = false; 2653 return false; 2654 } 2655 } 2656 2657 const Sized_relobj_file<32, false>* object = relinfo->object; 2658 2659 // Pick the value to use for symbols defined in shared objects. 2660 Symbol_value<32> symval; 2661 if (gsym != NULL 2662 && gsym->type() == elfcpp::STT_GNU_IFUNC 2663 && r_type == elfcpp::R_386_32 2664 && gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)) 2665 && gsym->can_use_relative_reloc(false) 2666 && !gsym->is_from_dynobj() 2667 && !gsym->is_undefined() 2668 && !gsym->is_preemptible()) 2669 { 2670 // In this case we are generating a R_386_IRELATIVE reloc. We 2671 // want to use the real value of the symbol, not the PLT offset. 2672 } 2673 else if (gsym != NULL 2674 && gsym->use_plt_offset(Scan::get_reference_flags(r_type))) 2675 { 2676 symval.set_output_value(target->plt_address_for_global(gsym) 2677 + gsym->plt_offset()); 2678 psymval = &symval; 2679 } 2680 else if (gsym == NULL && psymval->is_ifunc_symbol()) 2681 { 2682 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info()); 2683 if (object->local_has_plt_offset(r_sym)) 2684 { 2685 symval.set_output_value(target->plt_address_for_local(object, r_sym) 2686 + object->local_plt_offset(r_sym)); 2687 psymval = &symval; 2688 } 2689 } 2690 2691 // Get the GOT offset if needed. 2692 // The GOT pointer points to the end of the GOT section. 2693 // We need to subtract the size of the GOT section to get 2694 // the actual offset to use in the relocation. 2695 bool have_got_offset = false; 2696 unsigned int got_offset = 0; 2697 switch (r_type) 2698 { 2699 case elfcpp::R_386_GOT32: 2700 if (gsym != NULL) 2701 { 2702 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD)); 2703 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD) 2704 - target->got_size()); 2705 } 2706 else 2707 { 2708 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info()); 2709 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD)); 2710 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD) 2711 - target->got_size()); 2712 } 2713 have_got_offset = true; 2714 break; 2715 2716 default: 2717 break; 2718 } 2719 2720 switch (r_type) 2721 { 2722 case elfcpp::R_386_NONE: 2723 case elfcpp::R_386_GNU_VTINHERIT: 2724 case elfcpp::R_386_GNU_VTENTRY: 2725 break; 2726 2727 case elfcpp::R_386_32: 2728 if (should_apply_static_reloc(gsym, r_type, true, output_section)) 2729 Relocate_functions<32, false>::rel32(view, object, psymval); 2730 break; 2731 2732 case elfcpp::R_386_PC32: 2733 if (should_apply_static_reloc(gsym, r_type, true, output_section)) 2734 Relocate_functions<32, false>::pcrel32(view, object, psymval, address); 2735 break; 2736 2737 case elfcpp::R_386_16: 2738 if (should_apply_static_reloc(gsym, r_type, false, output_section)) 2739 Relocate_functions<32, false>::rel16(view, object, psymval); 2740 break; 2741 2742 case elfcpp::R_386_PC16: 2743 if (should_apply_static_reloc(gsym, r_type, false, output_section)) 2744 Relocate_functions<32, false>::pcrel16(view, object, psymval, address); 2745 break; 2746 2747 case elfcpp::R_386_8: 2748 if (should_apply_static_reloc(gsym, r_type, false, output_section)) 2749 Relocate_functions<32, false>::rel8(view, object, psymval); 2750 break; 2751 2752 case elfcpp::R_386_PC8: 2753 if (should_apply_static_reloc(gsym, r_type, false, output_section)) 2754 Relocate_functions<32, false>::pcrel8(view, object, psymval, address); 2755 break; 2756 2757 case elfcpp::R_386_PLT32: 2758 gold_assert(gsym == NULL 2759 || gsym->has_plt_offset() 2760 || gsym->final_value_is_known() 2761 || (gsym->is_defined() 2762 && !gsym->is_from_dynobj() 2763 && !gsym->is_preemptible())); 2764 Relocate_functions<32, false>::pcrel32(view, object, psymval, address); 2765 break; 2766 2767 case elfcpp::R_386_GOT32: 2768 gold_assert(have_got_offset); 2769 Relocate_functions<32, false>::rel32(view, got_offset); 2770 break; 2771 2772 case elfcpp::R_386_GOTOFF: 2773 { 2774 elfcpp::Elf_types<32>::Elf_Addr value; 2775 value = (psymval->value(object, 0) 2776 - target->got_plt_section()->address()); 2777 Relocate_functions<32, false>::rel32(view, value); 2778 } 2779 break; 2780 2781 case elfcpp::R_386_GOTPC: 2782 { 2783 elfcpp::Elf_types<32>::Elf_Addr value; 2784 value = target->got_plt_section()->address(); 2785 Relocate_functions<32, false>::pcrel32(view, value, address); 2786 } 2787 break; 2788 2789 case elfcpp::R_386_COPY: 2790 case elfcpp::R_386_GLOB_DAT: 2791 case elfcpp::R_386_JUMP_SLOT: 2792 case elfcpp::R_386_RELATIVE: 2793 case elfcpp::R_386_IRELATIVE: 2794 // These are outstanding tls relocs, which are unexpected when 2795 // linking. 2796 case elfcpp::R_386_TLS_TPOFF: 2797 case elfcpp::R_386_TLS_DTPMOD32: 2798 case elfcpp::R_386_TLS_DTPOFF32: 2799 case elfcpp::R_386_TLS_TPOFF32: 2800 case elfcpp::R_386_TLS_DESC: 2801 gold_error_at_location(relinfo, relnum, rel.get_r_offset(), 2802 _("unexpected reloc %u in object file"), 2803 r_type); 2804 break; 2805 2806 // These are initial tls relocs, which are expected when 2807 // linking. 2808 case elfcpp::R_386_TLS_GD: // Global-dynamic 2809 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url) 2810 case elfcpp::R_386_TLS_DESC_CALL: 2811 case elfcpp::R_386_TLS_LDM: // Local-dynamic 2812 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic 2813 case elfcpp::R_386_TLS_IE: // Initial-exec 2814 case elfcpp::R_386_TLS_IE_32: 2815 case elfcpp::R_386_TLS_GOTIE: 2816 case elfcpp::R_386_TLS_LE: // Local-exec 2817 case elfcpp::R_386_TLS_LE_32: 2818 this->relocate_tls(relinfo, target, relnum, rel, r_type, gsym, psymval, 2819 view, address, view_size); 2820 break; 2821 2822 case elfcpp::R_386_32PLT: 2823 case elfcpp::R_386_TLS_GD_32: 2824 case elfcpp::R_386_TLS_GD_PUSH: 2825 case elfcpp::R_386_TLS_GD_CALL: 2826 case elfcpp::R_386_TLS_GD_POP: 2827 case elfcpp::R_386_TLS_LDM_32: 2828 case elfcpp::R_386_TLS_LDM_PUSH: 2829 case elfcpp::R_386_TLS_LDM_CALL: 2830 case elfcpp::R_386_TLS_LDM_POP: 2831 case elfcpp::R_386_USED_BY_INTEL_200: 2832 default: 2833 gold_error_at_location(relinfo, relnum, rel.get_r_offset(), 2834 _("unsupported reloc %u"), 2835 r_type); 2836 break; 2837 } 2838 2839 return true; 2840 } 2841 2842 // Perform a TLS relocation. 2843 2844 inline void 2845 Target_i386::Relocate::relocate_tls(const Relocate_info<32, false>* relinfo, 2846 Target_i386* target, 2847 size_t relnum, 2848 const elfcpp::Rel<32, false>& rel, 2849 unsigned int r_type, 2850 const Sized_symbol<32>* gsym, 2851 const Symbol_value<32>* psymval, 2852 unsigned char* view, 2853 elfcpp::Elf_types<32>::Elf_Addr, 2854 section_size_type view_size) 2855 { 2856 Output_segment* tls_segment = relinfo->layout->tls_segment(); 2857 2858 const Sized_relobj_file<32, false>* object = relinfo->object; 2859 2860 elfcpp::Elf_types<32>::Elf_Addr value = psymval->value(object, 0); 2861 2862 const bool is_final = (gsym == NULL 2863 ? !parameters->options().shared() 2864 : gsym->final_value_is_known()); 2865 const tls::Tls_optimization optimized_type 2866 = Target_i386::optimize_tls_reloc(is_final, r_type); 2867 switch (r_type) 2868 { 2869 case elfcpp::R_386_TLS_GD: // Global-dynamic 2870 if (optimized_type == tls::TLSOPT_TO_LE) 2871 { 2872 if (tls_segment == NULL) 2873 { 2874 gold_assert(parameters->errors()->error_count() > 0 2875 || issue_undefined_symbol_error(gsym)); 2876 return; 2877 } 2878 this->tls_gd_to_le(relinfo, relnum, tls_segment, 2879 rel, r_type, value, view, 2880 view_size); 2881 break; 2882 } 2883 else 2884 { 2885 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE 2886 ? GOT_TYPE_TLS_NOFFSET 2887 : GOT_TYPE_TLS_PAIR); 2888 unsigned int got_offset; 2889 if (gsym != NULL) 2890 { 2891 gold_assert(gsym->has_got_offset(got_type)); 2892 got_offset = gsym->got_offset(got_type) - target->got_size(); 2893 } 2894 else 2895 { 2896 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info()); 2897 gold_assert(object->local_has_got_offset(r_sym, got_type)); 2898 got_offset = (object->local_got_offset(r_sym, got_type) 2899 - target->got_size()); 2900 } 2901 if (optimized_type == tls::TLSOPT_TO_IE) 2902 { 2903 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rel, r_type, 2904 got_offset, view, view_size); 2905 break; 2906 } 2907 else if (optimized_type == tls::TLSOPT_NONE) 2908 { 2909 // Relocate the field with the offset of the pair of GOT 2910 // entries. 2911 Relocate_functions<32, false>::rel32(view, got_offset); 2912 break; 2913 } 2914 } 2915 gold_error_at_location(relinfo, relnum, rel.get_r_offset(), 2916 _("unsupported reloc %u"), 2917 r_type); 2918 break; 2919 2920 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url) 2921 case elfcpp::R_386_TLS_DESC_CALL: 2922 this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU; 2923 if (optimized_type == tls::TLSOPT_TO_LE) 2924 { 2925 if (tls_segment == NULL) 2926 { 2927 gold_assert(parameters->errors()->error_count() > 0 2928 || issue_undefined_symbol_error(gsym)); 2929 return; 2930 } 2931 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment, 2932 rel, r_type, value, view, 2933 view_size); 2934 break; 2935 } 2936 else 2937 { 2938 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE 2939 ? GOT_TYPE_TLS_NOFFSET 2940 : GOT_TYPE_TLS_DESC); 2941 unsigned int got_offset = 0; 2942 if (r_type == elfcpp::R_386_TLS_GOTDESC 2943 && optimized_type == tls::TLSOPT_NONE) 2944 { 2945 // We created GOT entries in the .got.tlsdesc portion of 2946 // the .got.plt section, but the offset stored in the 2947 // symbol is the offset within .got.tlsdesc. 2948 got_offset = (target->got_size() 2949 + target->got_plt_section()->data_size()); 2950 } 2951 if (gsym != NULL) 2952 { 2953 gold_assert(gsym->has_got_offset(got_type)); 2954 got_offset += gsym->got_offset(got_type) - target->got_size(); 2955 } 2956 else 2957 { 2958 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info()); 2959 gold_assert(object->local_has_got_offset(r_sym, got_type)); 2960 got_offset += (object->local_got_offset(r_sym, got_type) 2961 - target->got_size()); 2962 } 2963 if (optimized_type == tls::TLSOPT_TO_IE) 2964 { 2965 if (tls_segment == NULL) 2966 { 2967 gold_assert(parameters->errors()->error_count() > 0 2968 || issue_undefined_symbol_error(gsym)); 2969 return; 2970 } 2971 this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment, rel, r_type, 2972 got_offset, view, view_size); 2973 break; 2974 } 2975 else if (optimized_type == tls::TLSOPT_NONE) 2976 { 2977 if (r_type == elfcpp::R_386_TLS_GOTDESC) 2978 { 2979 // Relocate the field with the offset of the pair of GOT 2980 // entries. 2981 Relocate_functions<32, false>::rel32(view, got_offset); 2982 } 2983 break; 2984 } 2985 } 2986 gold_error_at_location(relinfo, relnum, rel.get_r_offset(), 2987 _("unsupported reloc %u"), 2988 r_type); 2989 break; 2990 2991 case elfcpp::R_386_TLS_LDM: // Local-dynamic 2992 if (this->local_dynamic_type_ == LOCAL_DYNAMIC_SUN) 2993 { 2994 gold_error_at_location(relinfo, relnum, rel.get_r_offset(), 2995 _("both SUN and GNU model " 2996 "TLS relocations")); 2997 break; 2998 } 2999 this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU; 3000 if (optimized_type == tls::TLSOPT_TO_LE) 3001 { 3002 if (tls_segment == NULL) 3003 { 3004 gold_assert(parameters->errors()->error_count() > 0 3005 || issue_undefined_symbol_error(gsym)); 3006 return; 3007 } 3008 this->tls_ld_to_le(relinfo, relnum, tls_segment, rel, r_type, 3009 value, view, view_size); 3010 break; 3011 } 3012 else if (optimized_type == tls::TLSOPT_NONE) 3013 { 3014 // Relocate the field with the offset of the GOT entry for 3015 // the module index. 3016 unsigned int got_offset; 3017 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL) 3018 - target->got_size()); 3019 Relocate_functions<32, false>::rel32(view, got_offset); 3020 break; 3021 } 3022 gold_error_at_location(relinfo, relnum, rel.get_r_offset(), 3023 _("unsupported reloc %u"), 3024 r_type); 3025 break; 3026 3027 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic 3028 if (optimized_type == tls::TLSOPT_TO_LE) 3029 { 3030 // This reloc can appear in debugging sections, in which 3031 // case we must not convert to local-exec. We decide what 3032 // to do based on whether the section is marked as 3033 // containing executable code. That is what the GNU linker 3034 // does as well. 3035 elfcpp::Shdr<32, false> shdr(relinfo->data_shdr); 3036 if ((shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0) 3037 { 3038 if (tls_segment == NULL) 3039 { 3040 gold_assert(parameters->errors()->error_count() > 0 3041 || issue_undefined_symbol_error(gsym)); 3042 return; 3043 } 3044 value -= tls_segment->memsz(); 3045 } 3046 } 3047 Relocate_functions<32, false>::rel32(view, value); 3048 break; 3049 3050 case elfcpp::R_386_TLS_IE: // Initial-exec 3051 case elfcpp::R_386_TLS_GOTIE: 3052 case elfcpp::R_386_TLS_IE_32: 3053 if (optimized_type == tls::TLSOPT_TO_LE) 3054 { 3055 if (tls_segment == NULL) 3056 { 3057 gold_assert(parameters->errors()->error_count() > 0 3058 || issue_undefined_symbol_error(gsym)); 3059 return; 3060 } 3061 Target_i386::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment, 3062 rel, r_type, value, view, 3063 view_size); 3064 break; 3065 } 3066 else if (optimized_type == tls::TLSOPT_NONE) 3067 { 3068 // Relocate the field with the offset of the GOT entry for 3069 // the tp-relative offset of the symbol. 3070 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32 3071 ? GOT_TYPE_TLS_OFFSET 3072 : GOT_TYPE_TLS_NOFFSET); 3073 unsigned int got_offset; 3074 if (gsym != NULL) 3075 { 3076 gold_assert(gsym->has_got_offset(got_type)); 3077 got_offset = gsym->got_offset(got_type); 3078 } 3079 else 3080 { 3081 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info()); 3082 gold_assert(object->local_has_got_offset(r_sym, got_type)); 3083 got_offset = object->local_got_offset(r_sym, got_type); 3084 } 3085 // For the R_386_TLS_IE relocation, we need to apply the 3086 // absolute address of the GOT entry. 3087 if (r_type == elfcpp::R_386_TLS_IE) 3088 got_offset += target->got_plt_section()->address(); 3089 // All GOT offsets are relative to the end of the GOT. 3090 got_offset -= target->got_size(); 3091 Relocate_functions<32, false>::rel32(view, got_offset); 3092 break; 3093 } 3094 gold_error_at_location(relinfo, relnum, rel.get_r_offset(), 3095 _("unsupported reloc %u"), 3096 r_type); 3097 break; 3098 3099 case elfcpp::R_386_TLS_LE: // Local-exec 3100 // If we're creating a shared library, a dynamic relocation will 3101 // have been created for this location, so do not apply it now. 3102 if (!parameters->options().shared()) 3103 { 3104 if (tls_segment == NULL) 3105 { 3106 gold_assert(parameters->errors()->error_count() > 0 3107 || issue_undefined_symbol_error(gsym)); 3108 return; 3109 } 3110 value -= tls_segment->memsz(); 3111 Relocate_functions<32, false>::rel32(view, value); 3112 } 3113 break; 3114 3115 case elfcpp::R_386_TLS_LE_32: 3116 // If we're creating a shared library, a dynamic relocation will 3117 // have been created for this location, so do not apply it now. 3118 if (!parameters->options().shared()) 3119 { 3120 if (tls_segment == NULL) 3121 { 3122 gold_assert(parameters->errors()->error_count() > 0 3123 || issue_undefined_symbol_error(gsym)); 3124 return; 3125 } 3126 value = tls_segment->memsz() - value; 3127 Relocate_functions<32, false>::rel32(view, value); 3128 } 3129 break; 3130 } 3131 } 3132 3133 // Do a relocation in which we convert a TLS General-Dynamic to a 3134 // Local-Exec. 3135 3136 inline void 3137 Target_i386::Relocate::tls_gd_to_le(const Relocate_info<32, false>* relinfo, 3138 size_t relnum, 3139 Output_segment* tls_segment, 3140 const elfcpp::Rel<32, false>& rel, 3141 unsigned int, 3142 elfcpp::Elf_types<32>::Elf_Addr value, 3143 unsigned char* view, 3144 section_size_type view_size) 3145 { 3146 // leal foo(,%reg,1),%eax; call ___tls_get_addr 3147 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax 3148 // leal foo(%reg),%eax; call ___tls_get_addr 3149 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax 3150 3151 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2); 3152 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9); 3153 3154 unsigned char op1 = view[-1]; 3155 unsigned char op2 = view[-2]; 3156 3157 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 3158 op2 == 0x8d || op2 == 0x04); 3159 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8); 3160 3161 int roff = 5; 3162 3163 if (op2 == 0x04) 3164 { 3165 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3); 3166 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d); 3167 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 3168 ((op1 & 0xc7) == 0x05 && op1 != (4 << 3))); 3169 memcpy(view - 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12); 3170 } 3171 else 3172 { 3173 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 3174 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4); 3175 if (rel.get_r_offset() + 9 < view_size 3176 && view[9] == 0x90) 3177 { 3178 // There is a trailing nop. Use the size byte subl. 3179 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12); 3180 roff = 6; 3181 } 3182 else 3183 { 3184 // Use the five byte subl. 3185 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11); 3186 } 3187 } 3188 3189 value = tls_segment->memsz() - value; 3190 Relocate_functions<32, false>::rel32(view + roff, value); 3191 3192 // The next reloc should be a PLT32 reloc against __tls_get_addr. 3193 // We can skip it. 3194 this->skip_call_tls_get_addr_ = true; 3195 } 3196 3197 // Do a relocation in which we convert a TLS General-Dynamic to an 3198 // Initial-Exec. 3199 3200 inline void 3201 Target_i386::Relocate::tls_gd_to_ie(const Relocate_info<32, false>* relinfo, 3202 size_t relnum, 3203 Output_segment*, 3204 const elfcpp::Rel<32, false>& rel, 3205 unsigned int, 3206 elfcpp::Elf_types<32>::Elf_Addr value, 3207 unsigned char* view, 3208 section_size_type view_size) 3209 { 3210 // leal foo(,%ebx,1),%eax; call ___tls_get_addr 3211 // ==> movl %gs:0,%eax; addl foo@gotntpoff(%ebx),%eax 3212 3213 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2); 3214 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9); 3215 3216 unsigned char op1 = view[-1]; 3217 unsigned char op2 = view[-2]; 3218 3219 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 3220 op2 == 0x8d || op2 == 0x04); 3221 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8); 3222 3223 int roff = 5; 3224 3225 // FIXME: For now, support only the first (SIB) form. 3226 tls::check_tls(relinfo, relnum, rel.get_r_offset(), op2 == 0x04); 3227 3228 if (op2 == 0x04) 3229 { 3230 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3); 3231 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d); 3232 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 3233 ((op1 & 0xc7) == 0x05 && op1 != (4 << 3))); 3234 memcpy(view - 3, "\x65\xa1\0\0\0\0\x03\x83\0\0\0", 12); 3235 } 3236 else 3237 { 3238 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 3239 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4); 3240 if (rel.get_r_offset() + 9 < view_size 3241 && view[9] == 0x90) 3242 { 3243 // FIXME: This is not the right instruction sequence. 3244 // There is a trailing nop. Use the size byte subl. 3245 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12); 3246 roff = 6; 3247 } 3248 else 3249 { 3250 // FIXME: This is not the right instruction sequence. 3251 // Use the five byte subl. 3252 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11); 3253 } 3254 } 3255 3256 Relocate_functions<32, false>::rel32(view + roff, value); 3257 3258 // The next reloc should be a PLT32 reloc against __tls_get_addr. 3259 // We can skip it. 3260 this->skip_call_tls_get_addr_ = true; 3261 } 3262 3263 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL 3264 // General-Dynamic to a Local-Exec. 3265 3266 inline void 3267 Target_i386::Relocate::tls_desc_gd_to_le( 3268 const Relocate_info<32, false>* relinfo, 3269 size_t relnum, 3270 Output_segment* tls_segment, 3271 const elfcpp::Rel<32, false>& rel, 3272 unsigned int r_type, 3273 elfcpp::Elf_types<32>::Elf_Addr value, 3274 unsigned char* view, 3275 section_size_type view_size) 3276 { 3277 if (r_type == elfcpp::R_386_TLS_GOTDESC) 3278 { 3279 // leal foo@TLSDESC(%ebx), %eax 3280 // ==> leal foo@NTPOFF, %eax 3281 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2); 3282 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4); 3283 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 3284 view[-2] == 0x8d && view[-1] == 0x83); 3285 view[-1] = 0x05; 3286 value -= tls_segment->memsz(); 3287 Relocate_functions<32, false>::rel32(view, value); 3288 } 3289 else 3290 { 3291 // call *foo@TLSCALL(%eax) 3292 // ==> nop; nop 3293 gold_assert(r_type == elfcpp::R_386_TLS_DESC_CALL); 3294 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 2); 3295 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 3296 view[0] == 0xff && view[1] == 0x10); 3297 view[0] = 0x66; 3298 view[1] = 0x90; 3299 } 3300 } 3301 3302 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL 3303 // General-Dynamic to an Initial-Exec. 3304 3305 inline void 3306 Target_i386::Relocate::tls_desc_gd_to_ie( 3307 const Relocate_info<32, false>* relinfo, 3308 size_t relnum, 3309 Output_segment*, 3310 const elfcpp::Rel<32, false>& rel, 3311 unsigned int r_type, 3312 elfcpp::Elf_types<32>::Elf_Addr value, 3313 unsigned char* view, 3314 section_size_type view_size) 3315 { 3316 if (r_type == elfcpp::R_386_TLS_GOTDESC) 3317 { 3318 // leal foo@TLSDESC(%ebx), %eax 3319 // ==> movl foo@GOTNTPOFF(%ebx), %eax 3320 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2); 3321 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4); 3322 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 3323 view[-2] == 0x8d && view[-1] == 0x83); 3324 view[-2] = 0x8b; 3325 Relocate_functions<32, false>::rel32(view, value); 3326 } 3327 else 3328 { 3329 // call *foo@TLSCALL(%eax) 3330 // ==> nop; nop 3331 gold_assert(r_type == elfcpp::R_386_TLS_DESC_CALL); 3332 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 2); 3333 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 3334 view[0] == 0xff && view[1] == 0x10); 3335 view[0] = 0x66; 3336 view[1] = 0x90; 3337 } 3338 } 3339 3340 // Do a relocation in which we convert a TLS Local-Dynamic to a 3341 // Local-Exec. 3342 3343 inline void 3344 Target_i386::Relocate::tls_ld_to_le(const Relocate_info<32, false>* relinfo, 3345 size_t relnum, 3346 Output_segment*, 3347 const elfcpp::Rel<32, false>& rel, 3348 unsigned int, 3349 elfcpp::Elf_types<32>::Elf_Addr, 3350 unsigned char* view, 3351 section_size_type view_size) 3352 { 3353 // leal foo(%reg), %eax; call ___tls_get_addr 3354 // ==> movl %gs:0,%eax; nop; leal 0(%esi,1),%esi 3355 3356 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2); 3357 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9); 3358 3359 // FIXME: Does this test really always pass? 3360 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 3361 view[-2] == 0x8d && view[-1] == 0x83); 3362 3363 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8); 3364 3365 memcpy(view - 2, "\x65\xa1\0\0\0\0\x90\x8d\x74\x26\0", 11); 3366 3367 // The next reloc should be a PLT32 reloc against __tls_get_addr. 3368 // We can skip it. 3369 this->skip_call_tls_get_addr_ = true; 3370 } 3371 3372 // Do a relocation in which we convert a TLS Initial-Exec to a 3373 // Local-Exec. 3374 3375 inline void 3376 Target_i386::Relocate::tls_ie_to_le(const Relocate_info<32, false>* relinfo, 3377 size_t relnum, 3378 Output_segment* tls_segment, 3379 const elfcpp::Rel<32, false>& rel, 3380 unsigned int r_type, 3381 elfcpp::Elf_types<32>::Elf_Addr value, 3382 unsigned char* view, 3383 section_size_type view_size) 3384 { 3385 // We have to actually change the instructions, which means that we 3386 // need to examine the opcodes to figure out which instruction we 3387 // are looking at. 3388 if (r_type == elfcpp::R_386_TLS_IE) 3389 { 3390 // movl %gs:XX,%eax ==> movl $YY,%eax 3391 // movl %gs:XX,%reg ==> movl $YY,%reg 3392 // addl %gs:XX,%reg ==> addl $YY,%reg 3393 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -1); 3394 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4); 3395 3396 unsigned char op1 = view[-1]; 3397 if (op1 == 0xa1) 3398 { 3399 // movl XX,%eax ==> movl $YY,%eax 3400 view[-1] = 0xb8; 3401 } 3402 else 3403 { 3404 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2); 3405 3406 unsigned char op2 = view[-2]; 3407 if (op2 == 0x8b) 3408 { 3409 // movl XX,%reg ==> movl $YY,%reg 3410 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 3411 (op1 & 0xc7) == 0x05); 3412 view[-2] = 0xc7; 3413 view[-1] = 0xc0 | ((op1 >> 3) & 7); 3414 } 3415 else if (op2 == 0x03) 3416 { 3417 // addl XX,%reg ==> addl $YY,%reg 3418 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 3419 (op1 & 0xc7) == 0x05); 3420 view[-2] = 0x81; 3421 view[-1] = 0xc0 | ((op1 >> 3) & 7); 3422 } 3423 else 3424 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0); 3425 } 3426 } 3427 else 3428 { 3429 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2 3430 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2 3431 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2 3432 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2); 3433 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4); 3434 3435 unsigned char op1 = view[-1]; 3436 unsigned char op2 = view[-2]; 3437 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 3438 (op1 & 0xc0) == 0x80 && (op1 & 7) != 4); 3439 if (op2 == 0x8b) 3440 { 3441 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2 3442 view[-2] = 0xc7; 3443 view[-1] = 0xc0 | ((op1 >> 3) & 7); 3444 } 3445 else if (op2 == 0x2b) 3446 { 3447 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2 3448 view[-2] = 0x81; 3449 view[-1] = 0xe8 | ((op1 >> 3) & 7); 3450 } 3451 else if (op2 == 0x03) 3452 { 3453 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2 3454 view[-2] = 0x81; 3455 view[-1] = 0xc0 | ((op1 >> 3) & 7); 3456 } 3457 else 3458 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0); 3459 } 3460 3461 value = tls_segment->memsz() - value; 3462 if (r_type == elfcpp::R_386_TLS_IE || r_type == elfcpp::R_386_TLS_GOTIE) 3463 value = - value; 3464 3465 Relocate_functions<32, false>::rel32(view, value); 3466 } 3467 3468 // Relocate section data. 3469 3470 void 3471 Target_i386::relocate_section(const Relocate_info<32, false>* relinfo, 3472 unsigned int sh_type, 3473 const unsigned char* prelocs, 3474 size_t reloc_count, 3475 Output_section* output_section, 3476 bool needs_special_offset_handling, 3477 unsigned char* view, 3478 elfcpp::Elf_types<32>::Elf_Addr address, 3479 section_size_type view_size, 3480 const Reloc_symbol_changes* reloc_symbol_changes) 3481 { 3482 gold_assert(sh_type == elfcpp::SHT_REL); 3483 3484 gold::relocate_section<32, false, Target_i386, elfcpp::SHT_REL, 3485 Target_i386::Relocate>( 3486 relinfo, 3487 this, 3488 prelocs, 3489 reloc_count, 3490 output_section, 3491 needs_special_offset_handling, 3492 view, 3493 address, 3494 view_size, 3495 reloc_symbol_changes); 3496 } 3497 3498 // Return the size of a relocation while scanning during a relocatable 3499 // link. 3500 3501 unsigned int 3502 Target_i386::Relocatable_size_for_reloc::get_size_for_reloc( 3503 unsigned int r_type, 3504 Relobj* object) 3505 { 3506 switch (r_type) 3507 { 3508 case elfcpp::R_386_NONE: 3509 case elfcpp::R_386_GNU_VTINHERIT: 3510 case elfcpp::R_386_GNU_VTENTRY: 3511 case elfcpp::R_386_TLS_GD: // Global-dynamic 3512 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url) 3513 case elfcpp::R_386_TLS_DESC_CALL: 3514 case elfcpp::R_386_TLS_LDM: // Local-dynamic 3515 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic 3516 case elfcpp::R_386_TLS_IE: // Initial-exec 3517 case elfcpp::R_386_TLS_IE_32: 3518 case elfcpp::R_386_TLS_GOTIE: 3519 case elfcpp::R_386_TLS_LE: // Local-exec 3520 case elfcpp::R_386_TLS_LE_32: 3521 return 0; 3522 3523 case elfcpp::R_386_32: 3524 case elfcpp::R_386_PC32: 3525 case elfcpp::R_386_GOT32: 3526 case elfcpp::R_386_PLT32: 3527 case elfcpp::R_386_GOTOFF: 3528 case elfcpp::R_386_GOTPC: 3529 return 4; 3530 3531 case elfcpp::R_386_16: 3532 case elfcpp::R_386_PC16: 3533 return 2; 3534 3535 case elfcpp::R_386_8: 3536 case elfcpp::R_386_PC8: 3537 return 1; 3538 3539 // These are relocations which should only be seen by the 3540 // dynamic linker, and should never be seen here. 3541 case elfcpp::R_386_COPY: 3542 case elfcpp::R_386_GLOB_DAT: 3543 case elfcpp::R_386_JUMP_SLOT: 3544 case elfcpp::R_386_RELATIVE: 3545 case elfcpp::R_386_IRELATIVE: 3546 case elfcpp::R_386_TLS_TPOFF: 3547 case elfcpp::R_386_TLS_DTPMOD32: 3548 case elfcpp::R_386_TLS_DTPOFF32: 3549 case elfcpp::R_386_TLS_TPOFF32: 3550 case elfcpp::R_386_TLS_DESC: 3551 object->error(_("unexpected reloc %u in object file"), r_type); 3552 return 0; 3553 3554 case elfcpp::R_386_32PLT: 3555 case elfcpp::R_386_TLS_GD_32: 3556 case elfcpp::R_386_TLS_GD_PUSH: 3557 case elfcpp::R_386_TLS_GD_CALL: 3558 case elfcpp::R_386_TLS_GD_POP: 3559 case elfcpp::R_386_TLS_LDM_32: 3560 case elfcpp::R_386_TLS_LDM_PUSH: 3561 case elfcpp::R_386_TLS_LDM_CALL: 3562 case elfcpp::R_386_TLS_LDM_POP: 3563 case elfcpp::R_386_USED_BY_INTEL_200: 3564 default: 3565 object->error(_("unsupported reloc %u in object file"), r_type); 3566 return 0; 3567 } 3568 } 3569 3570 // Scan the relocs during a relocatable link. 3571 3572 void 3573 Target_i386::scan_relocatable_relocs(Symbol_table* symtab, 3574 Layout* layout, 3575 Sized_relobj_file<32, false>* object, 3576 unsigned int data_shndx, 3577 unsigned int sh_type, 3578 const unsigned char* prelocs, 3579 size_t reloc_count, 3580 Output_section* output_section, 3581 bool needs_special_offset_handling, 3582 size_t local_symbol_count, 3583 const unsigned char* plocal_symbols, 3584 Relocatable_relocs* rr) 3585 { 3586 gold_assert(sh_type == elfcpp::SHT_REL); 3587 3588 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL, 3589 Relocatable_size_for_reloc> Scan_relocatable_relocs; 3590 3591 gold::scan_relocatable_relocs<32, false, elfcpp::SHT_REL, 3592 Scan_relocatable_relocs>( 3593 symtab, 3594 layout, 3595 object, 3596 data_shndx, 3597 prelocs, 3598 reloc_count, 3599 output_section, 3600 needs_special_offset_handling, 3601 local_symbol_count, 3602 plocal_symbols, 3603 rr); 3604 } 3605 3606 // Relocate a section during a relocatable link. 3607 3608 void 3609 Target_i386::relocate_for_relocatable( 3610 const Relocate_info<32, false>* relinfo, 3611 unsigned int sh_type, 3612 const unsigned char* prelocs, 3613 size_t reloc_count, 3614 Output_section* output_section, 3615 elfcpp::Elf_types<32>::Elf_Off offset_in_output_section, 3616 const Relocatable_relocs* rr, 3617 unsigned char* view, 3618 elfcpp::Elf_types<32>::Elf_Addr view_address, 3619 section_size_type view_size, 3620 unsigned char* reloc_view, 3621 section_size_type reloc_view_size) 3622 { 3623 gold_assert(sh_type == elfcpp::SHT_REL); 3624 3625 gold::relocate_for_relocatable<32, false, elfcpp::SHT_REL>( 3626 relinfo, 3627 prelocs, 3628 reloc_count, 3629 output_section, 3630 offset_in_output_section, 3631 rr, 3632 view, 3633 view_address, 3634 view_size, 3635 reloc_view, 3636 reloc_view_size); 3637 } 3638 3639 // Return the value to use for a dynamic which requires special 3640 // treatment. This is how we support equality comparisons of function 3641 // pointers across shared library boundaries, as described in the 3642 // processor specific ABI supplement. 3643 3644 uint64_t 3645 Target_i386::do_dynsym_value(const Symbol* gsym) const 3646 { 3647 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset()); 3648 return this->plt_address_for_global(gsym) + gsym->plt_offset(); 3649 } 3650 3651 // Return a string used to fill a code section with nops to take up 3652 // the specified length. 3653 3654 std::string 3655 Target_i386::do_code_fill(section_size_type length) const 3656 { 3657 if (length >= 16) 3658 { 3659 // Build a jmp instruction to skip over the bytes. 3660 unsigned char jmp[5]; 3661 jmp[0] = 0xe9; 3662 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5); 3663 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5) 3664 + std::string(length - 5, static_cast<char>(0x90))); 3665 } 3666 3667 // Nop sequences of various lengths. 3668 const char nop1[1] = { '\x90' }; // nop 3669 const char nop2[2] = { '\x66', '\x90' }; // xchg %ax %ax 3670 const char nop3[3] = { '\x8d', '\x76', '\x00' }; // leal 0(%esi),%esi 3671 const char nop4[4] = { '\x8d', '\x74', '\x26', // leal 0(%esi,1),%esi 3672 '\x00'}; 3673 const char nop5[5] = { '\x90', '\x8d', '\x74', // nop 3674 '\x26', '\x00' }; // leal 0(%esi,1),%esi 3675 const char nop6[6] = { '\x8d', '\xb6', '\x00', // leal 0L(%esi),%esi 3676 '\x00', '\x00', '\x00' }; 3677 const char nop7[7] = { '\x8d', '\xb4', '\x26', // leal 0L(%esi,1),%esi 3678 '\x00', '\x00', '\x00', 3679 '\x00' }; 3680 const char nop8[8] = { '\x90', '\x8d', '\xb4', // nop 3681 '\x26', '\x00', '\x00', // leal 0L(%esi,1),%esi 3682 '\x00', '\x00' }; 3683 const char nop9[9] = { '\x89', '\xf6', '\x8d', // movl %esi,%esi 3684 '\xbc', '\x27', '\x00', // leal 0L(%edi,1),%edi 3685 '\x00', '\x00', '\x00' }; 3686 const char nop10[10] = { '\x8d', '\x76', '\x00', // leal 0(%esi),%esi 3687 '\x8d', '\xbc', '\x27', // leal 0L(%edi,1),%edi 3688 '\x00', '\x00', '\x00', 3689 '\x00' }; 3690 const char nop11[11] = { '\x8d', '\x74', '\x26', // leal 0(%esi,1),%esi 3691 '\x00', '\x8d', '\xbc', // leal 0L(%edi,1),%edi 3692 '\x27', '\x00', '\x00', 3693 '\x00', '\x00' }; 3694 const char nop12[12] = { '\x8d', '\xb6', '\x00', // leal 0L(%esi),%esi 3695 '\x00', '\x00', '\x00', // leal 0L(%edi),%edi 3696 '\x8d', '\xbf', '\x00', 3697 '\x00', '\x00', '\x00' }; 3698 const char nop13[13] = { '\x8d', '\xb6', '\x00', // leal 0L(%esi),%esi 3699 '\x00', '\x00', '\x00', // leal 0L(%edi,1),%edi 3700 '\x8d', '\xbc', '\x27', 3701 '\x00', '\x00', '\x00', 3702 '\x00' }; 3703 const char nop14[14] = { '\x8d', '\xb4', '\x26', // leal 0L(%esi,1),%esi 3704 '\x00', '\x00', '\x00', // leal 0L(%edi,1),%edi 3705 '\x00', '\x8d', '\xbc', 3706 '\x27', '\x00', '\x00', 3707 '\x00', '\x00' }; 3708 const char nop15[15] = { '\xeb', '\x0d', '\x90', // jmp .+15 3709 '\x90', '\x90', '\x90', // nop,nop,nop,... 3710 '\x90', '\x90', '\x90', 3711 '\x90', '\x90', '\x90', 3712 '\x90', '\x90', '\x90' }; 3713 3714 const char* nops[16] = { 3715 NULL, 3716 nop1, nop2, nop3, nop4, nop5, nop6, nop7, 3717 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15 3718 }; 3719 3720 return std::string(nops[length], length); 3721 } 3722 3723 // Return the value to use for the base of a DW_EH_PE_datarel offset 3724 // in an FDE. Solaris and SVR4 use DW_EH_PE_datarel because their 3725 // assembler can not write out the difference between two labels in 3726 // different sections, so instead of using a pc-relative value they 3727 // use an offset from the GOT. 3728 3729 uint64_t 3730 Target_i386::do_ehframe_datarel_base() const 3731 { 3732 gold_assert(this->global_offset_table_ != NULL); 3733 Symbol* sym = this->global_offset_table_; 3734 Sized_symbol<32>* ssym = static_cast<Sized_symbol<32>*>(sym); 3735 return ssym->value(); 3736 } 3737 3738 // Return whether SYM should be treated as a call to a non-split 3739 // function. We don't want that to be true of a call to a 3740 // get_pc_thunk function. 3741 3742 bool 3743 Target_i386::do_is_call_to_non_split(const Symbol* sym, unsigned int) const 3744 { 3745 return (sym->type() == elfcpp::STT_FUNC 3746 && !is_prefix_of("__i686.get_pc_thunk.", sym->name())); 3747 } 3748 3749 // FNOFFSET in section SHNDX in OBJECT is the start of a function 3750 // compiled with -fsplit-stack. The function calls non-split-stack 3751 // code. We have to change the function so that it always ensures 3752 // that it has enough stack space to run some random function. 3753 3754 void 3755 Target_i386::do_calls_non_split(Relobj* object, unsigned int shndx, 3756 section_offset_type fnoffset, 3757 section_size_type fnsize, 3758 unsigned char* view, 3759 section_size_type view_size, 3760 std::string* from, 3761 std::string* to) const 3762 { 3763 // The function starts with a comparison of the stack pointer and a 3764 // field in the TCB. This is followed by a jump. 3765 3766 // cmp %gs:NN,%esp 3767 if (this->match_view(view, view_size, fnoffset, "\x65\x3b\x25", 3) 3768 && fnsize > 7) 3769 { 3770 // We will call __morestack if the carry flag is set after this 3771 // comparison. We turn the comparison into an stc instruction 3772 // and some nops. 3773 view[fnoffset] = '\xf9'; 3774 this->set_view_to_nop(view, view_size, fnoffset + 1, 6); 3775 } 3776 // lea NN(%esp),%ecx 3777 // lea NN(%esp),%edx 3778 else if ((this->match_view(view, view_size, fnoffset, "\x8d\x8c\x24", 3) 3779 || this->match_view(view, view_size, fnoffset, "\x8d\x94\x24", 3)) 3780 && fnsize > 7) 3781 { 3782 // This is loading an offset from the stack pointer for a 3783 // comparison. The offset is negative, so we decrease the 3784 // offset by the amount of space we need for the stack. This 3785 // means we will avoid calling __morestack if there happens to 3786 // be plenty of space on the stack already. 3787 unsigned char* pval = view + fnoffset + 3; 3788 uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval); 3789 val -= parameters->options().split_stack_adjust_size(); 3790 elfcpp::Swap_unaligned<32, false>::writeval(pval, val); 3791 } 3792 else 3793 { 3794 if (!object->has_no_split_stack()) 3795 object->error(_("failed to match split-stack sequence at " 3796 "section %u offset %0zx"), 3797 shndx, static_cast<size_t>(fnoffset)); 3798 return; 3799 } 3800 3801 // We have to change the function so that it calls 3802 // __morestack_non_split instead of __morestack. The former will 3803 // allocate additional stack space. 3804 *from = "__morestack"; 3805 *to = "__morestack_non_split"; 3806 } 3807 3808 // The selector for i386 object files. Note this is never instantiated 3809 // directly. It's only used in Target_selector_i386_nacl, below. 3810 3811 class Target_selector_i386 : public Target_selector_freebsd 3812 { 3813 public: 3814 Target_selector_i386() 3815 : Target_selector_freebsd(elfcpp::EM_386, 32, false, 3816 "elf32-i386", "elf32-i386-freebsd", 3817 "elf_i386") 3818 { } 3819 3820 Target* 3821 do_instantiate_target() 3822 { return new Target_i386(); } 3823 }; 3824 3825 // NaCl variant. It uses different PLT contents. 3826 3827 class Output_data_plt_i386_nacl : public Output_data_plt_i386 3828 { 3829 public: 3830 Output_data_plt_i386_nacl(Layout* layout, 3831 Output_data_space* got_plt, 3832 Output_data_space* got_irelative) 3833 : Output_data_plt_i386(layout, plt_entry_size, got_plt, got_irelative) 3834 { } 3835 3836 protected: 3837 virtual unsigned int 3838 do_get_plt_entry_size() const 3839 { return plt_entry_size; } 3840 3841 virtual void 3842 do_add_eh_frame(Layout* layout) 3843 { 3844 layout->add_eh_frame_for_plt(this, plt_eh_frame_cie, plt_eh_frame_cie_size, 3845 plt_eh_frame_fde, plt_eh_frame_fde_size); 3846 } 3847 3848 // The size of an entry in the PLT. 3849 static const int plt_entry_size = 64; 3850 3851 // The .eh_frame unwind information for the PLT. 3852 static const int plt_eh_frame_fde_size = 32; 3853 static const unsigned char plt_eh_frame_fde[plt_eh_frame_fde_size]; 3854 }; 3855 3856 class Output_data_plt_i386_nacl_exec : public Output_data_plt_i386_nacl 3857 { 3858 public: 3859 Output_data_plt_i386_nacl_exec(Layout* layout, 3860 Output_data_space* got_plt, 3861 Output_data_space* got_irelative) 3862 : Output_data_plt_i386_nacl(layout, got_plt, got_irelative) 3863 { } 3864 3865 protected: 3866 virtual void 3867 do_fill_first_plt_entry(unsigned char* pov, 3868 elfcpp::Elf_types<32>::Elf_Addr got_address); 3869 3870 virtual unsigned int 3871 do_fill_plt_entry(unsigned char* pov, 3872 elfcpp::Elf_types<32>::Elf_Addr got_address, 3873 unsigned int got_offset, 3874 unsigned int plt_offset, 3875 unsigned int plt_rel_offset); 3876 3877 private: 3878 // The first entry in the PLT for an executable. 3879 static const unsigned char first_plt_entry[plt_entry_size]; 3880 3881 // Other entries in the PLT for an executable. 3882 static const unsigned char plt_entry[plt_entry_size]; 3883 }; 3884 3885 class Output_data_plt_i386_nacl_dyn : public Output_data_plt_i386_nacl 3886 { 3887 public: 3888 Output_data_plt_i386_nacl_dyn(Layout* layout, 3889 Output_data_space* got_plt, 3890 Output_data_space* got_irelative) 3891 : Output_data_plt_i386_nacl(layout, got_plt, got_irelative) 3892 { } 3893 3894 protected: 3895 virtual void 3896 do_fill_first_plt_entry(unsigned char* pov, elfcpp::Elf_types<32>::Elf_Addr); 3897 3898 virtual unsigned int 3899 do_fill_plt_entry(unsigned char* pov, 3900 elfcpp::Elf_types<32>::Elf_Addr, 3901 unsigned int got_offset, 3902 unsigned int plt_offset, 3903 unsigned int plt_rel_offset); 3904 3905 private: 3906 // The first entry in the PLT for a shared object. 3907 static const unsigned char first_plt_entry[plt_entry_size]; 3908 3909 // Other entries in the PLT for a shared object. 3910 static const unsigned char plt_entry[plt_entry_size]; 3911 }; 3912 3913 class Target_i386_nacl : public Target_i386 3914 { 3915 public: 3916 Target_i386_nacl() 3917 : Target_i386(&i386_nacl_info) 3918 { } 3919 3920 protected: 3921 virtual Output_data_plt_i386* 3922 do_make_data_plt(Layout* layout, 3923 Output_data_space* got_plt, 3924 Output_data_space* got_irelative, 3925 bool dyn) 3926 { 3927 if (dyn) 3928 return new Output_data_plt_i386_nacl_dyn(layout, got_plt, got_irelative); 3929 else 3930 return new Output_data_plt_i386_nacl_exec(layout, got_plt, got_irelative); 3931 } 3932 3933 private: 3934 static const Target::Target_info i386_nacl_info; 3935 }; 3936 3937 const Target::Target_info Target_i386_nacl::i386_nacl_info = 3938 { 3939 32, // size 3940 false, // is_big_endian 3941 elfcpp::EM_386, // machine_code 3942 false, // has_make_symbol 3943 false, // has_resolve 3944 true, // has_code_fill 3945 true, // is_default_stack_executable 3946 true, // can_icf_inline_merge_sections 3947 '\0', // wrap_char 3948 "/lib/ld-nacl-x86-32.so.1", // dynamic_linker 3949 0x20000, // default_text_segment_address 3950 0x10000, // abi_pagesize (overridable by -z max-page-size) 3951 0x10000, // common_pagesize (overridable by -z common-page-size) 3952 true, // isolate_execinstr 3953 0x10000000, // rosegment_gap 3954 elfcpp::SHN_UNDEF, // small_common_shndx 3955 elfcpp::SHN_UNDEF, // large_common_shndx 3956 0, // small_common_section_flags 3957 0, // large_common_section_flags 3958 NULL, // attributes_section 3959 NULL // attributes_vendor 3960 }; 3961 3962 #define NACLMASK 0xe0 // 32-byte alignment mask 3963 3964 const unsigned char 3965 Output_data_plt_i386_nacl_exec::first_plt_entry[plt_entry_size] = 3966 { 3967 0xff, 0x35, // pushl contents of memory address 3968 0, 0, 0, 0, // replaced with address of .got + 4 3969 0x8b, 0x0d, // movl contents of address, %ecx 3970 0, 0, 0, 0, // replaced with address of .got + 8 3971 0x83, 0xe1, NACLMASK, // andl $NACLMASK, %ecx 3972 0xff, 0xe1, // jmp *%ecx 3973 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, // nops 3974 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, // nops 3975 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, // nops 3976 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, // nops 3977 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, // nops 3978 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, // nops 3979 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, // nops 3980 0x90, 0x90, 0x90, 0x90, 0x90 3981 }; 3982 3983 void 3984 Output_data_plt_i386_nacl_exec::do_fill_first_plt_entry( 3985 unsigned char* pov, 3986 elfcpp::Elf_types<32>::Elf_Addr got_address) 3987 { 3988 memcpy(pov, first_plt_entry, plt_entry_size); 3989 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_address + 4); 3990 elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 8); 3991 } 3992 3993 // The first entry in the PLT for a shared object. 3994 3995 const unsigned char 3996 Output_data_plt_i386_nacl_dyn::first_plt_entry[plt_entry_size] = 3997 { 3998 0xff, 0xb3, 4, 0, 0, 0, // pushl 4(%ebx) 3999 0x8b, 0x4b, 0x08, // mov 0x8(%ebx), %ecx 4000 0x83, 0xe1, NACLMASK, // andl $NACLMASK, %ecx 4001 0xff, 0xe1, // jmp *%ecx 4002 0x90, 0x90, 0x90, 0x90, 0x90, // nops 4003 0x90, 0x90, 0x90, 0x90, 0x90, // nops 4004 0x90, 0x90, 0x90, 0x90, 0x90, // nops 4005 0x90, 0x90, 0x90, 0x90, 0x90, // nops 4006 0x90, 0x90, 0x90, 0x90, 0x90, // nops 4007 0x90, 0x90, 0x90, 0x90, 0x90, // nops 4008 0x90, 0x90, 0x90, 0x90, 0x90, // nops 4009 0x90, 0x90, 0x90, 0x90, 0x90, // nops 4010 0x90, 0x90, 0x90, 0x90, 0x90, // nops 4011 0x90, 0x90, 0x90, 0x90, 0x90 // nops 4012 }; 4013 4014 void 4015 Output_data_plt_i386_nacl_dyn::do_fill_first_plt_entry( 4016 unsigned char* pov, 4017 elfcpp::Elf_types<32>::Elf_Addr) 4018 { 4019 memcpy(pov, first_plt_entry, plt_entry_size); 4020 } 4021 4022 // Subsequent entries in the PLT for an executable. 4023 4024 const unsigned char 4025 Output_data_plt_i386_nacl_exec::plt_entry[plt_entry_size] = 4026 { 4027 0x8b, 0x0d, // movl contents of address, %ecx */ 4028 0, 0, 0, 0, // replaced with address of symbol in .got 4029 0x83, 0xe1, NACLMASK, // andl $NACLMASK, %ecx 4030 0xff, 0xe1, // jmp *%ecx 4031 4032 // Pad to the next 32-byte boundary with nop instructions. 4033 0x90, 4034 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 4035 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 4036 4037 // Lazy GOT entries point here (32-byte aligned). 4038 0x68, // pushl immediate 4039 0, 0, 0, 0, // replaced with offset into relocation table 4040 0xe9, // jmp relative 4041 0, 0, 0, 0, // replaced with offset to start of .plt 4042 4043 // Pad to the next 32-byte boundary with nop instructions. 4044 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 4045 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 4046 0x90, 0x90 4047 }; 4048 4049 unsigned int 4050 Output_data_plt_i386_nacl_exec::do_fill_plt_entry( 4051 unsigned char* pov, 4052 elfcpp::Elf_types<32>::Elf_Addr got_address, 4053 unsigned int got_offset, 4054 unsigned int plt_offset, 4055 unsigned int plt_rel_offset) 4056 { 4057 memcpy(pov, plt_entry, plt_entry_size); 4058 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, 4059 got_address + got_offset); 4060 elfcpp::Swap_unaligned<32, false>::writeval(pov + 33, plt_rel_offset); 4061 elfcpp::Swap<32, false>::writeval(pov + 38, - (plt_offset + 38 + 4)); 4062 return 32; 4063 } 4064 4065 // Subsequent entries in the PLT for a shared object. 4066 4067 const unsigned char 4068 Output_data_plt_i386_nacl_dyn::plt_entry[plt_entry_size] = 4069 { 4070 0x8b, 0x8b, // movl offset(%ebx), %ecx 4071 0, 0, 0, 0, // replaced with offset of symbol in .got 4072 0x83, 0xe1, 0xe0, // andl $NACLMASK, %ecx 4073 0xff, 0xe1, // jmp *%ecx 4074 4075 // Pad to the next 32-byte boundary with nop instructions. 4076 0x90, 4077 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 4078 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 4079 4080 // Lazy GOT entries point here (32-byte aligned). 4081 0x68, // pushl immediate 4082 0, 0, 0, 0, // replaced with offset into relocation table. 4083 0xe9, // jmp relative 4084 0, 0, 0, 0, // replaced with offset to start of .plt. 4085 4086 // Pad to the next 32-byte boundary with nop instructions. 4087 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 4088 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 4089 0x90, 0x90 4090 }; 4091 4092 unsigned int 4093 Output_data_plt_i386_nacl_dyn::do_fill_plt_entry( 4094 unsigned char* pov, 4095 elfcpp::Elf_types<32>::Elf_Addr, 4096 unsigned int got_offset, 4097 unsigned int plt_offset, 4098 unsigned int plt_rel_offset) 4099 { 4100 memcpy(pov, plt_entry, plt_entry_size); 4101 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_offset); 4102 elfcpp::Swap_unaligned<32, false>::writeval(pov + 33, plt_rel_offset); 4103 elfcpp::Swap<32, false>::writeval(pov + 38, - (plt_offset + 38 + 4)); 4104 return 32; 4105 } 4106 4107 const unsigned char 4108 Output_data_plt_i386_nacl::plt_eh_frame_fde[plt_eh_frame_fde_size] = 4109 { 4110 0, 0, 0, 0, // Replaced with offset to .plt. 4111 0, 0, 0, 0, // Replaced with size of .plt. 4112 0, // Augmentation size. 4113 elfcpp::DW_CFA_def_cfa_offset, 8, // DW_CFA_def_cfa_offset: 8. 4114 elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6. 4115 elfcpp::DW_CFA_def_cfa_offset, 12, // DW_CFA_def_cfa_offset: 12. 4116 elfcpp::DW_CFA_advance_loc + 58, // Advance 58 to __PLT__ + 64. 4117 elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression. 4118 13, // Block length. 4119 elfcpp::DW_OP_breg4, 4, // Push %esp + 4. 4120 elfcpp::DW_OP_breg8, 0, // Push %eip. 4121 elfcpp::DW_OP_const1u, 63, // Push 0x3f. 4122 elfcpp::DW_OP_and, // & (%eip & 0x3f). 4123 elfcpp::DW_OP_const1u, 37, // Push 0x25. 4124 elfcpp::DW_OP_ge, // >= ((%eip & 0x3f) >= 0x25) 4125 elfcpp::DW_OP_lit2, // Push 2. 4126 elfcpp::DW_OP_shl, // << (((%eip & 0x3f) >= 0x25) << 2) 4127 elfcpp::DW_OP_plus, // + ((((%eip&0x3f)>=0x25)<<2)+%esp+4 4128 elfcpp::DW_CFA_nop, // Align to 32 bytes. 4129 elfcpp::DW_CFA_nop 4130 }; 4131 4132 // The selector for i386-nacl object files. 4133 4134 class Target_selector_i386_nacl 4135 : public Target_selector_nacl<Target_selector_i386, Target_i386_nacl> 4136 { 4137 public: 4138 Target_selector_i386_nacl() 4139 : Target_selector_nacl<Target_selector_i386, 4140 Target_i386_nacl>("x86-32", 4141 "elf32-i386-nacl", 4142 "elf_i386_nacl") 4143 { } 4144 }; 4145 4146 Target_selector_i386_nacl target_selector_i386; 4147 4148 } // End anonymous namespace. 4149