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