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[] = {
30 StringRef name = amh.getName();
31 for (std::size_t i = 0; i < sizeof(internals) / sizeof(*internals); ++i) {
32 if (name == internals[i])
38 void Archive::anchor() { }
40 error_code Archive::Child::getName(StringRef &Result) const {
41 StringRef name = ToHeader(Data.data())->getName();
42 // Check if it's a special name.
44 if (name.size() == 1) { // Linker member.
46 return object_error::success;
48 if (name.size() == 2 && name[1] == '/') { // String table.
50 return object_error::success;
55 if (name.substr(1).rtrim(" ").getAsInteger(10, offset))
56 llvm_unreachable("Long name offset is not an integer");
57 const char *addr = Parent->StringTable->Data.begin()
58 + sizeof(ArchiveMemberHeader)
61 if (Parent->StringTable == Parent->end_children()
62 || addr < (Parent->StringTable->Data.begin()
63 + sizeof(ArchiveMemberHeader))
64 || addr > (Parent->StringTable->Data.begin()
65 + sizeof(ArchiveMemberHeader)
66 + Parent->StringTable->getSize()))
67 return object_error::parse_failed;
69 // GNU long file names end with a /.
70 if (Parent->kind() == K_GNU) {
71 StringRef::size_type End = StringRef(addr).find('/');
72 Result = StringRef(addr, End);
76 return object_error::success;
77 } else if (name.startswith("#1/")) {
79 if (name.substr(3).rtrim(" ").getAsInteger(10, name_size))
80 llvm_unreachable("Long name length is not an ingeter");
81 Result = Data.substr(sizeof(ArchiveMemberHeader), name_size);
82 return object_error::success;
84 // It's a simple name.
85 if (name[name.size() - 1] == '/')
86 Result = name.substr(0, name.size() - 1);
89 return object_error::success;
92 error_code Archive::Child::getAsBinary(OwningPtr<Binary> &Result) const {
93 OwningPtr<Binary> ret;
94 OwningPtr<MemoryBuffer> Buff;
95 if (error_code ec = getMemoryBuffer(Buff))
97 if (error_code ec = createBinary(Buff.take(), ret))
100 return object_error::success;
103 Archive::Archive(MemoryBuffer *source, error_code &ec)
104 : Binary(Binary::ID_Archive, source) {
105 // Check for sufficient magic.
106 if (!source || source->getBufferSize()
107 < (8 + sizeof(ArchiveMemberHeader) + 2) // Smallest archive.
108 || StringRef(source->getBufferStart(), 8) != Magic) {
109 ec = object_error::invalid_file_type;
113 // Get the special members.
114 child_iterator i = begin_children(false);
115 child_iterator e = end_children();
118 ec = object_error::parse_failed;
122 // FIXME: this function should be able to use raw names.
124 if ((ec = i->getName(name)))
127 // Below is the pattern that is used to figure out the archive format
128 // GNU archive format
129 // First member : / (may exist, if it exists, points to the symbol table )
130 // Second member : // (may exist, if it exists, points to the string table)
131 // Note : The string table is used if the filename exceeds 15 characters
132 // BSD archive format
133 // First member : __.SYMDEF (points to the symbol table)
134 // There is no string table, if the filename exceeds 15 characters or has a
135 // embedded space, the filename has #1/<size>, The size represents the size
136 // of the filename that needs to be read after the archive header
137 // COFF archive format
139 // Second member : / (provides a directory of symbols)
140 // Third member : // (may exist, if it exists, contains the string table)
141 // Note: Microsoft PE/COFF Spec 8.3 says that the third member is present
142 // even if the string table is empty. However, lib.exe does not in fact
143 // seem to create the third member if there's no member whose filename
144 // exceeds 15 characters. So the third member is optional.
146 if (name == "__.SYMDEF") {
149 ec = object_error::success;
158 ec = object_error::parse_failed;
161 if ((ec = i->getName(name)))
168 ec = object_error::success;
172 if (name[0] != '/') {
174 ec = object_error::success;
179 ec = object_error::parse_failed;
188 ec = object_error::success;
192 if ((ec = i->getName(name)))
198 ec = object_error::success;
201 Archive::child_iterator Archive::begin_children(bool skip_internal) const {
202 const char *Loc = Data->getBufferStart() + strlen(Magic);
203 size_t Size = sizeof(ArchiveMemberHeader) +
204 ToHeader(Loc)->getSize();
205 Child c(this, StringRef(Loc, Size));
206 // Skip internals at the beginning of an archive.
207 if (skip_internal && isInternalMember(*ToHeader(Loc)))
212 Archive::child_iterator Archive::end_children() const {
213 return Child(this, StringRef(0, 0));
216 error_code Archive::Symbol::getName(StringRef &Result) const {
217 Result = StringRef(Parent->SymbolTable->getBuffer().begin() + StringIndex);
218 return object_error::success;
221 error_code Archive::Symbol::getMember(child_iterator &Result) const {
222 const char *Buf = Parent->SymbolTable->getBuffer().begin();
223 const char *Offsets = Buf + 4;
225 if (Parent->kind() == K_GNU) {
226 Offset = *(reinterpret_cast<const support::ubig32_t*>(Offsets)
228 } else if (Parent->kind() == K_BSD) {
229 llvm_unreachable("BSD format is not supported");
231 uint32_t MemberCount = *reinterpret_cast<const support::ulittle32_t*>(Buf);
234 Buf += sizeof(support::ulittle32_t)
235 + (MemberCount * sizeof(support::ulittle32_t));
237 uint32_t SymbolCount = *reinterpret_cast<const support::ulittle32_t*>(Buf);
239 if (SymbolIndex >= SymbolCount)
240 return object_error::parse_failed;
242 // Skip SymbolCount to get to the indices table.
243 const char *Indices = Buf + sizeof(support::ulittle32_t);
245 // Get the index of the offset in the file member offset table for this
247 uint16_t OffsetIndex =
248 *(reinterpret_cast<const support::ulittle16_t*>(Indices)
250 // Subtract 1 since OffsetIndex is 1 based.
253 if (OffsetIndex >= MemberCount)
254 return object_error::parse_failed;
256 Offset = *(reinterpret_cast<const support::ulittle32_t*>(Offsets)
260 const char *Loc = Parent->getData().begin() + Offset;
261 size_t Size = sizeof(ArchiveMemberHeader) +
262 ToHeader(Loc)->getSize();
263 Result = Child(Parent, StringRef(Loc, Size));
265 return object_error::success;
268 Archive::Symbol Archive::Symbol::getNext() const {
270 // Go to one past next null.
272 Parent->SymbolTable->getBuffer().find('\0', t.StringIndex) + 1;
277 Archive::symbol_iterator Archive::begin_symbols() const {
278 const char *buf = SymbolTable->getBuffer().begin();
279 if (kind() == K_GNU) {
280 uint32_t symbol_count = 0;
281 symbol_count = *reinterpret_cast<const support::ubig32_t*>(buf);
282 buf += sizeof(uint32_t) + (symbol_count * (sizeof(uint32_t)));
283 } else if (kind() == K_BSD) {
284 llvm_unreachable("BSD archive format is not supported");
286 uint32_t member_count = 0;
287 uint32_t symbol_count = 0;
288 member_count = *reinterpret_cast<const support::ulittle32_t*>(buf);
289 buf += 4 + (member_count * 4); // Skip offsets.
290 symbol_count = *reinterpret_cast<const support::ulittle32_t*>(buf);
291 buf += 4 + (symbol_count * 2); // Skip indices.
293 uint32_t string_start_offset = buf - SymbolTable->getBuffer().begin();
294 return symbol_iterator(Symbol(this, 0, string_start_offset));
297 Archive::symbol_iterator Archive::end_symbols() const {
298 const char *buf = SymbolTable->getBuffer().begin();
299 uint32_t symbol_count = 0;
300 if (kind() == K_GNU) {
301 symbol_count = *reinterpret_cast<const support::ubig32_t*>(buf);
302 buf += sizeof(uint32_t) + (symbol_count * (sizeof(uint32_t)));
303 } else if (kind() == K_BSD) {
304 llvm_unreachable("BSD archive format is not supported");
306 uint32_t member_count = 0;
307 member_count = *reinterpret_cast<const support::ulittle32_t*>(buf);
308 buf += 4 + (member_count * 4); // Skip offsets.
309 symbol_count = *reinterpret_cast<const support::ulittle32_t*>(buf);
311 return symbol_iterator(
312 Symbol(this, symbol_count, 0));
315 Archive::child_iterator Archive::findSym(StringRef name) const {
316 Archive::symbol_iterator bs = begin_symbols();
317 Archive::symbol_iterator es = end_symbols();
318 Archive::child_iterator result;
321 for (; bs != es; ++bs) {
322 if (bs->getName(symname))
323 return end_children();
324 if (symname == name) {
325 if (bs->getMember(result))
326 return end_children();
330 return end_children();