1 //===- Archive.cpp - ar File Format implementation --------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the ArchiveObjectFile class.
12 //===----------------------------------------------------------------------===//
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"
22 using namespace object;
23 using namespace llvm::support::endian;
25 static const char *const Magic = "!<arch>\n";
26 static const char *const ThinMagic = "!<thin>\n";
28 void Archive::anchor() { }
31 malformedError(Twine Msg) {
32 std::string StringMsg = "truncated or malformed archive (" + Msg.str() + ")";
33 return make_error<GenericBinaryError>(std::move(StringMsg),
34 object_error::parse_failed);
37 ArchiveMemberHeader::ArchiveMemberHeader(const Archive *Parent,
38 const char *RawHeaderPtr,
39 uint64_t Size, Error *Err)
41 ArMemHdr(reinterpret_cast<const ArMemHdrType *>(RawHeaderPtr)) {
42 if (RawHeaderPtr == nullptr)
44 ErrorAsOutParameter ErrAsOutParam(Err);
46 if (Size < sizeof(ArMemHdrType)) {
48 std::string Msg("remaining size of archive too small for next archive "
50 Expected<StringRef> NameOrErr = getName(Size);
52 consumeError(NameOrErr.takeError());
53 uint64_t Offset = RawHeaderPtr - Parent->getData().data();
54 *Err = malformedError(Msg + "at offset " + Twine(Offset));
56 *Err = malformedError(Msg + "for " + NameOrErr.get());
60 if (ArMemHdr->Terminator[0] != '`' || ArMemHdr->Terminator[1] != '\n') {
63 raw_string_ostream OS(Buf);
64 OS.write_escaped(llvm::StringRef(ArMemHdr->Terminator,
65 sizeof(ArMemHdr->Terminator)));
67 std::string Msg("terminator characters in archive member \"" + Buf +
68 "\" not the correct \"`\\n\" values for the archive "
70 Expected<StringRef> NameOrErr = getName(Size);
72 consumeError(NameOrErr.takeError());
73 uint64_t Offset = RawHeaderPtr - Parent->getData().data();
74 *Err = malformedError(Msg + "at offset " + Twine(Offset));
76 *Err = malformedError(Msg + "for " + NameOrErr.get());
82 // This gets the raw name from the ArMemHdr->Name field and checks that it is
83 // valid for the kind of archive. If it is not valid it returns an Error.
84 Expected<StringRef> ArchiveMemberHeader::getRawName() const {
86 auto Kind = Parent->kind();
87 if (Kind == Archive::K_BSD || Kind == Archive::K_DARWIN64) {
88 if (ArMemHdr->Name[0] == ' ') {
89 uint64_t Offset = reinterpret_cast<const char *>(ArMemHdr) -
90 Parent->getData().data();
91 return malformedError("name contains a leading space for archive member "
92 "header at offset " + Twine(Offset));
96 else if (ArMemHdr->Name[0] == '/' || ArMemHdr->Name[0] == '#')
100 llvm::StringRef::size_type end =
101 llvm::StringRef(ArMemHdr->Name, sizeof(ArMemHdr->Name)).find(EndCond);
102 if (end == llvm::StringRef::npos)
103 end = sizeof(ArMemHdr->Name);
104 assert(end <= sizeof(ArMemHdr->Name) && end > 0);
105 // Don't include the EndCond if there is one.
106 return llvm::StringRef(ArMemHdr->Name, end);
109 // This gets the name looking up long names. Size is the size of the archive
110 // member including the header, so the size of any name following the header
111 // is checked to make sure it does not overflow.
112 Expected<StringRef> ArchiveMemberHeader::getName(uint64_t Size) const {
114 // This can be called from the ArchiveMemberHeader constructor when the
115 // archive header is truncated to produce an error message with the name.
116 // Make sure the name field is not truncated.
117 if (Size < offsetof(ArMemHdrType, Name) + sizeof(ArMemHdr->Name)) {
118 uint64_t ArchiveOffset = reinterpret_cast<const char *>(ArMemHdr) -
119 Parent->getData().data();
120 return malformedError("archive header truncated before the name field "
121 "for archive member header at offset " +
122 Twine(ArchiveOffset));
125 // The raw name itself can be invalid.
126 Expected<StringRef> NameOrErr = getRawName();
128 return NameOrErr.takeError();
129 StringRef Name = NameOrErr.get();
131 // Check if it's a special name.
132 if (Name[0] == '/') {
133 if (Name.size() == 1) // Linker member.
135 if (Name.size() == 2 && Name[1] == '/') // String table.
138 // Get the string table offset.
139 std::size_t StringOffset;
140 if (Name.substr(1).rtrim(' ').getAsInteger(10, StringOffset)) {
142 raw_string_ostream OS(Buf);
143 OS.write_escaped(Name.substr(1).rtrim(' '));
145 uint64_t ArchiveOffset = reinterpret_cast<const char *>(ArMemHdr) -
146 Parent->getData().data();
147 return malformedError("long name offset characters after the '/' are "
148 "not all decimal numbers: '" + Buf + "' for "
149 "archive member header at offset " +
150 Twine(ArchiveOffset));
154 if (StringOffset >= Parent->getStringTable().size()) {
155 uint64_t ArchiveOffset = reinterpret_cast<const char *>(ArMemHdr) -
156 Parent->getData().data();
157 return malformedError("long name offset " + Twine(StringOffset) + " past "
158 "the end of the string table for archive member "
159 "header at offset " + Twine(ArchiveOffset));
161 const char *addr = Parent->getStringTable().begin() + StringOffset;
163 // GNU long file names end with a "/\n".
164 if (Parent->kind() == Archive::K_GNU ||
165 Parent->kind() == Archive::K_MIPS64) {
166 StringRef::size_type End = StringRef(addr).find('\n');
167 return StringRef(addr, End - 1);
172 if (Name.startswith("#1/")) {
174 if (Name.substr(3).rtrim(' ').getAsInteger(10, NameLength)) {
176 raw_string_ostream OS(Buf);
177 OS.write_escaped(Name.substr(3).rtrim(' '));
179 uint64_t ArchiveOffset = reinterpret_cast<const char *>(ArMemHdr) -
180 Parent->getData().data();
181 return malformedError("long name length characters after the #1/ are "
182 "not all decimal numbers: '" + Buf + "' for "
183 "archive member header at offset " +
184 Twine(ArchiveOffset));
186 if (getSizeOf() + NameLength > Size) {
187 uint64_t ArchiveOffset = reinterpret_cast<const char *>(ArMemHdr) -
188 Parent->getData().data();
189 return malformedError("long name length: " + Twine(NameLength) +
190 " extends past the end of the member or archive "
191 "for archive member header at offset " +
192 Twine(ArchiveOffset));
194 return StringRef(reinterpret_cast<const char *>(ArMemHdr) + getSizeOf(),
195 NameLength).rtrim('\0');
198 // It is not a long name so trim the blanks at the end of the name.
199 if (Name[Name.size() - 1] != '/')
200 return Name.rtrim(' ');
202 // It's a simple name.
203 return Name.drop_back(1);
206 Expected<uint32_t> ArchiveMemberHeader::getSize() const {
208 if (llvm::StringRef(ArMemHdr->Size,
209 sizeof(ArMemHdr->Size)).rtrim(" ").getAsInteger(10, Ret)) {
211 raw_string_ostream OS(Buf);
212 OS.write_escaped(llvm::StringRef(ArMemHdr->Size,
213 sizeof(ArMemHdr->Size)).rtrim(" "));
215 uint64_t Offset = reinterpret_cast<const char *>(ArMemHdr) -
216 Parent->getData().data();
217 return malformedError("characters in size field in archive header are not "
218 "all decimal numbers: '" + Buf + "' for archive "
219 "member header at offset " + Twine(Offset));
224 Expected<sys::fs::perms> ArchiveMemberHeader::getAccessMode() const {
226 if (StringRef(ArMemHdr->AccessMode,
227 sizeof(ArMemHdr->AccessMode)).rtrim(' ').getAsInteger(8, Ret)) {
229 raw_string_ostream OS(Buf);
230 OS.write_escaped(llvm::StringRef(ArMemHdr->AccessMode,
231 sizeof(ArMemHdr->AccessMode)).rtrim(" "));
233 uint64_t Offset = reinterpret_cast<const char *>(ArMemHdr) -
234 Parent->getData().data();
235 return malformedError("characters in AccessMode field in archive header "
236 "are not all decimal numbers: '" + Buf + "' for the "
237 "archive member header at offset " + Twine(Offset));
239 return static_cast<sys::fs::perms>(Ret);
242 Expected<sys::TimePoint<std::chrono::seconds>>
243 ArchiveMemberHeader::getLastModified() const {
245 if (StringRef(ArMemHdr->LastModified,
246 sizeof(ArMemHdr->LastModified)).rtrim(' ')
247 .getAsInteger(10, Seconds)) {
249 raw_string_ostream OS(Buf);
250 OS.write_escaped(llvm::StringRef(ArMemHdr->LastModified,
251 sizeof(ArMemHdr->LastModified)).rtrim(" "));
253 uint64_t Offset = reinterpret_cast<const char *>(ArMemHdr) -
254 Parent->getData().data();
255 return malformedError("characters in LastModified field in archive header "
256 "are not all decimal numbers: '" + Buf + "' for the "
257 "archive member header at offset " + Twine(Offset));
260 return sys::toTimePoint(Seconds);
263 Expected<unsigned> ArchiveMemberHeader::getUID() const {
265 StringRef User = StringRef(ArMemHdr->UID, sizeof(ArMemHdr->UID)).rtrim(' ');
268 if (User.getAsInteger(10, Ret)) {
270 raw_string_ostream OS(Buf);
271 OS.write_escaped(User);
273 uint64_t Offset = reinterpret_cast<const char *>(ArMemHdr) -
274 Parent->getData().data();
275 return malformedError("characters in UID field in archive header "
276 "are not all decimal numbers: '" + Buf + "' for the "
277 "archive member header at offset " + Twine(Offset));
282 Expected<unsigned> ArchiveMemberHeader::getGID() const {
284 StringRef Group = StringRef(ArMemHdr->GID, sizeof(ArMemHdr->GID)).rtrim(' ');
287 if (Group.getAsInteger(10, Ret)) {
289 raw_string_ostream OS(Buf);
290 OS.write_escaped(Group);
292 uint64_t Offset = reinterpret_cast<const char *>(ArMemHdr) -
293 Parent->getData().data();
294 return malformedError("characters in GID field in archive header "
295 "are not all decimal numbers: '" + Buf + "' for the "
296 "archive member header at offset " + Twine(Offset));
301 Archive::Child::Child(const Archive *Parent, StringRef Data,
302 uint16_t StartOfFile)
303 : Parent(Parent), Header(Parent, Data.data(), Data.size(), nullptr),
304 Data(Data), StartOfFile(StartOfFile) {
307 Archive::Child::Child(const Archive *Parent, const char *Start, Error *Err)
309 Header(Parent, Start,
311 ? Parent->getData().size() - (Start - Parent->getData().data())
316 // If we are pointed to real data, Start is not a nullptr, then there must be
317 // a non-null Err pointer available to report malformed data on. Only in
318 // the case sentinel value is being constructed is Err is permitted to be a
320 assert(Err && "Err can't be nullptr if Start is not a nullptr");
322 ErrorAsOutParameter ErrAsOutParam(Err);
324 // If there was an error in the construction of the Header
325 // then just return with the error now set.
329 uint64_t Size = Header.getSizeOf();
330 Data = StringRef(Start, Size);
331 Expected<bool> isThinOrErr = isThinMember();
333 *Err = isThinOrErr.takeError();
336 bool isThin = isThinOrErr.get();
338 Expected<uint64_t> MemberSize = getRawSize();
340 *Err = MemberSize.takeError();
343 Size += MemberSize.get();
344 Data = StringRef(Start, Size);
347 // Setup StartOfFile and PaddingBytes.
348 StartOfFile = Header.getSizeOf();
349 // Don't include attached name.
350 Expected<StringRef> NameOrErr = getRawName();
352 *Err = NameOrErr.takeError();
355 StringRef Name = NameOrErr.get();
356 if (Name.startswith("#1/")) {
358 if (Name.substr(3).rtrim(' ').getAsInteger(10, NameSize)) {
360 raw_string_ostream OS(Buf);
361 OS.write_escaped(Name.substr(3).rtrim(' '));
363 uint64_t Offset = Start - Parent->getData().data();
364 *Err = malformedError("long name length characters after the #1/ are "
365 "not all decimal numbers: '" + Buf + "' for "
366 "archive member header at offset " +
370 StartOfFile += NameSize;
374 Expected<uint64_t> Archive::Child::getSize() const {
375 if (Parent->IsThin) {
376 Expected<uint32_t> Size = Header.getSize();
378 return Size.takeError();
381 return Data.size() - StartOfFile;
384 Expected<uint64_t> Archive::Child::getRawSize() const {
385 return Header.getSize();
388 Expected<bool> Archive::Child::isThinMember() const {
389 Expected<StringRef> NameOrErr = Header.getRawName();
391 return NameOrErr.takeError();
392 StringRef Name = NameOrErr.get();
393 return Parent->IsThin && Name != "/" && Name != "//";
396 Expected<std::string> Archive::Child::getFullName() const {
397 Expected<bool> isThin = isThinMember();
399 return isThin.takeError();
400 assert(isThin.get());
401 Expected<StringRef> NameOrErr = getName();
403 return NameOrErr.takeError();
404 StringRef Name = *NameOrErr;
405 if (sys::path::is_absolute(Name))
408 SmallString<128> FullName = sys::path::parent_path(
409 Parent->getMemoryBufferRef().getBufferIdentifier());
410 sys::path::append(FullName, Name);
411 return StringRef(FullName);
414 Expected<StringRef> Archive::Child::getBuffer() const {
415 Expected<bool> isThinOrErr = isThinMember();
417 return isThinOrErr.takeError();
418 bool isThin = isThinOrErr.get();
420 Expected<uint32_t> Size = getSize();
422 return Size.takeError();
423 return StringRef(Data.data() + StartOfFile, Size.get());
425 Expected<std::string> FullNameOrErr = getFullName();
427 return FullNameOrErr.takeError();
428 const std::string &FullName = *FullNameOrErr;
429 ErrorOr<std::unique_ptr<MemoryBuffer>> Buf = MemoryBuffer::getFile(FullName);
430 if (std::error_code EC = Buf.getError())
431 return errorCodeToError(EC);
432 Parent->ThinBuffers.push_back(std::move(*Buf));
433 return Parent->ThinBuffers.back()->getBuffer();
436 Expected<Archive::Child> Archive::Child::getNext() const {
437 size_t SpaceToSkip = Data.size();
438 // If it's odd, add 1 to make it even.
442 const char *NextLoc = Data.data() + SpaceToSkip;
444 // Check to see if this is at the end of the archive.
445 if (NextLoc == Parent->Data.getBufferEnd())
446 return Child(nullptr, nullptr, nullptr);
448 // Check to see if this is past the end of the archive.
449 if (NextLoc > Parent->Data.getBufferEnd()) {
450 std::string Msg("offset to next archive member past the end of the archive "
452 Expected<StringRef> NameOrErr = getName();
454 consumeError(NameOrErr.takeError());
455 uint64_t Offset = Data.data() - Parent->getData().data();
456 return malformedError(Msg + "at offset " + Twine(Offset));
458 return malformedError(Msg + NameOrErr.get());
461 Error Err = Error::success();
462 Child Ret(Parent, NextLoc, &Err);
464 return std::move(Err);
468 uint64_t Archive::Child::getChildOffset() const {
469 const char *a = Parent->Data.getBuffer().data();
470 const char *c = Data.data();
471 uint64_t offset = c - a;
475 Expected<StringRef> Archive::Child::getName() const {
476 Expected<uint64_t> RawSizeOrErr = getRawSize();
478 return RawSizeOrErr.takeError();
479 uint64_t RawSize = RawSizeOrErr.get();
480 Expected<StringRef> NameOrErr = Header.getName(Header.getSizeOf() + RawSize);
482 return NameOrErr.takeError();
483 StringRef Name = NameOrErr.get();
487 Expected<MemoryBufferRef> Archive::Child::getMemoryBufferRef() const {
488 Expected<StringRef> NameOrErr = getName();
490 return NameOrErr.takeError();
491 StringRef Name = NameOrErr.get();
492 Expected<StringRef> Buf = getBuffer();
494 return Buf.takeError();
495 return MemoryBufferRef(*Buf, Name);
498 Expected<std::unique_ptr<Binary>>
499 Archive::Child::getAsBinary(LLVMContext *Context) const {
500 Expected<MemoryBufferRef> BuffOrErr = getMemoryBufferRef();
502 return BuffOrErr.takeError();
504 auto BinaryOrErr = createBinary(BuffOrErr.get(), Context);
506 return std::move(*BinaryOrErr);
507 return BinaryOrErr.takeError();
510 Expected<std::unique_ptr<Archive>> Archive::create(MemoryBufferRef Source) {
511 Error Err = Error::success();
512 std::unique_ptr<Archive> Ret(new Archive(Source, Err));
514 return std::move(Err);
515 return std::move(Ret);
518 void Archive::setFirstRegular(const Child &C) {
519 FirstRegularData = C.Data;
520 FirstRegularStartOfFile = C.StartOfFile;
523 Archive::Archive(MemoryBufferRef Source, Error &Err)
524 : Binary(Binary::ID_Archive, Source) {
525 ErrorAsOutParameter ErrAsOutParam(&Err);
526 StringRef Buffer = Data.getBuffer();
527 // Check for sufficient magic.
528 if (Buffer.startswith(ThinMagic)) {
530 } else if (Buffer.startswith(Magic)) {
533 Err = make_error<GenericBinaryError>("File too small to be an archive",
534 object_error::invalid_file_type);
538 // Make sure Format is initialized before any call to
539 // ArchiveMemberHeader::getName() is made. This could be a valid empty
540 // archive which is the same in all formats. So claiming it to be gnu to is
541 // fine if not totally correct before we look for a string table or table of
545 // Get the special members.
546 child_iterator I = child_begin(Err, false);
549 child_iterator E = child_end();
551 // See if this is a valid empty archive and if so return.
553 Err = Error::success();
556 const Child *C = &*I;
558 auto Increment = [&]() {
566 Expected<StringRef> NameOrErr = C->getRawName();
568 Err = NameOrErr.takeError();
571 StringRef Name = NameOrErr.get();
573 // Below is the pattern that is used to figure out the archive format
574 // GNU archive format
575 // First member : / (may exist, if it exists, points to the symbol table )
576 // Second member : // (may exist, if it exists, points to the string table)
577 // Note : The string table is used if the filename exceeds 15 characters
578 // BSD archive format
579 // First member : __.SYMDEF or "__.SYMDEF SORTED" (the symbol table)
580 // There is no string table, if the filename exceeds 15 characters or has a
581 // embedded space, the filename has #1/<size>, The size represents the size
582 // of the filename that needs to be read after the archive header
583 // COFF archive format
585 // Second member : / (provides a directory of symbols)
586 // Third member : // (may exist, if it exists, contains the string table)
587 // Note: Microsoft PE/COFF Spec 8.3 says that the third member is present
588 // even if the string table is empty. However, lib.exe does not in fact
589 // seem to create the third member if there's no member whose filename
590 // exceeds 15 characters. So the third member is optional.
592 if (Name == "__.SYMDEF" || Name == "__.SYMDEF_64") {
593 if (Name == "__.SYMDEF")
595 else // Name == "__.SYMDEF_64"
597 // We know that the symbol table is not an external file, but we still must
598 // check any Expected<> return value.
599 Expected<StringRef> BufOrErr = C->getBuffer();
601 Err = BufOrErr.takeError();
604 SymbolTable = BufOrErr.get();
609 Err = Error::success();
613 if (Name.startswith("#1/")) {
615 // We know this is BSD, so getName will work since there is no string table.
616 Expected<StringRef> NameOrErr = C->getName();
618 Err = NameOrErr.takeError();
621 Name = NameOrErr.get();
622 if (Name == "__.SYMDEF SORTED" || Name == "__.SYMDEF") {
623 // We know that the symbol table is not an external file, but we still
624 // must check any Expected<> return value.
625 Expected<StringRef> BufOrErr = C->getBuffer();
627 Err = BufOrErr.takeError();
630 SymbolTable = BufOrErr.get();
634 else if (Name == "__.SYMDEF_64 SORTED" || Name == "__.SYMDEF_64") {
636 // We know that the symbol table is not an external file, but we still
637 // must check any Expected<> return value.
638 Expected<StringRef> BufOrErr = C->getBuffer();
640 Err = BufOrErr.takeError();
643 SymbolTable = BufOrErr.get();
651 // MIPS 64-bit ELF archives use a special format of a symbol table.
652 // This format is marked by `ar_name` field equals to "/SYM64/".
653 // For detailed description see page 96 in the following document:
654 // http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf
656 bool has64SymTable = false;
657 if (Name == "/" || Name == "/SYM64/") {
658 // We know that the symbol table is not an external file, but we still
659 // must check any Expected<> return value.
660 Expected<StringRef> BufOrErr = C->getBuffer();
662 Err = BufOrErr.takeError();
665 SymbolTable = BufOrErr.get();
666 if (Name == "/SYM64/")
667 has64SymTable = true;
672 Err = Error::success();
675 Expected<StringRef> NameOrErr = C->getRawName();
677 Err = NameOrErr.takeError();
680 Name = NameOrErr.get();
684 Format = has64SymTable ? K_MIPS64 : K_GNU;
685 // The string table is never an external member, but we still
686 // must check any Expected<> return value.
687 Expected<StringRef> BufOrErr = C->getBuffer();
689 Err = BufOrErr.takeError();
692 StringTable = BufOrErr.get();
696 Err = Error::success();
700 if (Name[0] != '/') {
701 Format = has64SymTable ? K_MIPS64 : K_GNU;
703 Err = Error::success();
708 Err = errorCodeToError(object_error::parse_failed);
713 // We know that the symbol table is not an external file, but we still
714 // must check any Expected<> return value.
715 Expected<StringRef> BufOrErr = C->getBuffer();
717 Err = BufOrErr.takeError();
720 SymbolTable = BufOrErr.get();
727 Err = Error::success();
731 NameOrErr = C->getRawName();
733 Err = NameOrErr.takeError();
736 Name = NameOrErr.get();
739 // The string table is never an external member, but we still
740 // must check any Expected<> return value.
741 Expected<StringRef> BufOrErr = C->getBuffer();
743 Err = BufOrErr.takeError();
746 StringTable = BufOrErr.get();
752 Err = Error::success();
755 Archive::child_iterator Archive::child_begin(Error &Err,
756 bool SkipInternal) const {
761 return child_iterator(Child(this, FirstRegularData,
762 FirstRegularStartOfFile),
765 const char *Loc = Data.getBufferStart() + strlen(Magic);
766 Child C(this, Loc, &Err);
769 return child_iterator(C, &Err);
772 Archive::child_iterator Archive::child_end() const {
773 return child_iterator(Child(nullptr, nullptr, nullptr), nullptr);
776 StringRef Archive::Symbol::getName() const {
777 return Parent->getSymbolTable().begin() + StringIndex;
780 Expected<Archive::Child> Archive::Symbol::getMember() const {
781 const char *Buf = Parent->getSymbolTable().begin();
782 const char *Offsets = Buf;
783 if (Parent->kind() == K_MIPS64 || Parent->kind() == K_DARWIN64)
784 Offsets += sizeof(uint64_t);
786 Offsets += sizeof(uint32_t);
788 if (Parent->kind() == K_GNU) {
789 Offset = read32be(Offsets + SymbolIndex * 4);
790 } else if (Parent->kind() == K_MIPS64) {
791 Offset = read64be(Offsets + SymbolIndex * 8);
792 } else if (Parent->kind() == K_BSD) {
793 // The SymbolIndex is an index into the ranlib structs that start at
794 // Offsets (the first uint32_t is the number of bytes of the ranlib
795 // structs). The ranlib structs are a pair of uint32_t's the first
796 // being a string table offset and the second being the offset into
797 // the archive of the member that defines the symbol. Which is what
799 Offset = read32le(Offsets + SymbolIndex * 8 + 4);
800 } else if (Parent->kind() == K_DARWIN64) {
801 // The SymbolIndex is an index into the ranlib_64 structs that start at
802 // Offsets (the first uint64_t is the number of bytes of the ranlib_64
803 // structs). The ranlib_64 structs are a pair of uint64_t's the first
804 // being a string table offset and the second being the offset into
805 // the archive of the member that defines the symbol. Which is what
807 Offset = read64le(Offsets + SymbolIndex * 16 + 8);
810 uint32_t MemberCount = read32le(Buf);
811 Buf += MemberCount * 4 + 4;
813 uint32_t SymbolCount = read32le(Buf);
814 if (SymbolIndex >= SymbolCount)
815 return errorCodeToError(object_error::parse_failed);
817 // Skip SymbolCount to get to the indices table.
818 const char *Indices = Buf + 4;
820 // Get the index of the offset in the file member offset table for this
822 uint16_t OffsetIndex = read16le(Indices + SymbolIndex * 2);
823 // Subtract 1 since OffsetIndex is 1 based.
826 if (OffsetIndex >= MemberCount)
827 return errorCodeToError(object_error::parse_failed);
829 Offset = read32le(Offsets + OffsetIndex * 4);
832 const char *Loc = Parent->getData().begin() + Offset;
833 Error Err = Error::success();
834 Child C(Parent, Loc, &Err);
836 return std::move(Err);
840 Archive::Symbol Archive::Symbol::getNext() const {
842 if (Parent->kind() == K_BSD) {
843 // t.StringIndex is an offset from the start of the __.SYMDEF or
844 // "__.SYMDEF SORTED" member into the string table for the ranlib
845 // struct indexed by t.SymbolIndex . To change t.StringIndex to the
846 // offset in the string table for t.SymbolIndex+1 we subtract the
847 // its offset from the start of the string table for t.SymbolIndex
848 // and add the offset of the string table for t.SymbolIndex+1.
850 // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t
851 // which is the number of bytes of ranlib structs that follow. The ranlib
852 // structs are a pair of uint32_t's the first being a string table offset
853 // and the second being the offset into the archive of the member that
854 // define the symbol. After that the next uint32_t is the byte count of
855 // the string table followed by the string table.
856 const char *Buf = Parent->getSymbolTable().begin();
857 uint32_t RanlibCount = 0;
858 RanlibCount = read32le(Buf) / 8;
859 // If t.SymbolIndex + 1 will be past the count of symbols (the RanlibCount)
860 // don't change the t.StringIndex as we don't want to reference a ranlib
862 if (t.SymbolIndex + 1 < RanlibCount) {
863 const char *Ranlibs = Buf + 4;
864 uint32_t CurRanStrx = 0;
865 uint32_t NextRanStrx = 0;
866 CurRanStrx = read32le(Ranlibs + t.SymbolIndex * 8);
867 NextRanStrx = read32le(Ranlibs + (t.SymbolIndex + 1) * 8);
868 t.StringIndex -= CurRanStrx;
869 t.StringIndex += NextRanStrx;
872 // Go to one past next null.
873 t.StringIndex = Parent->getSymbolTable().find('\0', t.StringIndex) + 1;
879 Archive::symbol_iterator Archive::symbol_begin() const {
880 if (!hasSymbolTable())
881 return symbol_iterator(Symbol(this, 0, 0));
883 const char *buf = getSymbolTable().begin();
884 if (kind() == K_GNU) {
885 uint32_t symbol_count = 0;
886 symbol_count = read32be(buf);
887 buf += sizeof(uint32_t) + (symbol_count * (sizeof(uint32_t)));
888 } else if (kind() == K_MIPS64) {
889 uint64_t symbol_count = read64be(buf);
890 buf += sizeof(uint64_t) + (symbol_count * (sizeof(uint64_t)));
891 } else if (kind() == K_BSD) {
892 // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t
893 // which is the number of bytes of ranlib structs that follow. The ranlib
894 // structs are a pair of uint32_t's the first being a string table offset
895 // and the second being the offset into the archive of the member that
896 // define the symbol. After that the next uint32_t is the byte count of
897 // the string table followed by the string table.
898 uint32_t ranlib_count = 0;
899 ranlib_count = read32le(buf) / 8;
900 const char *ranlibs = buf + 4;
901 uint32_t ran_strx = 0;
902 ran_strx = read32le(ranlibs);
903 buf += sizeof(uint32_t) + (ranlib_count * (2 * (sizeof(uint32_t))));
904 // Skip the byte count of the string table.
905 buf += sizeof(uint32_t);
907 } else if (kind() == K_DARWIN64) {
908 // The __.SYMDEF_64 or "__.SYMDEF_64 SORTED" member starts with a uint64_t
909 // which is the number of bytes of ranlib_64 structs that follow. The
910 // ranlib_64 structs are a pair of uint64_t's the first being a string
911 // table offset and the second being the offset into the archive of the
912 // member that define the symbol. After that the next uint64_t is the byte
913 // count of the string table followed by the string table.
914 uint64_t ranlib_count = 0;
915 ranlib_count = read64le(buf) / 16;
916 const char *ranlibs = buf + 8;
917 uint64_t ran_strx = 0;
918 ran_strx = read64le(ranlibs);
919 buf += sizeof(uint64_t) + (ranlib_count * (2 * (sizeof(uint64_t))));
920 // Skip the byte count of the string table.
921 buf += sizeof(uint64_t);
924 uint32_t member_count = 0;
925 uint32_t symbol_count = 0;
926 member_count = read32le(buf);
927 buf += 4 + (member_count * 4); // Skip offsets.
928 symbol_count = read32le(buf);
929 buf += 4 + (symbol_count * 2); // Skip indices.
931 uint32_t string_start_offset = buf - getSymbolTable().begin();
932 return symbol_iterator(Symbol(this, 0, string_start_offset));
935 Archive::symbol_iterator Archive::symbol_end() const {
936 return symbol_iterator(Symbol(this, getNumberOfSymbols(), 0));
939 uint32_t Archive::getNumberOfSymbols() const {
940 if (!hasSymbolTable())
942 const char *buf = getSymbolTable().begin();
944 return read32be(buf);
945 if (kind() == K_MIPS64)
946 return read64be(buf);
948 return read32le(buf) / 8;
949 if (kind() == K_DARWIN64)
950 return read64le(buf) / 16;
951 uint32_t member_count = 0;
952 member_count = read32le(buf);
953 buf += 4 + (member_count * 4); // Skip offsets.
954 return read32le(buf);
957 Expected<Optional<Archive::Child>> Archive::findSym(StringRef name) const {
958 Archive::symbol_iterator bs = symbol_begin();
959 Archive::symbol_iterator es = symbol_end();
961 for (; bs != es; ++bs) {
962 StringRef SymName = bs->getName();
963 if (SymName == name) {
964 if (auto MemberOrErr = bs->getMember())
965 return Child(*MemberOrErr);
967 return MemberOrErr.takeError();
970 return Optional<Child>();
973 // Returns true if archive file contains no member file.
974 bool Archive::isEmpty() const { return Data.getBufferSize() == 8; }
976 bool Archive::hasSymbolTable() const { return !SymbolTable.empty(); }