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 ErrorOr<std::unique_ptr<Archive>> Archive::create(MemoryBufferRef Source) { 267 std::error_code EC; 268 std::unique_ptr<Archive> Ret(new Archive(Source, EC)); 269 if (EC) 270 return EC; 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, std::error_code &ec) 280 : Binary(Binary::ID_Archive, Source) { 281 StringRef Buffer = Data.getBuffer(); 282 // Check for sufficient magic. 283 if (Buffer.startswith(ThinMagic)) { 284 IsThin = true; 285 } else if (Buffer.startswith(Magic)) { 286 IsThin = false; 287 } else { 288 ec = object_error::invalid_file_type; 289 return; 290 } 291 292 // Get the special members. 293 child_iterator I = child_begin(false); 294 if ((ec = I->getError())) 295 return; 296 child_iterator E = child_end(); 297 298 // This is at least a valid empty archive. Since an empty archive is the 299 // same in all formats, just claim it to be gnu to make sure Format is 300 // initialized. 301 Format = K_GNU; 302 303 if (I == E) { 304 ec = std::error_code(); 305 return; 306 } 307 const Child *C = &**I; 308 309 auto Increment = [&]() { 310 ++I; 311 if ((ec = I->getError())) 312 return true; 313 C = &**I; 314 return false; 315 }; 316 317 StringRef Name = C->getRawName(); 318 319 // Below is the pattern that is used to figure out the archive format 320 // GNU archive format 321 // First member : / (may exist, if it exists, points to the symbol table ) 322 // Second member : // (may exist, if it exists, points to the string table) 323 // Note : The string table is used if the filename exceeds 15 characters 324 // BSD archive format 325 // First member : __.SYMDEF or "__.SYMDEF SORTED" (the symbol table) 326 // There is no string table, if the filename exceeds 15 characters or has a 327 // embedded space, the filename has #1/<size>, The size represents the size 328 // of the filename that needs to be read after the archive header 329 // COFF archive format 330 // First member : / 331 // Second member : / (provides a directory of symbols) 332 // Third member : // (may exist, if it exists, contains the string table) 333 // Note: Microsoft PE/COFF Spec 8.3 says that the third member is present 334 // even if the string table is empty. However, lib.exe does not in fact 335 // seem to create the third member if there's no member whose filename 336 // exceeds 15 characters. So the third member is optional. 337 338 if (Name == "__.SYMDEF" || Name == "__.SYMDEF_64") { 339 if (Name == "__.SYMDEF") 340 Format = K_BSD; 341 else // Name == "__.SYMDEF_64" 342 Format = K_DARWIN64; 343 // We know that the symbol table is not an external file, so we just assert 344 // there is no error. 345 SymbolTable = *C->getBuffer(); 346 if (Increment()) 347 return; 348 setFirstRegular(*C); 349 350 ec = std::error_code(); 351 return; 352 } 353 354 if (Name.startswith("#1/")) { 355 Format = K_BSD; 356 // We know this is BSD, so getName will work since there is no string table. 357 ErrorOr<StringRef> NameOrErr = C->getName(); 358 ec = NameOrErr.getError(); 359 if (ec) 360 return; 361 Name = NameOrErr.get(); 362 if (Name == "__.SYMDEF SORTED" || Name == "__.SYMDEF") { 363 // We know that the symbol table is not an external file, so we just 364 // assert there is no error. 365 SymbolTable = *C->getBuffer(); 366 if (Increment()) 367 return; 368 } 369 else if (Name == "__.SYMDEF_64 SORTED" || Name == "__.SYMDEF_64") { 370 Format = K_DARWIN64; 371 // We know that the symbol table is not an external file, so we just 372 // assert there is no error. 373 SymbolTable = *C->getBuffer(); 374 if (Increment()) 375 return; 376 } 377 setFirstRegular(*C); 378 return; 379 } 380 381 // MIPS 64-bit ELF archives use a special format of a symbol table. 382 // This format is marked by `ar_name` field equals to "/SYM64/". 383 // For detailed description see page 96 in the following document: 384 // http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf 385 386 bool has64SymTable = false; 387 if (Name == "/" || Name == "/SYM64/") { 388 // We know that the symbol table is not an external file, so we just assert 389 // there is no error. 390 SymbolTable = *C->getBuffer(); 391 if (Name == "/SYM64/") 392 has64SymTable = true; 393 394 if (Increment()) 395 return; 396 if (I == E) { 397 ec = std::error_code(); 398 return; 399 } 400 Name = C->getRawName(); 401 } 402 403 if (Name == "//") { 404 Format = has64SymTable ? K_MIPS64 : K_GNU; 405 // The string table is never an external member, so we just assert on the 406 // ErrorOr. 407 StringTable = *C->getBuffer(); 408 if (Increment()) 409 return; 410 setFirstRegular(*C); 411 ec = std::error_code(); 412 return; 413 } 414 415 if (Name[0] != '/') { 416 Format = has64SymTable ? K_MIPS64 : K_GNU; 417 setFirstRegular(*C); 418 ec = std::error_code(); 419 return; 420 } 421 422 if (Name != "/") { 423 ec = object_error::parse_failed; 424 return; 425 } 426 427 Format = K_COFF; 428 // We know that the symbol table is not an external file, so we just assert 429 // there is no error. 430 SymbolTable = *C->getBuffer(); 431 432 if (Increment()) 433 return; 434 435 if (I == E) { 436 setFirstRegular(*C); 437 ec = std::error_code(); 438 return; 439 } 440 441 Name = C->getRawName(); 442 443 if (Name == "//") { 444 // The string table is never an external member, so we just assert on the 445 // ErrorOr. 446 StringTable = *C->getBuffer(); 447 if (Increment()) 448 return; 449 } 450 451 setFirstRegular(*C); 452 ec = std::error_code(); 453 } 454 455 Archive::child_iterator Archive::child_begin(bool SkipInternal) const { 456 if (Data.getBufferSize() == 8) // empty archive. 457 return child_end(); 458 459 if (SkipInternal) 460 return Child(this, FirstRegularData, FirstRegularStartOfFile); 461 462 const char *Loc = Data.getBufferStart() + strlen(Magic); 463 std::error_code EC; 464 Child c(this, Loc, &EC); 465 if (EC) 466 return child_iterator(EC); 467 return child_iterator(c); 468 } 469 470 Archive::child_iterator Archive::child_end() const { 471 return Child(this, nullptr, nullptr); 472 } 473 474 StringRef Archive::Symbol::getName() const { 475 return Parent->getSymbolTable().begin() + StringIndex; 476 } 477 478 ErrorOr<Archive::Child> Archive::Symbol::getMember() const { 479 const char *Buf = Parent->getSymbolTable().begin(); 480 const char *Offsets = Buf; 481 if (Parent->kind() == K_MIPS64 || Parent->kind() == K_DARWIN64) 482 Offsets += sizeof(uint64_t); 483 else 484 Offsets += sizeof(uint32_t); 485 uint32_t Offset = 0; 486 if (Parent->kind() == K_GNU) { 487 Offset = read32be(Offsets + SymbolIndex * 4); 488 } else if (Parent->kind() == K_MIPS64) { 489 Offset = read64be(Offsets + SymbolIndex * 8); 490 } else if (Parent->kind() == K_BSD) { 491 // The SymbolIndex is an index into the ranlib structs that start at 492 // Offsets (the first uint32_t is the number of bytes of the ranlib 493 // structs). The ranlib structs are a pair of uint32_t's the first 494 // being a string table offset and the second being the offset into 495 // the archive of the member that defines the symbol. Which is what 496 // is needed here. 497 Offset = read32le(Offsets + SymbolIndex * 8 + 4); 498 } else if (Parent->kind() == K_DARWIN64) { 499 // The SymbolIndex is an index into the ranlib_64 structs that start at 500 // Offsets (the first uint64_t is the number of bytes of the ranlib_64 501 // structs). The ranlib_64 structs are a pair of uint64_t's the first 502 // being a string table offset and the second being the offset into 503 // the archive of the member that defines the symbol. Which is what 504 // is needed here. 505 Offset = read64le(Offsets + SymbolIndex * 16 + 8); 506 } else { 507 // Skip offsets. 508 uint32_t MemberCount = read32le(Buf); 509 Buf += MemberCount * 4 + 4; 510 511 uint32_t SymbolCount = read32le(Buf); 512 if (SymbolIndex >= SymbolCount) 513 return object_error::parse_failed; 514 515 // Skip SymbolCount to get to the indices table. 516 const char *Indices = Buf + 4; 517 518 // Get the index of the offset in the file member offset table for this 519 // symbol. 520 uint16_t OffsetIndex = read16le(Indices + SymbolIndex * 2); 521 // Subtract 1 since OffsetIndex is 1 based. 522 --OffsetIndex; 523 524 if (OffsetIndex >= MemberCount) 525 return object_error::parse_failed; 526 527 Offset = read32le(Offsets + OffsetIndex * 4); 528 } 529 530 const char *Loc = Parent->getData().begin() + Offset; 531 std::error_code EC; 532 Child C(Parent, Loc, &EC); 533 if (EC) 534 return EC; 535 return C; 536 } 537 538 Archive::Symbol Archive::Symbol::getNext() const { 539 Symbol t(*this); 540 if (Parent->kind() == K_BSD) { 541 // t.StringIndex is an offset from the start of the __.SYMDEF or 542 // "__.SYMDEF SORTED" member into the string table for the ranlib 543 // struct indexed by t.SymbolIndex . To change t.StringIndex to the 544 // offset in the string table for t.SymbolIndex+1 we subtract the 545 // its offset from the start of the string table for t.SymbolIndex 546 // and add the offset of the string table for t.SymbolIndex+1. 547 548 // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t 549 // which is the number of bytes of ranlib structs that follow. The ranlib 550 // structs are a pair of uint32_t's the first being a string table offset 551 // and the second being the offset into the archive of the member that 552 // define the symbol. After that the next uint32_t is the byte count of 553 // the string table followed by the string table. 554 const char *Buf = Parent->getSymbolTable().begin(); 555 uint32_t RanlibCount = 0; 556 RanlibCount = read32le(Buf) / 8; 557 // If t.SymbolIndex + 1 will be past the count of symbols (the RanlibCount) 558 // don't change the t.StringIndex as we don't want to reference a ranlib 559 // past RanlibCount. 560 if (t.SymbolIndex + 1 < RanlibCount) { 561 const char *Ranlibs = Buf + 4; 562 uint32_t CurRanStrx = 0; 563 uint32_t NextRanStrx = 0; 564 CurRanStrx = read32le(Ranlibs + t.SymbolIndex * 8); 565 NextRanStrx = read32le(Ranlibs + (t.SymbolIndex + 1) * 8); 566 t.StringIndex -= CurRanStrx; 567 t.StringIndex += NextRanStrx; 568 } 569 } else { 570 // Go to one past next null. 571 t.StringIndex = Parent->getSymbolTable().find('\0', t.StringIndex) + 1; 572 } 573 ++t.SymbolIndex; 574 return t; 575 } 576 577 Archive::symbol_iterator Archive::symbol_begin() const { 578 if (!hasSymbolTable()) 579 return symbol_iterator(Symbol(this, 0, 0)); 580 581 const char *buf = getSymbolTable().begin(); 582 if (kind() == K_GNU) { 583 uint32_t symbol_count = 0; 584 symbol_count = read32be(buf); 585 buf += sizeof(uint32_t) + (symbol_count * (sizeof(uint32_t))); 586 } else if (kind() == K_MIPS64) { 587 uint64_t symbol_count = read64be(buf); 588 buf += sizeof(uint64_t) + (symbol_count * (sizeof(uint64_t))); 589 } else if (kind() == K_BSD) { 590 // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t 591 // which is the number of bytes of ranlib structs that follow. The ranlib 592 // structs are a pair of uint32_t's the first being a string table offset 593 // and the second being the offset into the archive of the member that 594 // define the symbol. After that the next uint32_t is the byte count of 595 // the string table followed by the string table. 596 uint32_t ranlib_count = 0; 597 ranlib_count = read32le(buf) / 8; 598 const char *ranlibs = buf + 4; 599 uint32_t ran_strx = 0; 600 ran_strx = read32le(ranlibs); 601 buf += sizeof(uint32_t) + (ranlib_count * (2 * (sizeof(uint32_t)))); 602 // Skip the byte count of the string table. 603 buf += sizeof(uint32_t); 604 buf += ran_strx; 605 } else if (kind() == K_DARWIN64) { 606 // The __.SYMDEF_64 or "__.SYMDEF_64 SORTED" member starts with a uint64_t 607 // which is the number of bytes of ranlib_64 structs that follow. The 608 // ranlib_64 structs are a pair of uint64_t's the first being a string 609 // table offset and the second being the offset into the archive of the 610 // member that define the symbol. After that the next uint64_t is the byte 611 // count of the string table followed by the string table. 612 uint64_t ranlib_count = 0; 613 ranlib_count = read64le(buf) / 16; 614 const char *ranlibs = buf + 8; 615 uint64_t ran_strx = 0; 616 ran_strx = read64le(ranlibs); 617 buf += sizeof(uint64_t) + (ranlib_count * (2 * (sizeof(uint64_t)))); 618 // Skip the byte count of the string table. 619 buf += sizeof(uint64_t); 620 buf += ran_strx; 621 } else { 622 uint32_t member_count = 0; 623 uint32_t symbol_count = 0; 624 member_count = read32le(buf); 625 buf += 4 + (member_count * 4); // Skip offsets. 626 symbol_count = read32le(buf); 627 buf += 4 + (symbol_count * 2); // Skip indices. 628 } 629 uint32_t string_start_offset = buf - getSymbolTable().begin(); 630 return symbol_iterator(Symbol(this, 0, string_start_offset)); 631 } 632 633 Archive::symbol_iterator Archive::symbol_end() const { 634 return symbol_iterator(Symbol(this, getNumberOfSymbols(), 0)); 635 } 636 637 uint32_t Archive::getNumberOfSymbols() const { 638 if (!hasSymbolTable()) 639 return 0; 640 const char *buf = getSymbolTable().begin(); 641 if (kind() == K_GNU) 642 return read32be(buf); 643 if (kind() == K_MIPS64) 644 return read64be(buf); 645 if (kind() == K_BSD) 646 return read32le(buf) / 8; 647 if (kind() == K_DARWIN64) 648 return read64le(buf) / 16; 649 uint32_t member_count = 0; 650 member_count = read32le(buf); 651 buf += 4 + (member_count * 4); // Skip offsets. 652 return read32le(buf); 653 } 654 655 Archive::child_iterator Archive::findSym(StringRef name) const { 656 Archive::symbol_iterator bs = symbol_begin(); 657 Archive::symbol_iterator es = symbol_end(); 658 659 for (; bs != es; ++bs) { 660 StringRef SymName = bs->getName(); 661 if (SymName == name) { 662 ErrorOr<Archive::child_iterator> ResultOrErr = bs->getMember(); 663 // FIXME: Should we really eat the error? 664 if (ResultOrErr.getError()) 665 return child_end(); 666 return ResultOrErr.get(); 667 } 668 } 669 return child_end(); 670 } 671 672 bool Archive::hasSymbolTable() const { return !SymbolTable.empty(); } 673