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/MemoryBuffer.h"
19 using namespace object;
22 const StringRef Magic = "!<arch>\n";
24 struct ArchiveMemberHeader {
26 char LastModified[12];
30 char Size[10]; //< Size of data, not including header or padding.
33 ///! Get the name without looking up long names.
34 StringRef getName() const {
36 if (Name[0] == '/' || Name[0] == '#')
40 StringRef::size_type end = StringRef(Name, sizeof(Name)).find(EndCond);
41 if (end == StringRef::npos)
43 assert(end <= sizeof(Name) && end > 0);
44 // Don't include the EndCond if there is one.
45 return StringRef(Name, end);
48 uint64_t getSize() const {
50 StringRef(Size, sizeof(Size)).getAsInteger(10, ret);
51 return ret.getZExtValue();
55 const ArchiveMemberHeader *ToHeader(const char *base) {
56 return reinterpret_cast<const ArchiveMemberHeader *>(base);
60 static bool isInternalMember(const ArchiveMemberHeader &amh) {
61 const char *internals[] = {
67 StringRef name = amh.getName();
68 for (std::size_t i = 0; i < sizeof(internals) / sizeof(*internals); ++i) {
69 if (name == internals[i])
75 Archive::Child Archive::Child::getNext() const {
76 size_t SpaceToSkip = sizeof(ArchiveMemberHeader) +
77 ToHeader(Data.data())->getSize();
78 // If it's odd, add 1 to make it even.
82 const char *NextLoc = Data.data() + SpaceToSkip;
84 // Check to see if this is past the end of the archive.
85 if (NextLoc >= Parent->Data->getBufferEnd())
86 return Child(Parent, StringRef(0, 0));
88 size_t NextSize = sizeof(ArchiveMemberHeader) +
89 ToHeader(NextLoc)->getSize();
91 return Child(Parent, StringRef(NextLoc, NextSize));
94 error_code Archive::Child::getName(StringRef &Result) const {
95 StringRef name = ToHeader(Data.data())->getName();
96 // Check if it's a special name.
98 if (name.size() == 1) { // Linker member.
100 return object_error::success;
102 if (name.size() == 2 && name[1] == '/') { // String table.
104 return object_error::success;
109 name.substr(1).getAsInteger(10, offset);
110 const char *addr = Parent->StringTable->Data.begin()
111 + sizeof(ArchiveMemberHeader)
112 + offset.getZExtValue();
114 if (Parent->StringTable == Parent->end_children()
115 || addr < (Parent->StringTable->Data.begin()
116 + sizeof(ArchiveMemberHeader))
117 || addr > (Parent->StringTable->Data.begin()
118 + sizeof(ArchiveMemberHeader)
119 + Parent->StringTable->getSize()))
120 return object_error::parse_failed;
122 return object_error::success;
123 } else if (name.startswith("#1/")) {
125 name.substr(3).getAsInteger(10, name_size);
126 Result = Data.substr(0, name_size.getZExtValue());
127 return object_error::success;
129 // It's a simple name.
130 if (name[name.size() - 1] == '/')
131 Result = name.substr(0, name.size() - 1);
134 return object_error::success;
137 uint64_t Archive::Child::getSize() const {
138 uint64_t size = ToHeader(Data.data())->getSize();
139 // Don't include attached name.
140 StringRef name = ToHeader(Data.data())->getName();
141 if (name.startswith("#1/")) {
143 name.substr(3).getAsInteger(10, name_size);
144 size -= name_size.getZExtValue();
149 MemoryBuffer *Archive::Child::getBuffer() const {
151 if (getName(name)) return NULL;
152 int size = sizeof(ArchiveMemberHeader);
153 if (name.startswith("#1/")) {
155 name.substr(3).getAsInteger(10, name_size);
156 size += name_size.getZExtValue();
158 return MemoryBuffer::getMemBuffer(Data.substr(size, getSize()),
163 error_code Archive::Child::getAsBinary(OwningPtr<Binary> &Result) const {
164 OwningPtr<Binary> ret;
166 createBinary(getBuffer(), ret))
169 return object_error::success;
172 Archive::Archive(MemoryBuffer *source, error_code &ec)
173 : Binary(Binary::isArchive, source)
174 , StringTable(Child(this, StringRef(0, 0))) {
175 // Check for sufficient magic.
176 if (!source || source->getBufferSize()
177 < (8 + sizeof(ArchiveMemberHeader) + 2) // Smallest archive.
178 || StringRef(source->getBufferStart(), 8) != Magic) {
179 ec = object_error::invalid_file_type;
183 // Get the string table. It's the 3rd member.
184 child_iterator StrTable = begin_children(false);
185 child_iterator e = end_children();
186 for (int i = 0; StrTable != e && i < 2; ++StrTable, ++i) {}
188 // Check to see if there were 3 members, or the 3rd member wasn't named "//".
190 if (StrTable != e && !StrTable->getName(name) && name == "//")
191 StringTable = StrTable;
193 ec = object_error::success;
196 Archive::child_iterator Archive::begin_children(bool skip_internal) const {
197 const char *Loc = Data->getBufferStart() + Magic.size();
198 size_t Size = sizeof(ArchiveMemberHeader) +
199 ToHeader(Loc)->getSize();
200 Child c(this, StringRef(Loc, Size));
201 // Skip internals at the beginning of an archive.
202 if (skip_internal && isInternalMember(*ToHeader(Loc)))
207 Archive::child_iterator Archive::end_children() const {
208 return Child(this, StringRef(0, 0));
213 } // end namespace llvm