1 //===- Archive.cpp - ar File Format implementation --------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file defines the ArchiveObjectFile class. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/Object/Archive.h" 15 #include "llvm/ADT/SmallString.h" 16 #include "llvm/ADT/Twine.h" 17 #include "llvm/Support/Endian.h" 18 #include "llvm/Support/MemoryBuffer.h" 19 #include "llvm/Support/Path.h" 20 21 using namespace llvm; 22 using namespace object; 23 using namespace llvm::support::endian; 24 25 static const char *const Magic = "!<arch>\n"; 26 static const char *const ThinMagic = "!<thin>\n"; 27 28 void Archive::anchor() { } 29 30 StringRef ArchiveMemberHeader::getName() const { 31 char EndCond; 32 if (Name[0] == '/' || Name[0] == '#') 33 EndCond = ' '; 34 else 35 EndCond = '/'; 36 llvm::StringRef::size_type end = 37 llvm::StringRef(Name, sizeof(Name)).find(EndCond); 38 if (end == llvm::StringRef::npos) 39 end = sizeof(Name); 40 assert(end <= sizeof(Name) && end > 0); 41 // Don't include the EndCond if there is one. 42 return llvm::StringRef(Name, end); 43 } 44 45 ErrorOr<uint32_t> ArchiveMemberHeader::getSize() const { 46 uint32_t Ret; 47 if (llvm::StringRef(Size, sizeof(Size)).rtrim(" ").getAsInteger(10, Ret)) 48 return object_error::parse_failed; // Size is not a decimal number. 49 return Ret; 50 } 51 52 sys::fs::perms ArchiveMemberHeader::getAccessMode() const { 53 unsigned Ret; 54 if (StringRef(AccessMode, sizeof(AccessMode)).rtrim(' ').getAsInteger(8, Ret)) 55 llvm_unreachable("Access mode is not an octal number."); 56 return static_cast<sys::fs::perms>(Ret); 57 } 58 59 sys::TimeValue ArchiveMemberHeader::getLastModified() const { 60 unsigned Seconds; 61 if (StringRef(LastModified, sizeof(LastModified)).rtrim(' ') 62 .getAsInteger(10, Seconds)) 63 llvm_unreachable("Last modified time not a decimal number."); 64 65 sys::TimeValue Ret; 66 Ret.fromEpochTime(Seconds); 67 return Ret; 68 } 69 70 unsigned ArchiveMemberHeader::getUID() const { 71 unsigned Ret; 72 if (StringRef(UID, sizeof(UID)).rtrim(' ').getAsInteger(10, Ret)) 73 llvm_unreachable("UID time not a decimal number."); 74 return Ret; 75 } 76 77 unsigned ArchiveMemberHeader::getGID() const { 78 unsigned Ret; 79 if (StringRef(GID, sizeof(GID)).rtrim(' ').getAsInteger(10, Ret)) 80 llvm_unreachable("GID time not a decimal number."); 81 return Ret; 82 } 83 84 Archive::Child::Child(const Archive *Parent, StringRef Data, 85 uint16_t StartOfFile) 86 : Parent(Parent), Data(Data), StartOfFile(StartOfFile) {} 87 88 Archive::Child::Child(const Archive *Parent, const char *Start, 89 std::error_code *EC) 90 : Parent(Parent) { 91 if (!Start) 92 return; 93 94 uint64_t Size = sizeof(ArchiveMemberHeader); 95 Data = StringRef(Start, Size); 96 if (!isThinMember()) { 97 ErrorOr<uint64_t> MemberSize = getRawSize(); 98 if ((*EC = MemberSize.getError())) 99 return; 100 Size += MemberSize.get(); 101 Data = StringRef(Start, Size); 102 } 103 104 // Setup StartOfFile and PaddingBytes. 105 StartOfFile = sizeof(ArchiveMemberHeader); 106 // Don't include attached name. 107 StringRef Name = getRawName(); 108 if (Name.startswith("#1/")) { 109 uint64_t NameSize; 110 if (Name.substr(3).rtrim(' ').getAsInteger(10, NameSize)) 111 llvm_unreachable("Long name length is not an integer"); 112 StartOfFile += NameSize; 113 } 114 } 115 116 ErrorOr<uint64_t> Archive::Child::getSize() const { 117 if (Parent->IsThin) { 118 ErrorOr<uint32_t> Size = getHeader()->getSize(); 119 if (std::error_code EC = Size.getError()) 120 return EC; 121 return Size.get(); 122 } 123 return Data.size() - StartOfFile; 124 } 125 126 ErrorOr<uint64_t> Archive::Child::getRawSize() const { 127 ErrorOr<uint32_t> Size = getHeader()->getSize(); 128 if (std::error_code EC = Size.getError()) 129 return EC; 130 return Size.get(); 131 } 132 133 bool Archive::Child::isThinMember() const { 134 StringRef Name = getHeader()->getName(); 135 return Parent->IsThin && Name != "/" && Name != "//"; 136 } 137 138 ErrorOr<std::string> Archive::Child::getFullName() const { 139 assert(isThinMember()); 140 ErrorOr<StringRef> NameOrErr = getName(); 141 if (std::error_code EC = NameOrErr.getError()) 142 return EC; 143 StringRef Name = *NameOrErr; 144 if (sys::path::is_absolute(Name)) 145 return Name; 146 147 SmallString<128> FullName = sys::path::parent_path( 148 Parent->getMemoryBufferRef().getBufferIdentifier()); 149 sys::path::append(FullName, Name); 150 return StringRef(FullName); 151 } 152 153 ErrorOr<StringRef> Archive::Child::getBuffer() const { 154 if (!isThinMember()) { 155 ErrorOr<uint32_t> Size = getSize(); 156 if (std::error_code EC = Size.getError()) 157 return EC; 158 return StringRef(Data.data() + StartOfFile, Size.get()); 159 } 160 ErrorOr<std::string> FullNameOrEr = getFullName(); 161 if (std::error_code EC = FullNameOrEr.getError()) 162 return EC; 163 const std::string &FullName = *FullNameOrEr; 164 ErrorOr<std::unique_ptr<MemoryBuffer>> Buf = MemoryBuffer::getFile(FullName); 165 if (std::error_code EC = Buf.getError()) 166 return EC; 167 Parent->ThinBuffers.push_back(std::move(*Buf)); 168 return Parent->ThinBuffers.back()->getBuffer(); 169 } 170 171 ErrorOr<Archive::Child> Archive::Child::getNext() const { 172 size_t SpaceToSkip = Data.size(); 173 // If it's odd, add 1 to make it even. 174 if (SpaceToSkip & 1) 175 ++SpaceToSkip; 176 177 const char *NextLoc = Data.data() + SpaceToSkip; 178 179 // Check to see if this is at the end of the archive. 180 if (NextLoc == Parent->Data.getBufferEnd()) 181 return Child(Parent, nullptr, nullptr); 182 183 // Check to see if this is past the end of the archive. 184 if (NextLoc > Parent->Data.getBufferEnd()) 185 return object_error::parse_failed; 186 187 std::error_code EC; 188 Child Ret(Parent, NextLoc, &EC); 189 if (EC) 190 return EC; 191 return Ret; 192 } 193 194 uint64_t Archive::Child::getChildOffset() const { 195 const char *a = Parent->Data.getBuffer().data(); 196 const char *c = Data.data(); 197 uint64_t offset = c - a; 198 return offset; 199 } 200 201 ErrorOr<StringRef> Archive::Child::getName() const { 202 StringRef name = getRawName(); 203 // Check if it's a special name. 204 if (name[0] == '/') { 205 if (name.size() == 1) // Linker member. 206 return name; 207 if (name.size() == 2 && name[1] == '/') // String table. 208 return name; 209 // It's a long name. 210 // Get the offset. 211 std::size_t offset; 212 if (name.substr(1).rtrim(' ').getAsInteger(10, offset)) 213 llvm_unreachable("Long name offset is not an integer"); 214 215 // Verify it. 216 if (offset >= Parent->StringTable.size()) 217 return object_error::parse_failed; 218 const char *addr = Parent->StringTable.begin() + offset; 219 220 // GNU long file names end with a "/\n". 221 if (Parent->kind() == K_GNU || Parent->kind() == K_MIPS64) { 222 StringRef::size_type End = StringRef(addr).find('\n'); 223 return StringRef(addr, End - 1); 224 } 225 return StringRef(addr); 226 } else if (name.startswith("#1/")) { 227 uint64_t name_size; 228 if (name.substr(3).rtrim(' ').getAsInteger(10, name_size)) 229 llvm_unreachable("Long name length is not an ingeter"); 230 return Data.substr(sizeof(ArchiveMemberHeader), name_size).rtrim('\0'); 231 } else { 232 // It is not a long name so trim the blanks at the end of the name. 233 if (name[name.size() - 1] != '/') { 234 return name.rtrim(' '); 235 } 236 } 237 // It's a simple name. 238 if (name[name.size() - 1] == '/') 239 return name.substr(0, name.size() - 1); 240 return name; 241 } 242 243 ErrorOr<MemoryBufferRef> Archive::Child::getMemoryBufferRef() const { 244 ErrorOr<StringRef> NameOrErr = getName(); 245 if (std::error_code EC = NameOrErr.getError()) 246 return EC; 247 StringRef Name = NameOrErr.get(); 248 ErrorOr<StringRef> Buf = getBuffer(); 249 if (std::error_code EC = Buf.getError()) 250 return EC; 251 return MemoryBufferRef(*Buf, Name); 252 } 253 254 Expected<std::unique_ptr<Binary>> 255 Archive::Child::getAsBinary(LLVMContext *Context) const { 256 ErrorOr<MemoryBufferRef> BuffOrErr = getMemoryBufferRef(); 257 if (std::error_code EC = BuffOrErr.getError()) 258 return errorCodeToError(EC); 259 260 auto BinaryOrErr = createBinary(BuffOrErr.get(), Context); 261 if (BinaryOrErr) 262 return std::move(*BinaryOrErr); 263 return BinaryOrErr.takeError(); 264 } 265 266 Expected<std::unique_ptr<Archive>> Archive::create(MemoryBufferRef Source) { 267 Error Err; 268 std::unique_ptr<Archive> Ret(new Archive(Source, Err)); 269 if (Err) 270 return std::move(Err); 271 return std::move(Ret); 272 } 273 274 void Archive::setFirstRegular(const Child &C) { 275 FirstRegularData = C.Data; 276 FirstRegularStartOfFile = C.StartOfFile; 277 } 278 279 Archive::Archive(MemoryBufferRef Source, Error &Err) 280 : Binary(Binary::ID_Archive, Source) { 281 ErrorAsOutParameter ErrAsOutParam(Err); 282 StringRef Buffer = Data.getBuffer(); 283 // Check for sufficient magic. 284 if (Buffer.startswith(ThinMagic)) { 285 IsThin = true; 286 } else if (Buffer.startswith(Magic)) { 287 IsThin = false; 288 } else { 289 Err = make_error<GenericBinaryError>("File too small to be an archive", 290 object_error::invalid_file_type); 291 return; 292 } 293 294 // Get the special members. 295 child_iterator I = child_begin(false); 296 std::error_code ec; 297 if ((ec = I->getError())) { 298 Err = errorCodeToError(ec); 299 return; 300 } 301 child_iterator E = child_end(); 302 303 // This is at least a valid empty archive. Since an empty archive is the 304 // same in all formats, just claim it to be gnu to make sure Format is 305 // initialized. 306 Format = K_GNU; 307 308 if (I == E) { 309 Err = Error::success(); 310 return; 311 } 312 const Child *C = &**I; 313 314 auto Increment = [&]() { 315 ++I; 316 if ((Err = errorCodeToError(I->getError()))) 317 return true; 318 C = &**I; 319 return false; 320 }; 321 322 StringRef Name = C->getRawName(); 323 324 // Below is the pattern that is used to figure out the archive format 325 // GNU archive format 326 // First member : / (may exist, if it exists, points to the symbol table ) 327 // Second member : // (may exist, if it exists, points to the string table) 328 // Note : The string table is used if the filename exceeds 15 characters 329 // BSD archive format 330 // First member : __.SYMDEF or "__.SYMDEF SORTED" (the symbol table) 331 // There is no string table, if the filename exceeds 15 characters or has a 332 // embedded space, the filename has #1/<size>, The size represents the size 333 // of the filename that needs to be read after the archive header 334 // COFF archive format 335 // First member : / 336 // Second member : / (provides a directory of symbols) 337 // Third member : // (may exist, if it exists, contains the string table) 338 // Note: Microsoft PE/COFF Spec 8.3 says that the third member is present 339 // even if the string table is empty. However, lib.exe does not in fact 340 // seem to create the third member if there's no member whose filename 341 // exceeds 15 characters. So the third member is optional. 342 343 if (Name == "__.SYMDEF" || Name == "__.SYMDEF_64") { 344 if (Name == "__.SYMDEF") 345 Format = K_BSD; 346 else // Name == "__.SYMDEF_64" 347 Format = K_DARWIN64; 348 // We know that the symbol table is not an external file, so we just assert 349 // there is no error. 350 SymbolTable = *C->getBuffer(); 351 if (Increment()) 352 return; 353 setFirstRegular(*C); 354 355 Err = Error::success(); 356 return; 357 } 358 359 if (Name.startswith("#1/")) { 360 Format = K_BSD; 361 // We know this is BSD, so getName will work since there is no string table. 362 ErrorOr<StringRef> NameOrErr = C->getName(); 363 ec = NameOrErr.getError(); 364 if (ec) { 365 Err = errorCodeToError(ec); 366 return; 367 } 368 Name = NameOrErr.get(); 369 if (Name == "__.SYMDEF SORTED" || Name == "__.SYMDEF") { 370 // We know that the symbol table is not an external file, so we just 371 // assert there is no error. 372 SymbolTable = *C->getBuffer(); 373 if (Increment()) 374 return; 375 } 376 else if (Name == "__.SYMDEF_64 SORTED" || Name == "__.SYMDEF_64") { 377 Format = K_DARWIN64; 378 // We know that the symbol table is not an external file, so we just 379 // assert there is no error. 380 SymbolTable = *C->getBuffer(); 381 if (Increment()) 382 return; 383 } 384 setFirstRegular(*C); 385 return; 386 } 387 388 // MIPS 64-bit ELF archives use a special format of a symbol table. 389 // This format is marked by `ar_name` field equals to "/SYM64/". 390 // For detailed description see page 96 in the following document: 391 // http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf 392 393 bool has64SymTable = false; 394 if (Name == "/" || Name == "/SYM64/") { 395 // We know that the symbol table is not an external file, so we just assert 396 // there is no error. 397 SymbolTable = *C->getBuffer(); 398 if (Name == "/SYM64/") 399 has64SymTable = true; 400 401 if (Increment()) 402 return; 403 if (I == E) { 404 Err = Error::success(); 405 return; 406 } 407 Name = C->getRawName(); 408 } 409 410 if (Name == "//") { 411 Format = has64SymTable ? K_MIPS64 : K_GNU; 412 // The string table is never an external member, so we just assert on the 413 // ErrorOr. 414 StringTable = *C->getBuffer(); 415 if (Increment()) 416 return; 417 setFirstRegular(*C); 418 Err = Error::success(); 419 return; 420 } 421 422 if (Name[0] != '/') { 423 Format = has64SymTable ? K_MIPS64 : K_GNU; 424 setFirstRegular(*C); 425 Err = Error::success(); 426 return; 427 } 428 429 if (Name != "/") { 430 Err = errorCodeToError(object_error::parse_failed); 431 return; 432 } 433 434 Format = K_COFF; 435 // We know that the symbol table is not an external file, so we just assert 436 // there is no error. 437 SymbolTable = *C->getBuffer(); 438 439 if (Increment()) 440 return; 441 442 if (I == E) { 443 setFirstRegular(*C); 444 Err = Error::success(); 445 return; 446 } 447 448 Name = C->getRawName(); 449 450 if (Name == "//") { 451 // The string table is never an external member, so we just assert on the 452 // ErrorOr. 453 StringTable = *C->getBuffer(); 454 if (Increment()) 455 return; 456 } 457 458 setFirstRegular(*C); 459 Err = Error::success(); 460 } 461 462 Archive::child_iterator Archive::child_begin(bool SkipInternal) const { 463 if (Data.getBufferSize() == 8) // empty archive. 464 return child_end(); 465 466 if (SkipInternal) 467 return Child(this, FirstRegularData, FirstRegularStartOfFile); 468 469 const char *Loc = Data.getBufferStart() + strlen(Magic); 470 std::error_code EC; 471 Child c(this, Loc, &EC); 472 if (EC) 473 return child_iterator(EC); 474 return child_iterator(c); 475 } 476 477 Archive::child_iterator Archive::child_end() const { 478 return Child(this, nullptr, nullptr); 479 } 480 481 StringRef Archive::Symbol::getName() const { 482 return Parent->getSymbolTable().begin() + StringIndex; 483 } 484 485 ErrorOr<Archive::Child> Archive::Symbol::getMember() const { 486 const char *Buf = Parent->getSymbolTable().begin(); 487 const char *Offsets = Buf; 488 if (Parent->kind() == K_MIPS64 || Parent->kind() == K_DARWIN64) 489 Offsets += sizeof(uint64_t); 490 else 491 Offsets += sizeof(uint32_t); 492 uint32_t Offset = 0; 493 if (Parent->kind() == K_GNU) { 494 Offset = read32be(Offsets + SymbolIndex * 4); 495 } else if (Parent->kind() == K_MIPS64) { 496 Offset = read64be(Offsets + SymbolIndex * 8); 497 } else if (Parent->kind() == K_BSD) { 498 // The SymbolIndex is an index into the ranlib structs that start at 499 // Offsets (the first uint32_t is the number of bytes of the ranlib 500 // structs). The ranlib structs are a pair of uint32_t's the first 501 // being a string table offset and the second being the offset into 502 // the archive of the member that defines the symbol. Which is what 503 // is needed here. 504 Offset = read32le(Offsets + SymbolIndex * 8 + 4); 505 } else if (Parent->kind() == K_DARWIN64) { 506 // The SymbolIndex is an index into the ranlib_64 structs that start at 507 // Offsets (the first uint64_t is the number of bytes of the ranlib_64 508 // structs). The ranlib_64 structs are a pair of uint64_t's the first 509 // being a string table offset and the second being the offset into 510 // the archive of the member that defines the symbol. Which is what 511 // is needed here. 512 Offset = read64le(Offsets + SymbolIndex * 16 + 8); 513 } else { 514 // Skip offsets. 515 uint32_t MemberCount = read32le(Buf); 516 Buf += MemberCount * 4 + 4; 517 518 uint32_t SymbolCount = read32le(Buf); 519 if (SymbolIndex >= SymbolCount) 520 return object_error::parse_failed; 521 522 // Skip SymbolCount to get to the indices table. 523 const char *Indices = Buf + 4; 524 525 // Get the index of the offset in the file member offset table for this 526 // symbol. 527 uint16_t OffsetIndex = read16le(Indices + SymbolIndex * 2); 528 // Subtract 1 since OffsetIndex is 1 based. 529 --OffsetIndex; 530 531 if (OffsetIndex >= MemberCount) 532 return object_error::parse_failed; 533 534 Offset = read32le(Offsets + OffsetIndex * 4); 535 } 536 537 const char *Loc = Parent->getData().begin() + Offset; 538 std::error_code EC; 539 Child C(Parent, Loc, &EC); 540 if (EC) 541 return EC; 542 return C; 543 } 544 545 Archive::Symbol Archive::Symbol::getNext() const { 546 Symbol t(*this); 547 if (Parent->kind() == K_BSD) { 548 // t.StringIndex is an offset from the start of the __.SYMDEF or 549 // "__.SYMDEF SORTED" member into the string table for the ranlib 550 // struct indexed by t.SymbolIndex . To change t.StringIndex to the 551 // offset in the string table for t.SymbolIndex+1 we subtract the 552 // its offset from the start of the string table for t.SymbolIndex 553 // and add the offset of the string table for t.SymbolIndex+1. 554 555 // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t 556 // which is the number of bytes of ranlib structs that follow. The ranlib 557 // structs are a pair of uint32_t's the first being a string table offset 558 // and the second being the offset into the archive of the member that 559 // define the symbol. After that the next uint32_t is the byte count of 560 // the string table followed by the string table. 561 const char *Buf = Parent->getSymbolTable().begin(); 562 uint32_t RanlibCount = 0; 563 RanlibCount = read32le(Buf) / 8; 564 // If t.SymbolIndex + 1 will be past the count of symbols (the RanlibCount) 565 // don't change the t.StringIndex as we don't want to reference a ranlib 566 // past RanlibCount. 567 if (t.SymbolIndex + 1 < RanlibCount) { 568 const char *Ranlibs = Buf + 4; 569 uint32_t CurRanStrx = 0; 570 uint32_t NextRanStrx = 0; 571 CurRanStrx = read32le(Ranlibs + t.SymbolIndex * 8); 572 NextRanStrx = read32le(Ranlibs + (t.SymbolIndex + 1) * 8); 573 t.StringIndex -= CurRanStrx; 574 t.StringIndex += NextRanStrx; 575 } 576 } else { 577 // Go to one past next null. 578 t.StringIndex = Parent->getSymbolTable().find('\0', t.StringIndex) + 1; 579 } 580 ++t.SymbolIndex; 581 return t; 582 } 583 584 Archive::symbol_iterator Archive::symbol_begin() const { 585 if (!hasSymbolTable()) 586 return symbol_iterator(Symbol(this, 0, 0)); 587 588 const char *buf = getSymbolTable().begin(); 589 if (kind() == K_GNU) { 590 uint32_t symbol_count = 0; 591 symbol_count = read32be(buf); 592 buf += sizeof(uint32_t) + (symbol_count * (sizeof(uint32_t))); 593 } else if (kind() == K_MIPS64) { 594 uint64_t symbol_count = read64be(buf); 595 buf += sizeof(uint64_t) + (symbol_count * (sizeof(uint64_t))); 596 } else if (kind() == K_BSD) { 597 // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t 598 // which is the number of bytes of ranlib structs that follow. The ranlib 599 // structs are a pair of uint32_t's the first being a string table offset 600 // and the second being the offset into the archive of the member that 601 // define the symbol. After that the next uint32_t is the byte count of 602 // the string table followed by the string table. 603 uint32_t ranlib_count = 0; 604 ranlib_count = read32le(buf) / 8; 605 const char *ranlibs = buf + 4; 606 uint32_t ran_strx = 0; 607 ran_strx = read32le(ranlibs); 608 buf += sizeof(uint32_t) + (ranlib_count * (2 * (sizeof(uint32_t)))); 609 // Skip the byte count of the string table. 610 buf += sizeof(uint32_t); 611 buf += ran_strx; 612 } else if (kind() == K_DARWIN64) { 613 // The __.SYMDEF_64 or "__.SYMDEF_64 SORTED" member starts with a uint64_t 614 // which is the number of bytes of ranlib_64 structs that follow. The 615 // ranlib_64 structs are a pair of uint64_t's the first being a string 616 // table offset and the second being the offset into the archive of the 617 // member that define the symbol. After that the next uint64_t is the byte 618 // count of the string table followed by the string table. 619 uint64_t ranlib_count = 0; 620 ranlib_count = read64le(buf) / 16; 621 const char *ranlibs = buf + 8; 622 uint64_t ran_strx = 0; 623 ran_strx = read64le(ranlibs); 624 buf += sizeof(uint64_t) + (ranlib_count * (2 * (sizeof(uint64_t)))); 625 // Skip the byte count of the string table. 626 buf += sizeof(uint64_t); 627 buf += ran_strx; 628 } else { 629 uint32_t member_count = 0; 630 uint32_t symbol_count = 0; 631 member_count = read32le(buf); 632 buf += 4 + (member_count * 4); // Skip offsets. 633 symbol_count = read32le(buf); 634 buf += 4 + (symbol_count * 2); // Skip indices. 635 } 636 uint32_t string_start_offset = buf - getSymbolTable().begin(); 637 return symbol_iterator(Symbol(this, 0, string_start_offset)); 638 } 639 640 Archive::symbol_iterator Archive::symbol_end() const { 641 return symbol_iterator(Symbol(this, getNumberOfSymbols(), 0)); 642 } 643 644 uint32_t Archive::getNumberOfSymbols() const { 645 if (!hasSymbolTable()) 646 return 0; 647 const char *buf = getSymbolTable().begin(); 648 if (kind() == K_GNU) 649 return read32be(buf); 650 if (kind() == K_MIPS64) 651 return read64be(buf); 652 if (kind() == K_BSD) 653 return read32le(buf) / 8; 654 if (kind() == K_DARWIN64) 655 return read64le(buf) / 16; 656 uint32_t member_count = 0; 657 member_count = read32le(buf); 658 buf += 4 + (member_count * 4); // Skip offsets. 659 return read32le(buf); 660 } 661 662 Archive::child_iterator Archive::findSym(StringRef name) const { 663 Archive::symbol_iterator bs = symbol_begin(); 664 Archive::symbol_iterator es = symbol_end(); 665 666 for (; bs != es; ++bs) { 667 StringRef SymName = bs->getName(); 668 if (SymName == name) { 669 ErrorOr<Archive::child_iterator> ResultOrErr = bs->getMember(); 670 // FIXME: Should we really eat the error? 671 if (ResultOrErr.getError()) 672 return child_end(); 673 return ResultOrErr.get(); 674 } 675 } 676 return child_end(); 677 } 678 679 bool Archive::hasSymbolTable() const { return !SymbolTable.empty(); } 680