1 // object.h -- support for an object file for linking in gold -*- C++ -*- 2 3 // Copyright (C) 2006-2014 Free Software Foundation, Inc. 4 // Written by Ian Lance Taylor <iant (at) google.com>. 5 6 // This file is part of gold. 7 8 // This program is free software; you can redistribute it and/or modify 9 // it under the terms of the GNU General Public License as published by 10 // the Free Software Foundation; either version 3 of the License, or 11 // (at your option) any later version. 12 13 // This program is distributed in the hope that it will be useful, 14 // but WITHOUT ANY WARRANTY; without even the implied warranty of 15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 // GNU General Public License for more details. 17 18 // You should have received a copy of the GNU General Public License 19 // along with this program; if not, write to the Free Software 20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, 21 // MA 02110-1301, USA. 22 23 #ifndef GOLD_OBJECT_H 24 #define GOLD_OBJECT_H 25 26 #include <string> 27 #include <vector> 28 29 #include "elfcpp.h" 30 #include "elfcpp_file.h" 31 #include "fileread.h" 32 #include "target.h" 33 #include "archive.h" 34 35 namespace gold 36 { 37 38 class General_options; 39 class Task; 40 class Cref; 41 class Layout; 42 class Output_data; 43 class Output_section; 44 class Output_file; 45 class Output_symtab_xindex; 46 class Pluginobj; 47 class Dynobj; 48 class Object_merge_map; 49 class Relocatable_relocs; 50 struct Symbols_data; 51 52 template<typename Stringpool_char> 53 class Stringpool_template; 54 55 // Data to pass from read_symbols() to add_symbols(). 56 57 struct Read_symbols_data 58 { 59 Read_symbols_data() 60 : section_headers(NULL), section_names(NULL), symbols(NULL), 61 symbol_names(NULL), versym(NULL), verdef(NULL), verneed(NULL) 62 { } 63 64 ~Read_symbols_data(); 65 66 // Section headers. 67 File_view* section_headers; 68 // Section names. 69 File_view* section_names; 70 // Size of section name data in bytes. 71 section_size_type section_names_size; 72 // Symbol data. 73 File_view* symbols; 74 // Size of symbol data in bytes. 75 section_size_type symbols_size; 76 // Offset of external symbols within symbol data. This structure 77 // sometimes contains only external symbols, in which case this will 78 // be zero. Sometimes it contains all symbols. 79 section_offset_type external_symbols_offset; 80 // Symbol names. 81 File_view* symbol_names; 82 // Size of symbol name data in bytes. 83 section_size_type symbol_names_size; 84 85 // Version information. This is only used on dynamic objects. 86 // Version symbol data (from SHT_GNU_versym section). 87 File_view* versym; 88 section_size_type versym_size; 89 // Version definition data (from SHT_GNU_verdef section). 90 File_view* verdef; 91 section_size_type verdef_size; 92 unsigned int verdef_info; 93 // Needed version data (from SHT_GNU_verneed section). 94 File_view* verneed; 95 section_size_type verneed_size; 96 unsigned int verneed_info; 97 }; 98 99 // Information used to print error messages. 100 101 struct Symbol_location_info 102 { 103 std::string source_file; 104 std::string enclosing_symbol_name; 105 elfcpp::STT enclosing_symbol_type; 106 }; 107 108 // Data about a single relocation section. This is read in 109 // read_relocs and processed in scan_relocs. 110 111 struct Section_relocs 112 { 113 Section_relocs() 114 : contents(NULL) 115 { } 116 117 ~Section_relocs() 118 { delete this->contents; } 119 120 // Index of reloc section. 121 unsigned int reloc_shndx; 122 // Index of section that relocs apply to. 123 unsigned int data_shndx; 124 // Contents of reloc section. 125 File_view* contents; 126 // Reloc section type. 127 unsigned int sh_type; 128 // Number of reloc entries. 129 size_t reloc_count; 130 // Output section. 131 Output_section* output_section; 132 // Whether this section has special handling for offsets. 133 bool needs_special_offset_handling; 134 // Whether the data section is allocated (has the SHF_ALLOC flag set). 135 bool is_data_section_allocated; 136 }; 137 138 // Relocations in an object file. This is read in read_relocs and 139 // processed in scan_relocs. 140 141 struct Read_relocs_data 142 { 143 Read_relocs_data() 144 : local_symbols(NULL) 145 { } 146 147 ~Read_relocs_data() 148 { delete this->local_symbols; } 149 150 typedef std::vector<Section_relocs> Relocs_list; 151 // The relocations. 152 Relocs_list relocs; 153 // The local symbols. 154 File_view* local_symbols; 155 }; 156 157 // The Xindex class manages section indexes for objects with more than 158 // 0xff00 sections. 159 160 class Xindex 161 { 162 public: 163 Xindex(int large_shndx_offset) 164 : large_shndx_offset_(large_shndx_offset), symtab_xindex_() 165 { } 166 167 // Initialize the symtab_xindex_ array, given the object and the 168 // section index of the symbol table to use. 169 template<int size, bool big_endian> 170 void 171 initialize_symtab_xindex(Object*, unsigned int symtab_shndx); 172 173 // Read in the symtab_xindex_ array, given its section index. 174 // PSHDRS may optionally point to the section headers. 175 template<int size, bool big_endian> 176 void 177 read_symtab_xindex(Object*, unsigned int xindex_shndx, 178 const unsigned char* pshdrs); 179 180 // Symbol SYMNDX in OBJECT has a section of SHN_XINDEX; return the 181 // real section index. 182 unsigned int 183 sym_xindex_to_shndx(Object* object, unsigned int symndx); 184 185 private: 186 // The type of the array giving the real section index for symbols 187 // whose st_shndx field holds SHN_XINDEX. 188 typedef std::vector<unsigned int> Symtab_xindex; 189 190 // Adjust a section index if necessary. This should only be called 191 // for ordinary section indexes. 192 unsigned int 193 adjust_shndx(unsigned int shndx) 194 { 195 if (shndx >= elfcpp::SHN_LORESERVE) 196 shndx += this->large_shndx_offset_; 197 return shndx; 198 } 199 200 // Adjust to apply to large section indexes. 201 int large_shndx_offset_; 202 // The data from the SHT_SYMTAB_SHNDX section. 203 Symtab_xindex symtab_xindex_; 204 }; 205 206 // A GOT offset list. A symbol may have more than one GOT offset 207 // (e.g., when mixing modules compiled with two different TLS models), 208 // but will usually have at most one. GOT_TYPE identifies the type of 209 // GOT entry; its values are specific to each target. 210 211 class Got_offset_list 212 { 213 public: 214 Got_offset_list() 215 : got_type_(-1U), got_offset_(0), got_next_(NULL) 216 { } 217 218 Got_offset_list(unsigned int got_type, unsigned int got_offset) 219 : got_type_(got_type), got_offset_(got_offset), got_next_(NULL) 220 { } 221 222 ~Got_offset_list() 223 { 224 if (this->got_next_ != NULL) 225 { 226 delete this->got_next_; 227 this->got_next_ = NULL; 228 } 229 } 230 231 // Initialize the fields to their default values. 232 void 233 init() 234 { 235 this->got_type_ = -1U; 236 this->got_offset_ = 0; 237 this->got_next_ = NULL; 238 } 239 240 // Set the offset for the GOT entry of type GOT_TYPE. 241 void 242 set_offset(unsigned int got_type, unsigned int got_offset) 243 { 244 if (this->got_type_ == -1U) 245 { 246 this->got_type_ = got_type; 247 this->got_offset_ = got_offset; 248 } 249 else 250 { 251 for (Got_offset_list* g = this; g != NULL; g = g->got_next_) 252 { 253 if (g->got_type_ == got_type) 254 { 255 g->got_offset_ = got_offset; 256 return; 257 } 258 } 259 Got_offset_list* g = new Got_offset_list(got_type, got_offset); 260 g->got_next_ = this->got_next_; 261 this->got_next_ = g; 262 } 263 } 264 265 // Return the offset for a GOT entry of type GOT_TYPE. 266 unsigned int 267 get_offset(unsigned int got_type) const 268 { 269 for (const Got_offset_list* g = this; g != NULL; g = g->got_next_) 270 { 271 if (g->got_type_ == got_type) 272 return g->got_offset_; 273 } 274 return -1U; 275 } 276 277 // Return a pointer to the list, or NULL if the list is empty. 278 const Got_offset_list* 279 get_list() const 280 { 281 if (this->got_type_ == -1U) 282 return NULL; 283 return this; 284 } 285 286 // Abstract visitor class for iterating over GOT offsets. 287 class Visitor 288 { 289 public: 290 Visitor() 291 { } 292 293 virtual 294 ~Visitor() 295 { } 296 297 virtual void 298 visit(unsigned int, unsigned int) = 0; 299 }; 300 301 // Loop over all GOT offset entries, calling a visitor class V for each. 302 void 303 for_all_got_offsets(Visitor* v) const 304 { 305 if (this->got_type_ == -1U) 306 return; 307 for (const Got_offset_list* g = this; g != NULL; g = g->got_next_) 308 v->visit(g->got_type_, g->got_offset_); 309 } 310 311 private: 312 unsigned int got_type_; 313 unsigned int got_offset_; 314 Got_offset_list* got_next_; 315 }; 316 317 // Type for mapping section index to uncompressed size and contents. 318 319 struct Compressed_section_info 320 { 321 section_size_type size; 322 const unsigned char* contents; 323 }; 324 typedef std::map<unsigned int, Compressed_section_info> Compressed_section_map; 325 326 template<int size, bool big_endian> 327 Compressed_section_map* 328 build_compressed_section_map(const unsigned char* pshdrs, unsigned int shnum, 329 const char* names, section_size_type names_size, 330 Object* obj, bool decompress_if_needed); 331 332 // Object is an abstract base class which represents either a 32-bit 333 // or a 64-bit input object. This can be a regular object file 334 // (ET_REL) or a shared object (ET_DYN). 335 336 class Object 337 { 338 public: 339 typedef std::vector<Symbol*> Symbols; 340 341 // NAME is the name of the object as we would report it to the user 342 // (e.g., libfoo.a(bar.o) if this is in an archive. INPUT_FILE is 343 // used to read the file. OFFSET is the offset within the input 344 // file--0 for a .o or .so file, something else for a .a file. 345 Object(const std::string& name, Input_file* input_file, bool is_dynamic, 346 off_t offset = 0) 347 : name_(name), input_file_(input_file), offset_(offset), shnum_(-1U), 348 is_dynamic_(is_dynamic), is_needed_(false), uses_split_stack_(false), 349 has_no_split_stack_(false), no_export_(false), 350 is_in_system_directory_(false), as_needed_(false), xindex_(NULL), 351 compressed_sections_(NULL) 352 { 353 if (input_file != NULL) 354 { 355 input_file->file().add_object(); 356 this->is_in_system_directory_ = input_file->is_in_system_directory(); 357 this->as_needed_ = input_file->options().as_needed(); 358 } 359 } 360 361 virtual ~Object() 362 { 363 if (this->input_file_ != NULL) 364 this->input_file_->file().remove_object(); 365 } 366 367 // Return the name of the object as we would report it to the user. 368 const std::string& 369 name() const 370 { return this->name_; } 371 372 // Get the offset into the file. 373 off_t 374 offset() const 375 { return this->offset_; } 376 377 // Return whether this is a dynamic object. 378 bool 379 is_dynamic() const 380 { return this->is_dynamic_; } 381 382 // Return whether this object is needed--true if it is a dynamic 383 // object which defines some symbol referenced by a regular object. 384 // We keep the flag here rather than in Dynobj for convenience when 385 // setting it. 386 bool 387 is_needed() const 388 { return this->is_needed_; } 389 390 // Record that this object is needed. 391 void 392 set_is_needed() 393 { this->is_needed_ = true; } 394 395 // Return whether this object was compiled with -fsplit-stack. 396 bool 397 uses_split_stack() const 398 { return this->uses_split_stack_; } 399 400 // Return whether this object contains any functions compiled with 401 // the no_split_stack attribute. 402 bool 403 has_no_split_stack() const 404 { return this->has_no_split_stack_; } 405 406 // Returns NULL for Objects that are not dynamic objects. This method 407 // is overridden in the Dynobj class. 408 Dynobj* 409 dynobj() 410 { return this->do_dynobj(); } 411 412 // Returns NULL for Objects that are not plugin objects. This method 413 // is overridden in the Pluginobj class. 414 Pluginobj* 415 pluginobj() 416 { return this->do_pluginobj(); } 417 418 // Get the file. We pass on const-ness. 419 Input_file* 420 input_file() 421 { 422 gold_assert(this->input_file_ != NULL); 423 return this->input_file_; 424 } 425 426 const Input_file* 427 input_file() const 428 { 429 gold_assert(this->input_file_ != NULL); 430 return this->input_file_; 431 } 432 433 // Lock the underlying file. 434 void 435 lock(const Task* t) 436 { 437 if (this->input_file_ != NULL) 438 this->input_file_->file().lock(t); 439 } 440 441 // Unlock the underlying file. 442 void 443 unlock(const Task* t) 444 { 445 if (this->input_file_ != NULL) 446 this->input_file()->file().unlock(t); 447 } 448 449 // Return whether the underlying file is locked. 450 bool 451 is_locked() const 452 { return this->input_file_ != NULL && this->input_file_->file().is_locked(); } 453 454 // Return the token, so that the task can be queued. 455 Task_token* 456 token() 457 { 458 if (this->input_file_ == NULL) 459 return NULL; 460 return this->input_file()->file().token(); 461 } 462 463 // Release the underlying file. 464 void 465 release() 466 { 467 if (this->input_file_ != NULL) 468 this->input_file()->file().release(); 469 } 470 471 // Return whether we should just read symbols from this file. 472 bool 473 just_symbols() const 474 { return this->input_file()->just_symbols(); } 475 476 // Return whether this is an incremental object. 477 bool 478 is_incremental() const 479 { return this->do_is_incremental(); } 480 481 // Return the last modified time of the file. 482 Timespec 483 get_mtime() 484 { return this->do_get_mtime(); } 485 486 // Get the number of sections. 487 unsigned int 488 shnum() const 489 { return this->shnum_; } 490 491 // Return a view of the contents of a section. Set *PLEN to the 492 // size. CACHE is a hint as in File_read::get_view. 493 const unsigned char* 494 section_contents(unsigned int shndx, section_size_type* plen, bool cache); 495 496 // Adjust a symbol's section index as needed. SYMNDX is the index 497 // of the symbol and SHNDX is the symbol's section from 498 // get_st_shndx. This returns the section index. It sets 499 // *IS_ORDINARY to indicate whether this is a normal section index, 500 // rather than a special code between SHN_LORESERVE and 501 // SHN_HIRESERVE. 502 unsigned int 503 adjust_sym_shndx(unsigned int symndx, unsigned int shndx, bool* is_ordinary) 504 { 505 if (shndx < elfcpp::SHN_LORESERVE) 506 *is_ordinary = true; 507 else if (shndx == elfcpp::SHN_XINDEX) 508 { 509 if (this->xindex_ == NULL) 510 this->xindex_ = this->do_initialize_xindex(); 511 shndx = this->xindex_->sym_xindex_to_shndx(this, symndx); 512 *is_ordinary = true; 513 } 514 else 515 *is_ordinary = false; 516 return shndx; 517 } 518 519 // Return the size of a section given a section index. 520 uint64_t 521 section_size(unsigned int shndx) 522 { return this->do_section_size(shndx); } 523 524 // Return the name of a section given a section index. 525 std::string 526 section_name(unsigned int shndx) const 527 { return this->do_section_name(shndx); } 528 529 // Return the section flags given a section index. 530 uint64_t 531 section_flags(unsigned int shndx) 532 { return this->do_section_flags(shndx); } 533 534 // Return the section entsize given a section index. 535 uint64_t 536 section_entsize(unsigned int shndx) 537 { return this->do_section_entsize(shndx); } 538 539 // Return the section address given a section index. 540 uint64_t 541 section_address(unsigned int shndx) 542 { return this->do_section_address(shndx); } 543 544 // Return the section type given a section index. 545 unsigned int 546 section_type(unsigned int shndx) 547 { return this->do_section_type(shndx); } 548 549 // Return the section link field given a section index. 550 unsigned int 551 section_link(unsigned int shndx) 552 { return this->do_section_link(shndx); } 553 554 // Return the section info field given a section index. 555 unsigned int 556 section_info(unsigned int shndx) 557 { return this->do_section_info(shndx); } 558 559 // Return the required section alignment given a section index. 560 uint64_t 561 section_addralign(unsigned int shndx) 562 { return this->do_section_addralign(shndx); } 563 564 // Return the output section given a section index. 565 Output_section* 566 output_section(unsigned int shndx) const 567 { return this->do_output_section(shndx); } 568 569 // Given a section index, return its address. 570 // The return value will be -1U if the section is specially mapped, 571 // such as a merge section. 572 uint64_t 573 output_section_address(unsigned int shndx) 574 { return this->do_output_section_address(shndx); } 575 576 // Given a section index, return the offset in the Output_section. 577 // The return value will be -1U if the section is specially mapped, 578 // such as a merge section. 579 uint64_t 580 output_section_offset(unsigned int shndx) const 581 { return this->do_output_section_offset(shndx); } 582 583 // Read the symbol information. 584 void 585 read_symbols(Read_symbols_data* sd) 586 { return this->do_read_symbols(sd); } 587 588 // Pass sections which should be included in the link to the Layout 589 // object, and record where the sections go in the output file. 590 void 591 layout(Symbol_table* symtab, Layout* layout, Read_symbols_data* sd) 592 { this->do_layout(symtab, layout, sd); } 593 594 // Add symbol information to the global symbol table. 595 void 596 add_symbols(Symbol_table* symtab, Read_symbols_data* sd, Layout *layout) 597 { this->do_add_symbols(symtab, sd, layout); } 598 599 // Add symbol information to the global symbol table. 600 Archive::Should_include 601 should_include_member(Symbol_table* symtab, Layout* layout, 602 Read_symbols_data* sd, std::string* why) 603 { return this->do_should_include_member(symtab, layout, sd, why); } 604 605 // Iterate over global symbols, calling a visitor class V for each. 606 void 607 for_all_global_symbols(Read_symbols_data* sd, 608 Library_base::Symbol_visitor_base* v) 609 { return this->do_for_all_global_symbols(sd, v); } 610 611 // Iterate over local symbols, calling a visitor class V for each GOT offset 612 // associated with a local symbol. 613 void 614 for_all_local_got_entries(Got_offset_list::Visitor* v) const 615 { this->do_for_all_local_got_entries(v); } 616 617 // Functions and types for the elfcpp::Elf_file interface. This 618 // permit us to use Object as the File template parameter for 619 // elfcpp::Elf_file. 620 621 // The View class is returned by view. It must support a single 622 // method, data(). This is trivial, because get_view does what we 623 // need. 624 class View 625 { 626 public: 627 View(const unsigned char* p) 628 : p_(p) 629 { } 630 631 const unsigned char* 632 data() const 633 { return this->p_; } 634 635 private: 636 const unsigned char* p_; 637 }; 638 639 // Return a View. 640 View 641 view(off_t file_offset, section_size_type data_size) 642 { return View(this->get_view(file_offset, data_size, true, true)); } 643 644 // Report an error. 645 void 646 error(const char* format, ...) const ATTRIBUTE_PRINTF_2; 647 648 // A location in the file. 649 struct Location 650 { 651 off_t file_offset; 652 off_t data_size; 653 654 Location(off_t fo, section_size_type ds) 655 : file_offset(fo), data_size(ds) 656 { } 657 }; 658 659 // Get a View given a Location. 660 View view(Location loc) 661 { return View(this->get_view(loc.file_offset, loc.data_size, true, true)); } 662 663 // Get a view into the underlying file. 664 const unsigned char* 665 get_view(off_t start, section_size_type size, bool aligned, bool cache) 666 { 667 return this->input_file()->file().get_view(this->offset_, start, size, 668 aligned, cache); 669 } 670 671 // Get a lasting view into the underlying file. 672 File_view* 673 get_lasting_view(off_t start, section_size_type size, bool aligned, 674 bool cache) 675 { 676 return this->input_file()->file().get_lasting_view(this->offset_, start, 677 size, aligned, cache); 678 } 679 680 // Read data from the underlying file. 681 void 682 read(off_t start, section_size_type size, void* p) 683 { this->input_file()->file().read(start + this->offset_, size, p); } 684 685 // Read multiple data from the underlying file. 686 void 687 read_multiple(const File_read::Read_multiple& rm) 688 { this->input_file()->file().read_multiple(this->offset_, rm); } 689 690 // Stop caching views in the underlying file. 691 void 692 clear_view_cache_marks() 693 { 694 if (this->input_file_ != NULL) 695 this->input_file_->file().clear_view_cache_marks(); 696 } 697 698 // Get the number of global symbols defined by this object, and the 699 // number of the symbols whose final definition came from this 700 // object. 701 void 702 get_global_symbol_counts(const Symbol_table* symtab, size_t* defined, 703 size_t* used) const 704 { this->do_get_global_symbol_counts(symtab, defined, used); } 705 706 // Get the symbols defined in this object. 707 const Symbols* 708 get_global_symbols() const 709 { return this->do_get_global_symbols(); } 710 711 // Set flag that this object was found in a system directory. 712 void 713 set_is_in_system_directory() 714 { this->is_in_system_directory_ = true; } 715 716 // Return whether this object was found in a system directory. 717 bool 718 is_in_system_directory() const 719 { return this->is_in_system_directory_; } 720 721 // Set flag that this object was linked with --as-needed. 722 void 723 set_as_needed() 724 { this->as_needed_ = true; } 725 726 // Return whether this object was linked with --as-needed. 727 bool 728 as_needed() const 729 { return this->as_needed_; } 730 731 // Return whether we found this object by searching a directory. 732 bool 733 searched_for() const 734 { return this->input_file()->will_search_for(); } 735 736 bool 737 no_export() const 738 { return this->no_export_; } 739 740 void 741 set_no_export(bool value) 742 { this->no_export_ = value; } 743 744 bool 745 section_is_compressed(unsigned int shndx, 746 section_size_type* uncompressed_size) const 747 { 748 if (this->compressed_sections_ == NULL) 749 return false; 750 Compressed_section_map::const_iterator p = 751 this->compressed_sections_->find(shndx); 752 if (p != this->compressed_sections_->end()) 753 { 754 if (uncompressed_size != NULL) 755 *uncompressed_size = p->second.size; 756 return true; 757 } 758 return false; 759 } 760 761 // Return a view of the decompressed contents of a section. Set *PLEN 762 // to the size. Set *IS_NEW to true if the contents need to be freed 763 // by the caller. 764 const unsigned char* 765 decompressed_section_contents(unsigned int shndx, section_size_type* plen, 766 bool* is_cached); 767 768 // Discard any buffers of decompressed sections. This is done 769 // at the end of the Add_symbols task. 770 void 771 discard_decompressed_sections(); 772 773 // Return the index of the first incremental relocation for symbol SYMNDX. 774 unsigned int 775 get_incremental_reloc_base(unsigned int symndx) const 776 { return this->do_get_incremental_reloc_base(symndx); } 777 778 // Return the number of incremental relocations for symbol SYMNDX. 779 unsigned int 780 get_incremental_reloc_count(unsigned int symndx) const 781 { return this->do_get_incremental_reloc_count(symndx); } 782 783 protected: 784 // Returns NULL for Objects that are not dynamic objects. This method 785 // is overridden in the Dynobj class. 786 virtual Dynobj* 787 do_dynobj() 788 { return NULL; } 789 790 // Returns NULL for Objects that are not plugin objects. This method 791 // is overridden in the Pluginobj class. 792 virtual Pluginobj* 793 do_pluginobj() 794 { return NULL; } 795 796 // Return TRUE if this is an incremental (unchanged) input file. 797 // We return FALSE by default; the incremental object classes 798 // override this method. 799 virtual bool 800 do_is_incremental() const 801 { return false; } 802 803 // Return the last modified time of the file. This method may be 804 // overridden for subclasses that don't use an actual file (e.g., 805 // Incremental objects). 806 virtual Timespec 807 do_get_mtime() 808 { return this->input_file()->file().get_mtime(); } 809 810 // Read the symbols--implemented by child class. 811 virtual void 812 do_read_symbols(Read_symbols_data*) = 0; 813 814 // Lay out sections--implemented by child class. 815 virtual void 816 do_layout(Symbol_table*, Layout*, Read_symbols_data*) = 0; 817 818 // Add symbol information to the global symbol table--implemented by 819 // child class. 820 virtual void 821 do_add_symbols(Symbol_table*, Read_symbols_data*, Layout*) = 0; 822 823 virtual Archive::Should_include 824 do_should_include_member(Symbol_table* symtab, Layout*, Read_symbols_data*, 825 std::string* why) = 0; 826 827 // Iterate over global symbols, calling a visitor class V for each. 828 virtual void 829 do_for_all_global_symbols(Read_symbols_data* sd, 830 Library_base::Symbol_visitor_base* v) = 0; 831 832 // Iterate over local symbols, calling a visitor class V for each GOT offset 833 // associated with a local symbol. 834 virtual void 835 do_for_all_local_got_entries(Got_offset_list::Visitor* v) const = 0; 836 837 // Return the location of the contents of a section. Implemented by 838 // child class. 839 virtual const unsigned char* 840 do_section_contents(unsigned int shndx, section_size_type* plen, 841 bool cache) = 0; 842 843 // Get the size of a section--implemented by child class. 844 virtual uint64_t 845 do_section_size(unsigned int shndx) = 0; 846 847 // Get the name of a section--implemented by child class. 848 virtual std::string 849 do_section_name(unsigned int shndx) const = 0; 850 851 // Get section flags--implemented by child class. 852 virtual uint64_t 853 do_section_flags(unsigned int shndx) = 0; 854 855 // Get section entsize--implemented by child class. 856 virtual uint64_t 857 do_section_entsize(unsigned int shndx) = 0; 858 859 // Get section address--implemented by child class. 860 virtual uint64_t 861 do_section_address(unsigned int shndx) = 0; 862 863 // Get section type--implemented by child class. 864 virtual unsigned int 865 do_section_type(unsigned int shndx) = 0; 866 867 // Get section link field--implemented by child class. 868 virtual unsigned int 869 do_section_link(unsigned int shndx) = 0; 870 871 // Get section info field--implemented by child class. 872 virtual unsigned int 873 do_section_info(unsigned int shndx) = 0; 874 875 // Get section alignment--implemented by child class. 876 virtual uint64_t 877 do_section_addralign(unsigned int shndx) = 0; 878 879 // Return the output section given a section index--implemented 880 // by child class. 881 virtual Output_section* 882 do_output_section(unsigned int) const 883 { gold_unreachable(); } 884 885 // Get the address of a section--implemented by child class. 886 virtual uint64_t 887 do_output_section_address(unsigned int) 888 { gold_unreachable(); } 889 890 // Get the offset of a section--implemented by child class. 891 virtual uint64_t 892 do_output_section_offset(unsigned int) const 893 { gold_unreachable(); } 894 895 // Return the Xindex structure to use. 896 virtual Xindex* 897 do_initialize_xindex() = 0; 898 899 // Implement get_global_symbol_counts--implemented by child class. 900 virtual void 901 do_get_global_symbol_counts(const Symbol_table*, size_t*, size_t*) const = 0; 902 903 virtual const Symbols* 904 do_get_global_symbols() const = 0; 905 906 // Set the number of sections. 907 void 908 set_shnum(int shnum) 909 { this->shnum_ = shnum; } 910 911 // Functions used by both Sized_relobj_file and Sized_dynobj. 912 913 // Read the section data into a Read_symbols_data object. 914 template<int size, bool big_endian> 915 void 916 read_section_data(elfcpp::Elf_file<size, big_endian, Object>*, 917 Read_symbols_data*); 918 919 // Find the section header with the given NAME. If HDR is non-NULL 920 // then it is a section header returned from a previous call to this 921 // function and the next section header with the same name will be 922 // returned. 923 template<int size, bool big_endian> 924 const unsigned char* 925 find_shdr(const unsigned char* pshdrs, const char* name, 926 const char* names, section_size_type names_size, 927 const unsigned char* hdr) const; 928 929 // Let the child class initialize the xindex object directly. 930 void 931 set_xindex(Xindex* xindex) 932 { 933 gold_assert(this->xindex_ == NULL); 934 this->xindex_ = xindex; 935 } 936 937 // If NAME is the name of a special .gnu.warning section, arrange 938 // for the warning to be issued. SHNDX is the section index. 939 // Return whether it is a warning section. 940 bool 941 handle_gnu_warning_section(const char* name, unsigned int shndx, 942 Symbol_table*); 943 944 // If NAME is the name of the special section which indicates that 945 // this object was compiled with -fsplit-stack, mark it accordingly, 946 // and return true. Otherwise return false. 947 bool 948 handle_split_stack_section(const char* name); 949 950 // Discard any buffers of decompressed sections. This is done 951 // at the end of the Add_symbols task. 952 virtual void 953 do_discard_decompressed_sections() 954 { } 955 956 // Return the index of the first incremental relocation for symbol SYMNDX-- 957 // implemented by child class. 958 virtual unsigned int 959 do_get_incremental_reloc_base(unsigned int) const 960 { gold_unreachable(); } 961 962 // Return the number of incremental relocations for symbol SYMNDX-- 963 // implemented by child class. 964 virtual unsigned int 965 do_get_incremental_reloc_count(unsigned int) const 966 { gold_unreachable(); } 967 968 void 969 set_compressed_sections(Compressed_section_map* compressed_sections) 970 { this->compressed_sections_ = compressed_sections; } 971 972 Compressed_section_map* 973 compressed_sections() 974 { return this->compressed_sections_; } 975 976 private: 977 // This class may not be copied. 978 Object(const Object&); 979 Object& operator=(const Object&); 980 981 // Name of object as printed to user. 982 std::string name_; 983 // For reading the file. 984 Input_file* input_file_; 985 // Offset within the file--0 for an object file, non-0 for an 986 // archive. 987 off_t offset_; 988 // Number of input sections. 989 unsigned int shnum_; 990 // Whether this is a dynamic object. 991 bool is_dynamic_ : 1; 992 // Whether this object is needed. This is only set for dynamic 993 // objects, and means that the object defined a symbol which was 994 // used by a reference from a regular object. 995 bool is_needed_ : 1; 996 // Whether this object was compiled with -fsplit-stack. 997 bool uses_split_stack_ : 1; 998 // Whether this object contains any functions compiled with the 999 // no_split_stack attribute. 1000 bool has_no_split_stack_ : 1; 1001 // True if exclude this object from automatic symbol export. 1002 // This is used only for archive objects. 1003 bool no_export_ : 1; 1004 // True if the object was found in a system directory. 1005 bool is_in_system_directory_ : 1; 1006 // True if the object was linked with --as-needed. 1007 bool as_needed_ : 1; 1008 // Many sections for objects with more than SHN_LORESERVE sections. 1009 Xindex* xindex_; 1010 // For compressed debug sections, map section index to uncompressed size 1011 // and contents. 1012 Compressed_section_map* compressed_sections_; 1013 }; 1014 1015 // A regular object (ET_REL). This is an abstract base class itself. 1016 // The implementation is the template class Sized_relobj_file. 1017 1018 class Relobj : public Object 1019 { 1020 public: 1021 Relobj(const std::string& name, Input_file* input_file, off_t offset = 0) 1022 : Object(name, input_file, false, offset), 1023 output_sections_(), 1024 map_to_relocatable_relocs_(NULL), 1025 object_merge_map_(NULL), 1026 relocs_must_follow_section_writes_(false), 1027 sd_(NULL), 1028 reloc_counts_(NULL), 1029 reloc_bases_(NULL), 1030 first_dyn_reloc_(0), 1031 dyn_reloc_count_(0) 1032 { } 1033 1034 // During garbage collection, the Read_symbols_data pass for 1035 // each object is stored as layout needs to be done after 1036 // reloc processing. 1037 Symbols_data* 1038 get_symbols_data() 1039 { return this->sd_; } 1040 1041 // Decides which section names have to be included in the worklist 1042 // as roots. 1043 bool 1044 is_section_name_included(const char* name); 1045 1046 void 1047 copy_symbols_data(Symbols_data* gc_sd, Read_symbols_data* sd, 1048 unsigned int section_header_size); 1049 1050 void 1051 set_symbols_data(Symbols_data* sd) 1052 { this->sd_ = sd; } 1053 1054 // During garbage collection, the Read_relocs pass for all objects 1055 // is done before scanning the relocs. In that case, this->rd_ is 1056 // used to store the information from Read_relocs for each object. 1057 // This data is also used to compute the list of relevant sections. 1058 Read_relocs_data* 1059 get_relocs_data() 1060 { return this->rd_; } 1061 1062 void 1063 set_relocs_data(Read_relocs_data* rd) 1064 { this->rd_ = rd; } 1065 1066 virtual bool 1067 is_output_section_offset_invalid(unsigned int shndx) const = 0; 1068 1069 // Read the relocs. 1070 void 1071 read_relocs(Read_relocs_data* rd) 1072 { return this->do_read_relocs(rd); } 1073 1074 // Process the relocs, during garbage collection only. 1075 void 1076 gc_process_relocs(Symbol_table* symtab, Layout* layout, Read_relocs_data* rd) 1077 { return this->do_gc_process_relocs(symtab, layout, rd); } 1078 1079 // Scan the relocs and adjust the symbol table. 1080 void 1081 scan_relocs(Symbol_table* symtab, Layout* layout, Read_relocs_data* rd) 1082 { return this->do_scan_relocs(symtab, layout, rd); } 1083 1084 // Return the value of the local symbol whose index is SYMNDX, plus 1085 // ADDEND. ADDEND is passed in so that we can correctly handle the 1086 // section symbol for a merge section. 1087 uint64_t 1088 local_symbol_value(unsigned int symndx, uint64_t addend) const 1089 { return this->do_local_symbol_value(symndx, addend); } 1090 1091 // Return the PLT offset for a local symbol. It is an error to call 1092 // this if it doesn't have one. 1093 unsigned int 1094 local_plt_offset(unsigned int symndx) const 1095 { return this->do_local_plt_offset(symndx); } 1096 1097 // Return whether the local symbol SYMNDX has a GOT offset of type 1098 // GOT_TYPE. 1099 bool 1100 local_has_got_offset(unsigned int symndx, unsigned int got_type) const 1101 { return this->do_local_has_got_offset(symndx, got_type); } 1102 1103 // Return the GOT offset of type GOT_TYPE of the local symbol 1104 // SYMNDX. It is an error to call this if the symbol does not have 1105 // a GOT offset of the specified type. 1106 unsigned int 1107 local_got_offset(unsigned int symndx, unsigned int got_type) const 1108 { return this->do_local_got_offset(symndx, got_type); } 1109 1110 // Set the GOT offset with type GOT_TYPE of the local symbol SYMNDX 1111 // to GOT_OFFSET. 1112 void 1113 set_local_got_offset(unsigned int symndx, unsigned int got_type, 1114 unsigned int got_offset) 1115 { this->do_set_local_got_offset(symndx, got_type, got_offset); } 1116 1117 // Return whether the local symbol SYMNDX is a TLS symbol. 1118 bool 1119 local_is_tls(unsigned int symndx) const 1120 { return this->do_local_is_tls(symndx); } 1121 1122 // The number of local symbols in the input symbol table. 1123 virtual unsigned int 1124 local_symbol_count() const 1125 { return this->do_local_symbol_count(); } 1126 1127 // The number of local symbols in the output symbol table. 1128 virtual unsigned int 1129 output_local_symbol_count() const 1130 { return this->do_output_local_symbol_count(); } 1131 1132 // The file offset for local symbols in the output symbol table. 1133 virtual off_t 1134 local_symbol_offset() const 1135 { return this->do_local_symbol_offset(); } 1136 1137 // Initial local symbol processing: count the number of local symbols 1138 // in the output symbol table and dynamic symbol table; add local symbol 1139 // names to *POOL and *DYNPOOL. 1140 void 1141 count_local_symbols(Stringpool_template<char>* pool, 1142 Stringpool_template<char>* dynpool) 1143 { return this->do_count_local_symbols(pool, dynpool); } 1144 1145 // Set the values of the local symbols, set the output symbol table 1146 // indexes for the local variables, and set the offset where local 1147 // symbol information will be stored. Returns the new local symbol index. 1148 unsigned int 1149 finalize_local_symbols(unsigned int index, off_t off, Symbol_table* symtab) 1150 { return this->do_finalize_local_symbols(index, off, symtab); } 1151 1152 // Set the output dynamic symbol table indexes for the local variables. 1153 unsigned int 1154 set_local_dynsym_indexes(unsigned int index) 1155 { return this->do_set_local_dynsym_indexes(index); } 1156 1157 // Set the offset where local dynamic symbol information will be stored. 1158 unsigned int 1159 set_local_dynsym_offset(off_t off) 1160 { return this->do_set_local_dynsym_offset(off); } 1161 1162 // Record a dynamic relocation against an input section from this object. 1163 void 1164 add_dyn_reloc(unsigned int index) 1165 { 1166 if (this->dyn_reloc_count_ == 0) 1167 this->first_dyn_reloc_ = index; 1168 ++this->dyn_reloc_count_; 1169 } 1170 1171 // Return the index of the first dynamic relocation. 1172 unsigned int 1173 first_dyn_reloc() const 1174 { return this->first_dyn_reloc_; } 1175 1176 // Return the count of dynamic relocations. 1177 unsigned int 1178 dyn_reloc_count() const 1179 { return this->dyn_reloc_count_; } 1180 1181 // Relocate the input sections and write out the local symbols. 1182 void 1183 relocate(const Symbol_table* symtab, const Layout* layout, Output_file* of) 1184 { return this->do_relocate(symtab, layout, of); } 1185 1186 void 1187 relocate_stub_tables(const Symbol_table* symtab, const Layout* layout) 1188 { return this->do_relocate_stub_tables(symtab, layout); } 1189 1190 // Return whether an input section is being included in the link. 1191 bool 1192 is_section_included(unsigned int shndx) const 1193 { 1194 gold_assert(shndx < this->output_sections_.size()); 1195 return this->output_sections_[shndx] != NULL; 1196 } 1197 1198 // The output section of the input section with index SHNDX. 1199 // This is only used currently to remove a section from the link in 1200 // relaxation. 1201 void 1202 set_output_section(unsigned int shndx, Output_section* os) 1203 { 1204 gold_assert(shndx < this->output_sections_.size()); 1205 this->output_sections_[shndx] = os; 1206 } 1207 1208 // Set the offset of an input section within its output section. 1209 void 1210 set_section_offset(unsigned int shndx, uint64_t off) 1211 { this->do_set_section_offset(shndx, off); } 1212 1213 // Return true if we need to wait for output sections to be written 1214 // before we can apply relocations. This is true if the object has 1215 // any relocations for sections which require special handling, such 1216 // as the exception frame section. 1217 bool 1218 relocs_must_follow_section_writes() const 1219 { return this->relocs_must_follow_section_writes_; } 1220 1221 // Return the object merge map. 1222 Object_merge_map* 1223 merge_map() const 1224 { return this->object_merge_map_; } 1225 1226 // Set the object merge map. 1227 void 1228 set_merge_map(Object_merge_map* object_merge_map) 1229 { 1230 gold_assert(this->object_merge_map_ == NULL); 1231 this->object_merge_map_ = object_merge_map; 1232 } 1233 1234 // Record the relocatable reloc info for an input reloc section. 1235 void 1236 set_relocatable_relocs(unsigned int reloc_shndx, Relocatable_relocs* rr) 1237 { 1238 gold_assert(reloc_shndx < this->shnum()); 1239 (*this->map_to_relocatable_relocs_)[reloc_shndx] = rr; 1240 } 1241 1242 // Get the relocatable reloc info for an input reloc section. 1243 Relocatable_relocs* 1244 relocatable_relocs(unsigned int reloc_shndx) 1245 { 1246 gold_assert(reloc_shndx < this->shnum()); 1247 return (*this->map_to_relocatable_relocs_)[reloc_shndx]; 1248 } 1249 1250 // Layout sections whose layout was deferred while waiting for 1251 // input files from a plugin. 1252 void 1253 layout_deferred_sections(Layout* layout) 1254 { this->do_layout_deferred_sections(layout); } 1255 1256 // Return the index of the first incremental relocation for symbol SYMNDX. 1257 virtual unsigned int 1258 do_get_incremental_reloc_base(unsigned int symndx) const 1259 { return this->reloc_bases_[symndx]; } 1260 1261 // Return the number of incremental relocations for symbol SYMNDX. 1262 virtual unsigned int 1263 do_get_incremental_reloc_count(unsigned int symndx) const 1264 { return this->reloc_counts_[symndx]; } 1265 1266 // Return the word size of the object file. 1267 int 1268 elfsize() const 1269 { return this->do_elfsize(); } 1270 1271 // Return TRUE if this is a big-endian object file. 1272 bool 1273 is_big_endian() const 1274 { return this->do_is_big_endian(); } 1275 1276 virtual void 1277 clear_views() = 0; 1278 1279 protected: 1280 // The output section to be used for each input section, indexed by 1281 // the input section number. The output section is NULL if the 1282 // input section is to be discarded. 1283 typedef std::vector<Output_section*> Output_sections; 1284 1285 // Read the relocs--implemented by child class. 1286 virtual void 1287 do_read_relocs(Read_relocs_data*) = 0; 1288 1289 // Process the relocs--implemented by child class. 1290 virtual void 1291 do_gc_process_relocs(Symbol_table*, Layout*, Read_relocs_data*) = 0; 1292 1293 // Scan the relocs--implemented by child class. 1294 virtual void 1295 do_scan_relocs(Symbol_table*, Layout*, Read_relocs_data*) = 0; 1296 1297 // Return the value of a local symbol. 1298 virtual uint64_t 1299 do_local_symbol_value(unsigned int symndx, uint64_t addend) const = 0; 1300 1301 // Return the PLT offset of a local symbol. 1302 virtual unsigned int 1303 do_local_plt_offset(unsigned int symndx) const = 0; 1304 1305 // Return whether a local symbol has a GOT offset of a given type. 1306 virtual bool 1307 do_local_has_got_offset(unsigned int symndx, 1308 unsigned int got_type) const = 0; 1309 1310 // Return the GOT offset of a given type of a local symbol. 1311 virtual unsigned int 1312 do_local_got_offset(unsigned int symndx, unsigned int got_type) const = 0; 1313 1314 // Set the GOT offset with a given type for a local symbol. 1315 virtual void 1316 do_set_local_got_offset(unsigned int symndx, unsigned int got_type, 1317 unsigned int got_offset) = 0; 1318 1319 // Return whether local symbol SYMNDX is a TLS symbol. 1320 virtual bool 1321 do_local_is_tls(unsigned int symndx) const = 0; 1322 1323 // Return the number of local symbols--implemented by child class. 1324 virtual unsigned int 1325 do_local_symbol_count() const = 0; 1326 1327 // Return the number of output local symbols--implemented by child class. 1328 virtual unsigned int 1329 do_output_local_symbol_count() const = 0; 1330 1331 // Return the file offset for local symbols--implemented by child class. 1332 virtual off_t 1333 do_local_symbol_offset() const = 0; 1334 1335 // Count local symbols--implemented by child class. 1336 virtual void 1337 do_count_local_symbols(Stringpool_template<char>*, 1338 Stringpool_template<char>*) = 0; 1339 1340 // Finalize the local symbols. Set the output symbol table indexes 1341 // for the local variables, and set the offset where local symbol 1342 // information will be stored. 1343 virtual unsigned int 1344 do_finalize_local_symbols(unsigned int, off_t, Symbol_table*) = 0; 1345 1346 // Set the output dynamic symbol table indexes for the local variables. 1347 virtual unsigned int 1348 do_set_local_dynsym_indexes(unsigned int) = 0; 1349 1350 // Set the offset where local dynamic symbol information will be stored. 1351 virtual unsigned int 1352 do_set_local_dynsym_offset(off_t) = 0; 1353 1354 // Relocate the input sections and write out the local 1355 // symbols--implemented by child class. 1356 virtual void 1357 do_relocate(const Symbol_table* symtab, const Layout*, Output_file* of) = 0; 1358 1359 virtual void 1360 do_relocate_stub_tables(const Symbol_table*, const Layout*) = 0; 1361 1362 // Set the offset of a section--implemented by child class. 1363 virtual void 1364 do_set_section_offset(unsigned int shndx, uint64_t off) = 0; 1365 1366 // Layout sections whose layout was deferred while waiting for 1367 // input files from a plugin--implemented by child class. 1368 virtual void 1369 do_layout_deferred_sections(Layout*) = 0; 1370 1371 // Given a section index, return the corresponding Output_section. 1372 // The return value will be NULL if the section is not included in 1373 // the link. 1374 Output_section* 1375 do_output_section(unsigned int shndx) const 1376 { 1377 gold_assert(shndx < this->output_sections_.size()); 1378 return this->output_sections_[shndx]; 1379 } 1380 1381 // Return the vector mapping input sections to output sections. 1382 Output_sections& 1383 output_sections() 1384 { return this->output_sections_; } 1385 1386 const Output_sections& 1387 output_sections() const 1388 { return this->output_sections_; } 1389 1390 // Set the size of the relocatable relocs array. 1391 void 1392 size_relocatable_relocs() 1393 { 1394 this->map_to_relocatable_relocs_ = 1395 new std::vector<Relocatable_relocs*>(this->shnum()); 1396 } 1397 1398 // Record that we must wait for the output sections to be written 1399 // before applying relocations. 1400 void 1401 set_relocs_must_follow_section_writes() 1402 { this->relocs_must_follow_section_writes_ = true; } 1403 1404 // Allocate the array for counting incremental relocations. 1405 void 1406 allocate_incremental_reloc_counts() 1407 { 1408 unsigned int nsyms = this->do_get_global_symbols()->size(); 1409 this->reloc_counts_ = new unsigned int[nsyms]; 1410 gold_assert(this->reloc_counts_ != NULL); 1411 memset(this->reloc_counts_, 0, nsyms * sizeof(unsigned int)); 1412 } 1413 1414 // Record a relocation in this object referencing global symbol SYMNDX. 1415 // Used for tracking incremental link information. 1416 void 1417 count_incremental_reloc(unsigned int symndx) 1418 { 1419 unsigned int nsyms = this->do_get_global_symbols()->size(); 1420 gold_assert(symndx < nsyms); 1421 gold_assert(this->reloc_counts_ != NULL); 1422 ++this->reloc_counts_[symndx]; 1423 } 1424 1425 // Finalize the incremental relocation information. 1426 void 1427 finalize_incremental_relocs(Layout* layout, bool clear_counts); 1428 1429 // Return the index of the next relocation to be written for global symbol 1430 // SYMNDX. Only valid after finalize_incremental_relocs() has been called. 1431 unsigned int 1432 next_incremental_reloc_index(unsigned int symndx) 1433 { 1434 unsigned int nsyms = this->do_get_global_symbols()->size(); 1435 1436 gold_assert(this->reloc_counts_ != NULL); 1437 gold_assert(this->reloc_bases_ != NULL); 1438 gold_assert(symndx < nsyms); 1439 1440 unsigned int counter = this->reloc_counts_[symndx]++; 1441 return this->reloc_bases_[symndx] + counter; 1442 } 1443 1444 // Return the word size of the object file-- 1445 // implemented by child class. 1446 virtual int 1447 do_elfsize() const = 0; 1448 1449 // Return TRUE if this is a big-endian object file-- 1450 // implemented by child class. 1451 virtual bool 1452 do_is_big_endian() const = 0; 1453 1454 private: 1455 // Mapping from input sections to output section. 1456 Output_sections output_sections_; 1457 // Mapping from input section index to the information recorded for 1458 // the relocations. This is only used for a relocatable link. 1459 std::vector<Relocatable_relocs*>* map_to_relocatable_relocs_; 1460 // Mappings for merge sections. This is managed by the code in the 1461 // Merge_map class. 1462 Object_merge_map* object_merge_map_; 1463 // Whether we need to wait for output sections to be written before 1464 // we can apply relocations. 1465 bool relocs_must_follow_section_writes_; 1466 // Used to store the relocs data computed by the Read_relocs pass. 1467 // Used during garbage collection of unused sections. 1468 Read_relocs_data* rd_; 1469 // Used to store the symbols data computed by the Read_symbols pass. 1470 // Again used during garbage collection when laying out referenced 1471 // sections. 1472 gold::Symbols_data* sd_; 1473 // Per-symbol counts of relocations, for incremental links. 1474 unsigned int* reloc_counts_; 1475 // Per-symbol base indexes of relocations, for incremental links. 1476 unsigned int* reloc_bases_; 1477 // Index of the first dynamic relocation for this object. 1478 unsigned int first_dyn_reloc_; 1479 // Count of dynamic relocations for this object. 1480 unsigned int dyn_reloc_count_; 1481 }; 1482 1483 // This class is used to handle relocations against a section symbol 1484 // in an SHF_MERGE section. For such a symbol, we need to know the 1485 // addend of the relocation before we can determine the final value. 1486 // The addend gives us the location in the input section, and we can 1487 // determine how it is mapped to the output section. For a 1488 // non-section symbol, we apply the addend to the final value of the 1489 // symbol; that is done in finalize_local_symbols, and does not use 1490 // this class. 1491 1492 template<int size> 1493 class Merged_symbol_value 1494 { 1495 public: 1496 typedef typename elfcpp::Elf_types<size>::Elf_Addr Value; 1497 1498 // We use a hash table to map offsets in the input section to output 1499 // addresses. 1500 typedef Unordered_map<section_offset_type, Value> Output_addresses; 1501 1502 Merged_symbol_value(Value input_value, Value output_start_address) 1503 : input_value_(input_value), output_start_address_(output_start_address), 1504 output_addresses_() 1505 { } 1506 1507 // Initialize the hash table. 1508 void 1509 initialize_input_to_output_map(const Relobj*, unsigned int input_shndx); 1510 1511 // Release the hash table to save space. 1512 void 1513 free_input_to_output_map() 1514 { this->output_addresses_.clear(); } 1515 1516 // Get the output value corresponding to an addend. The object and 1517 // input section index are passed in because the caller will have 1518 // them; otherwise we could store them here. 1519 Value 1520 value(const Relobj* object, unsigned int input_shndx, Value addend) const 1521 { 1522 // This is a relocation against a section symbol. ADDEND is the 1523 // offset in the section. The result should be the start of some 1524 // merge area. If the object file wants something else, it should 1525 // use a regular symbol rather than a section symbol. 1526 // Unfortunately, PR 6658 shows a case in which the object file 1527 // refers to the section symbol, but uses a negative ADDEND to 1528 // compensate for a PC relative reloc. We can't handle the 1529 // general case. However, we can handle the special case of a 1530 // negative addend, by assuming that it refers to the start of the 1531 // section. Of course, that means that we have to guess when 1532 // ADDEND is negative. It is normal to see a 32-bit value here 1533 // even when the template parameter size is 64, as 64-bit object 1534 // file formats have 32-bit relocations. We know this is a merge 1535 // section, so we know it has to fit into memory. So we assume 1536 // that we won't see a value larger than a large 32-bit unsigned 1537 // value. This will break objects with very very large merge 1538 // sections; they probably break in other ways anyhow. 1539 Value input_offset = this->input_value_; 1540 if (addend < 0xffffff00) 1541 { 1542 input_offset += addend; 1543 addend = 0; 1544 } 1545 typename Output_addresses::const_iterator p = 1546 this->output_addresses_.find(input_offset); 1547 if (p != this->output_addresses_.end()) 1548 return p->second + addend; 1549 1550 return (this->value_from_output_section(object, input_shndx, input_offset) 1551 + addend); 1552 } 1553 1554 private: 1555 // Get the output value for an input offset if we couldn't find it 1556 // in the hash table. 1557 Value 1558 value_from_output_section(const Relobj*, unsigned int input_shndx, 1559 Value input_offset) const; 1560 1561 // The value of the section symbol in the input file. This is 1562 // normally zero, but could in principle be something else. 1563 Value input_value_; 1564 // The start address of this merged section in the output file. 1565 Value output_start_address_; 1566 // A hash table which maps offsets in the input section to output 1567 // addresses. This only maps specific offsets, not all offsets. 1568 Output_addresses output_addresses_; 1569 }; 1570 1571 // This POD class is holds the value of a symbol. This is used for 1572 // local symbols, and for all symbols during relocation processing. 1573 // For special sections, such as SHF_MERGE sections, this calls a 1574 // function to get the final symbol value. 1575 1576 template<int size> 1577 class Symbol_value 1578 { 1579 public: 1580 typedef typename elfcpp::Elf_types<size>::Elf_Addr Value; 1581 1582 Symbol_value() 1583 : output_symtab_index_(0), output_dynsym_index_(-1U), input_shndx_(0), 1584 is_ordinary_shndx_(false), is_section_symbol_(false), 1585 is_tls_symbol_(false), is_ifunc_symbol_(false), has_output_value_(true) 1586 { this->u_.value = 0; } 1587 1588 ~Symbol_value() 1589 { 1590 if (!this->has_output_value_) 1591 delete this->u_.merged_symbol_value; 1592 } 1593 1594 // Get the value of this symbol. OBJECT is the object in which this 1595 // symbol is defined, and ADDEND is an addend to add to the value. 1596 template<bool big_endian> 1597 Value 1598 value(const Sized_relobj_file<size, big_endian>* object, Value addend) const 1599 { 1600 if (this->has_output_value_) 1601 return this->u_.value + addend; 1602 else 1603 { 1604 gold_assert(this->is_ordinary_shndx_); 1605 return this->u_.merged_symbol_value->value(object, this->input_shndx_, 1606 addend); 1607 } 1608 } 1609 1610 // Set the value of this symbol in the output symbol table. 1611 void 1612 set_output_value(Value value) 1613 { this->u_.value = value; } 1614 1615 // For a section symbol in a merged section, we need more 1616 // information. 1617 void 1618 set_merged_symbol_value(Merged_symbol_value<size>* msv) 1619 { 1620 gold_assert(this->is_section_symbol_); 1621 this->has_output_value_ = false; 1622 this->u_.merged_symbol_value = msv; 1623 } 1624 1625 // Initialize the input to output map for a section symbol in a 1626 // merged section. We also initialize the value of a non-section 1627 // symbol in a merged section. 1628 void 1629 initialize_input_to_output_map(const Relobj* object) 1630 { 1631 if (!this->has_output_value_) 1632 { 1633 gold_assert(this->is_section_symbol_ && this->is_ordinary_shndx_); 1634 Merged_symbol_value<size>* msv = this->u_.merged_symbol_value; 1635 msv->initialize_input_to_output_map(object, this->input_shndx_); 1636 } 1637 } 1638 1639 // Free the input to output map for a section symbol in a merged 1640 // section. 1641 void 1642 free_input_to_output_map() 1643 { 1644 if (!this->has_output_value_) 1645 this->u_.merged_symbol_value->free_input_to_output_map(); 1646 } 1647 1648 // Set the value of the symbol from the input file. This is only 1649 // called by count_local_symbols, to communicate the value to 1650 // finalize_local_symbols. 1651 void 1652 set_input_value(Value value) 1653 { this->u_.value = value; } 1654 1655 // Return the input value. This is only called by 1656 // finalize_local_symbols and (in special cases) relocate_section. 1657 Value 1658 input_value() const 1659 { return this->u_.value; } 1660 1661 // Return whether we have set the index in the output symbol table 1662 // yet. 1663 bool 1664 is_output_symtab_index_set() const 1665 { 1666 return (this->output_symtab_index_ != 0 1667 && this->output_symtab_index_ != -2U); 1668 } 1669 1670 // Return whether this symbol may be discarded from the normal 1671 // symbol table. 1672 bool 1673 may_be_discarded_from_output_symtab() const 1674 { 1675 gold_assert(!this->is_output_symtab_index_set()); 1676 return this->output_symtab_index_ != -2U; 1677 } 1678 1679 // Return whether this symbol has an entry in the output symbol 1680 // table. 1681 bool 1682 has_output_symtab_entry() const 1683 { 1684 gold_assert(this->is_output_symtab_index_set()); 1685 return this->output_symtab_index_ != -1U; 1686 } 1687 1688 // Return the index in the output symbol table. 1689 unsigned int 1690 output_symtab_index() const 1691 { 1692 gold_assert(this->is_output_symtab_index_set() 1693 && this->output_symtab_index_ != -1U); 1694 return this->output_symtab_index_; 1695 } 1696 1697 // Set the index in the output symbol table. 1698 void 1699 set_output_symtab_index(unsigned int i) 1700 { 1701 gold_assert(!this->is_output_symtab_index_set()); 1702 gold_assert(i != 0 && i != -1U && i != -2U); 1703 this->output_symtab_index_ = i; 1704 } 1705 1706 // Record that this symbol should not go into the output symbol 1707 // table. 1708 void 1709 set_no_output_symtab_entry() 1710 { 1711 gold_assert(this->output_symtab_index_ == 0); 1712 this->output_symtab_index_ = -1U; 1713 } 1714 1715 // Record that this symbol must go into the output symbol table, 1716 // because it there is a relocation that uses it. 1717 void 1718 set_must_have_output_symtab_entry() 1719 { 1720 gold_assert(!this->is_output_symtab_index_set()); 1721 this->output_symtab_index_ = -2U; 1722 } 1723 1724 // Set the index in the output dynamic symbol table. 1725 void 1726 set_needs_output_dynsym_entry() 1727 { 1728 gold_assert(!this->is_section_symbol()); 1729 this->output_dynsym_index_ = 0; 1730 } 1731 1732 // Return whether this symbol should go into the dynamic symbol 1733 // table. 1734 bool 1735 needs_output_dynsym_entry() const 1736 { 1737 return this->output_dynsym_index_ != -1U; 1738 } 1739 1740 // Return whether this symbol has an entry in the dynamic symbol 1741 // table. 1742 bool 1743 has_output_dynsym_entry() const 1744 { 1745 gold_assert(this->output_dynsym_index_ != 0); 1746 return this->output_dynsym_index_ != -1U; 1747 } 1748 1749 // Record that this symbol should go into the dynamic symbol table. 1750 void 1751 set_output_dynsym_index(unsigned int i) 1752 { 1753 gold_assert(this->output_dynsym_index_ == 0); 1754 gold_assert(i != 0 && i != -1U); 1755 this->output_dynsym_index_ = i; 1756 } 1757 1758 // Return the index in the output dynamic symbol table. 1759 unsigned int 1760 output_dynsym_index() const 1761 { 1762 gold_assert(this->output_dynsym_index_ != 0 1763 && this->output_dynsym_index_ != -1U); 1764 return this->output_dynsym_index_; 1765 } 1766 1767 // Set the index of the input section in the input file. 1768 void 1769 set_input_shndx(unsigned int i, bool is_ordinary) 1770 { 1771 this->input_shndx_ = i; 1772 // input_shndx_ field is a bitfield, so make sure that the value 1773 // fits. 1774 gold_assert(this->input_shndx_ == i); 1775 this->is_ordinary_shndx_ = is_ordinary; 1776 } 1777 1778 // Return the index of the input section in the input file. 1779 unsigned int 1780 input_shndx(bool* is_ordinary) const 1781 { 1782 *is_ordinary = this->is_ordinary_shndx_; 1783 return this->input_shndx_; 1784 } 1785 1786 // Whether this is a section symbol. 1787 bool 1788 is_section_symbol() const 1789 { return this->is_section_symbol_; } 1790 1791 // Record that this is a section symbol. 1792 void 1793 set_is_section_symbol() 1794 { 1795 gold_assert(!this->needs_output_dynsym_entry()); 1796 this->is_section_symbol_ = true; 1797 } 1798 1799 // Record that this is a TLS symbol. 1800 void 1801 set_is_tls_symbol() 1802 { this->is_tls_symbol_ = true; } 1803 1804 // Return true if this is a TLS symbol. 1805 bool 1806 is_tls_symbol() const 1807 { return this->is_tls_symbol_; } 1808 1809 // Record that this is an IFUNC symbol. 1810 void 1811 set_is_ifunc_symbol() 1812 { this->is_ifunc_symbol_ = true; } 1813 1814 // Return true if this is an IFUNC symbol. 1815 bool 1816 is_ifunc_symbol() const 1817 { return this->is_ifunc_symbol_; } 1818 1819 // Return true if this has output value. 1820 bool 1821 has_output_value() const 1822 { return this->has_output_value_; } 1823 1824 private: 1825 // The index of this local symbol in the output symbol table. This 1826 // will be 0 if no value has been assigned yet, and the symbol may 1827 // be omitted. This will be -1U if the symbol should not go into 1828 // the symbol table. This will be -2U if the symbol must go into 1829 // the symbol table, but no index has been assigned yet. 1830 unsigned int output_symtab_index_; 1831 // The index of this local symbol in the dynamic symbol table. This 1832 // will be -1U if the symbol should not go into the symbol table. 1833 unsigned int output_dynsym_index_; 1834 // The section index in the input file in which this symbol is 1835 // defined. 1836 unsigned int input_shndx_ : 27; 1837 // Whether the section index is an ordinary index, not a special 1838 // value. 1839 bool is_ordinary_shndx_ : 1; 1840 // Whether this is a STT_SECTION symbol. 1841 bool is_section_symbol_ : 1; 1842 // Whether this is a STT_TLS symbol. 1843 bool is_tls_symbol_ : 1; 1844 // Whether this is a STT_GNU_IFUNC symbol. 1845 bool is_ifunc_symbol_ : 1; 1846 // Whether this symbol has a value for the output file. This is 1847 // normally set to true during Layout::finalize, by 1848 // finalize_local_symbols. It will be false for a section symbol in 1849 // a merge section, as for such symbols we can not determine the 1850 // value to use in a relocation until we see the addend. 1851 bool has_output_value_ : 1; 1852 union 1853 { 1854 // This is used if has_output_value_ is true. Between 1855 // count_local_symbols and finalize_local_symbols, this is the 1856 // value in the input file. After finalize_local_symbols, it is 1857 // the value in the output file. 1858 Value value; 1859 // This is used if has_output_value_ is false. It points to the 1860 // information we need to get the value for a merge section. 1861 Merged_symbol_value<size>* merged_symbol_value; 1862 } u_; 1863 }; 1864 1865 // This type is used to modify relocations for -fsplit-stack. It is 1866 // indexed by relocation index, and means that the relocation at that 1867 // index should use the symbol from the vector, rather than the one 1868 // indicated by the relocation. 1869 1870 class Reloc_symbol_changes 1871 { 1872 public: 1873 Reloc_symbol_changes(size_t count) 1874 : vec_(count, NULL) 1875 { } 1876 1877 void 1878 set(size_t i, Symbol* sym) 1879 { this->vec_[i] = sym; } 1880 1881 const Symbol* 1882 operator[](size_t i) const 1883 { return this->vec_[i]; } 1884 1885 private: 1886 std::vector<Symbol*> vec_; 1887 }; 1888 1889 // Abstract base class for a regular object file, either a real object file 1890 // or an incremental (unchanged) object. This is size and endian specific. 1891 1892 template<int size, bool big_endian> 1893 class Sized_relobj : public Relobj 1894 { 1895 public: 1896 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address; 1897 typedef Relobj::Symbols Symbols; 1898 1899 static const Address invalid_address = static_cast<Address>(0) - 1; 1900 1901 Sized_relobj(const std::string& name, Input_file* input_file) 1902 : Relobj(name, input_file), local_got_offsets_(), section_offsets_() 1903 { } 1904 1905 Sized_relobj(const std::string& name, Input_file* input_file, 1906 off_t offset) 1907 : Relobj(name, input_file, offset), local_got_offsets_(), section_offsets_() 1908 { } 1909 1910 ~Sized_relobj() 1911 { } 1912 1913 // If this is a regular object, return a pointer to the Sized_relobj_file 1914 // object. Otherwise, return NULL. 1915 virtual Sized_relobj_file<size, big_endian>* 1916 sized_relobj() 1917 { return NULL; } 1918 1919 const virtual Sized_relobj_file<size, big_endian>* 1920 sized_relobj() const 1921 { return NULL; } 1922 1923 // Checks if the offset of input section SHNDX within its output 1924 // section is invalid. 1925 bool 1926 is_output_section_offset_invalid(unsigned int shndx) const 1927 { return this->get_output_section_offset(shndx) == invalid_address; } 1928 1929 // Get the offset of input section SHNDX within its output section. 1930 // This is -1 if the input section requires a special mapping, such 1931 // as a merge section. The output section can be found in the 1932 // output_sections_ field of the parent class Relobj. 1933 Address 1934 get_output_section_offset(unsigned int shndx) const 1935 { 1936 gold_assert(shndx < this->section_offsets_.size()); 1937 return this->section_offsets_[shndx]; 1938 } 1939 1940 // Iterate over local symbols, calling a visitor class V for each GOT offset 1941 // associated with a local symbol. 1942 void 1943 do_for_all_local_got_entries(Got_offset_list::Visitor* v) const; 1944 1945 protected: 1946 typedef Relobj::Output_sections Output_sections; 1947 1948 // Clear the local symbol information. 1949 void 1950 clear_got_offsets() 1951 { this->local_got_offsets_.clear(); } 1952 1953 // Return the vector of section offsets. 1954 std::vector<Address>& 1955 section_offsets() 1956 { return this->section_offsets_; } 1957 1958 virtual void 1959 do_relocate_stub_tables(const Symbol_table*, const Layout*); 1960 1961 // Get the address of an output section. 1962 uint64_t 1963 do_output_section_address(unsigned int shndx); 1964 1965 // Get the offset of a section. 1966 uint64_t 1967 do_output_section_offset(unsigned int shndx) const 1968 { 1969 Address off = this->get_output_section_offset(shndx); 1970 if (off == invalid_address) 1971 return -1ULL; 1972 return off; 1973 } 1974 1975 // Set the offset of a section. 1976 void 1977 do_set_section_offset(unsigned int shndx, uint64_t off) 1978 { 1979 gold_assert(shndx < this->section_offsets_.size()); 1980 this->section_offsets_[shndx] = 1981 (off == static_cast<uint64_t>(-1) 1982 ? invalid_address 1983 : convert_types<Address, uint64_t>(off)); 1984 } 1985 1986 // Return whether the local symbol SYMNDX has a GOT offset of type 1987 // GOT_TYPE. 1988 bool 1989 do_local_has_got_offset(unsigned int symndx, unsigned int got_type) const 1990 { 1991 Local_got_offsets::const_iterator p = 1992 this->local_got_offsets_.find(symndx); 1993 return (p != this->local_got_offsets_.end() 1994 && p->second->get_offset(got_type) != -1U); 1995 } 1996 1997 // Return the GOT offset of type GOT_TYPE of the local symbol 1998 // SYMNDX. 1999 unsigned int 2000 do_local_got_offset(unsigned int symndx, unsigned int got_type) const 2001 { 2002 Local_got_offsets::const_iterator p = 2003 this->local_got_offsets_.find(symndx); 2004 gold_assert(p != this->local_got_offsets_.end()); 2005 unsigned int off = p->second->get_offset(got_type); 2006 gold_assert(off != -1U); 2007 return off; 2008 } 2009 2010 // Set the GOT offset with type GOT_TYPE of the local symbol SYMNDX 2011 // to GOT_OFFSET. 2012 void 2013 do_set_local_got_offset(unsigned int symndx, unsigned int got_type, 2014 unsigned int got_offset) 2015 { 2016 Local_got_offsets::const_iterator p = 2017 this->local_got_offsets_.find(symndx); 2018 if (p != this->local_got_offsets_.end()) 2019 p->second->set_offset(got_type, got_offset); 2020 else 2021 { 2022 Got_offset_list* g = new Got_offset_list(got_type, got_offset); 2023 std::pair<Local_got_offsets::iterator, bool> ins = 2024 this->local_got_offsets_.insert(std::make_pair(symndx, g)); 2025 gold_assert(ins.second); 2026 } 2027 } 2028 2029 // Return the word size of the object file. 2030 virtual int 2031 do_elfsize() const 2032 { return size; } 2033 2034 // Return TRUE if this is a big-endian object file. 2035 virtual bool 2036 do_is_big_endian() const 2037 { return big_endian; } 2038 2039 private: 2040 // The GOT offsets of local symbols. This map also stores GOT offsets 2041 // for tp-relative offsets for TLS symbols. 2042 typedef Unordered_map<unsigned int, Got_offset_list*> Local_got_offsets; 2043 2044 // GOT offsets for local non-TLS symbols, and tp-relative offsets 2045 // for TLS symbols, indexed by symbol number. 2046 Local_got_offsets local_got_offsets_; 2047 // For each input section, the offset of the input section in its 2048 // output section. This is INVALID_ADDRESS if the input section requires a 2049 // special mapping. 2050 std::vector<Address> section_offsets_; 2051 }; 2052 2053 // A regular object file. This is size and endian specific. 2054 2055 template<int size, bool big_endian> 2056 class Sized_relobj_file : public Sized_relobj<size, big_endian> 2057 { 2058 public: 2059 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address; 2060 typedef typename Sized_relobj<size, big_endian>::Symbols Symbols; 2061 typedef std::vector<Symbol_value<size> > Local_values; 2062 2063 static const Address invalid_address = static_cast<Address>(0) - 1; 2064 2065 enum Compute_final_local_value_status 2066 { 2067 // No error. 2068 CFLV_OK, 2069 // An error occurred. 2070 CFLV_ERROR, 2071 // The local symbol has no output section. 2072 CFLV_DISCARDED 2073 }; 2074 2075 Sized_relobj_file(const std::string& name, 2076 Input_file* input_file, 2077 off_t offset, 2078 const typename elfcpp::Ehdr<size, big_endian>&); 2079 2080 ~Sized_relobj_file(); 2081 2082 // Set up the object file based on TARGET. 2083 void 2084 setup() 2085 { this->do_setup(); } 2086 2087 // Return a pointer to the Sized_relobj_file object. 2088 Sized_relobj_file<size, big_endian>* 2089 sized_relobj() 2090 { return this; } 2091 2092 const Sized_relobj_file<size, big_endian>* 2093 sized_relobj() const 2094 { return this; } 2095 2096 // Return the ELF file type. 2097 int 2098 e_type() const 2099 { return this->e_type_; } 2100 2101 // Return the number of symbols. This is only valid after 2102 // Object::add_symbols has been called. 2103 unsigned int 2104 symbol_count() const 2105 { return this->local_symbol_count_ + this->symbols_.size(); } 2106 2107 // If SYM is the index of a global symbol in the object file's 2108 // symbol table, return the Symbol object. Otherwise, return NULL. 2109 Symbol* 2110 global_symbol(unsigned int sym) const 2111 { 2112 if (sym >= this->local_symbol_count_) 2113 { 2114 gold_assert(sym - this->local_symbol_count_ < this->symbols_.size()); 2115 return this->symbols_[sym - this->local_symbol_count_]; 2116 } 2117 return NULL; 2118 } 2119 2120 // Return the section index of symbol SYM. Set *VALUE to its value 2121 // in the object file. Set *IS_ORDINARY if this is an ordinary 2122 // section index, not a special code between SHN_LORESERVE and 2123 // SHN_HIRESERVE. Note that for a symbol which is not defined in 2124 // this object file, this will set *VALUE to 0 and return SHN_UNDEF; 2125 // it will not return the final value of the symbol in the link. 2126 unsigned int 2127 symbol_section_and_value(unsigned int sym, Address* value, bool* is_ordinary); 2128 2129 // Return a pointer to the Symbol_value structure which holds the 2130 // value of a local symbol. 2131 const Symbol_value<size>* 2132 local_symbol(unsigned int sym) const 2133 { 2134 gold_assert(sym < this->local_values_.size()); 2135 return &this->local_values_[sym]; 2136 } 2137 2138 // Return the index of local symbol SYM in the ordinary symbol 2139 // table. A value of -1U means that the symbol is not being output. 2140 unsigned int 2141 symtab_index(unsigned int sym) const 2142 { 2143 gold_assert(sym < this->local_values_.size()); 2144 return this->local_values_[sym].output_symtab_index(); 2145 } 2146 2147 // Return the index of local symbol SYM in the dynamic symbol 2148 // table. A value of -1U means that the symbol is not being output. 2149 unsigned int 2150 dynsym_index(unsigned int sym) const 2151 { 2152 gold_assert(sym < this->local_values_.size()); 2153 return this->local_values_[sym].output_dynsym_index(); 2154 } 2155 2156 // Return the input section index of local symbol SYM. 2157 unsigned int 2158 local_symbol_input_shndx(unsigned int sym, bool* is_ordinary) const 2159 { 2160 gold_assert(sym < this->local_values_.size()); 2161 return this->local_values_[sym].input_shndx(is_ordinary); 2162 } 2163 2164 // Record that local symbol SYM must be in the output symbol table. 2165 void 2166 set_must_have_output_symtab_entry(unsigned int sym) 2167 { 2168 gold_assert(sym < this->local_values_.size()); 2169 this->local_values_[sym].set_must_have_output_symtab_entry(); 2170 } 2171 2172 // Record that local symbol SYM needs a dynamic symbol entry. 2173 void 2174 set_needs_output_dynsym_entry(unsigned int sym) 2175 { 2176 gold_assert(sym < this->local_values_.size()); 2177 this->local_values_[sym].set_needs_output_dynsym_entry(); 2178 } 2179 2180 // Return whether the local symbol SYMNDX has a PLT offset. 2181 bool 2182 local_has_plt_offset(unsigned int symndx) const; 2183 2184 // Set the PLT offset of the local symbol SYMNDX. 2185 void 2186 set_local_plt_offset(unsigned int symndx, unsigned int plt_offset); 2187 2188 // Adjust this local symbol value. Return false if the symbol 2189 // should be discarded from the output file. 2190 bool 2191 adjust_local_symbol(Symbol_value<size>* lv) const 2192 { return this->do_adjust_local_symbol(lv); } 2193 2194 // Return the name of the symbol that spans the given offset in the 2195 // specified section in this object. This is used only for error 2196 // messages and is not particularly efficient. 2197 bool 2198 get_symbol_location_info(unsigned int shndx, off_t offset, 2199 Symbol_location_info* info); 2200 2201 // Look for a kept section corresponding to the given discarded section, 2202 // and return its output address. This is used only for relocations in 2203 // debugging sections. 2204 Address 2205 map_to_kept_section(unsigned int shndx, bool* found) const; 2206 2207 // Compute final local symbol value. R_SYM is the local symbol index. 2208 // LV_IN points to a local symbol value containing the input value. 2209 // LV_OUT points to a local symbol value storing the final output value, 2210 // which must not be a merged symbol value since before calling this 2211 // method to avoid memory leak. SYMTAB points to a symbol table. 2212 // 2213 // The method returns a status code at return. If the return status is 2214 // CFLV_OK, *LV_OUT contains the final value. If the return status is 2215 // CFLV_ERROR, *LV_OUT is 0. If the return status is CFLV_DISCARDED, 2216 // *LV_OUT is not modified. 2217 Compute_final_local_value_status 2218 compute_final_local_value(unsigned int r_sym, 2219 const Symbol_value<size>* lv_in, 2220 Symbol_value<size>* lv_out, 2221 const Symbol_table* symtab); 2222 2223 // Return true if the layout for this object was deferred. 2224 bool is_deferred_layout() const 2225 { return this->is_deferred_layout_; } 2226 2227 // Discard output_views_ created in create_views(). 2228 virtual void 2229 clear_views(); 2230 2231 protected: 2232 typedef typename Sized_relobj<size, big_endian>::Output_sections 2233 Output_sections; 2234 2235 // Set up. 2236 virtual void 2237 do_setup(); 2238 2239 // Read the symbols. 2240 void 2241 do_read_symbols(Read_symbols_data*); 2242 2243 // Read the symbols. This is common code for all target-specific 2244 // overrides of do_read_symbols. 2245 void 2246 base_read_symbols(Read_symbols_data*); 2247 2248 // Return the value of a local symbol. 2249 uint64_t 2250 do_local_symbol_value(unsigned int symndx, uint64_t addend) const 2251 { 2252 const Symbol_value<size>* symval = this->local_symbol(symndx); 2253 return symval->value(this, addend); 2254 } 2255 2256 // Return the PLT offset for a local symbol. It is an error to call 2257 // this if it doesn't have one. 2258 unsigned int 2259 do_local_plt_offset(unsigned int symndx) const; 2260 2261 // Return whether local symbol SYMNDX is a TLS symbol. 2262 bool 2263 do_local_is_tls(unsigned int symndx) const 2264 { return this->local_symbol(symndx)->is_tls_symbol(); } 2265 2266 // Return the number of local symbols. 2267 unsigned int 2268 do_local_symbol_count() const 2269 { return this->local_symbol_count_; } 2270 2271 // Return the number of local symbols in the output symbol table. 2272 unsigned int 2273 do_output_local_symbol_count() const 2274 { return this->output_local_symbol_count_; } 2275 2276 // Return the number of local symbols in the output symbol table. 2277 off_t 2278 do_local_symbol_offset() const 2279 { return this->local_symbol_offset_; } 2280 2281 // Lay out the input sections. 2282 void 2283 do_layout(Symbol_table*, Layout*, Read_symbols_data*); 2284 2285 // Layout sections whose layout was deferred while waiting for 2286 // input files from a plugin. 2287 void 2288 do_layout_deferred_sections(Layout*); 2289 2290 // Add the symbols to the symbol table. 2291 void 2292 do_add_symbols(Symbol_table*, Read_symbols_data*, Layout*); 2293 2294 Archive::Should_include 2295 do_should_include_member(Symbol_table* symtab, Layout*, Read_symbols_data*, 2296 std::string* why); 2297 2298 // Iterate over global symbols, calling a visitor class V for each. 2299 void 2300 do_for_all_global_symbols(Read_symbols_data* sd, 2301 Library_base::Symbol_visitor_base* v); 2302 2303 // Read the relocs. 2304 void 2305 do_read_relocs(Read_relocs_data*); 2306 2307 // Process the relocs to find list of referenced sections. Used only 2308 // during garbage collection. 2309 void 2310 do_gc_process_relocs(Symbol_table*, Layout*, Read_relocs_data*); 2311 2312 // Scan the relocs and adjust the symbol table. 2313 void 2314 do_scan_relocs(Symbol_table*, Layout*, Read_relocs_data*); 2315 2316 // Count the local symbols. 2317 void 2318 do_count_local_symbols(Stringpool_template<char>*, 2319 Stringpool_template<char>*); 2320 2321 // Finalize the local symbols. 2322 unsigned int 2323 do_finalize_local_symbols(unsigned int, off_t, Symbol_table*); 2324 2325 // Set the offset where local dynamic symbol information will be stored. 2326 unsigned int 2327 do_set_local_dynsym_indexes(unsigned int); 2328 2329 // Set the offset where local dynamic symbol information will be stored. 2330 unsigned int 2331 do_set_local_dynsym_offset(off_t); 2332 2333 // Relocate the input sections and write out the local symbols. 2334 void 2335 do_relocate(const Symbol_table* symtab, const Layout*, Output_file* of); 2336 2337 // Get the size of a section. 2338 uint64_t 2339 do_section_size(unsigned int shndx) 2340 { return this->elf_file_.section_size(shndx); } 2341 2342 // Get the name of a section. 2343 std::string 2344 do_section_name(unsigned int shndx) const 2345 { return this->elf_file_.section_name(shndx); } 2346 2347 // Return the location of the contents of a section. 2348 const unsigned char* 2349 do_section_contents(unsigned int shndx, section_size_type* plen, 2350 bool cache) 2351 { 2352 Object::Location loc(this->elf_file_.section_contents(shndx)); 2353 *plen = convert_to_section_size_type(loc.data_size); 2354 if (*plen == 0) 2355 { 2356 static const unsigned char empty[1] = { '\0' }; 2357 return empty; 2358 } 2359 return this->get_view(loc.file_offset, *plen, true, cache); 2360 } 2361 2362 // Return section flags. 2363 uint64_t 2364 do_section_flags(unsigned int shndx); 2365 2366 // Return section entsize. 2367 uint64_t 2368 do_section_entsize(unsigned int shndx); 2369 2370 // Return section address. 2371 uint64_t 2372 do_section_address(unsigned int shndx) 2373 { return this->elf_file_.section_addr(shndx); } 2374 2375 // Return section type. 2376 unsigned int 2377 do_section_type(unsigned int shndx) 2378 { return this->elf_file_.section_type(shndx); } 2379 2380 // Return the section link field. 2381 unsigned int 2382 do_section_link(unsigned int shndx) 2383 { return this->elf_file_.section_link(shndx); } 2384 2385 // Return the section info field. 2386 unsigned int 2387 do_section_info(unsigned int shndx) 2388 { return this->elf_file_.section_info(shndx); } 2389 2390 // Return the section alignment. 2391 uint64_t 2392 do_section_addralign(unsigned int shndx) 2393 { return this->elf_file_.section_addralign(shndx); } 2394 2395 // Return the Xindex structure to use. 2396 Xindex* 2397 do_initialize_xindex(); 2398 2399 // Get symbol counts. 2400 void 2401 do_get_global_symbol_counts(const Symbol_table*, size_t*, size_t*) const; 2402 2403 // Get the global symbols. 2404 const Symbols* 2405 do_get_global_symbols() const 2406 { return &this->symbols_; } 2407 2408 // Adjust a section index if necessary. 2409 unsigned int 2410 adjust_shndx(unsigned int shndx) 2411 { 2412 if (shndx >= elfcpp::SHN_LORESERVE) 2413 shndx += this->elf_file_.large_shndx_offset(); 2414 return shndx; 2415 } 2416 2417 // Initialize input to output maps for section symbols in merged 2418 // sections. 2419 void 2420 initialize_input_to_output_maps(); 2421 2422 // Free the input to output maps for section symbols in merged 2423 // sections. 2424 void 2425 free_input_to_output_maps(); 2426 2427 // Return symbol table section index. 2428 unsigned int 2429 symtab_shndx() const 2430 { return this->symtab_shndx_; } 2431 2432 // Allow a child class to access the ELF file. 2433 elfcpp::Elf_file<size, big_endian, Object>* 2434 elf_file() 2435 { return &this->elf_file_; } 2436 2437 // Allow a child class to access the local values. 2438 Local_values* 2439 local_values() 2440 { return &this->local_values_; } 2441 2442 // Views and sizes when relocating. 2443 struct View_size 2444 { 2445 unsigned char* view; 2446 typename elfcpp::Elf_types<size>::Elf_Addr address; 2447 off_t offset; 2448 section_size_type view_size; 2449 bool is_input_output_view; 2450 bool is_postprocessing_view; 2451 bool is_ctors_reverse_view; 2452 }; 2453 2454 typedef std::vector<View_size> Views; 2455 2456 // Stash away info for a number of special sections. 2457 // Return true if any of the sections found require local symbols to be read. 2458 virtual bool 2459 do_find_special_sections(Read_symbols_data* sd); 2460 2461 // This may be overriden by a child class. 2462 virtual void 2463 do_relocate_sections(const Symbol_table* symtab, const Layout* layout, 2464 const unsigned char* pshdrs, Output_file* of, 2465 Views* pviews); 2466 2467 // Adjust this local symbol value. Return false if the symbol 2468 // should be discarded from the output file. 2469 virtual bool 2470 do_adjust_local_symbol(Symbol_value<size>*) const 2471 { return true; } 2472 2473 // Allow a child to set output local symbol count. 2474 void 2475 set_output_local_symbol_count(unsigned int value) 2476 { this->output_local_symbol_count_ = value; } 2477 2478 Views* 2479 create_views() 2480 { 2481 gold_assert(this->output_views_ == NULL); 2482 this->output_views_ = new Views(); 2483 return this->output_views_; 2484 } 2485 2486 // Allow child access to output_views_. 2487 Views* 2488 get_views() const 2489 { 2490 gold_assert(this->output_views_); 2491 return this->output_views_; 2492 } 2493 2494 private: 2495 // For convenience. 2496 typedef Sized_relobj_file<size, big_endian> This; 2497 static const int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size; 2498 static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size; 2499 static const int sym_size = elfcpp::Elf_sizes<size>::sym_size; 2500 typedef elfcpp::Shdr<size, big_endian> Shdr; 2501 2502 // To keep track of discarded comdat sections, we need to map a member 2503 // section index to the object and section index of the corresponding 2504 // kept section. 2505 struct Kept_comdat_section 2506 { 2507 Kept_comdat_section(Relobj* a_object, unsigned int a_shndx) 2508 : object(a_object), shndx(a_shndx) 2509 { } 2510 Relobj* object; 2511 unsigned int shndx; 2512 }; 2513 typedef std::map<unsigned int, Kept_comdat_section> 2514 Kept_comdat_section_table; 2515 2516 // Find the SHT_SYMTAB section, given the section headers. 2517 void 2518 find_symtab(const unsigned char* pshdrs); 2519 2520 // Return whether SHDR has the right flags for a GNU style exception 2521 // frame section. 2522 bool 2523 check_eh_frame_flags(const elfcpp::Shdr<size, big_endian>* shdr) const; 2524 2525 // Return whether there is a section named .eh_frame which might be 2526 // a GNU style exception frame section. 2527 bool 2528 find_eh_frame(const unsigned char* pshdrs, const char* names, 2529 section_size_type names_size) const; 2530 2531 // Whether to include a section group in the link. 2532 bool 2533 include_section_group(Symbol_table*, Layout*, unsigned int, const char*, 2534 const unsigned char*, const char*, section_size_type, 2535 std::vector<bool>*); 2536 2537 // Whether to include a linkonce section in the link. 2538 bool 2539 include_linkonce_section(Layout*, unsigned int, const char*, 2540 const elfcpp::Shdr<size, big_endian>&); 2541 2542 // Layout an input section. 2543 void 2544 layout_section(Layout* layout, unsigned int shndx, const char* name, 2545 const typename This::Shdr& shdr, unsigned int reloc_shndx, 2546 unsigned int reloc_type); 2547 2548 // Layout an input .eh_frame section. 2549 void 2550 layout_eh_frame_section(Layout* layout, const unsigned char* symbols_data, 2551 section_size_type symbols_size, 2552 const unsigned char* symbol_names_data, 2553 section_size_type symbol_names_size, 2554 unsigned int shndx, const typename This::Shdr&, 2555 unsigned int reloc_shndx, unsigned int reloc_type); 2556 2557 // Write section data to the output file. Record the views and 2558 // sizes in VIEWS for use when relocating. 2559 void 2560 write_sections(const Layout*, const unsigned char* pshdrs, Output_file*, 2561 Views*); 2562 2563 // Relocate the sections in the output file. 2564 void 2565 relocate_sections(const Symbol_table* symtab, const Layout* layout, 2566 const unsigned char* pshdrs, Output_file* of, 2567 Views* pviews) 2568 { this->do_relocate_sections(symtab, layout, pshdrs, of, pviews); } 2569 2570 // Reverse the words in a section. Used for .ctors sections mapped 2571 // to .init_array sections. 2572 void 2573 reverse_words(unsigned char*, section_size_type); 2574 2575 // Scan the input relocations for --emit-relocs. 2576 void 2577 emit_relocs_scan(Symbol_table*, Layout*, const unsigned char* plocal_syms, 2578 const Read_relocs_data::Relocs_list::iterator&); 2579 2580 // Scan the input relocations for --emit-relocs, templatized on the 2581 // type of the relocation section. 2582 template<int sh_type> 2583 void 2584 emit_relocs_scan_reltype(Symbol_table*, Layout*, 2585 const unsigned char* plocal_syms, 2586 const Read_relocs_data::Relocs_list::iterator&, 2587 Relocatable_relocs*); 2588 2589 // Scan the input relocations for --incremental. 2590 void 2591 incremental_relocs_scan(const Read_relocs_data::Relocs_list::iterator&); 2592 2593 // Scan the input relocations for --incremental, templatized on the 2594 // type of the relocation section. 2595 template<int sh_type> 2596 void 2597 incremental_relocs_scan_reltype( 2598 const Read_relocs_data::Relocs_list::iterator&); 2599 2600 void 2601 incremental_relocs_write(const Relocate_info<size, big_endian>*, 2602 unsigned int sh_type, 2603 const unsigned char* prelocs, 2604 size_t reloc_count, 2605 Output_section*, 2606 Address output_offset, 2607 Output_file*); 2608 2609 template<int sh_type> 2610 void 2611 incremental_relocs_write_reltype(const Relocate_info<size, big_endian>*, 2612 const unsigned char* prelocs, 2613 size_t reloc_count, 2614 Output_section*, 2615 Address output_offset, 2616 Output_file*); 2617 2618 // A type shared by split_stack_adjust_reltype and find_functions. 2619 typedef std::map<section_offset_type, section_size_type> Function_offsets; 2620 2621 // Check for -fsplit-stack routines calling non-split-stack routines. 2622 void 2623 split_stack_adjust(const Symbol_table*, const unsigned char* pshdrs, 2624 unsigned int sh_type, unsigned int shndx, 2625 const unsigned char* prelocs, size_t reloc_count, 2626 unsigned char* view, section_size_type view_size, 2627 Reloc_symbol_changes** reloc_map); 2628 2629 template<int sh_type> 2630 void 2631 split_stack_adjust_reltype(const Symbol_table*, const unsigned char* pshdrs, 2632 unsigned int shndx, const unsigned char* prelocs, 2633 size_t reloc_count, unsigned char* view, 2634 section_size_type view_size, 2635 Reloc_symbol_changes** reloc_map); 2636 2637 // Find all functions in a section. 2638 void 2639 find_functions(const unsigned char* pshdrs, unsigned int shndx, 2640 Function_offsets*); 2641 2642 // Write out the local symbols. 2643 void 2644 write_local_symbols(Output_file*, 2645 const Stringpool_template<char>*, 2646 const Stringpool_template<char>*, 2647 Output_symtab_xindex*, 2648 Output_symtab_xindex*, 2649 off_t); 2650 2651 // Record a mapping from discarded section SHNDX to the corresponding 2652 // kept section. 2653 void 2654 set_kept_comdat_section(unsigned int shndx, Relobj* kept_object, 2655 unsigned int kept_shndx) 2656 { 2657 Kept_comdat_section kept(kept_object, kept_shndx); 2658 this->kept_comdat_sections_.insert(std::make_pair(shndx, kept)); 2659 } 2660 2661 // Find the kept section corresponding to the discarded section 2662 // SHNDX. Return true if found. 2663 bool 2664 get_kept_comdat_section(unsigned int shndx, Relobj** kept_object, 2665 unsigned int* kept_shndx) const 2666 { 2667 typename Kept_comdat_section_table::const_iterator p = 2668 this->kept_comdat_sections_.find(shndx); 2669 if (p == this->kept_comdat_sections_.end()) 2670 return false; 2671 *kept_object = p->second.object; 2672 *kept_shndx = p->second.shndx; 2673 return true; 2674 } 2675 2676 // Compute final local symbol value. R_SYM is the local symbol index. 2677 // LV_IN points to a local symbol value containing the input value. 2678 // LV_OUT points to a local symbol value storing the final output value, 2679 // which must not be a merged symbol value since before calling this 2680 // method to avoid memory leak. RELOCATABLE indicates whether we are 2681 // linking a relocatable output. OUT_SECTIONS is an array of output 2682 // sections. OUT_OFFSETS is an array of offsets of the sections. SYMTAB 2683 // points to a symbol table. 2684 // 2685 // The method returns a status code at return. If the return status is 2686 // CFLV_OK, *LV_OUT contains the final value. If the return status is 2687 // CFLV_ERROR, *LV_OUT is 0. If the return status is CFLV_DISCARDED, 2688 // *LV_OUT is not modified. 2689 inline Compute_final_local_value_status 2690 compute_final_local_value_internal(unsigned int r_sym, 2691 const Symbol_value<size>* lv_in, 2692 Symbol_value<size>* lv_out, 2693 bool relocatable, 2694 const Output_sections& out_sections, 2695 const std::vector<Address>& out_offsets, 2696 const Symbol_table* symtab); 2697 2698 // The PLT offsets of local symbols. 2699 typedef Unordered_map<unsigned int, unsigned int> Local_plt_offsets; 2700 2701 // Saved information for sections whose layout was deferred. 2702 struct Deferred_layout 2703 { 2704 static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size; 2705 Deferred_layout(unsigned int shndx, const char* name, 2706 const unsigned char* pshdr, 2707 unsigned int reloc_shndx, unsigned int reloc_type) 2708 : shndx_(shndx), name_(name), reloc_shndx_(reloc_shndx), 2709 reloc_type_(reloc_type) 2710 { 2711 memcpy(this->shdr_data_, pshdr, shdr_size); 2712 } 2713 unsigned int shndx_; 2714 std::string name_; 2715 unsigned int reloc_shndx_; 2716 unsigned int reloc_type_; 2717 unsigned char shdr_data_[shdr_size]; 2718 }; 2719 2720 // General access to the ELF file. 2721 elfcpp::Elf_file<size, big_endian, Object> elf_file_; 2722 // Type of ELF file (ET_REL or ET_EXEC). ET_EXEC files are allowed 2723 // as input files only for the --just-symbols option. 2724 int e_type_; 2725 // Index of SHT_SYMTAB section. 2726 unsigned int symtab_shndx_; 2727 // The number of local symbols. 2728 unsigned int local_symbol_count_; 2729 // The number of local symbols which go into the output file. 2730 unsigned int output_local_symbol_count_; 2731 // The number of local symbols which go into the output file's dynamic 2732 // symbol table. 2733 unsigned int output_local_dynsym_count_; 2734 // The entries in the symbol table for the external symbols. 2735 Symbols symbols_; 2736 // Number of symbols defined in object file itself. 2737 size_t defined_count_; 2738 // File offset for local symbols (relative to start of symbol table). 2739 off_t local_symbol_offset_; 2740 // File offset for local dynamic symbols (absolute). 2741 off_t local_dynsym_offset_; 2742 // Values of local symbols. 2743 Local_values local_values_; 2744 // PLT offsets for local symbols. 2745 Local_plt_offsets local_plt_offsets_; 2746 // Table mapping discarded comdat sections to corresponding kept sections. 2747 Kept_comdat_section_table kept_comdat_sections_; 2748 // Whether this object has a GNU style .eh_frame section. 2749 bool has_eh_frame_; 2750 // If this object has a GNU style .eh_frame section that is discarded in 2751 // output, record the index here. Otherwise it is -1U. 2752 unsigned int discarded_eh_frame_shndx_; 2753 // True if the layout of this object was deferred, waiting for plugin 2754 // replacement files. 2755 bool is_deferred_layout_; 2756 // The list of sections whose layout was deferred. 2757 std::vector<Deferred_layout> deferred_layout_; 2758 // The list of relocation sections whose layout was deferred. 2759 std::vector<Deferred_layout> deferred_layout_relocs_; 2760 // Pointer to the list of output views; valid only during do_relocate(). 2761 Views* output_views_; 2762 }; 2763 2764 // A class to manage the list of all objects. 2765 2766 class Input_objects 2767 { 2768 public: 2769 Input_objects() 2770 : relobj_list_(), dynobj_list_(), sonames_(), cref_(NULL) 2771 { } 2772 2773 // The type of the list of input relocateable objects. 2774 typedef std::vector<Relobj*> Relobj_list; 2775 typedef Relobj_list::const_iterator Relobj_iterator; 2776 2777 // The type of the list of input dynamic objects. 2778 typedef std::vector<Dynobj*> Dynobj_list; 2779 typedef Dynobj_list::const_iterator Dynobj_iterator; 2780 2781 // Add an object to the list. Return true if all is well, or false 2782 // if this object should be ignored. 2783 bool 2784 add_object(Object*); 2785 2786 // Start processing an archive. 2787 void 2788 archive_start(Archive*); 2789 2790 // Stop processing an archive. 2791 void 2792 archive_stop(Archive*); 2793 2794 // For each dynamic object, check whether we've seen all of its 2795 // explicit dependencies. 2796 void 2797 check_dynamic_dependencies() const; 2798 2799 // Return whether an object was found in the system library 2800 // directory. 2801 bool 2802 found_in_system_library_directory(const Object*) const; 2803 2804 // Print symbol counts. 2805 void 2806 print_symbol_counts(const Symbol_table*) const; 2807 2808 // Print a cross reference table. 2809 void 2810 print_cref(const Symbol_table*, FILE*) const; 2811 2812 // Iterate over all regular objects. 2813 2814 Relobj_iterator 2815 relobj_begin() const 2816 { return this->relobj_list_.begin(); } 2817 2818 Relobj_iterator 2819 relobj_end() const 2820 { return this->relobj_list_.end(); } 2821 2822 // Iterate over all dynamic objects. 2823 2824 Dynobj_iterator 2825 dynobj_begin() const 2826 { return this->dynobj_list_.begin(); } 2827 2828 Dynobj_iterator 2829 dynobj_end() const 2830 { return this->dynobj_list_.end(); } 2831 2832 // Return whether we have seen any dynamic objects. 2833 bool 2834 any_dynamic() const 2835 { return !this->dynobj_list_.empty(); } 2836 2837 // Return the number of non dynamic objects. 2838 int 2839 number_of_relobjs() const 2840 { return this->relobj_list_.size(); } 2841 2842 // Return the number of input objects. 2843 int 2844 number_of_input_objects() const 2845 { return this->relobj_list_.size() + this->dynobj_list_.size(); } 2846 2847 private: 2848 Input_objects(const Input_objects&); 2849 Input_objects& operator=(const Input_objects&); 2850 2851 // The list of ordinary objects included in the link. 2852 Relobj_list relobj_list_; 2853 // The list of dynamic objects included in the link. 2854 Dynobj_list dynobj_list_; 2855 // SONAMEs that we have seen. 2856 Unordered_set<std::string> sonames_; 2857 // Manage cross-references if requested. 2858 Cref* cref_; 2859 }; 2860 2861 // Some of the information we pass to the relocation routines. We 2862 // group this together to avoid passing a dozen different arguments. 2863 2864 template<int size, bool big_endian> 2865 struct Relocate_info 2866 { 2867 // Symbol table. 2868 const Symbol_table* symtab; 2869 // Layout. 2870 const Layout* layout; 2871 // Object being relocated. 2872 Sized_relobj_file<size, big_endian>* object; 2873 // Section index of relocation section. 2874 unsigned int reloc_shndx; 2875 // Section header of relocation section. 2876 const unsigned char* reloc_shdr; 2877 // Section index of section being relocated. 2878 unsigned int data_shndx; 2879 // Section header of data section. 2880 const unsigned char* data_shdr; 2881 2882 // Return a string showing the location of a relocation. This is 2883 // only used for error messages. 2884 std::string 2885 location(size_t relnum, off_t reloffset) const; 2886 }; 2887 2888 // This is used to represent a section in an object and is used as the 2889 // key type for various section maps. 2890 typedef std::pair<Object*, unsigned int> Section_id; 2891 2892 // This is similar to Section_id but is used when the section 2893 // pointers are const. 2894 typedef std::pair<const Object*, unsigned int> Const_section_id; 2895 2896 // The hash value is based on the address of an object in memory during 2897 // linking. It is okay to use this for looking up sections but never use 2898 // this in an unordered container that we want to traverse in a repeatable 2899 // manner. 2900 2901 struct Section_id_hash 2902 { 2903 size_t operator()(const Section_id& loc) const 2904 { return reinterpret_cast<uintptr_t>(loc.first) ^ loc.second; } 2905 }; 2906 2907 struct Const_section_id_hash 2908 { 2909 size_t operator()(const Const_section_id& loc) const 2910 { return reinterpret_cast<uintptr_t>(loc.first) ^ loc.second; } 2911 }; 2912 2913 // Return whether INPUT_FILE contains an ELF object start at file 2914 // offset OFFSET. This sets *START to point to a view of the start of 2915 // the file. It sets *READ_SIZE to the number of bytes in the view. 2916 2917 extern bool 2918 is_elf_object(Input_file* input_file, off_t offset, 2919 const unsigned char** start, int* read_size); 2920 2921 // Return an Object appropriate for the input file. P is BYTES long, 2922 // and holds the ELF header. If PUNCONFIGURED is not NULL, then if 2923 // this sees an object the linker is not configured to support, it 2924 // sets *PUNCONFIGURED to true and returns NULL without giving an 2925 // error message. 2926 2927 extern Object* 2928 make_elf_object(const std::string& name, Input_file*, 2929 off_t offset, const unsigned char* p, 2930 section_offset_type bytes, bool* punconfigured); 2931 2932 } // end namespace gold 2933 2934 #endif // !defined(GOLD_OBJECT_H) 2935