blob: be359240626fef2dedf80dbe56b9839270ac4581 [file] [log] [blame]
Michael J. Spencera51d7d92011-09-27 19:36:55 +00001//===- Archive.cpp - ar File Format implementation --------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the ArchiveObjectFile class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Object/Archive.h"
15#include "llvm/ADT/APInt.h"
Michael J. Spencerc8a55a62011-11-02 19:33:12 +000016#include "llvm/Support/Endian.h"
Michael J. Spencera51d7d92011-09-27 19:36:55 +000017#include "llvm/Support/MemoryBuffer.h"
18
19using namespace llvm;
20using namespace object;
21
Benjamin Kramere1a44272012-02-22 13:42:11 +000022static const char *Magic = "!<arch>\n";
Michael J. Spencera51d7d92011-09-27 19:36:55 +000023
Michael J. Spenceraaf98ea2011-10-25 22:30:42 +000024static bool isInternalMember(const ArchiveMemberHeader &amh) {
Craig Toppere3298102012-05-24 06:35:32 +000025 static const char *const internals[] = {
Michael J. Spenceraaf98ea2011-10-25 22:30:42 +000026 "/",
27 "//",
28 "#_LLVM_SYM_TAB_#"
Craig Toppere3298102012-05-24 06:35:32 +000029 };
Michael J. Spenceraaf98ea2011-10-25 22:30:42 +000030
31 StringRef name = amh.getName();
32 for (std::size_t i = 0; i < sizeof(internals) / sizeof(*internals); ++i) {
33 if (name == internals[i])
34 return true;
35 }
36 return false;
37}
38
David Blaikie2d24e2a2011-12-20 02:50:00 +000039void Archive::anchor() { }
40
Michael J. Spencera51d7d92011-09-27 19:36:55 +000041error_code Archive::Child::getName(StringRef &Result) const {
42 StringRef name = ToHeader(Data.data())->getName();
43 // Check if it's a special name.
44 if (name[0] == '/') {
45 if (name.size() == 1) { // Linker member.
46 Result = name;
47 return object_error::success;
48 }
49 if (name.size() == 2 && name[1] == '/') { // String table.
50 Result = name;
51 return object_error::success;
52 }
53 // It's a long name.
54 // Get the offset.
Michael J. Spencerbf82b072013-01-10 00:07:38 +000055 std::size_t offset;
Michael J. Spencer7932c412013-01-10 01:05:34 +000056 if (name.substr(1).rtrim(" ").getAsInteger(10, offset))
57 llvm_unreachable("Long name offset is not an integer");
Michael J. Spencera51d7d92011-09-27 19:36:55 +000058 const char *addr = Parent->StringTable->Data.begin()
59 + sizeof(ArchiveMemberHeader)
Michael J. Spencere92800d2013-01-09 22:58:43 +000060 + offset;
Michael J. Spencera51d7d92011-09-27 19:36:55 +000061 // Verify it.
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;
Shankar Easwaran206252c2012-11-13 18:38:42 +000069
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);
74 } else {
75 Result = addr;
76 }
Michael J. Spencera51d7d92011-09-27 19:36:55 +000077 return object_error::success;
Michael J. Spenceraaf98ea2011-10-25 22:30:42 +000078 } else if (name.startswith("#1/")) {
Michael J. Spencere92800d2013-01-09 22:58:43 +000079 uint64_t name_size;
Michael J. Spencer7932c412013-01-10 01:05:34 +000080 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);
Michael J. Spenceraaf98ea2011-10-25 22:30:42 +000083 return object_error::success;
Michael J. Spencera51d7d92011-09-27 19:36:55 +000084 }
85 // It's a simple name.
86 if (name[name.size() - 1] == '/')
87 Result = name.substr(0, name.size() - 1);
88 else
89 Result = name;
90 return object_error::success;
91}
92
Michael J. Spencera51d7d92011-09-27 19:36:55 +000093error_code Archive::Child::getAsBinary(OwningPtr<Binary> &Result) const {
94 OwningPtr<Binary> ret;
Michael J. Spencer0f76e642013-02-03 10:48:50 +000095 OwningPtr<MemoryBuffer> Buff;
96 if (error_code ec = getMemoryBuffer(Buff))
97 return ec;
98 if (error_code ec = createBinary(Buff.take(), ret))
Michael J. Spencera51d7d92011-09-27 19:36:55 +000099 return ec;
100 Result.swap(ret);
101 return object_error::success;
102}
103
104Archive::Archive(MemoryBuffer *source, error_code &ec)
David Meyer6f9489a2012-03-09 20:41:57 +0000105 : Binary(Binary::ID_Archive, source) {
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000106 // 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;
111 return;
112 }
113
Michael J. Spencerc8a55a62011-11-02 19:33:12 +0000114 // Get the special members.
115 child_iterator i = begin_children(false);
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000116 child_iterator e = end_children();
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000117
Shankar Easwaran206252c2012-11-13 18:38:42 +0000118 StringRef name;
119 if ((ec = i->getName(name)))
120 return;
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000121
Shankar Easwaran206252c2012-11-13 18:38:42 +0000122 // 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
133 // First member : /
134 // Second member : / (provides a directory of symbols)
Rui Ueyama891c0cd2013-06-03 00:27:03 +0000135 // 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.
Shankar Easwaran206252c2012-11-13 18:38:42 +0000140 if (name == "/") {
141 SymbolTable = i;
142 StringTable = e;
143 if (i != e) ++i;
Michael J. Spencerbf82b072013-01-10 00:07:38 +0000144 if (i == e) {
145 ec = object_error::parse_failed;
146 return;
147 }
Shankar Easwaran206252c2012-11-13 18:38:42 +0000148 if ((ec = i->getName(name)))
149 return;
150 if (name[0] != '/') {
151 Format = K_GNU;
152 } else if ((name.size() > 1) && (name == "//")) {
153 Format = K_GNU;
154 StringTable = i;
155 ++i;
Rui Ueyama891c0cd2013-06-03 00:27:03 +0000156 } else {
Shankar Easwaran206252c2012-11-13 18:38:42 +0000157 Format = K_COFF;
158 if (i != e) {
159 SymbolTable = i;
160 ++i;
161 }
162 if (i != e) {
Rui Ueyama891c0cd2013-06-03 00:27:03 +0000163 if ((ec = i->getName(name)))
164 return;
165 if (name == "//")
166 StringTable = i;
Shankar Easwaran206252c2012-11-13 18:38:42 +0000167 }
168 }
169 } else if (name == "__.SYMDEF") {
170 Format = K_BSD;
171 SymbolTable = i;
172 StringTable = e;
173 }
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000174 ec = object_error::success;
175}
176
Michael J. Spenceraaf98ea2011-10-25 22:30:42 +0000177Archive::child_iterator Archive::begin_children(bool skip_internal) const {
Benjamin Kramere1a44272012-02-22 13:42:11 +0000178 const char *Loc = Data->getBufferStart() + strlen(Magic);
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000179 size_t Size = sizeof(ArchiveMemberHeader) +
180 ToHeader(Loc)->getSize();
Michael J. Spenceraaf98ea2011-10-25 22:30:42 +0000181 Child c(this, StringRef(Loc, Size));
182 // Skip internals at the beginning of an archive.
183 if (skip_internal && isInternalMember(*ToHeader(Loc)))
184 return c.getNext();
185 return c;
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000186}
187
Michael J. Spencer58618932011-10-08 00:17:45 +0000188Archive::child_iterator Archive::end_children() const {
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000189 return Child(this, StringRef(0, 0));
190}
Michael J. Spencerc8a55a62011-11-02 19:33:12 +0000191
192error_code Archive::Symbol::getName(StringRef &Result) const {
Michael J. Spencer0f76e642013-02-03 10:48:50 +0000193 Result = StringRef(Parent->SymbolTable->getBuffer().begin() + StringIndex);
Michael J. Spencerc8a55a62011-11-02 19:33:12 +0000194 return object_error::success;
195}
196
197error_code Archive::Symbol::getMember(child_iterator &Result) const {
Michael J. Spencer0f76e642013-02-03 10:48:50 +0000198 const char *Buf = Parent->SymbolTable->getBuffer().begin();
Michael J. Spencer768a7072012-11-14 00:04:13 +0000199 const char *Offsets = Buf + 4;
200 uint32_t Offset = 0;
Shankar Easwaran206252c2012-11-13 18:38:42 +0000201 if (Parent->kind() == K_GNU) {
Michael J. Spencer768a7072012-11-14 00:04:13 +0000202 Offset = *(reinterpret_cast<const support::ubig32_t*>(Offsets)
203 + SymbolIndex);
Shankar Easwaran206252c2012-11-13 18:38:42 +0000204 } else if (Parent->kind() == K_BSD) {
Matt Beaumont-Gay7af4b9b2012-11-14 17:58:11 +0000205 llvm_unreachable("BSD format is not supported");
Shankar Easwaran206252c2012-11-13 18:38:42 +0000206 } else {
Michael J. Spencer768a7072012-11-14 00:04:13 +0000207 uint32_t MemberCount = *reinterpret_cast<const support::ulittle32_t*>(Buf);
208
209 // Skip offsets.
210 Buf += sizeof(support::ulittle32_t)
211 + (MemberCount * sizeof(support::ulittle32_t));
212
213 uint32_t SymbolCount = *reinterpret_cast<const support::ulittle32_t*>(Buf);
214
215 if (SymbolIndex >= SymbolCount)
216 return object_error::parse_failed;
217
Matt Beaumont-Gayf1c2a6b2012-11-14 00:21:27 +0000218 // Skip SymbolCount to get to the indices table.
219 const char *Indices = Buf + sizeof(support::ulittle32_t);
Michael J. Spencer768a7072012-11-14 00:04:13 +0000220
221 // Get the index of the offset in the file member offset table for this
222 // symbol.
223 uint16_t OffsetIndex =
Matt Beaumont-Gayf1c2a6b2012-11-14 00:21:27 +0000224 *(reinterpret_cast<const support::ulittle16_t*>(Indices)
Shankar Easwaran206252c2012-11-13 18:38:42 +0000225 + SymbolIndex);
Michael J. Spencer768a7072012-11-14 00:04:13 +0000226 // Subtract 1 since OffsetIndex is 1 based.
227 --OffsetIndex;
228
229 if (OffsetIndex >= MemberCount)
230 return object_error::parse_failed;
231
232 Offset = *(reinterpret_cast<const support::ulittle32_t*>(Offsets)
233 + OffsetIndex);
Shankar Easwaran206252c2012-11-13 18:38:42 +0000234 }
Michael J. Spencerc8a55a62011-11-02 19:33:12 +0000235
Michael J. Spencer768a7072012-11-14 00:04:13 +0000236 const char *Loc = Parent->getData().begin() + Offset;
Michael J. Spencerc8a55a62011-11-02 19:33:12 +0000237 size_t Size = sizeof(ArchiveMemberHeader) +
238 ToHeader(Loc)->getSize();
239 Result = Child(Parent, StringRef(Loc, Size));
240
241 return object_error::success;
242}
243
244Archive::Symbol Archive::Symbol::getNext() const {
245 Symbol t(*this);
Benjamin Kramerefd2d5e2011-11-04 13:52:17 +0000246 // Go to one past next null.
247 t.StringIndex =
Michael J. Spencer0f76e642013-02-03 10:48:50 +0000248 Parent->SymbolTable->getBuffer().find('\0', t.StringIndex) + 1;
Michael J. Spencerc8a55a62011-11-02 19:33:12 +0000249 ++t.SymbolIndex;
250 return t;
251}
252
253Archive::symbol_iterator Archive::begin_symbols() const {
Michael J. Spencer0f76e642013-02-03 10:48:50 +0000254 const char *buf = SymbolTable->getBuffer().begin();
Shankar Easwaran206252c2012-11-13 18:38:42 +0000255 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) {
Matt Beaumont-Gay7af4b9b2012-11-14 17:58:11 +0000260 llvm_unreachable("BSD archive format is not supported");
Shankar Easwaran206252c2012-11-13 18:38:42 +0000261 } else {
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.
268 }
Michael J. Spencer0f76e642013-02-03 10:48:50 +0000269 uint32_t string_start_offset = buf - SymbolTable->getBuffer().begin();
Michael J. Spencerc8a55a62011-11-02 19:33:12 +0000270 return symbol_iterator(Symbol(this, 0, string_start_offset));
271}
272
273Archive::symbol_iterator Archive::end_symbols() const {
Michael J. Spencer0f76e642013-02-03 10:48:50 +0000274 const char *buf = SymbolTable->getBuffer().begin();
Shankar Easwaran206252c2012-11-13 18:38:42 +0000275 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) {
Matt Beaumont-Gay7af4b9b2012-11-14 17:58:11 +0000280 llvm_unreachable("BSD archive format is not supported");
Shankar Easwaran206252c2012-11-13 18:38:42 +0000281 } else {
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);
286 }
Michael J. Spencerc8a55a62011-11-02 19:33:12 +0000287 return symbol_iterator(
288 Symbol(this, symbol_count, 0));
289}
Shankar Easwaran206252c2012-11-13 18:38:42 +0000290
291Archive::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;
295
296 StringRef symname;
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();
303 return result;
304 }
305 }
306 return end_children();
307}