1 // archive.cc -- archive support for gold 2 3 // Copyright (C) 2006-2022 Free Software Foundation, Inc. 4 // Written by Ian Lance Taylor <iant@google.com>. 5 6 // This file is part of gold. 7 8 // This program is free software; you can redistribute it and/or modify 9 // it under the terms of the GNU General Public License as published by 10 // the Free Software Foundation; either version 3 of the License, or 11 // (at your option) any later version. 12 13 // This program is distributed in the hope that it will be useful, 14 // but WITHOUT ANY WARRANTY; without even the implied warranty of 15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 // GNU General Public License for more details. 17 18 // You should have received a copy of the GNU General Public License 19 // along with this program; if not, write to the Free Software 20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, 21 // MA 02110-1301, USA. 22 23 #include "gold.h" 24 25 #include <cerrno> 26 #include <cstring> 27 #include <climits> 28 #include <vector> 29 #include "libiberty.h" 30 #include "filenames.h" 31 32 #include "elfcpp.h" 33 #include "options.h" 34 #include "mapfile.h" 35 #include "fileread.h" 36 #include "readsyms.h" 37 #include "symtab.h" 38 #include "object.h" 39 #include "layout.h" 40 #include "archive.h" 41 #include "plugin.h" 42 #include "incremental.h" 43 44 namespace gold 45 { 46 47 // Library_base methods. 48 49 // Determine whether a definition of SYM_NAME should cause an archive 50 // library member to be included in the link. Returns SHOULD_INCLUDE_YES 51 // if the symbol is referenced but not defined, SHOULD_INCLUDE_NO if the 52 // symbol is already defined, and SHOULD_INCLUDE_UNKNOWN if the symbol is 53 // neither referenced nor defined. 54 55 Library_base::Should_include 56 Library_base::should_include_member(Symbol_table* symtab, Layout* layout, 57 const char* sym_name, Symbol** symp, 58 std::string* why, char** tmpbufp, 59 size_t* tmpbuflen) 60 { 61 // In an object file, and therefore in an archive map, an 62 // '@' in the name separates the symbol name from the 63 // version name. If there are two '@' characters, this is 64 // the default version. 65 char* tmpbuf = *tmpbufp; 66 const char* ver = strchr(sym_name, '@'); 67 bool def = false; 68 if (ver != NULL) 69 { 70 size_t symlen = ver - sym_name; 71 if (symlen + 1 > *tmpbuflen) 72 { 73 tmpbuf = static_cast<char*>(xrealloc(tmpbuf, symlen + 1)); 74 *tmpbufp = tmpbuf; 75 *tmpbuflen = symlen + 1; 76 } 77 memcpy(tmpbuf, sym_name, symlen); 78 tmpbuf[symlen] = '\0'; 79 sym_name = tmpbuf; 80 81 ++ver; 82 if (*ver == '@') 83 { 84 ++ver; 85 def = true; 86 } 87 } 88 89 Symbol* sym = symtab->lookup(sym_name, ver); 90 if (def 91 && ver != NULL 92 && (sym == NULL 93 || !sym->is_undefined() 94 || sym->binding() == elfcpp::STB_WEAK)) 95 sym = symtab->lookup(sym_name, NULL); 96 97 *symp = sym; 98 99 if (sym != NULL) 100 { 101 if (!sym->is_undefined()) 102 return Library_base::SHOULD_INCLUDE_NO; 103 104 // PR 12001: Do not include an archive when the undefined 105 // symbol has actually been defined on the command line. 106 if (layout->script_options()->is_pending_assignment(sym_name)) 107 return Library_base::SHOULD_INCLUDE_NO; 108 109 // If the symbol is weak undefined, we still need to check 110 // for other reasons (like a -u option). 111 if (sym->binding() != elfcpp::STB_WEAK) 112 return Library_base::SHOULD_INCLUDE_YES; 113 } 114 115 // Check whether the symbol was named in a -u option. 116 if (parameters->options().is_undefined(sym_name)) 117 { 118 *why = "-u "; 119 *why += sym_name; 120 return Library_base::SHOULD_INCLUDE_YES; 121 } 122 123 if (layout->script_options()->is_referenced(sym_name)) 124 { 125 size_t alc = 100 + strlen(sym_name); 126 char* buf = new char[alc]; 127 snprintf(buf, alc, _("script or expression reference to %s"), 128 sym_name); 129 *why = buf; 130 delete[] buf; 131 return Library_base::SHOULD_INCLUDE_YES; 132 } 133 134 if (!parameters->options().relocatable()) 135 { 136 const char* entry_sym = parameters->entry(); 137 if (entry_sym != NULL && strcmp(sym_name, entry_sym) == 0) 138 { 139 *why = "entry symbol "; 140 *why += sym_name; 141 return Library_base::SHOULD_INCLUDE_YES; 142 } 143 } 144 145 return Library_base::SHOULD_INCLUDE_UNKNOWN; 146 } 147 148 // The header of an entry in the archive. This is all readable text, 149 // padded with spaces where necessary. If the contents of an archive 150 // are all text file, the entire archive is readable. 151 152 struct Archive::Archive_header 153 { 154 // The entry name. 155 char ar_name[16]; 156 // The file modification time. 157 char ar_date[12]; 158 // The user's UID in decimal. 159 char ar_uid[6]; 160 // The user's GID in decimal. 161 char ar_gid[6]; 162 // The file mode in octal. 163 char ar_mode[8]; 164 // The file size in decimal. 165 char ar_size[10]; 166 // The final magic code. 167 char ar_fmag[2]; 168 }; 169 170 // Class Archive static variables. 171 unsigned int Archive::total_archives; 172 unsigned int Archive::total_members; 173 unsigned int Archive::total_members_loaded; 174 175 // Archive methods. 176 177 const char Archive::armag[sarmag] = 178 { 179 '!', '<', 'a', 'r', 'c', 'h', '>', '\n' 180 }; 181 182 const char Archive::armagt[sarmag] = 183 { 184 '!', '<', 't', 'h', 'i', 'n', '>', '\n' 185 }; 186 187 const char Archive::arfmag[2] = { '`', '\n' }; 188 189 const char Archive::sym64name[7] = { '/', 'S', 'Y', 'M', '6', '4', '/' }; 190 191 Archive::Archive(const std::string& name, Input_file* input_file, 192 bool is_thin_archive, Dirsearch* dirpath, Task* task) 193 : Library_base(task), name_(name), input_file_(input_file), armap_(), 194 armap_names_(), extended_names_(), armap_checked_(), seen_offsets_(), 195 members_(), is_thin_archive_(is_thin_archive), included_member_(false), 196 nested_archives_(), dirpath_(dirpath), num_members_(0), 197 included_all_members_(false) 198 { 199 this->no_export_ = 200 parameters->options().check_excluded_libs(input_file->found_name()); 201 } 202 203 // Set up the archive: read the symbol map and the extended name 204 // table. 205 206 void 207 Archive::setup() 208 { 209 // We need to ignore empty archives. 210 if (this->input_file_->file().filesize() == sarmag) 211 return; 212 213 // The first member of the archive should be the symbol table. 214 std::string armap_name; 215 off_t header_size = this->read_header(sarmag, false, &armap_name, NULL); 216 if (header_size == -1) 217 return; 218 219 section_size_type armap_size = convert_to_section_size_type(header_size); 220 off_t off = sarmag; 221 if (armap_name.empty()) 222 { 223 this->read_armap<32>(sarmag + sizeof(Archive_header), armap_size); 224 off = sarmag + sizeof(Archive_header) + armap_size; 225 } 226 else if (armap_name == "/SYM64/") 227 { 228 this->read_armap<64>(sarmag + sizeof(Archive_header), armap_size); 229 off = sarmag + sizeof(Archive_header) + armap_size; 230 } 231 else if (!this->input_file_->options().whole_archive()) 232 gold_error(_("%s: no archive symbol table (run ranlib)"), 233 this->name().c_str()); 234 235 // See if there is an extended name table. We cache these views 236 // because it is likely that we will want to read the following 237 // header in the add_symbols routine. 238 if ((off & 1) != 0) 239 ++off; 240 std::string xname; 241 header_size = this->read_header(off, true, &xname, NULL); 242 if (header_size == -1) 243 return; 244 245 section_size_type extended_size = convert_to_section_size_type(header_size); 246 if (xname == "/") 247 { 248 const unsigned char* p = this->get_view(off + sizeof(Archive_header), 249 extended_size, false, true); 250 const char* px = reinterpret_cast<const char*>(p); 251 this->extended_names_.assign(px, extended_size); 252 } 253 bool preread_syms = (parameters->options().threads() 254 && parameters->options().preread_archive_symbols()); 255 #ifndef ENABLE_THREADS 256 preread_syms = false; 257 #else 258 if (parameters->options().has_plugins()) 259 preread_syms = false; 260 #endif 261 if (preread_syms) 262 this->read_all_symbols(); 263 } 264 265 // Unlock any nested archives. 266 267 void 268 Archive::unlock_nested_archives() 269 { 270 for (Nested_archive_table::iterator p = this->nested_archives_.begin(); 271 p != this->nested_archives_.end(); 272 ++p) 273 { 274 p->second->unlock(this->task_); 275 } 276 } 277 278 // Read the archive symbol map. 279 280 template<int mapsize> 281 void 282 Archive::read_armap(off_t start, section_size_type size) 283 { 284 // To count the total number of archive members, we'll just count 285 // the number of times the file offset changes. Since most archives 286 // group the symbols in the armap by object, this ought to give us 287 // an accurate count. 288 off_t last_seen_offset = -1; 289 290 // Read in the entire armap. 291 const unsigned char* p = this->get_view(start, size, true, false); 292 293 // Numbers in the armap are always big-endian. 294 typedef typename elfcpp::Elf_types<mapsize>::Elf_Addr Entry_type; 295 const Entry_type* pword = reinterpret_cast<const Entry_type*>(p); 296 unsigned long nsyms = convert_types<unsigned long, Entry_type>( 297 elfcpp::Swap<mapsize, true>::readval(pword)); 298 ++pword; 299 300 // Note that the addition is in units of sizeof(elfcpp::Elf_Word). 301 const char* pnames = reinterpret_cast<const char*>(pword + nsyms); 302 section_size_type names_size = 303 reinterpret_cast<const char*>(p) + size - pnames; 304 this->armap_names_.assign(pnames, names_size); 305 306 this->armap_.resize(nsyms); 307 308 section_offset_type name_offset = 0; 309 for (unsigned long i = 0; i < nsyms; ++i) 310 { 311 this->armap_[i].name_offset = name_offset; 312 this->armap_[i].file_offset = convert_types<off_t, Entry_type>( 313 elfcpp::Swap<mapsize, true>::readval(pword)); 314 name_offset += strlen(pnames + name_offset) + 1; 315 ++pword; 316 if (this->armap_[i].file_offset != last_seen_offset) 317 { 318 last_seen_offset = this->armap_[i].file_offset; 319 ++this->num_members_; 320 } 321 } 322 323 if (static_cast<section_size_type>(name_offset) > names_size) 324 gold_error(_("%s: bad archive symbol table names"), 325 this->name().c_str()); 326 327 // This array keeps track of which symbols are for archive elements 328 // which we have already included in the link. 329 this->armap_checked_.resize(nsyms); 330 } 331 332 // Read the header of an archive member at OFF. Fail if something 333 // goes wrong. Return the size of the member. Set *PNAME to the name 334 // of the member. 335 336 off_t 337 Archive::read_header(off_t off, bool cache, std::string* pname, 338 off_t* nested_off) 339 { 340 const unsigned char* p = this->get_view(off, sizeof(Archive_header), true, 341 cache); 342 const Archive_header* hdr = reinterpret_cast<const Archive_header*>(p); 343 return this->interpret_header(hdr, off, pname, nested_off); 344 } 345 346 // Interpret the header of HDR, the header of the archive member at 347 // file offset OFF. Return the size of the member, or -1 if something 348 // has gone wrong. Set *PNAME to the name of the member. 349 350 off_t 351 Archive::interpret_header(const Archive_header* hdr, off_t off, 352 std::string* pname, off_t* nested_off) const 353 { 354 if (memcmp(hdr->ar_fmag, arfmag, sizeof arfmag) != 0) 355 { 356 gold_error(_("%s: malformed archive header at %zu"), 357 this->name().c_str(), static_cast<size_t>(off)); 358 return -1; 359 } 360 361 const int size_string_size = sizeof hdr->ar_size; 362 char size_string[size_string_size + 1]; 363 memcpy(size_string, hdr->ar_size, size_string_size); 364 char* ps = size_string + size_string_size; 365 while (ps[-1] == ' ') 366 --ps; 367 *ps = '\0'; 368 369 errno = 0; 370 char* end; 371 off_t member_size = strtol(size_string, &end, 10); 372 if (*end != '\0' 373 || member_size < 0 374 || (member_size == LONG_MAX && errno == ERANGE)) 375 { 376 gold_error(_("%s: malformed archive header size at %zu"), 377 this->name().c_str(), static_cast<size_t>(off)); 378 return -1; 379 } 380 381 if (hdr->ar_name[0] != '/') 382 { 383 const char* name_end = strchr(hdr->ar_name, '/'); 384 if (name_end == NULL 385 || name_end - hdr->ar_name >= static_cast<int>(sizeof hdr->ar_name)) 386 { 387 gold_error(_("%s: malformed archive header name at %zu"), 388 this->name().c_str(), static_cast<size_t>(off)); 389 return -1; 390 } 391 pname->assign(hdr->ar_name, name_end - hdr->ar_name); 392 if (nested_off != NULL) 393 *nested_off = 0; 394 } 395 else if (hdr->ar_name[1] == ' ') 396 { 397 // This is the symbol table. 398 if (!pname->empty()) 399 pname->clear(); 400 } 401 else if (memcmp(hdr->ar_name, sym64name, sizeof sym64name) == 0) 402 { 403 // This is the symbol table, 64-bit version. 404 pname->assign(sym64name, sizeof sym64name); 405 } 406 else if (hdr->ar_name[1] == '/') 407 { 408 // This is the extended name table. 409 pname->assign(1, '/'); 410 } 411 else 412 { 413 errno = 0; 414 long x = strtol(hdr->ar_name + 1, &end, 10); 415 long y = 0; 416 if (*end == ':') 417 y = strtol(end + 1, &end, 10); 418 if (*end != ' ' 419 || x < 0 420 || (x == LONG_MAX && errno == ERANGE) 421 || static_cast<size_t>(x) >= this->extended_names_.size()) 422 { 423 gold_error(_("%s: bad extended name index at %zu"), 424 this->name().c_str(), static_cast<size_t>(off)); 425 return -1; 426 } 427 428 const char* name = this->extended_names_.data() + x; 429 const char* name_end = strchr(name, '\n'); 430 if (static_cast<size_t>(name_end - name) > this->extended_names_.size() 431 || name_end[-1] != '/') 432 { 433 gold_error(_("%s: bad extended name entry at header %zu"), 434 this->name().c_str(), static_cast<size_t>(off)); 435 return -1; 436 } 437 pname->assign(name, name_end - 1 - name); 438 if (nested_off != NULL) 439 *nested_off = y; 440 } 441 442 return member_size; 443 } 444 445 // An archive member iterator. 446 447 class Archive::const_iterator 448 { 449 public: 450 // The header of an archive member. This is what this iterator 451 // points to. 452 struct Header 453 { 454 // The name of the member. 455 std::string name; 456 // The file offset of the member. 457 off_t off; 458 // The file offset of a nested archive member. 459 off_t nested_off; 460 // The size of the member. 461 off_t size; 462 }; 463 464 const_iterator(Archive* archive, off_t off) 465 : archive_(archive), off_(off) 466 { this->read_next_header(); } 467 468 const Header& 469 operator*() const 470 { return this->header_; } 471 472 const Header* 473 operator->() const 474 { return &this->header_; } 475 476 const_iterator& 477 operator++() 478 { 479 if (this->off_ == this->archive_->file().filesize()) 480 return *this; 481 this->off_ += sizeof(Archive_header); 482 if (!this->archive_->is_thin_archive()) 483 this->off_ += this->header_.size; 484 if ((this->off_ & 1) != 0) 485 ++this->off_; 486 this->read_next_header(); 487 return *this; 488 } 489 490 const_iterator 491 operator++(int) 492 { 493 const_iterator ret = *this; 494 ++*this; 495 return ret; 496 } 497 498 bool 499 operator==(const const_iterator p) const 500 { return this->off_ == p->off; } 501 502 bool 503 operator!=(const const_iterator p) const 504 { return this->off_ != p->off; } 505 506 private: 507 void 508 read_next_header(); 509 510 // The underlying archive. 511 Archive* archive_; 512 // The current offset in the file. 513 off_t off_; 514 // The current archive header. 515 Header header_; 516 }; 517 518 // Read the next archive header. 519 520 void 521 Archive::const_iterator::read_next_header() 522 { 523 off_t filesize = this->archive_->file().filesize(); 524 while (true) 525 { 526 if (filesize - this->off_ < static_cast<off_t>(sizeof(Archive_header))) 527 { 528 if (filesize != this->off_) 529 { 530 gold_error(_("%s: short archive header at %zu"), 531 this->archive_->filename().c_str(), 532 static_cast<size_t>(this->off_)); 533 this->off_ = filesize; 534 } 535 this->header_.off = filesize; 536 return; 537 } 538 539 unsigned char buf[sizeof(Archive_header)]; 540 this->archive_->file().read(this->off_, sizeof(Archive_header), buf); 541 542 const Archive_header* hdr = reinterpret_cast<const Archive_header*>(buf); 543 off_t size = this->archive_->interpret_header(hdr, this->off_, 544 &this->header_.name, 545 &this->header_.nested_off); 546 if (size == -1) 547 { 548 this->header_.off = filesize; 549 return; 550 } 551 552 this->header_.size = size; 553 this->header_.off = this->off_; 554 555 // Skip special members. 556 if (!this->header_.name.empty() 557 && this->header_.name != "/" 558 && this->header_.name != "/SYM64/") 559 return; 560 561 this->off_ += sizeof(Archive_header) + this->header_.size; 562 if ((this->off_ & 1) != 0) 563 ++this->off_; 564 } 565 } 566 567 // Initial iterator. 568 569 Archive::const_iterator 570 Archive::begin() 571 { 572 return Archive::const_iterator(this, sarmag); 573 } 574 575 // Final iterator. 576 577 Archive::const_iterator 578 Archive::end() 579 { 580 return Archive::const_iterator(this, this->input_file_->file().filesize()); 581 } 582 583 // Get the file and offset for an archive member, which may be an 584 // external member of a thin archive. Set *INPUT_FILE to the 585 // file containing the actual member, *MEMOFF to the offset 586 // within that file (0 if not a nested archive), and *MEMBER_NAME 587 // to the name of the archive member. Return TRUE on success. 588 589 bool 590 Archive::get_file_and_offset(off_t off, Input_file** input_file, off_t* memoff, 591 off_t* memsize, std::string* member_name) 592 { 593 off_t nested_off; 594 595 *memsize = this->read_header(off, false, member_name, &nested_off); 596 if (*memsize == -1) 597 return false; 598 599 *input_file = this->input_file_; 600 *memoff = off + static_cast<off_t>(sizeof(Archive_header)); 601 602 if (!this->is_thin_archive_) 603 return true; 604 605 // Adjust a relative pathname so that it is relative 606 // to the directory containing the archive. 607 if (!IS_ABSOLUTE_PATH(member_name->c_str())) 608 { 609 const char* arch_path = this->filename().c_str(); 610 const char* basename = lbasename(arch_path); 611 if (basename > arch_path) 612 member_name->replace(0, 0, 613 this->filename().substr(0, basename - arch_path)); 614 } 615 616 if (nested_off > 0) 617 { 618 // This is a member of a nested archive. Open the containing 619 // archive if we don't already have it open, then do a recursive 620 // call to include the member from that archive. 621 Archive* arch; 622 Nested_archive_table::const_iterator p = 623 this->nested_archives_.find(*member_name); 624 if (p != this->nested_archives_.end()) 625 arch = p->second; 626 else 627 { 628 Input_file_argument* input_file_arg = 629 new Input_file_argument(member_name->c_str(), 630 Input_file_argument::INPUT_FILE_TYPE_FILE, 631 "", false, parameters->options()); 632 *input_file = new Input_file(input_file_arg); 633 int dummy = 0; 634 if (!(*input_file)->open(*this->dirpath_, this->task_, &dummy)) 635 return false; 636 arch = new Archive(*member_name, *input_file, false, this->dirpath_, 637 this->task_); 638 arch->setup(); 639 std::pair<Nested_archive_table::iterator, bool> ins = 640 this->nested_archives_.insert(std::make_pair(*member_name, arch)); 641 gold_assert(ins.second); 642 } 643 return arch->get_file_and_offset(nested_off, input_file, memoff, 644 memsize, member_name); 645 } 646 647 // This is an external member of a thin archive. Open the 648 // file as a regular relocatable object file. 649 Input_file_argument* input_file_arg = 650 new Input_file_argument(member_name->c_str(), 651 Input_file_argument::INPUT_FILE_TYPE_FILE, 652 "", false, this->input_file_->options()); 653 *input_file = new Input_file(input_file_arg); 654 int dummy = 0; 655 if (!(*input_file)->open(*this->dirpath_, this->task_, &dummy)) 656 return false; 657 658 *memoff = 0; 659 *memsize = (*input_file)->file().filesize(); 660 return true; 661 } 662 663 // Return an ELF object for the member at offset OFF. If 664 // PUNCONFIGURED is not NULL, then if the ELF object has an 665 // unsupported target type, set *PUNCONFIGURED to true and return 666 // NULL. 667 668 Object* 669 Archive::get_elf_object_for_member(off_t off, bool* punconfigured) 670 { 671 if (punconfigured != NULL) 672 *punconfigured = false; 673 674 Input_file* input_file; 675 off_t memoff; 676 off_t memsize; 677 std::string member_name; 678 if (!this->get_file_and_offset(off, &input_file, &memoff, &memsize, 679 &member_name)) 680 return NULL; 681 682 const unsigned char* ehdr; 683 int read_size; 684 Object *obj = NULL; 685 bool is_elf_obj = false; 686 bool unclaimed = false; 687 688 if (is_elf_object(input_file, memoff, &ehdr, &read_size)) 689 { 690 obj = make_elf_object((std::string(this->input_file_->filename()) 691 + "(" + member_name + ")"), 692 input_file, memoff, ehdr, read_size, 693 punconfigured); 694 is_elf_obj = true; 695 } 696 697 if (parameters->options().has_plugins()) 698 { 699 Object* plugin_obj 700 = parameters->options().plugins()->claim_file(input_file, 701 memoff, 702 memsize, 703 obj); 704 if (plugin_obj != NULL) 705 { 706 // The input file was claimed by a plugin, and its symbols 707 // have been provided by the plugin. 708 // Delete its elf object. 709 if (obj != NULL) 710 delete obj; 711 return plugin_obj; 712 } 713 714 unclaimed = true; 715 } 716 717 if (!is_elf_obj) 718 { 719 if (unclaimed) 720 gold_error(_("%s: plugin failed to claim member %s at %zu"), 721 this->name().c_str(), member_name.c_str(), 722 static_cast<size_t>(off)); 723 else 724 gold_error(_("%s: member %s at %zu is not an ELF object"), 725 this->name().c_str(), member_name.c_str(), 726 static_cast<size_t>(off)); 727 return NULL; 728 } 729 730 if (obj == NULL) 731 return NULL; 732 obj->set_no_export(this->no_export()); 733 return obj; 734 } 735 736 // Read the symbols from all the archive members in the link. 737 738 void 739 Archive::read_all_symbols() 740 { 741 for (Archive::const_iterator p = this->begin(); 742 p != this->end(); 743 ++p) 744 this->read_symbols(p->off); 745 } 746 747 // Read the symbols from an archive member in the link. OFF is the file 748 // offset of the member header. 749 750 void 751 Archive::read_symbols(off_t off) 752 { 753 Object* obj = this->get_elf_object_for_member(off, NULL); 754 if (obj == NULL) 755 return; 756 757 Read_symbols_data* sd = new Read_symbols_data; 758 obj->read_symbols(sd); 759 Archive_member member(obj, sd); 760 this->members_[off] = member; 761 } 762 763 // Select members from the archive and add them to the link. We walk 764 // through the elements in the archive map, and look each one up in 765 // the symbol table. If it exists as a strong undefined symbol, we 766 // pull in the corresponding element. We have to do this in a loop, 767 // since pulling in one element may create new undefined symbols which 768 // may be satisfied by other objects in the archive. Return true in 769 // the normal case, false if the first member we tried to add from 770 // this archive had an incompatible target. 771 772 bool 773 Archive::add_symbols(Symbol_table* symtab, Layout* layout, 774 Input_objects* input_objects, Mapfile* mapfile) 775 { 776 ++Archive::total_archives; 777 778 if (this->input_file_->options().whole_archive()) 779 return this->include_all_members(symtab, layout, input_objects, 780 mapfile); 781 782 Archive::total_members += this->num_members_; 783 784 input_objects->archive_start(this); 785 786 const size_t armap_size = this->armap_.size(); 787 788 // This is a quick optimization, since we usually see many symbols 789 // in a row with the same offset. last_seen_offset holds the last 790 // offset we saw that was present in the seen_offsets_ set. 791 off_t last_seen_offset = -1; 792 793 // Track which symbols in the symbol table we've already found to be 794 // defined. 795 796 char* tmpbuf = NULL; 797 size_t tmpbuflen = 0; 798 bool added_new_object; 799 do 800 { 801 added_new_object = false; 802 for (size_t i = 0; i < armap_size; ++i) 803 { 804 if (this->armap_checked_[i]) 805 continue; 806 if (this->armap_[i].file_offset == last_seen_offset) 807 { 808 this->armap_checked_[i] = true; 809 continue; 810 } 811 if (this->seen_offsets_.find(this->armap_[i].file_offset) 812 != this->seen_offsets_.end()) 813 { 814 this->armap_checked_[i] = true; 815 last_seen_offset = this->armap_[i].file_offset; 816 continue; 817 } 818 819 const char* sym_name = (this->armap_names_.data() 820 + this->armap_[i].name_offset); 821 822 Symbol* sym; 823 std::string why; 824 Archive::Should_include t = 825 Archive::should_include_member(symtab, layout, sym_name, &sym, 826 &why, &tmpbuf, &tmpbuflen); 827 828 if (t == Archive::SHOULD_INCLUDE_NO 829 || t == Archive::SHOULD_INCLUDE_YES) 830 this->armap_checked_[i] = true; 831 832 if (t != Archive::SHOULD_INCLUDE_YES) 833 continue; 834 835 // We want to include this object in the link. 836 last_seen_offset = this->armap_[i].file_offset; 837 this->seen_offsets_.insert(last_seen_offset); 838 839 if (!this->include_member(symtab, layout, input_objects, 840 last_seen_offset, mapfile, sym, 841 why.c_str())) 842 { 843 if (tmpbuf != NULL) 844 free(tmpbuf); 845 return false; 846 } 847 848 added_new_object = true; 849 } 850 } 851 while (added_new_object); 852 853 if (tmpbuf != NULL) 854 free(tmpbuf); 855 856 input_objects->archive_stop(this); 857 858 return true; 859 } 860 861 // Return whether the archive includes a member which defines the 862 // symbol SYM. 863 864 bool 865 Archive::defines_symbol(Symbol* sym) const 866 { 867 const char* symname = sym->name(); 868 size_t symname_len = strlen(symname); 869 size_t armap_size = this->armap_.size(); 870 for (size_t i = 0; i < armap_size; ++i) 871 { 872 if (this->armap_checked_[i]) 873 continue; 874 const char* archive_symname = (this->armap_names_.data() 875 + this->armap_[i].name_offset); 876 if (strncmp(archive_symname, symname, symname_len) != 0) 877 continue; 878 char c = archive_symname[symname_len]; 879 if (c == '\0' && sym->version() == NULL) 880 return true; 881 if (c == '@') 882 { 883 const char* ver = archive_symname + symname_len + 1; 884 if (*ver == '@') 885 { 886 if (sym->version() == NULL) 887 return true; 888 ++ver; 889 } 890 if (sym->version() != NULL && strcmp(sym->version(), ver) == 0) 891 return true; 892 } 893 } 894 return false; 895 } 896 897 // Include all the archive members in the link. This is for --whole-archive. 898 899 bool 900 Archive::include_all_members(Symbol_table* symtab, Layout* layout, 901 Input_objects* input_objects, Mapfile* mapfile) 902 { 903 // Don't include the same archive twice. This can happen if 904 // --whole-archive is nested inside --start-group (PR gold/12163). 905 if (this->included_all_members_) 906 return true; 907 908 this->included_all_members_ = true; 909 910 input_objects->archive_start(this); 911 912 if (this->members_.size() > 0) 913 { 914 std::map<off_t, Archive_member>::const_iterator p; 915 for (p = this->members_.begin(); 916 p != this->members_.end(); 917 ++p) 918 { 919 if (!this->include_member(symtab, layout, input_objects, p->first, 920 mapfile, NULL, "--whole-archive")) 921 return false; 922 ++Archive::total_members; 923 } 924 } 925 else 926 { 927 for (Archive::const_iterator p = this->begin(); 928 p != this->end(); 929 ++p) 930 { 931 if (!this->include_member(symtab, layout, input_objects, p->off, 932 mapfile, NULL, "--whole-archive")) 933 return false; 934 ++Archive::total_members; 935 } 936 } 937 938 input_objects->archive_stop(this); 939 940 return true; 941 } 942 943 // Return the number of members in the archive. This is only used for 944 // reports. 945 946 size_t 947 Archive::count_members() 948 { 949 size_t ret = 0; 950 for (Archive::const_iterator p = this->begin(); 951 p != this->end(); 952 ++p) 953 ++ret; 954 return ret; 955 } 956 957 // RAII class to ensure we unlock the object if it's a member of a 958 // thin archive. We can't use Task_lock_obj in Archive::include_member 959 // because the object file is already locked when it's opened by 960 // get_elf_object_for_member. 961 962 class Thin_archive_object_unlocker 963 { 964 public: 965 Thin_archive_object_unlocker(const Task *task, Object* obj) 966 : task_(task), obj_(obj) 967 { } 968 969 ~Thin_archive_object_unlocker() 970 { 971 if (this->obj_->offset() == 0) 972 this->obj_->unlock(this->task_); 973 } 974 975 private: 976 Thin_archive_object_unlocker(const Thin_archive_object_unlocker&); 977 Thin_archive_object_unlocker& operator=(const Thin_archive_object_unlocker&); 978 979 const Task* task_; 980 Object* obj_; 981 }; 982 983 // Include an archive member in the link. OFF is the file offset of 984 // the member header. WHY is the reason we are including this member. 985 // Return true if we added the member or if we had an error, return 986 // false if this was the first member we tried to add from this 987 // archive and it had an incompatible format. 988 989 bool 990 Archive::include_member(Symbol_table* symtab, Layout* layout, 991 Input_objects* input_objects, off_t off, 992 Mapfile* mapfile, Symbol* sym, const char* why) 993 { 994 ++Archive::total_members_loaded; 995 996 std::map<off_t, Archive_member>::const_iterator p = this->members_.find(off); 997 if (p != this->members_.end()) 998 { 999 Object* obj = p->second.obj_; 1000 1001 Read_symbols_data* sd = p->second.sd_; 1002 if (mapfile != NULL) 1003 mapfile->report_include_archive_member(obj->name(), sym, why); 1004 if (input_objects->add_object(obj)) 1005 { 1006 obj->layout(symtab, layout, sd); 1007 obj->add_symbols(symtab, sd, layout); 1008 this->included_member_ = true; 1009 } 1010 delete sd; 1011 return true; 1012 } 1013 1014 // If this is the first object we are including from this archive, 1015 // and we searched for this archive, most likely because it was 1016 // found via a -l option, then if the target is incompatible we want 1017 // to move on to the next archive found in the search path. 1018 bool unconfigured = false; 1019 bool* punconfigured = NULL; 1020 if (!this->included_member_ && this->searched_for()) 1021 punconfigured = &unconfigured; 1022 1023 Object* obj = this->get_elf_object_for_member(off, punconfigured); 1024 if (obj == NULL) 1025 { 1026 // Return false to search for another archive, true if we found 1027 // an error. 1028 return unconfigured ? false : true; 1029 } 1030 1031 // If the object is an external member of a thin archive, 1032 // unlock it when we're done here. 1033 Thin_archive_object_unlocker unlocker(this->task_, obj); 1034 1035 if (mapfile != NULL) 1036 mapfile->report_include_archive_member(obj->name(), sym, why); 1037 1038 Pluginobj* pluginobj = obj->pluginobj(); 1039 if (pluginobj != NULL) 1040 { 1041 pluginobj->add_symbols(symtab, NULL, layout); 1042 this->included_member_ = true; 1043 return true; 1044 } 1045 1046 if (!input_objects->add_object(obj)) 1047 { 1048 delete obj; 1049 return true; 1050 } 1051 1052 if (layout->incremental_inputs() != NULL) 1053 layout->incremental_inputs()->report_object(obj, 0, this, NULL); 1054 1055 { 1056 Read_symbols_data sd; 1057 obj->read_symbols(&sd); 1058 obj->layout(symtab, layout, &sd); 1059 obj->add_symbols(symtab, &sd, layout); 1060 } 1061 1062 this->included_member_ = true; 1063 return true; 1064 } 1065 1066 // Iterate over all unused symbols, and call the visitor class V for each. 1067 1068 void 1069 Archive::do_for_all_unused_symbols(Symbol_visitor_base* v) const 1070 { 1071 for (std::vector<Armap_entry>::const_iterator p = this->armap_.begin(); 1072 p != this->armap_.end(); 1073 ++p) 1074 { 1075 if (this->seen_offsets_.find(p->file_offset) 1076 == this->seen_offsets_.end()) 1077 v->visit(this->armap_names_.data() + p->name_offset); 1078 } 1079 } 1080 1081 // Print statistical information to stderr. This is used for --stats. 1082 1083 void 1084 Archive::print_stats() 1085 { 1086 fprintf(stderr, _("%s: archive libraries: %u\n"), 1087 program_name, Archive::total_archives); 1088 fprintf(stderr, _("%s: total archive members: %u\n"), 1089 program_name, Archive::total_members); 1090 fprintf(stderr, _("%s: loaded archive members: %u\n"), 1091 program_name, Archive::total_members_loaded); 1092 } 1093 1094 // Add_archive_symbols methods. 1095 1096 Add_archive_symbols::~Add_archive_symbols() 1097 { 1098 if (this->this_blocker_ != NULL) 1099 delete this->this_blocker_; 1100 // next_blocker_ is deleted by the task associated with the next 1101 // input file. 1102 } 1103 1104 // Return whether we can add the archive symbols. We are blocked by 1105 // this_blocker_. We block next_blocker_. We also lock the file. 1106 1107 Task_token* 1108 Add_archive_symbols::is_runnable() 1109 { 1110 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked()) 1111 return this->this_blocker_; 1112 return NULL; 1113 } 1114 1115 void 1116 Add_archive_symbols::locks(Task_locker* tl) 1117 { 1118 tl->add(this, this->next_blocker_); 1119 tl->add(this, this->archive_->token()); 1120 } 1121 1122 void 1123 Add_archive_symbols::run(Workqueue* workqueue) 1124 { 1125 // For an incremental link, begin recording layout information. 1126 Incremental_inputs* incremental_inputs = this->layout_->incremental_inputs(); 1127 if (incremental_inputs != NULL) 1128 { 1129 unsigned int arg_serial = this->input_argument_->file().arg_serial(); 1130 Script_info* script_info = this->input_argument_->script_info(); 1131 incremental_inputs->report_archive_begin(this->archive_, arg_serial, 1132 script_info); 1133 } 1134 1135 bool added = this->archive_->add_symbols(this->symtab_, this->layout_, 1136 this->input_objects_, 1137 this->mapfile_); 1138 this->archive_->unlock_nested_archives(); 1139 1140 this->archive_->release(); 1141 this->archive_->clear_uncached_views(); 1142 1143 if (!added) 1144 { 1145 // This archive holds object files which are incompatible with 1146 // our output file. 1147 Read_symbols::incompatible_warning(this->input_argument_, 1148 this->archive_->input_file()); 1149 Read_symbols::requeue(workqueue, this->input_objects_, this->symtab_, 1150 this->layout_, this->dirpath_, this->dirindex_, 1151 this->mapfile_, this->input_argument_, 1152 this->input_group_, this->next_blocker_); 1153 delete this->archive_; 1154 return; 1155 } 1156 1157 if (this->input_group_ != NULL) 1158 this->input_group_->add_archive(this->archive_); 1159 else 1160 { 1161 // For an incremental link, finish recording the layout information. 1162 if (incremental_inputs != NULL) 1163 incremental_inputs->report_archive_end(this->archive_); 1164 1165 if (!parameters->options().has_plugins() 1166 || this->archive_->input_file()->options().whole_archive()) 1167 { 1168 // We no longer need to know about this archive. 1169 delete this->archive_; 1170 } 1171 else 1172 { 1173 // The plugin interface may want to rescan this archive. 1174 parameters->options().plugins()->save_archive(this->archive_); 1175 } 1176 1177 this->archive_ = NULL; 1178 } 1179 } 1180 1181 // Class Lib_group static variables. 1182 unsigned int Lib_group::total_lib_groups; 1183 unsigned int Lib_group::total_members; 1184 unsigned int Lib_group::total_members_loaded; 1185 1186 Lib_group::Lib_group(const Input_file_lib* lib, Task* task) 1187 : Library_base(task), members_() 1188 { 1189 this->members_.resize(lib->size()); 1190 } 1191 1192 const std::string& 1193 Lib_group::do_filename() const 1194 { 1195 std::string *filename = new std::string("/group/"); 1196 return *filename; 1197 } 1198 1199 // Select members from the lib group and add them to the link. We walk 1200 // through the members, and check if each one up should be included. 1201 // If the object says it should be included, we do so. We have to do 1202 // this in a loop, since including one member may create new undefined 1203 // symbols which may be satisfied by other members. 1204 1205 void 1206 Lib_group::add_symbols(Symbol_table* symtab, Layout* layout, 1207 Input_objects* input_objects) 1208 { 1209 ++Lib_group::total_lib_groups; 1210 1211 Lib_group::total_members += this->members_.size(); 1212 1213 bool added_new_object; 1214 do 1215 { 1216 added_new_object = false; 1217 unsigned int i = 0; 1218 while (i < this->members_.size()) 1219 { 1220 const Archive_member& member = this->members_[i]; 1221 Object* obj = member.obj_; 1222 std::string why; 1223 1224 // Skip files with no symbols. Plugin objects have 1225 // member.sd_ == NULL. 1226 if (obj != NULL 1227 && (member.sd_ == NULL || member.sd_->symbol_names != NULL)) 1228 { 1229 Archive::Should_include t = obj->should_include_member(symtab, 1230 layout, 1231 member.sd_, 1232 &why); 1233 1234 if (t != Archive::SHOULD_INCLUDE_YES) 1235 { 1236 ++i; 1237 continue; 1238 } 1239 1240 this->include_member(symtab, layout, input_objects, member); 1241 1242 added_new_object = true; 1243 } 1244 else 1245 { 1246 if (member.sd_ != NULL) 1247 { 1248 // The file must be locked in order to destroy the views 1249 // associated with it. 1250 gold_assert(obj != NULL); 1251 obj->lock(this->task_); 1252 delete member.sd_; 1253 obj->unlock(this->task_); 1254 } 1255 } 1256 1257 this->members_[i] = this->members_.back(); 1258 this->members_.pop_back(); 1259 } 1260 } 1261 while (added_new_object); 1262 } 1263 1264 // Include a lib group member in the link. 1265 1266 void 1267 Lib_group::include_member(Symbol_table* symtab, Layout* layout, 1268 Input_objects* input_objects, 1269 const Archive_member& member) 1270 { 1271 ++Lib_group::total_members_loaded; 1272 1273 Object* obj = member.obj_; 1274 gold_assert(obj != NULL); 1275 1276 Pluginobj* pluginobj = obj->pluginobj(); 1277 if (pluginobj != NULL) 1278 { 1279 pluginobj->add_symbols(symtab, NULL, layout); 1280 return; 1281 } 1282 1283 Read_symbols_data* sd = member.sd_; 1284 gold_assert(sd != NULL); 1285 obj->lock(this->task_); 1286 if (input_objects->add_object(obj)) 1287 { 1288 if (layout->incremental_inputs() != NULL) 1289 layout->incremental_inputs()->report_object(obj, member.arg_serial_, 1290 this, NULL); 1291 obj->layout(symtab, layout, sd); 1292 obj->add_symbols(symtab, sd, layout); 1293 } 1294 delete sd; 1295 // Unlock the file for the next task. 1296 obj->unlock(this->task_); 1297 } 1298 1299 // Iterate over all unused symbols, and call the visitor class V for each. 1300 1301 void 1302 Lib_group::do_for_all_unused_symbols(Symbol_visitor_base* v) const 1303 { 1304 // Files are removed from the members list when used, so all the 1305 // files remaining on the list are unused. 1306 for (std::vector<Archive_member>::const_iterator p = this->members_.begin(); 1307 p != this->members_.end(); 1308 ++p) 1309 { 1310 Object* obj = p->obj_; 1311 obj->for_all_global_symbols(p->sd_, v); 1312 } 1313 } 1314 1315 // Print statistical information to stderr. This is used for --stats. 1316 1317 void 1318 Lib_group::print_stats() 1319 { 1320 fprintf(stderr, _("%s: lib groups: %u\n"), 1321 program_name, Lib_group::total_lib_groups); 1322 fprintf(stderr, _("%s: total lib groups members: %u\n"), 1323 program_name, Lib_group::total_members); 1324 fprintf(stderr, _("%s: loaded lib groups members: %u\n"), 1325 program_name, Lib_group::total_members_loaded); 1326 } 1327 1328 Task_token* 1329 Add_lib_group_symbols::is_runnable() 1330 { 1331 if (this->readsyms_blocker_ != NULL && this->readsyms_blocker_->is_blocked()) 1332 return this->readsyms_blocker_; 1333 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked()) 1334 return this->this_blocker_; 1335 return NULL; 1336 } 1337 1338 void 1339 Add_lib_group_symbols::locks(Task_locker* tl) 1340 { 1341 tl->add(this, this->next_blocker_); 1342 } 1343 1344 void 1345 Add_lib_group_symbols::run(Workqueue*) 1346 { 1347 // For an incremental link, begin recording layout information. 1348 Incremental_inputs* incremental_inputs = this->layout_->incremental_inputs(); 1349 if (incremental_inputs != NULL) 1350 incremental_inputs->report_archive_begin(this->lib_, 0, NULL); 1351 1352 this->lib_->add_symbols(this->symtab_, this->layout_, this->input_objects_); 1353 1354 if (incremental_inputs != NULL) 1355 incremental_inputs->report_archive_end(this->lib_); 1356 } 1357 1358 Add_lib_group_symbols::~Add_lib_group_symbols() 1359 { 1360 if (this->this_blocker_ != NULL) 1361 delete this->this_blocker_; 1362 // next_blocker_ is deleted by the task associated with the next 1363 // input file. 1364 } 1365 1366 } // End namespace gold. 1367