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"
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() { }
30 StringRef ArchiveMemberHeader::getName() const {
32 if (Name[0] == '/' || Name[0] == '#')
36 llvm::StringRef::size_type end =
37 llvm::StringRef(Name, sizeof(Name)).find(EndCond);
38 if (end == llvm::StringRef::npos)
40 assert(end <= sizeof(Name) && end > 0);
41 // Don't include the EndCond if there is one.
42 return llvm::StringRef(Name, end);
45 uint32_t ArchiveMemberHeader::getSize() const {
47 if (llvm::StringRef(Size, sizeof(Size)).rtrim(" ").getAsInteger(10, Ret))
48 llvm_unreachable("Size is not a decimal number.");
52 sys::fs::perms ArchiveMemberHeader::getAccessMode() const {
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);
59 sys::TimeValue ArchiveMemberHeader::getLastModified() const {
61 if (StringRef(LastModified, sizeof(LastModified)).rtrim(" ")
62 .getAsInteger(10, Seconds))
63 llvm_unreachable("Last modified time not a decimal number.");
66 Ret.fromEpochTime(Seconds);
70 unsigned ArchiveMemberHeader::getUID() const {
72 if (StringRef(UID, sizeof(UID)).rtrim(" ").getAsInteger(10, Ret))
73 llvm_unreachable("UID time not a decimal number.");
77 unsigned ArchiveMemberHeader::getGID() const {
79 if (StringRef(GID, sizeof(GID)).rtrim(" ").getAsInteger(10, Ret))
80 llvm_unreachable("GID time not a decimal number.");
84 Archive::Child::Child(const Archive *Parent, const char *Start)
89 const ArchiveMemberHeader *Header =
90 reinterpret_cast<const ArchiveMemberHeader *>(Start);
91 uint64_t Size = sizeof(ArchiveMemberHeader);
92 if (!Parent->IsThin || Header->getName() == "/" || Header->getName() == "//")
93 Size += Header->getSize();
94 Data = StringRef(Start, Size);
96 // Setup StartOfFile and PaddingBytes.
97 StartOfFile = sizeof(ArchiveMemberHeader);
98 // Don't include attached name.
99 StringRef Name = Header->getName();
100 if (Name.startswith("#1/")) {
102 if (Name.substr(3).rtrim(" ").getAsInteger(10, NameSize))
103 llvm_unreachable("Long name length is not an integer");
104 StartOfFile += NameSize;
108 uint64_t Archive::Child::getSize() const {
110 return getHeader()->getSize();
111 return Data.size() - StartOfFile;
114 uint64_t Archive::Child::getRawSize() const {
115 return getHeader()->getSize();
118 Archive::Child Archive::Child::getNext() const {
119 size_t SpaceToSkip = Data.size();
120 // If it's odd, add 1 to make it even.
124 const char *NextLoc = Data.data() + SpaceToSkip;
126 // Check to see if this is past the end of the archive.
127 if (NextLoc >= Parent->Data.getBufferEnd())
128 return Child(Parent, nullptr);
130 return Child(Parent, NextLoc);
133 uint64_t Archive::Child::getChildOffset() const {
134 const char *a = Parent->Data.getBuffer().data();
135 const char *c = Data.data();
136 uint64_t offset = c - a;
140 ErrorOr<StringRef> Archive::Child::getName() const {
141 StringRef name = getRawName();
142 // Check if it's a special name.
143 if (name[0] == '/') {
144 if (name.size() == 1) // Linker member.
146 if (name.size() == 2 && name[1] == '/') // String table.
151 if (name.substr(1).rtrim(" ").getAsInteger(10, offset))
152 llvm_unreachable("Long name offset is not an integer");
153 const char *addr = Parent->StringTable->Data.begin()
154 + sizeof(ArchiveMemberHeader)
157 if (Parent->StringTable == Parent->child_end()
158 || addr < (Parent->StringTable->Data.begin()
159 + sizeof(ArchiveMemberHeader))
160 || addr > (Parent->StringTable->Data.begin()
161 + sizeof(ArchiveMemberHeader)
162 + Parent->StringTable->getSize()))
163 return object_error::parse_failed;
165 // GNU long file names end with a /.
166 if (Parent->kind() == K_GNU || Parent->kind() == K_MIPS64) {
167 StringRef::size_type End = StringRef(addr).find('/');
168 return StringRef(addr, End);
170 return StringRef(addr);
171 } else if (name.startswith("#1/")) {
173 if (name.substr(3).rtrim(" ").getAsInteger(10, name_size))
174 llvm_unreachable("Long name length is not an ingeter");
175 return Data.substr(sizeof(ArchiveMemberHeader), name_size)
176 .rtrim(StringRef("\0", 1));
178 // It's a simple name.
179 if (name[name.size() - 1] == '/')
180 return name.substr(0, name.size() - 1);
184 ErrorOr<MemoryBufferRef> Archive::Child::getMemoryBufferRef() const {
185 ErrorOr<StringRef> NameOrErr = getName();
186 if (std::error_code EC = NameOrErr.getError())
188 StringRef Name = NameOrErr.get();
189 return MemoryBufferRef(getBuffer(), Name);
192 ErrorOr<std::unique_ptr<Binary>>
193 Archive::Child::getAsBinary(LLVMContext *Context) const {
194 ErrorOr<MemoryBufferRef> BuffOrErr = getMemoryBufferRef();
195 if (std::error_code EC = BuffOrErr.getError())
198 return createBinary(BuffOrErr.get(), Context);
201 ErrorOr<std::unique_ptr<Archive>> Archive::create(MemoryBufferRef Source) {
203 std::unique_ptr<Archive> Ret(new Archive(Source, EC));
206 return std::move(Ret);
209 Archive::Archive(MemoryBufferRef Source, std::error_code &ec)
210 : Binary(Binary::ID_Archive, Source), SymbolTable(child_end()),
211 StringTable(child_end()), FirstRegular(child_end()) {
212 StringRef Buffer = Data.getBuffer();
213 // Check for sufficient magic.
214 if (Buffer.startswith(ThinMagic)) {
216 } else if (Buffer.startswith(Magic)) {
219 ec = object_error::invalid_file_type;
223 // Get the special members.
224 child_iterator i = child_begin(false);
225 child_iterator e = child_end();
228 ec = std::error_code();
232 StringRef Name = i->getRawName();
234 // Below is the pattern that is used to figure out the archive format
235 // GNU archive format
236 // First member : / (may exist, if it exists, points to the symbol table )
237 // Second member : // (may exist, if it exists, points to the string table)
238 // Note : The string table is used if the filename exceeds 15 characters
239 // BSD archive format
240 // First member : __.SYMDEF or "__.SYMDEF SORTED" (the symbol table)
241 // There is no string table, if the filename exceeds 15 characters or has a
242 // embedded space, the filename has #1/<size>, The size represents the size
243 // of the filename that needs to be read after the archive header
244 // COFF archive format
246 // Second member : / (provides a directory of symbols)
247 // Third member : // (may exist, if it exists, contains the string table)
248 // Note: Microsoft PE/COFF Spec 8.3 says that the third member is present
249 // even if the string table is empty. However, lib.exe does not in fact
250 // seem to create the third member if there's no member whose filename
251 // exceeds 15 characters. So the third member is optional.
253 if (Name == "__.SYMDEF") {
258 ec = std::error_code();
262 if (Name.startswith("#1/")) {
264 // We know this is BSD, so getName will work since there is no string table.
265 ErrorOr<StringRef> NameOrErr = i->getName();
266 ec = NameOrErr.getError();
269 Name = NameOrErr.get();
270 if (Name == "__.SYMDEF SORTED" || Name == "__.SYMDEF") {
278 // MIPS 64-bit ELF archives use a special format of a symbol table.
279 // This format is marked by `ar_name` field equals to "/SYM64/".
280 // For detailed description see page 96 in the following document:
281 // http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf
283 bool has64SymTable = false;
284 if (Name == "/" || Name == "/SYM64/") {
286 if (Name == "/SYM64/")
287 has64SymTable = true;
291 ec = std::error_code();
294 Name = i->getRawName();
298 Format = has64SymTable ? K_MIPS64 : K_GNU;
302 ec = std::error_code();
306 if (Name[0] != '/') {
307 Format = has64SymTable ? K_MIPS64 : K_GNU;
309 ec = std::error_code();
314 ec = object_error::parse_failed;
324 ec = std::error_code();
328 Name = i->getRawName();
336 ec = std::error_code();
339 Archive::child_iterator Archive::child_begin(bool SkipInternal) const {
340 if (Data.getBufferSize() == 8) // empty archive.
346 const char *Loc = Data.getBufferStart() + strlen(Magic);
351 Archive::child_iterator Archive::child_end() const {
352 return Child(this, nullptr);
355 StringRef Archive::Symbol::getName() const {
356 return Parent->SymbolTable->getBuffer().begin() + StringIndex;
359 ErrorOr<Archive::child_iterator> Archive::Symbol::getMember() const {
360 const char *Buf = Parent->SymbolTable->getBuffer().begin();
361 const char *Offsets = Buf;
362 if (Parent->kind() == K_MIPS64)
363 Offsets += sizeof(uint64_t);
365 Offsets += sizeof(uint32_t);
367 if (Parent->kind() == K_GNU) {
368 Offset = read32be(Offsets + SymbolIndex * 4);
369 } else if (Parent->kind() == K_MIPS64) {
370 Offset = read64be(Offsets + SymbolIndex * 8);
371 } else if (Parent->kind() == K_BSD) {
372 // The SymbolIndex is an index into the ranlib structs that start at
373 // Offsets (the first uint32_t is the number of bytes of the ranlib
374 // structs). The ranlib structs are a pair of uint32_t's the first
375 // being a string table offset and the second being the offset into
376 // the archive of the member that defines the symbol. Which is what
378 Offset = read32le(Offsets + SymbolIndex * 8 + 4);
381 uint32_t MemberCount = read32le(Buf);
382 Buf += MemberCount * 4 + 4;
384 uint32_t SymbolCount = read32le(Buf);
385 if (SymbolIndex >= SymbolCount)
386 return object_error::parse_failed;
388 // Skip SymbolCount to get to the indices table.
389 const char *Indices = Buf + 4;
391 // Get the index of the offset in the file member offset table for this
393 uint16_t OffsetIndex = read16le(Indices + SymbolIndex * 2);
394 // Subtract 1 since OffsetIndex is 1 based.
397 if (OffsetIndex >= MemberCount)
398 return object_error::parse_failed;
400 Offset = read32le(Offsets + OffsetIndex * 4);
403 const char *Loc = Parent->getData().begin() + Offset;
404 child_iterator Iter(Child(Parent, Loc));
408 Archive::Symbol Archive::Symbol::getNext() const {
410 if (Parent->kind() == K_BSD) {
411 // t.StringIndex is an offset from the start of the __.SYMDEF or
412 // "__.SYMDEF SORTED" member into the string table for the ranlib
413 // struct indexed by t.SymbolIndex . To change t.StringIndex to the
414 // offset in the string table for t.SymbolIndex+1 we subtract the
415 // its offset from the start of the string table for t.SymbolIndex
416 // and add the offset of the string table for t.SymbolIndex+1.
418 // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t
419 // which is the number of bytes of ranlib structs that follow. The ranlib
420 // structs are a pair of uint32_t's the first being a string table offset
421 // and the second being the offset into the archive of the member that
422 // define the symbol. After that the next uint32_t is the byte count of
423 // the string table followed by the string table.
424 const char *Buf = Parent->SymbolTable->getBuffer().begin();
425 uint32_t RanlibCount = 0;
426 RanlibCount = read32le(Buf) / 8;
427 // If t.SymbolIndex + 1 will be past the count of symbols (the RanlibCount)
428 // don't change the t.StringIndex as we don't want to reference a ranlib
430 if (t.SymbolIndex + 1 < RanlibCount) {
431 const char *Ranlibs = Buf + 4;
432 uint32_t CurRanStrx = 0;
433 uint32_t NextRanStrx = 0;
434 CurRanStrx = read32le(Ranlibs + t.SymbolIndex * 8);
435 NextRanStrx = read32le(Ranlibs + (t.SymbolIndex + 1) * 8);
436 t.StringIndex -= CurRanStrx;
437 t.StringIndex += NextRanStrx;
440 // Go to one past next null.
442 Parent->SymbolTable->getBuffer().find('\0', t.StringIndex) + 1;
448 Archive::symbol_iterator Archive::symbol_begin() const {
449 if (!hasSymbolTable())
450 return symbol_iterator(Symbol(this, 0, 0));
452 const char *buf = SymbolTable->getBuffer().begin();
453 if (kind() == K_GNU) {
454 uint32_t symbol_count = 0;
455 symbol_count = read32be(buf);
456 buf += sizeof(uint32_t) + (symbol_count * (sizeof(uint32_t)));
457 } else if (kind() == K_MIPS64) {
458 uint64_t symbol_count = read64be(buf);
459 buf += sizeof(uint64_t) + (symbol_count * (sizeof(uint64_t)));
460 } else if (kind() == K_BSD) {
461 // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t
462 // which is the number of bytes of ranlib structs that follow. The ranlib
463 // structs are a pair of uint32_t's the first being a string table offset
464 // and the second being the offset into the archive of the member that
465 // define the symbol. After that the next uint32_t is the byte count of
466 // the string table followed by the string table.
467 uint32_t ranlib_count = 0;
468 ranlib_count = read32le(buf) / 8;
469 const char *ranlibs = buf + 4;
470 uint32_t ran_strx = 0;
471 ran_strx = read32le(ranlibs);
472 buf += sizeof(uint32_t) + (ranlib_count * (2 * (sizeof(uint32_t))));
473 // Skip the byte count of the string table.
474 buf += sizeof(uint32_t);
477 uint32_t member_count = 0;
478 uint32_t symbol_count = 0;
479 member_count = read32le(buf);
480 buf += 4 + (member_count * 4); // Skip offsets.
481 symbol_count = read32le(buf);
482 buf += 4 + (symbol_count * 2); // Skip indices.
484 uint32_t string_start_offset = buf - SymbolTable->getBuffer().begin();
485 return symbol_iterator(Symbol(this, 0, string_start_offset));
488 Archive::symbol_iterator Archive::symbol_end() const {
489 if (!hasSymbolTable())
490 return symbol_iterator(Symbol(this, 0, 0));
491 return symbol_iterator(Symbol(this, getNumberOfSymbols(), 0));
494 uint32_t Archive::getNumberOfSymbols() const {
495 const char *buf = SymbolTable->getBuffer().begin();
497 return read32be(buf);
498 if (kind() == K_MIPS64)
499 return read64be(buf);
501 return read32le(buf) / 8;
502 uint32_t member_count = 0;
503 member_count = read32le(buf);
504 buf += 4 + (member_count * 4); // Skip offsets.
505 return read32le(buf);
508 Archive::child_iterator Archive::findSym(StringRef name) const {
509 Archive::symbol_iterator bs = symbol_begin();
510 Archive::symbol_iterator es = symbol_end();
512 for (; bs != es; ++bs) {
513 StringRef SymName = bs->getName();
514 if (SymName == name) {
515 ErrorOr<Archive::child_iterator> ResultOrErr = bs->getMember();
516 // FIXME: Should we really eat the error?
517 if (ResultOrErr.getError())
519 return ResultOrErr.get();
525 bool Archive::hasSymbolTable() const {
526 return SymbolTable != child_end();