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/Support/Endian.h"
17 #include "llvm/Support/MemoryBuffer.h"
20 using namespace object;
22 static const char *Magic = "!<arch>\n";
24 static bool isInternalMember(const ArchiveMemberHeader &amh) {
25 static const char *const internals[] = {
31 StringRef name = amh.getName();
32 for (std::size_t i = 0; i < sizeof(internals) / sizeof(*internals); ++i) {
33 if (name == internals[i])
39 void Archive::anchor() { }
41 error_code Archive::Child::getName(StringRef &Result) const {
42 StringRef name = ToHeader(Data.data())->getName();
43 // Check if it's a special name.
45 if (name.size() == 1) { // Linker member.
47 return object_error::success;
49 if (name.size() == 2 && name[1] == '/') { // String table.
51 return object_error::success;
56 if (name.substr(1).rtrim(" ").getAsInteger(10, offset))
57 llvm_unreachable("Long name offset is not an integer");
58 const char *addr = Parent->StringTable->Data.begin()
59 + sizeof(ArchiveMemberHeader)
62 if (Parent->StringTable == Parent->end_children()
63 || addr < (Parent->StringTable->Data.begin()
64 + sizeof(ArchiveMemberHeader))
65 || addr > (Parent->StringTable->Data.begin()
66 + sizeof(ArchiveMemberHeader)
67 + Parent->StringTable->getSize()))
68 return object_error::parse_failed;
70 // GNU long file names end with a /.
71 if (Parent->kind() == K_GNU) {
72 StringRef::size_type End = StringRef(addr).find('/');
73 Result = StringRef(addr, End);
77 return object_error::success;
78 } else if (name.startswith("#1/")) {
80 if (name.substr(3).rtrim(" ").getAsInteger(10, name_size))
81 llvm_unreachable("Long name length is not an ingeter");
82 Result = Data.substr(sizeof(ArchiveMemberHeader), name_size);
83 return object_error::success;
85 // It's a simple name.
86 if (name[name.size() - 1] == '/')
87 Result = name.substr(0, name.size() - 1);
90 return object_error::success;
93 error_code Archive::Child::getAsBinary(OwningPtr<Binary> &Result) const {
94 OwningPtr<Binary> ret;
95 OwningPtr<MemoryBuffer> Buff;
96 if (error_code ec = getMemoryBuffer(Buff))
98 if (error_code ec = createBinary(Buff.take(), ret))
101 return object_error::success;
104 Archive::Archive(MemoryBuffer *source, error_code &ec)
105 : Binary(Binary::ID_Archive, source) {
106 // Check for sufficient magic.
107 if (!source || source->getBufferSize()
108 < (8 + sizeof(ArchiveMemberHeader) + 2) // Smallest archive.
109 || StringRef(source->getBufferStart(), 8) != Magic) {
110 ec = object_error::invalid_file_type;
114 // Get the special members.
115 child_iterator i = begin_children(false);
116 child_iterator e = end_children();
119 if ((ec = i->getName(name)))
122 // Below is the pattern that is used to figure out the archive format
123 // GNU archive format
124 // First member : / (points to the symbol table )
125 // Second member : // (may exist, if it exists, points to the string table)
126 // Note : The string table is used if the filename exceeds 15 characters
127 // BSD archive format
128 // First member : __.SYMDEF (points to the symbol table)
129 // There is no string table, if the filename exceeds 15 characters or has a
130 // embedded space, the filename has #1/<size>, The size represents the size
131 // of the filename that needs to be read after the archive header
132 // COFF archive format
134 // Second member : / (provides a directory of symbols)
135 // Third member : // (may exist, if it exists, contains the string table)
136 // Note: Microsoft PE/COFF Spec 8.3 says that the third member is present
137 // even if the string table is empty. However, lib.exe does not in fact
138 // seem to create the third member if there's no member whose filename
139 // exceeds 15 characters. So the third member is optional.
145 ec = object_error::parse_failed;
148 if ((ec = i->getName(name)))
150 if (name[0] != '/') {
152 } else if ((name.size() > 1) && (name == "//")) {
163 if ((ec = i->getName(name)))
169 } else if (name == "__.SYMDEF") {
174 ec = object_error::success;
177 Archive::child_iterator Archive::begin_children(bool skip_internal) const {
178 const char *Loc = Data->getBufferStart() + strlen(Magic);
179 size_t Size = sizeof(ArchiveMemberHeader) +
180 ToHeader(Loc)->getSize();
181 Child c(this, StringRef(Loc, Size));
182 // Skip internals at the beginning of an archive.
183 if (skip_internal && isInternalMember(*ToHeader(Loc)))
188 Archive::child_iterator Archive::end_children() const {
189 return Child(this, StringRef(0, 0));
192 error_code Archive::Symbol::getName(StringRef &Result) const {
193 Result = StringRef(Parent->SymbolTable->getBuffer().begin() + StringIndex);
194 return object_error::success;
197 error_code Archive::Symbol::getMember(child_iterator &Result) const {
198 const char *Buf = Parent->SymbolTable->getBuffer().begin();
199 const char *Offsets = Buf + 4;
201 if (Parent->kind() == K_GNU) {
202 Offset = *(reinterpret_cast<const support::ubig32_t*>(Offsets)
204 } else if (Parent->kind() == K_BSD) {
205 llvm_unreachable("BSD format is not supported");
207 uint32_t MemberCount = *reinterpret_cast<const support::ulittle32_t*>(Buf);
210 Buf += sizeof(support::ulittle32_t)
211 + (MemberCount * sizeof(support::ulittle32_t));
213 uint32_t SymbolCount = *reinterpret_cast<const support::ulittle32_t*>(Buf);
215 if (SymbolIndex >= SymbolCount)
216 return object_error::parse_failed;
218 // Skip SymbolCount to get to the indices table.
219 const char *Indices = Buf + sizeof(support::ulittle32_t);
221 // Get the index of the offset in the file member offset table for this
223 uint16_t OffsetIndex =
224 *(reinterpret_cast<const support::ulittle16_t*>(Indices)
226 // Subtract 1 since OffsetIndex is 1 based.
229 if (OffsetIndex >= MemberCount)
230 return object_error::parse_failed;
232 Offset = *(reinterpret_cast<const support::ulittle32_t*>(Offsets)
236 const char *Loc = Parent->getData().begin() + Offset;
237 size_t Size = sizeof(ArchiveMemberHeader) +
238 ToHeader(Loc)->getSize();
239 Result = Child(Parent, StringRef(Loc, Size));
241 return object_error::success;
244 Archive::Symbol Archive::Symbol::getNext() const {
246 // Go to one past next null.
248 Parent->SymbolTable->getBuffer().find('\0', t.StringIndex) + 1;
253 Archive::symbol_iterator Archive::begin_symbols() const {
254 const char *buf = SymbolTable->getBuffer().begin();
255 if (kind() == K_GNU) {
256 uint32_t symbol_count = 0;
257 symbol_count = *reinterpret_cast<const support::ubig32_t*>(buf);
258 buf += sizeof(uint32_t) + (symbol_count * (sizeof(uint32_t)));
259 } else if (kind() == K_BSD) {
260 llvm_unreachable("BSD archive format is not supported");
262 uint32_t member_count = 0;
263 uint32_t symbol_count = 0;
264 member_count = *reinterpret_cast<const support::ulittle32_t*>(buf);
265 buf += 4 + (member_count * 4); // Skip offsets.
266 symbol_count = *reinterpret_cast<const support::ulittle32_t*>(buf);
267 buf += 4 + (symbol_count * 2); // Skip indices.
269 uint32_t string_start_offset = buf - SymbolTable->getBuffer().begin();
270 return symbol_iterator(Symbol(this, 0, string_start_offset));
273 Archive::symbol_iterator Archive::end_symbols() const {
274 const char *buf = SymbolTable->getBuffer().begin();
275 uint32_t symbol_count = 0;
276 if (kind() == K_GNU) {
277 symbol_count = *reinterpret_cast<const support::ubig32_t*>(buf);
278 buf += sizeof(uint32_t) + (symbol_count * (sizeof(uint32_t)));
279 } else if (kind() == K_BSD) {
280 llvm_unreachable("BSD archive format is not supported");
282 uint32_t member_count = 0;
283 member_count = *reinterpret_cast<const support::ulittle32_t*>(buf);
284 buf += 4 + (member_count * 4); // Skip offsets.
285 symbol_count = *reinterpret_cast<const support::ulittle32_t*>(buf);
287 return symbol_iterator(
288 Symbol(this, symbol_count, 0));
291 Archive::child_iterator Archive::findSym(StringRef name) const {
292 Archive::symbol_iterator bs = begin_symbols();
293 Archive::symbol_iterator es = end_symbols();
294 Archive::child_iterator result;
297 for (; bs != es; ++bs) {
298 if (bs->getName(symname))
299 return end_children();
300 if (symname == name) {
301 if (bs->getMember(result))
302 return end_children();
306 return end_children();