blob: 8206b0a18bc52558e5418d05fb09e74a5cfcbab7 [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 "/",
Rafael Espindola250bfb12013-06-14 23:25:53 +000027 "//"
Craig Toppere3298102012-05-24 06:35:32 +000028 };
Michael J. Spenceraaf98ea2011-10-25 22:30:42 +000029
30 StringRef name = amh.getName();
31 for (std::size_t i = 0; i < sizeof(internals) / sizeof(*internals); ++i) {
32 if (name == internals[i])
33 return true;
34 }
35 return false;
36}
37
David Blaikie2d24e2a2011-12-20 02:50:00 +000038void Archive::anchor() { }
39
Michael J. Spencera51d7d92011-09-27 19:36:55 +000040error_code Archive::Child::getName(StringRef &Result) const {
41 StringRef name = ToHeader(Data.data())->getName();
42 // Check if it's a special name.
43 if (name[0] == '/') {
44 if (name.size() == 1) { // Linker member.
45 Result = name;
46 return object_error::success;
47 }
48 if (name.size() == 2 && name[1] == '/') { // String table.
49 Result = name;
50 return object_error::success;
51 }
52 // It's a long name.
53 // Get the offset.
Michael J. Spencerbf82b072013-01-10 00:07:38 +000054 std::size_t offset;
Michael J. Spencer7932c412013-01-10 01:05:34 +000055 if (name.substr(1).rtrim(" ").getAsInteger(10, offset))
56 llvm_unreachable("Long name offset is not an integer");
Michael J. Spencera51d7d92011-09-27 19:36:55 +000057 const char *addr = Parent->StringTable->Data.begin()
58 + sizeof(ArchiveMemberHeader)
Michael J. Spencere92800d2013-01-09 22:58:43 +000059 + offset;
Michael J. Spencera51d7d92011-09-27 19:36:55 +000060 // Verify it.
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;
Shankar Easwaran206252c2012-11-13 18:38:42 +000068
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);
73 } else {
74 Result = addr;
75 }
Michael J. Spencera51d7d92011-09-27 19:36:55 +000076 return object_error::success;
Michael J. Spenceraaf98ea2011-10-25 22:30:42 +000077 } else if (name.startswith("#1/")) {
Michael J. Spencere92800d2013-01-09 22:58:43 +000078 uint64_t name_size;
Michael J. Spencer7932c412013-01-10 01:05:34 +000079 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);
Michael J. Spenceraaf98ea2011-10-25 22:30:42 +000082 return object_error::success;
Michael J. Spencera51d7d92011-09-27 19:36:55 +000083 }
84 // It's a simple name.
85 if (name[name.size() - 1] == '/')
86 Result = name.substr(0, name.size() - 1);
87 else
88 Result = name;
89 return object_error::success;
90}
91
Michael J. Spencera51d7d92011-09-27 19:36:55 +000092error_code Archive::Child::getAsBinary(OwningPtr<Binary> &Result) const {
93 OwningPtr<Binary> ret;
Michael J. Spencer0f76e642013-02-03 10:48:50 +000094 OwningPtr<MemoryBuffer> Buff;
95 if (error_code ec = getMemoryBuffer(Buff))
96 return ec;
97 if (error_code ec = createBinary(Buff.take(), ret))
Michael J. Spencera51d7d92011-09-27 19:36:55 +000098 return ec;
99 Result.swap(ret);
100 return object_error::success;
101}
102
103Archive::Archive(MemoryBuffer *source, error_code &ec)
David Meyer6f9489a2012-03-09 20:41:57 +0000104 : Binary(Binary::ID_Archive, source) {
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000105 // 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;
110 return;
111 }
112
Michael J. Spencerc8a55a62011-11-02 19:33:12 +0000113 // Get the special members.
114 child_iterator i = begin_children(false);
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000115 child_iterator e = end_children();
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000116
Rafael Espindola09a7f602013-07-03 15:57:14 +0000117 if (i == e) {
118 ec = object_error::parse_failed;
119 return;
120 }
121
122 // FIXME: this function should be able to use raw names.
Shankar Easwaran206252c2012-11-13 18:38:42 +0000123 StringRef name;
124 if ((ec = i->getName(name)))
125 return;
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000126
Shankar Easwaran206252c2012-11-13 18:38:42 +0000127 // Below is the pattern that is used to figure out the archive format
128 // GNU archive format
Rafael Espindola09a7f602013-07-03 15:57:14 +0000129 // First member : / (may exist, if it exists, points to the symbol table )
Shankar Easwaran206252c2012-11-13 18:38:42 +0000130 // 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
138 // First member : /
139 // Second member : / (provides a directory of symbols)
Rui Ueyama891c0cd2013-06-03 00:27:03 +0000140 // 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.
Rafael Espindola09a7f602013-07-03 15:57:14 +0000145
146 if (name == "__.SYMDEF") {
147 Format = K_BSD;
148 SymbolTable = i;
149 ec = object_error::success;
150 return;
151 }
152
Shankar Easwaran206252c2012-11-13 18:38:42 +0000153 if (name == "/") {
154 SymbolTable = i;
Rafael Espindola09a7f602013-07-03 15:57:14 +0000155
156 ++i;
Michael J. Spencerbf82b072013-01-10 00:07:38 +0000157 if (i == e) {
158 ec = object_error::parse_failed;
159 return;
160 }
Shankar Easwaran206252c2012-11-13 18:38:42 +0000161 if ((ec = i->getName(name)))
162 return;
Rafael Espindola09a7f602013-07-03 15:57:14 +0000163 }
164
165 if (name == "//") {
166 Format = K_GNU;
167 StringTable = i;
168 ec = object_error::success;
169 return;
170 }
171
172 if (name[0] != '/') {
173 Format = K_GNU;
174 ec = object_error::success;
175 return;
176 }
177
178 if (name != "/") {
179 ec = object_error::parse_failed;
180 return;
181 }
182
183 Format = K_COFF;
184 SymbolTable = i;
185
186 ++i;
187 if (i == e) {
188 ec = object_error::success;
189 return;
190 }
191
192 if ((ec = i->getName(name)))
193 return;
194
195 if (name == "//")
196 StringTable = i;
197
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000198 ec = object_error::success;
199}
200
Michael J. Spenceraaf98ea2011-10-25 22:30:42 +0000201Archive::child_iterator Archive::begin_children(bool skip_internal) const {
Benjamin Kramere1a44272012-02-22 13:42:11 +0000202 const char *Loc = Data->getBufferStart() + strlen(Magic);
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000203 size_t Size = sizeof(ArchiveMemberHeader) +
204 ToHeader(Loc)->getSize();
Michael J. Spenceraaf98ea2011-10-25 22:30:42 +0000205 Child c(this, StringRef(Loc, Size));
206 // Skip internals at the beginning of an archive.
207 if (skip_internal && isInternalMember(*ToHeader(Loc)))
208 return c.getNext();
209 return c;
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000210}
211
Michael J. Spencer58618932011-10-08 00:17:45 +0000212Archive::child_iterator Archive::end_children() const {
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000213 return Child(this, StringRef(0, 0));
214}
Michael J. Spencerc8a55a62011-11-02 19:33:12 +0000215
216error_code Archive::Symbol::getName(StringRef &Result) const {
Michael J. Spencer0f76e642013-02-03 10:48:50 +0000217 Result = StringRef(Parent->SymbolTable->getBuffer().begin() + StringIndex);
Michael J. Spencerc8a55a62011-11-02 19:33:12 +0000218 return object_error::success;
219}
220
221error_code Archive::Symbol::getMember(child_iterator &Result) const {
Michael J. Spencer0f76e642013-02-03 10:48:50 +0000222 const char *Buf = Parent->SymbolTable->getBuffer().begin();
Michael J. Spencer768a7072012-11-14 00:04:13 +0000223 const char *Offsets = Buf + 4;
224 uint32_t Offset = 0;
Shankar Easwaran206252c2012-11-13 18:38:42 +0000225 if (Parent->kind() == K_GNU) {
Michael J. Spencer768a7072012-11-14 00:04:13 +0000226 Offset = *(reinterpret_cast<const support::ubig32_t*>(Offsets)
227 + SymbolIndex);
Shankar Easwaran206252c2012-11-13 18:38:42 +0000228 } else if (Parent->kind() == K_BSD) {
Matt Beaumont-Gay7af4b9b2012-11-14 17:58:11 +0000229 llvm_unreachable("BSD format is not supported");
Shankar Easwaran206252c2012-11-13 18:38:42 +0000230 } else {
Michael J. Spencer768a7072012-11-14 00:04:13 +0000231 uint32_t MemberCount = *reinterpret_cast<const support::ulittle32_t*>(Buf);
232
233 // Skip offsets.
234 Buf += sizeof(support::ulittle32_t)
235 + (MemberCount * sizeof(support::ulittle32_t));
236
237 uint32_t SymbolCount = *reinterpret_cast<const support::ulittle32_t*>(Buf);
238
239 if (SymbolIndex >= SymbolCount)
240 return object_error::parse_failed;
241
Matt Beaumont-Gayf1c2a6b2012-11-14 00:21:27 +0000242 // Skip SymbolCount to get to the indices table.
243 const char *Indices = Buf + sizeof(support::ulittle32_t);
Michael J. Spencer768a7072012-11-14 00:04:13 +0000244
245 // Get the index of the offset in the file member offset table for this
246 // symbol.
247 uint16_t OffsetIndex =
Matt Beaumont-Gayf1c2a6b2012-11-14 00:21:27 +0000248 *(reinterpret_cast<const support::ulittle16_t*>(Indices)
Shankar Easwaran206252c2012-11-13 18:38:42 +0000249 + SymbolIndex);
Michael J. Spencer768a7072012-11-14 00:04:13 +0000250 // Subtract 1 since OffsetIndex is 1 based.
251 --OffsetIndex;
252
253 if (OffsetIndex >= MemberCount)
254 return object_error::parse_failed;
255
256 Offset = *(reinterpret_cast<const support::ulittle32_t*>(Offsets)
257 + OffsetIndex);
Shankar Easwaran206252c2012-11-13 18:38:42 +0000258 }
Michael J. Spencerc8a55a62011-11-02 19:33:12 +0000259
Michael J. Spencer768a7072012-11-14 00:04:13 +0000260 const char *Loc = Parent->getData().begin() + Offset;
Michael J. Spencerc8a55a62011-11-02 19:33:12 +0000261 size_t Size = sizeof(ArchiveMemberHeader) +
262 ToHeader(Loc)->getSize();
263 Result = Child(Parent, StringRef(Loc, Size));
264
265 return object_error::success;
266}
267
268Archive::Symbol Archive::Symbol::getNext() const {
269 Symbol t(*this);
Benjamin Kramerefd2d5e2011-11-04 13:52:17 +0000270 // Go to one past next null.
271 t.StringIndex =
Michael J. Spencer0f76e642013-02-03 10:48:50 +0000272 Parent->SymbolTable->getBuffer().find('\0', t.StringIndex) + 1;
Michael J. Spencerc8a55a62011-11-02 19:33:12 +0000273 ++t.SymbolIndex;
274 return t;
275}
276
277Archive::symbol_iterator Archive::begin_symbols() const {
Michael J. Spencer0f76e642013-02-03 10:48:50 +0000278 const char *buf = SymbolTable->getBuffer().begin();
Shankar Easwaran206252c2012-11-13 18:38:42 +0000279 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) {
Matt Beaumont-Gay7af4b9b2012-11-14 17:58:11 +0000284 llvm_unreachable("BSD archive format is not supported");
Shankar Easwaran206252c2012-11-13 18:38:42 +0000285 } else {
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.
292 }
Michael J. Spencer0f76e642013-02-03 10:48:50 +0000293 uint32_t string_start_offset = buf - SymbolTable->getBuffer().begin();
Michael J. Spencerc8a55a62011-11-02 19:33:12 +0000294 return symbol_iterator(Symbol(this, 0, string_start_offset));
295}
296
297Archive::symbol_iterator Archive::end_symbols() const {
Michael J. Spencer0f76e642013-02-03 10:48:50 +0000298 const char *buf = SymbolTable->getBuffer().begin();
Shankar Easwaran206252c2012-11-13 18:38:42 +0000299 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) {
Matt Beaumont-Gay7af4b9b2012-11-14 17:58:11 +0000304 llvm_unreachable("BSD archive format is not supported");
Shankar Easwaran206252c2012-11-13 18:38:42 +0000305 } else {
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);
310 }
Michael J. Spencerc8a55a62011-11-02 19:33:12 +0000311 return symbol_iterator(
312 Symbol(this, symbol_count, 0));
313}
Shankar Easwaran206252c2012-11-13 18:38:42 +0000314
315Archive::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;
319
320 StringRef symname;
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();
327 return result;
328 }
329 }
330 return end_children();
331}