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/ErrorHandling.h"
18 #include "llvm/Support/MemoryBuffer.h"
21 using namespace object;
23 static const char *Magic = "!<arch>\n";
26 struct ArchiveMemberHeader {
28 char LastModified[12];
32 char Size[10]; ///< Size of data, not including header or padding.
35 ///! Get the name without looking up long names.
36 StringRef getName() const {
38 if (Name[0] == '/' || Name[0] == '#')
42 StringRef::size_type end = StringRef(Name, sizeof(Name)).find(EndCond);
43 if (end == StringRef::npos)
45 assert(end <= sizeof(Name) && end > 0);
46 // Don't include the EndCond if there is one.
47 return StringRef(Name, end);
50 uint64_t getSize() const {
52 if (StringRef(Size, sizeof(Size)).rtrim(" ").getAsInteger(10, ret))
53 llvm_unreachable("Size is not an integer.");
59 static const ArchiveMemberHeader *ToHeader(const char *base) {
60 return reinterpret_cast<const ArchiveMemberHeader *>(base);
64 static bool isInternalMember(const ArchiveMemberHeader &amh) {
65 static const char *const internals[] = {
71 StringRef name = amh.getName();
72 for (std::size_t i = 0; i < sizeof(internals) / sizeof(*internals); ++i) {
73 if (name == internals[i])
79 void Archive::anchor() { }
81 Archive::Child Archive::Child::getNext() const {
82 size_t SpaceToSkip = sizeof(ArchiveMemberHeader) +
83 ToHeader(Data.data())->getSize();
84 // If it's odd, add 1 to make it even.
88 const char *NextLoc = Data.data() + SpaceToSkip;
90 // Check to see if this is past the end of the archive.
91 if (NextLoc >= Parent->Data->getBufferEnd())
92 return Child(Parent, StringRef(0, 0));
94 size_t NextSize = sizeof(ArchiveMemberHeader) +
95 ToHeader(NextLoc)->getSize();
97 return Child(Parent, StringRef(NextLoc, NextSize));
100 error_code Archive::Child::getName(StringRef &Result) const {
101 StringRef name = ToHeader(Data.data())->getName();
102 // Check if it's a special name.
103 if (name[0] == '/') {
104 if (name.size() == 1) { // Linker member.
106 return object_error::success;
108 if (name.size() == 2 && name[1] == '/') { // String table.
110 return object_error::success;
115 if (name.substr(1).rtrim(" ").getAsInteger(10, offset))
116 llvm_unreachable("Long name offset is not an integer");
117 const char *addr = Parent->StringTable->Data.begin()
118 + sizeof(ArchiveMemberHeader)
121 if (Parent->StringTable == Parent->end_children()
122 || addr < (Parent->StringTable->Data.begin()
123 + sizeof(ArchiveMemberHeader))
124 || addr > (Parent->StringTable->Data.begin()
125 + sizeof(ArchiveMemberHeader)
126 + Parent->StringTable->getSize()))
127 return object_error::parse_failed;
129 // GNU long file names end with a /.
130 if (Parent->kind() == K_GNU) {
131 StringRef::size_type End = StringRef(addr).find('/');
132 Result = StringRef(addr, End);
136 return object_error::success;
137 } else if (name.startswith("#1/")) {
139 if (name.substr(3).rtrim(" ").getAsInteger(10, name_size))
140 llvm_unreachable("Long name length is not an ingeter");
141 Result = Data.substr(sizeof(ArchiveMemberHeader), name_size);
142 return object_error::success;
144 // It's a simple name.
145 if (name[name.size() - 1] == '/')
146 Result = name.substr(0, name.size() - 1);
149 return object_error::success;
152 uint64_t Archive::Child::getSize() const {
153 uint64_t size = ToHeader(Data.data())->getSize();
154 // Don't include attached name.
155 StringRef name = ToHeader(Data.data())->getName();
156 if (name.startswith("#1/")) {
158 if (name.substr(3).rtrim(" ").getAsInteger(10, name_size))
159 llvm_unreachable("Long name length is not an integer");
165 MemoryBuffer *Archive::Child::getBuffer() const {
166 StringRef name = ToHeader(Data.data())->getName();
167 int size = sizeof(ArchiveMemberHeader);
168 if (name.startswith("#1/")) {
170 if (name.substr(3).rtrim(" ").getAsInteger(10, name_size))
171 llvm_unreachable("Long name length is not an integer");
176 return MemoryBuffer::getMemBuffer(Data.substr(size, getSize()),
181 error_code Archive::Child::getAsBinary(OwningPtr<Binary> &Result) const {
182 OwningPtr<Binary> ret;
184 createBinary(getBuffer(), ret))
187 return object_error::success;
190 Archive::Archive(MemoryBuffer *source, error_code &ec)
191 : Binary(Binary::ID_Archive, source) {
192 // Check for sufficient magic.
193 if (!source || source->getBufferSize()
194 < (8 + sizeof(ArchiveMemberHeader) + 2) // Smallest archive.
195 || StringRef(source->getBufferStart(), 8) != Magic) {
196 ec = object_error::invalid_file_type;
200 // Get the special members.
201 child_iterator i = begin_children(false);
202 child_iterator e = end_children();
205 if ((ec = i->getName(name)))
208 // Below is the pattern that is used to figure out the archive format
209 // GNU archive format
210 // First member : / (points to the symbol table )
211 // Second member : // (may exist, if it exists, points to the string table)
212 // Note : The string table is used if the filename exceeds 15 characters
213 // BSD archive format
214 // First member : __.SYMDEF (points to the symbol table)
215 // There is no string table, if the filename exceeds 15 characters or has a
216 // embedded space, the filename has #1/<size>, The size represents the size
217 // of the filename that needs to be read after the archive header
218 // COFF archive format
220 // Second member : / (provides a directory of symbols)
221 // Third member : // contains the string table, this is present even if the
222 // string table is empty
228 ec = object_error::parse_failed;
231 if ((ec = i->getName(name)))
233 if (name[0] != '/') {
235 } else if ((name.size() > 1) && (name == "//")) {
249 } else if (name == "__.SYMDEF") {
254 ec = object_error::success;
257 Archive::child_iterator Archive::begin_children(bool skip_internal) const {
258 const char *Loc = Data->getBufferStart() + strlen(Magic);
259 size_t Size = sizeof(ArchiveMemberHeader) +
260 ToHeader(Loc)->getSize();
261 Child c(this, StringRef(Loc, Size));
262 // Skip internals at the beginning of an archive.
263 if (skip_internal && isInternalMember(*ToHeader(Loc)))
268 Archive::child_iterator Archive::end_children() const {
269 return Child(this, StringRef(0, 0));
272 error_code Archive::Symbol::getName(StringRef &Result) const {
274 StringRef(Parent->SymbolTable->getBuffer()->getBufferStart() + StringIndex);
275 return object_error::success;
278 error_code Archive::Symbol::getMember(child_iterator &Result) const {
279 const char *Buf = Parent->SymbolTable->getBuffer()->getBufferStart();
280 const char *Offsets = Buf + 4;
282 if (Parent->kind() == K_GNU) {
283 Offset = *(reinterpret_cast<const support::ubig32_t*>(Offsets)
285 } else if (Parent->kind() == K_BSD) {
286 llvm_unreachable("BSD format is not supported");
288 uint32_t MemberCount = *reinterpret_cast<const support::ulittle32_t*>(Buf);
291 Buf += sizeof(support::ulittle32_t)
292 + (MemberCount * sizeof(support::ulittle32_t));
294 uint32_t SymbolCount = *reinterpret_cast<const support::ulittle32_t*>(Buf);
296 if (SymbolIndex >= SymbolCount)
297 return object_error::parse_failed;
299 // Skip SymbolCount to get to the indices table.
300 const char *Indices = Buf + sizeof(support::ulittle32_t);
302 // Get the index of the offset in the file member offset table for this
304 uint16_t OffsetIndex =
305 *(reinterpret_cast<const support::ulittle16_t*>(Indices)
307 // Subtract 1 since OffsetIndex is 1 based.
310 if (OffsetIndex >= MemberCount)
311 return object_error::parse_failed;
313 Offset = *(reinterpret_cast<const support::ulittle32_t*>(Offsets)
317 const char *Loc = Parent->getData().begin() + Offset;
318 size_t Size = sizeof(ArchiveMemberHeader) +
319 ToHeader(Loc)->getSize();
320 Result = Child(Parent, StringRef(Loc, Size));
322 return object_error::success;
325 Archive::Symbol Archive::Symbol::getNext() const {
327 // Go to one past next null.
329 Parent->SymbolTable->getBuffer()->getBuffer().find('\0', t.StringIndex) + 1;
334 Archive::symbol_iterator Archive::begin_symbols() const {
335 const char *buf = SymbolTable->getBuffer()->getBufferStart();
336 if (kind() == K_GNU) {
337 uint32_t symbol_count = 0;
338 symbol_count = *reinterpret_cast<const support::ubig32_t*>(buf);
339 buf += sizeof(uint32_t) + (symbol_count * (sizeof(uint32_t)));
340 } else if (kind() == K_BSD) {
341 llvm_unreachable("BSD archive format is not supported");
343 uint32_t member_count = 0;
344 uint32_t symbol_count = 0;
345 member_count = *reinterpret_cast<const support::ulittle32_t*>(buf);
346 buf += 4 + (member_count * 4); // Skip offsets.
347 symbol_count = *reinterpret_cast<const support::ulittle32_t*>(buf);
348 buf += 4 + (symbol_count * 2); // Skip indices.
350 uint32_t string_start_offset =
351 buf - SymbolTable->getBuffer()->getBufferStart();
352 return symbol_iterator(Symbol(this, 0, string_start_offset));
355 Archive::symbol_iterator Archive::end_symbols() const {
356 const char *buf = SymbolTable->getBuffer()->getBufferStart();
357 uint32_t symbol_count = 0;
358 if (kind() == K_GNU) {
359 symbol_count = *reinterpret_cast<const support::ubig32_t*>(buf);
360 buf += sizeof(uint32_t) + (symbol_count * (sizeof(uint32_t)));
361 } else if (kind() == K_BSD) {
362 llvm_unreachable("BSD archive format is not supported");
364 uint32_t member_count = 0;
365 member_count = *reinterpret_cast<const support::ulittle32_t*>(buf);
366 buf += 4 + (member_count * 4); // Skip offsets.
367 symbol_count = *reinterpret_cast<const support::ulittle32_t*>(buf);
369 return symbol_iterator(
370 Symbol(this, symbol_count, 0));
373 Archive::child_iterator Archive::findSym(StringRef name) const {
374 Archive::symbol_iterator bs = begin_symbols();
375 Archive::symbol_iterator es = end_symbols();
376 Archive::child_iterator result;
379 for (; bs != es; ++bs) {
380 if (bs->getName(symname))
381 return end_children();
382 if (symname == name) {
383 if (bs->getMember(result))
384 return end_children();
388 return end_children();