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/APInt.h"
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/ADT/Twine.h"
18 #include "llvm/Support/Endian.h"
19 #include "llvm/Support/MemoryBuffer.h"
20 #include "llvm/Support/Path.h"
23 using namespace object;
24 using namespace llvm::support::endian;
26 static const char *const Magic = "!<arch>\n";
27 static const char *const ThinMagic = "!<thin>\n";
29 void Archive::anchor() { }
31 StringRef ArchiveMemberHeader::getName() const {
33 if (Name[0] == '/' || Name[0] == '#')
37 llvm::StringRef::size_type end =
38 llvm::StringRef(Name, sizeof(Name)).find(EndCond);
39 if (end == llvm::StringRef::npos)
41 assert(end <= sizeof(Name) && end > 0);
42 // Don't include the EndCond if there is one.
43 return llvm::StringRef(Name, end);
46 ErrorOr<uint32_t> ArchiveMemberHeader::getSize() const {
48 if (llvm::StringRef(Size, sizeof(Size)).rtrim(" ").getAsInteger(10, Ret))
49 return object_error::parse_failed; // Size is not a decimal number.
53 sys::fs::perms ArchiveMemberHeader::getAccessMode() const {
55 if (StringRef(AccessMode, sizeof(AccessMode)).rtrim(" ").getAsInteger(8, Ret))
56 llvm_unreachable("Access mode is not an octal number.");
57 return static_cast<sys::fs::perms>(Ret);
60 sys::TimeValue ArchiveMemberHeader::getLastModified() const {
62 if (StringRef(LastModified, sizeof(LastModified)).rtrim(" ")
63 .getAsInteger(10, Seconds))
64 llvm_unreachable("Last modified time not a decimal number.");
67 Ret.fromEpochTime(Seconds);
71 unsigned ArchiveMemberHeader::getUID() const {
73 if (StringRef(UID, sizeof(UID)).rtrim(" ").getAsInteger(10, Ret))
74 llvm_unreachable("UID time not a decimal number.");
78 unsigned ArchiveMemberHeader::getGID() const {
80 if (StringRef(GID, sizeof(GID)).rtrim(" ").getAsInteger(10, Ret))
81 llvm_unreachable("GID time not a decimal number.");
85 Archive::Child::Child(const Archive *Parent, StringRef Data,
87 : Parent(Parent), Data(Data), StartOfFile(StartOfFile) {}
89 Archive::Child::Child(const Archive *Parent, const char *Start,
95 uint64_t Size = sizeof(ArchiveMemberHeader);
96 Data = StringRef(Start, Size);
97 if (!isThinMember()) {
98 ErrorOr<uint64_t> MemberSize = getRawSize();
99 if ((*EC = MemberSize.getError()))
101 Size += MemberSize.get();
102 Data = StringRef(Start, Size);
105 // Setup StartOfFile and PaddingBytes.
106 StartOfFile = sizeof(ArchiveMemberHeader);
107 // Don't include attached name.
108 StringRef Name = getRawName();
109 if (Name.startswith("#1/")) {
111 if (Name.substr(3).rtrim(" ").getAsInteger(10, NameSize))
112 llvm_unreachable("Long name length is not an integer");
113 StartOfFile += NameSize;
117 ErrorOr<uint64_t> Archive::Child::getSize() const {
118 if (Parent->IsThin) {
119 ErrorOr<uint32_t> Size = getHeader()->getSize();
120 if (std::error_code EC = Size.getError())
124 return Data.size() - StartOfFile;
127 ErrorOr<uint64_t> Archive::Child::getRawSize() const {
128 ErrorOr<uint32_t> Size = getHeader()->getSize();
129 if (std::error_code EC = Size.getError())
134 bool Archive::Child::isThinMember() const {
135 StringRef Name = getHeader()->getName();
136 return Parent->IsThin && Name != "/" && Name != "//";
139 ErrorOr<StringRef> Archive::Child::getBuffer() const {
140 if (!isThinMember()) {
141 ErrorOr<uint32_t> Size = getSize();
142 if (std::error_code EC = Size.getError())
144 return StringRef(Data.data() + StartOfFile, Size.get());
146 ErrorOr<StringRef> Name = getName();
147 if (std::error_code EC = Name.getError())
149 SmallString<128> FullName = sys::path::parent_path(
150 Parent->getMemoryBufferRef().getBufferIdentifier());
151 sys::path::append(FullName, *Name);
152 ErrorOr<std::unique_ptr<MemoryBuffer>> Buf = MemoryBuffer::getFile(FullName);
153 if (std::error_code EC = Buf.getError())
155 Parent->ThinBuffers.push_back(std::move(*Buf));
156 return Parent->ThinBuffers.back()->getBuffer();
159 ErrorOr<Archive::Child> Archive::Child::getNext() const {
160 size_t SpaceToSkip = Data.size();
161 // If it's odd, add 1 to make it even.
165 const char *NextLoc = Data.data() + SpaceToSkip;
167 // Check to see if this is at the end of the archive.
168 if (NextLoc == Parent->Data.getBufferEnd())
169 return Child(Parent, nullptr, nullptr);
171 // Check to see if this is past the end of the archive.
172 if (NextLoc > Parent->Data.getBufferEnd())
173 return object_error::parse_failed;
176 Child Ret(Parent, NextLoc, &EC);
182 uint64_t Archive::Child::getChildOffset() const {
183 const char *a = Parent->Data.getBuffer().data();
184 const char *c = Data.data();
185 uint64_t offset = c - a;
189 ErrorOr<StringRef> Archive::Child::getName() const {
190 StringRef name = getRawName();
191 // Check if it's a special name.
192 if (name[0] == '/') {
193 if (name.size() == 1) // Linker member.
195 if (name.size() == 2 && name[1] == '/') // String table.
200 if (name.substr(1).rtrim(" ").getAsInteger(10, offset))
201 llvm_unreachable("Long name offset is not an integer");
204 if (offset >= Parent->StringTable.size())
205 return object_error::parse_failed;
206 const char *addr = Parent->StringTable.begin() + offset;
208 // GNU long file names end with a "/\n".
209 if (Parent->kind() == K_GNU || Parent->kind() == K_MIPS64) {
210 StringRef::size_type End = StringRef(addr).find('\n');
211 return StringRef(addr, End - 1);
213 return StringRef(addr);
214 } else if (name.startswith("#1/")) {
216 if (name.substr(3).rtrim(" ").getAsInteger(10, name_size))
217 llvm_unreachable("Long name length is not an ingeter");
218 return Data.substr(sizeof(ArchiveMemberHeader), name_size)
219 .rtrim(StringRef("\0", 1));
221 // It's a simple name.
222 if (name[name.size() - 1] == '/')
223 return name.substr(0, name.size() - 1);
227 ErrorOr<MemoryBufferRef> Archive::Child::getMemoryBufferRef() const {
228 ErrorOr<StringRef> NameOrErr = getName();
229 if (std::error_code EC = NameOrErr.getError())
231 StringRef Name = NameOrErr.get();
232 ErrorOr<StringRef> Buf = getBuffer();
233 if (std::error_code EC = Buf.getError())
235 return MemoryBufferRef(*Buf, Name);
238 ErrorOr<std::unique_ptr<Binary>>
239 Archive::Child::getAsBinary(LLVMContext *Context) const {
240 ErrorOr<MemoryBufferRef> BuffOrErr = getMemoryBufferRef();
241 if (std::error_code EC = BuffOrErr.getError())
244 return createBinary(BuffOrErr.get(), Context);
247 ErrorOr<std::unique_ptr<Archive>> Archive::create(MemoryBufferRef Source) {
249 std::unique_ptr<Archive> Ret(new Archive(Source, EC));
252 return std::move(Ret);
255 void Archive::setFirstRegular(const Child &C) {
256 FirstRegularData = C.Data;
257 FirstRegularStartOfFile = C.StartOfFile;
260 Archive::Archive(MemoryBufferRef Source, std::error_code &ec)
261 : Binary(Binary::ID_Archive, Source) {
262 StringRef Buffer = Data.getBuffer();
263 // Check for sufficient magic.
264 if (Buffer.startswith(ThinMagic)) {
266 } else if (Buffer.startswith(Magic)) {
269 ec = object_error::invalid_file_type;
273 // Get the special members.
274 child_iterator I = child_begin(false);
275 if ((ec = I->getError()))
277 child_iterator E = child_end();
280 ec = std::error_code();
283 const Child *C = &**I;
285 auto Increment = [&]() {
287 if ((ec = I->getError()))
293 StringRef Name = C->getRawName();
295 // Below is the pattern that is used to figure out the archive format
296 // GNU archive format
297 // First member : / (may exist, if it exists, points to the symbol table )
298 // Second member : // (may exist, if it exists, points to the string table)
299 // Note : The string table is used if the filename exceeds 15 characters
300 // BSD archive format
301 // First member : __.SYMDEF or "__.SYMDEF SORTED" (the symbol table)
302 // There is no string table, if the filename exceeds 15 characters or has a
303 // embedded space, the filename has #1/<size>, The size represents the size
304 // of the filename that needs to be read after the archive header
305 // COFF archive format
307 // Second member : / (provides a directory of symbols)
308 // Third member : // (may exist, if it exists, contains the string table)
309 // Note: Microsoft PE/COFF Spec 8.3 says that the third member is present
310 // even if the string table is empty. However, lib.exe does not in fact
311 // seem to create the third member if there's no member whose filename
312 // exceeds 15 characters. So the third member is optional.
314 if (Name == "__.SYMDEF") {
316 // We know that the symbol table is not an external file, so we just assert
317 // there is no error.
318 SymbolTable = *C->getBuffer();
323 ec = std::error_code();
327 if (Name.startswith("#1/")) {
329 // We know this is BSD, so getName will work since there is no string table.
330 ErrorOr<StringRef> NameOrErr = C->getName();
331 ec = NameOrErr.getError();
334 Name = NameOrErr.get();
335 if (Name == "__.SYMDEF SORTED" || Name == "__.SYMDEF") {
336 // We know that the symbol table is not an external file, so we just
337 // assert there is no error.
338 SymbolTable = *C->getBuffer();
346 // MIPS 64-bit ELF archives use a special format of a symbol table.
347 // This format is marked by `ar_name` field equals to "/SYM64/".
348 // For detailed description see page 96 in the following document:
349 // http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf
351 bool has64SymTable = false;
352 if (Name == "/" || Name == "/SYM64/") {
353 // We know that the symbol table is not an external file, so we just assert
354 // there is no error.
355 SymbolTable = *C->getBuffer();
356 if (Name == "/SYM64/")
357 has64SymTable = true;
362 ec = std::error_code();
365 Name = C->getRawName();
369 Format = has64SymTable ? K_MIPS64 : K_GNU;
370 // The string table is never an external member, so we just assert on the
372 StringTable = *C->getBuffer();
376 ec = std::error_code();
380 if (Name[0] != '/') {
381 Format = has64SymTable ? K_MIPS64 : K_GNU;
383 ec = std::error_code();
388 ec = object_error::parse_failed;
393 // We know that the symbol table is not an external file, so we just assert
394 // there is no error.
395 SymbolTable = *C->getBuffer();
402 ec = std::error_code();
406 Name = C->getRawName();
409 // The string table is never an external member, so we just assert on the
411 StringTable = *C->getBuffer();
417 ec = std::error_code();
420 Archive::child_iterator Archive::child_begin(bool SkipInternal) const {
421 if (Data.getBufferSize() == 8) // empty archive.
425 return Child(this, FirstRegularData, FirstRegularStartOfFile);
427 const char *Loc = Data.getBufferStart() + strlen(Magic);
429 Child c(this, Loc, &EC);
431 return child_iterator(EC);
432 return child_iterator(c);
435 Archive::child_iterator Archive::child_end() const {
436 return Child(this, nullptr, nullptr);
439 StringRef Archive::Symbol::getName() const {
440 return Parent->getSymbolTable().begin() + StringIndex;
443 ErrorOr<Archive::Child> Archive::Symbol::getMember() const {
444 const char *Buf = Parent->getSymbolTable().begin();
445 const char *Offsets = Buf;
446 if (Parent->kind() == K_MIPS64)
447 Offsets += sizeof(uint64_t);
449 Offsets += sizeof(uint32_t);
451 if (Parent->kind() == K_GNU) {
452 Offset = read32be(Offsets + SymbolIndex * 4);
453 } else if (Parent->kind() == K_MIPS64) {
454 Offset = read64be(Offsets + SymbolIndex * 8);
455 } else if (Parent->kind() == K_BSD) {
456 // The SymbolIndex is an index into the ranlib structs that start at
457 // Offsets (the first uint32_t is the number of bytes of the ranlib
458 // structs). The ranlib structs are a pair of uint32_t's the first
459 // being a string table offset and the second being the offset into
460 // the archive of the member that defines the symbol. Which is what
462 Offset = read32le(Offsets + SymbolIndex * 8 + 4);
465 uint32_t MemberCount = read32le(Buf);
466 Buf += MemberCount * 4 + 4;
468 uint32_t SymbolCount = read32le(Buf);
469 if (SymbolIndex >= SymbolCount)
470 return object_error::parse_failed;
472 // Skip SymbolCount to get to the indices table.
473 const char *Indices = Buf + 4;
475 // Get the index of the offset in the file member offset table for this
477 uint16_t OffsetIndex = read16le(Indices + SymbolIndex * 2);
478 // Subtract 1 since OffsetIndex is 1 based.
481 if (OffsetIndex >= MemberCount)
482 return object_error::parse_failed;
484 Offset = read32le(Offsets + OffsetIndex * 4);
487 const char *Loc = Parent->getData().begin() + Offset;
489 Child C(Parent, Loc, &EC);
495 Archive::Symbol Archive::Symbol::getNext() const {
497 if (Parent->kind() == K_BSD) {
498 // t.StringIndex is an offset from the start of the __.SYMDEF or
499 // "__.SYMDEF SORTED" member into the string table for the ranlib
500 // struct indexed by t.SymbolIndex . To change t.StringIndex to the
501 // offset in the string table for t.SymbolIndex+1 we subtract the
502 // its offset from the start of the string table for t.SymbolIndex
503 // and add the offset of the string table for t.SymbolIndex+1.
505 // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t
506 // which is the number of bytes of ranlib structs that follow. The ranlib
507 // structs are a pair of uint32_t's the first being a string table offset
508 // and the second being the offset into the archive of the member that
509 // define the symbol. After that the next uint32_t is the byte count of
510 // the string table followed by the string table.
511 const char *Buf = Parent->getSymbolTable().begin();
512 uint32_t RanlibCount = 0;
513 RanlibCount = read32le(Buf) / 8;
514 // If t.SymbolIndex + 1 will be past the count of symbols (the RanlibCount)
515 // don't change the t.StringIndex as we don't want to reference a ranlib
517 if (t.SymbolIndex + 1 < RanlibCount) {
518 const char *Ranlibs = Buf + 4;
519 uint32_t CurRanStrx = 0;
520 uint32_t NextRanStrx = 0;
521 CurRanStrx = read32le(Ranlibs + t.SymbolIndex * 8);
522 NextRanStrx = read32le(Ranlibs + (t.SymbolIndex + 1) * 8);
523 t.StringIndex -= CurRanStrx;
524 t.StringIndex += NextRanStrx;
527 // Go to one past next null.
528 t.StringIndex = Parent->getSymbolTable().find('\0', t.StringIndex) + 1;
534 Archive::symbol_iterator Archive::symbol_begin() const {
535 if (!hasSymbolTable())
536 return symbol_iterator(Symbol(this, 0, 0));
538 const char *buf = getSymbolTable().begin();
539 if (kind() == K_GNU) {
540 uint32_t symbol_count = 0;
541 symbol_count = read32be(buf);
542 buf += sizeof(uint32_t) + (symbol_count * (sizeof(uint32_t)));
543 } else if (kind() == K_MIPS64) {
544 uint64_t symbol_count = read64be(buf);
545 buf += sizeof(uint64_t) + (symbol_count * (sizeof(uint64_t)));
546 } else if (kind() == K_BSD) {
547 // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t
548 // which is the number of bytes of ranlib structs that follow. The ranlib
549 // structs are a pair of uint32_t's the first being a string table offset
550 // and the second being the offset into the archive of the member that
551 // define the symbol. After that the next uint32_t is the byte count of
552 // the string table followed by the string table.
553 uint32_t ranlib_count = 0;
554 ranlib_count = read32le(buf) / 8;
555 const char *ranlibs = buf + 4;
556 uint32_t ran_strx = 0;
557 ran_strx = read32le(ranlibs);
558 buf += sizeof(uint32_t) + (ranlib_count * (2 * (sizeof(uint32_t))));
559 // Skip the byte count of the string table.
560 buf += sizeof(uint32_t);
563 uint32_t member_count = 0;
564 uint32_t symbol_count = 0;
565 member_count = read32le(buf);
566 buf += 4 + (member_count * 4); // Skip offsets.
567 symbol_count = read32le(buf);
568 buf += 4 + (symbol_count * 2); // Skip indices.
570 uint32_t string_start_offset = buf - getSymbolTable().begin();
571 return symbol_iterator(Symbol(this, 0, string_start_offset));
574 Archive::symbol_iterator Archive::symbol_end() const {
575 return symbol_iterator(Symbol(this, getNumberOfSymbols(), 0));
578 uint32_t Archive::getNumberOfSymbols() const {
579 if (!hasSymbolTable())
581 const char *buf = getSymbolTable().begin();
583 return read32be(buf);
584 if (kind() == K_MIPS64)
585 return read64be(buf);
587 return read32le(buf) / 8;
588 uint32_t member_count = 0;
589 member_count = read32le(buf);
590 buf += 4 + (member_count * 4); // Skip offsets.
591 return read32le(buf);
594 Archive::child_iterator Archive::findSym(StringRef name) const {
595 Archive::symbol_iterator bs = symbol_begin();
596 Archive::symbol_iterator es = symbol_end();
598 for (; bs != es; ++bs) {
599 StringRef SymName = bs->getName();
600 if (SymName == name) {
601 ErrorOr<Archive::child_iterator> ResultOrErr = bs->getMember();
602 // FIXME: Should we really eat the error?
603 if (ResultOrErr.getError())
605 return ResultOrErr.get();
611 bool Archive::hasSymbolTable() const { return !SymbolTable.empty(); }