blob: 14b21fd63612320ae7c02885c0d932c6a4ac7c87 [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"
Rafael Espindola5263d0a2013-07-09 03:39:35 +000016#include "llvm/ADT/SmallString.h"
17#include "llvm/ADT/Twine.h"
Michael J. Spencerc8a55a62011-11-02 19:33:12 +000018#include "llvm/Support/Endian.h"
Michael J. Spencera51d7d92011-09-27 19:36:55 +000019#include "llvm/Support/MemoryBuffer.h"
20
21using namespace llvm;
22using namespace object;
23
Benjamin Kramere1a44272012-02-22 13:42:11 +000024static const char *Magic = "!<arch>\n";
Michael J. Spencera51d7d92011-09-27 19:36:55 +000025
Michael J. Spenceraaf98ea2011-10-25 22:30:42 +000026static bool isInternalMember(const ArchiveMemberHeader &amh) {
Craig Toppere3298102012-05-24 06:35:32 +000027 static const char *const internals[] = {
Michael J. Spenceraaf98ea2011-10-25 22:30:42 +000028 "/",
Rafael Espindola250bfb12013-06-14 23:25:53 +000029 "//"
Craig Toppere3298102012-05-24 06:35:32 +000030 };
Michael J. Spenceraaf98ea2011-10-25 22:30:42 +000031
32 StringRef name = amh.getName();
33 for (std::size_t i = 0; i < sizeof(internals) / sizeof(*internals); ++i) {
34 if (name == internals[i])
35 return true;
36 }
37 return false;
38}
39
David Blaikie2d24e2a2011-12-20 02:50:00 +000040void Archive::anchor() { }
41
Rafael Espindola5263d0a2013-07-09 03:39:35 +000042StringRef ArchiveMemberHeader::getName() const {
43 char EndCond;
44 if (Name[0] == '/' || Name[0] == '#')
45 EndCond = ' ';
46 else
47 EndCond = '/';
48 llvm::StringRef::size_type end =
49 llvm::StringRef(Name, sizeof(Name)).find(EndCond);
50 if (end == llvm::StringRef::npos)
51 end = sizeof(Name);
52 assert(end <= sizeof(Name) && end > 0);
53 // Don't include the EndCond if there is one.
54 return llvm::StringRef(Name, end);
55}
56
57uint64_t ArchiveMemberHeader::getSize() const {
58 uint64_t ret;
59 if (llvm::StringRef(Size, sizeof(Size)).rtrim(" ").getAsInteger(10, ret))
60 llvm_unreachable("Size is not an integer.");
61 return ret;
62}
63
Rafael Espindolac5f87572013-07-09 12:22:05 +000064static const ArchiveMemberHeader *toHeader(const char *base) {
65 return reinterpret_cast<const ArchiveMemberHeader *>(base);
66}
67
Rafael Espindolabe6b9102013-07-09 05:26:25 +000068Archive::Child::Child(const Archive *Parent, const char *Start)
69 : Parent(Parent) {
70 if (!Start)
Rafael Espindola5263d0a2013-07-09 03:39:35 +000071 return;
Rafael Espindolabe6b9102013-07-09 05:26:25 +000072
Rafael Espindolac5f87572013-07-09 12:22:05 +000073 const ArchiveMemberHeader *Header = toHeader(Start);
Rafael Espindolabe6b9102013-07-09 05:26:25 +000074 Data = StringRef(Start, sizeof(ArchiveMemberHeader) + Header->getSize());
75
Rafael Espindola5263d0a2013-07-09 03:39:35 +000076 // Setup StartOfFile and PaddingBytes.
77 StartOfFile = sizeof(ArchiveMemberHeader);
78 // Don't include attached name.
Rafael Espindolabe6b9102013-07-09 05:26:25 +000079 StringRef Name = Header->getName();
Rafael Espindola5263d0a2013-07-09 03:39:35 +000080 if (Name.startswith("#1/")) {
81 uint64_t NameSize;
82 if (Name.substr(3).rtrim(" ").getAsInteger(10, NameSize))
83 llvm_unreachable("Long name length is not an integer");
84 StartOfFile += NameSize;
85 }
86}
87
88Archive::Child Archive::Child::getNext() const {
89 size_t SpaceToSkip = Data.size();
90 // If it's odd, add 1 to make it even.
91 if (SpaceToSkip & 1)
92 ++SpaceToSkip;
93
94 const char *NextLoc = Data.data() + SpaceToSkip;
95
96 // Check to see if this is past the end of the archive.
97 if (NextLoc >= Parent->Data->getBufferEnd())
Rafael Espindolabe6b9102013-07-09 05:26:25 +000098 return Child(Parent, NULL);
Rafael Espindola5263d0a2013-07-09 03:39:35 +000099
Rafael Espindolabe6b9102013-07-09 05:26:25 +0000100 return Child(Parent, NextLoc);
Rafael Espindola5263d0a2013-07-09 03:39:35 +0000101}
102
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000103error_code Archive::Child::getName(StringRef &Result) const {
Rafael Espindola4a0bf542013-07-05 03:35:15 +0000104 StringRef name = getRawName();
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000105 // Check if it's a special name.
106 if (name[0] == '/') {
107 if (name.size() == 1) { // Linker member.
108 Result = name;
109 return object_error::success;
110 }
111 if (name.size() == 2 && name[1] == '/') { // String table.
112 Result = name;
113 return object_error::success;
114 }
115 // It's a long name.
116 // Get the offset.
Michael J. Spencerbf82b072013-01-10 00:07:38 +0000117 std::size_t offset;
Michael J. Spencer7932c412013-01-10 01:05:34 +0000118 if (name.substr(1).rtrim(" ").getAsInteger(10, offset))
119 llvm_unreachable("Long name offset is not an integer");
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000120 const char *addr = Parent->StringTable->Data.begin()
121 + sizeof(ArchiveMemberHeader)
Michael J. Spencere92800d2013-01-09 22:58:43 +0000122 + offset;
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000123 // Verify it.
124 if (Parent->StringTable == Parent->end_children()
125 || addr < (Parent->StringTable->Data.begin()
126 + sizeof(ArchiveMemberHeader))
127 || addr > (Parent->StringTable->Data.begin()
128 + sizeof(ArchiveMemberHeader)
129 + Parent->StringTable->getSize()))
130 return object_error::parse_failed;
Shankar Easwaran206252c2012-11-13 18:38:42 +0000131
132 // GNU long file names end with a /.
133 if (Parent->kind() == K_GNU) {
134 StringRef::size_type End = StringRef(addr).find('/');
135 Result = StringRef(addr, End);
136 } else {
137 Result = addr;
138 }
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000139 return object_error::success;
Michael J. Spenceraaf98ea2011-10-25 22:30:42 +0000140 } else if (name.startswith("#1/")) {
Michael J. Spencere92800d2013-01-09 22:58:43 +0000141 uint64_t name_size;
Michael J. Spencer7932c412013-01-10 01:05:34 +0000142 if (name.substr(3).rtrim(" ").getAsInteger(10, name_size))
143 llvm_unreachable("Long name length is not an ingeter");
144 Result = Data.substr(sizeof(ArchiveMemberHeader), name_size);
Michael J. Spenceraaf98ea2011-10-25 22:30:42 +0000145 return object_error::success;
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000146 }
147 // It's a simple name.
148 if (name[name.size() - 1] == '/')
149 Result = name.substr(0, name.size() - 1);
150 else
151 Result = name;
152 return object_error::success;
153}
154
Rafael Espindola5263d0a2013-07-09 03:39:35 +0000155error_code Archive::Child::getMemoryBuffer(OwningPtr<MemoryBuffer> &Result,
156 bool FullPath) const {
157 StringRef Name;
158 if (error_code ec = getName(Name))
159 return ec;
160 SmallString<128> Path;
161 Result.reset(MemoryBuffer::getMemBuffer(
162 getBuffer(), FullPath ? (Twine(Parent->getFileName()) + "(" + Name + ")")
163 .toStringRef(Path)
164 : Name,
165 false));
166 return error_code::success();
167}
168
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000169error_code Archive::Child::getAsBinary(OwningPtr<Binary> &Result) const {
170 OwningPtr<Binary> ret;
Michael J. Spencer0f76e642013-02-03 10:48:50 +0000171 OwningPtr<MemoryBuffer> Buff;
172 if (error_code ec = getMemoryBuffer(Buff))
173 return ec;
174 if (error_code ec = createBinary(Buff.take(), ret))
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000175 return ec;
176 Result.swap(ret);
177 return object_error::success;
178}
179
180Archive::Archive(MemoryBuffer *source, error_code &ec)
David Meyer6f9489a2012-03-09 20:41:57 +0000181 : Binary(Binary::ID_Archive, source) {
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000182 // Check for sufficient magic.
183 if (!source || source->getBufferSize()
Rafael Espindola6d88f9b2013-07-04 19:40:23 +0000184 < (8 + sizeof(ArchiveMemberHeader)) // Smallest archive.
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000185 || StringRef(source->getBufferStart(), 8) != Magic) {
186 ec = object_error::invalid_file_type;
187 return;
188 }
189
Michael J. Spencerc8a55a62011-11-02 19:33:12 +0000190 // Get the special members.
191 child_iterator i = begin_children(false);
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000192 child_iterator e = end_children();
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000193
Rafael Espindola09a7f602013-07-03 15:57:14 +0000194 if (i == e) {
195 ec = object_error::parse_failed;
196 return;
197 }
198
Rafael Espindola4a0bf542013-07-05 03:35:15 +0000199 StringRef Name = i->getRawName();
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000200
Shankar Easwaran206252c2012-11-13 18:38:42 +0000201 // Below is the pattern that is used to figure out the archive format
202 // GNU archive format
Rafael Espindola09a7f602013-07-03 15:57:14 +0000203 // First member : / (may exist, if it exists, points to the symbol table )
Shankar Easwaran206252c2012-11-13 18:38:42 +0000204 // Second member : // (may exist, if it exists, points to the string table)
205 // Note : The string table is used if the filename exceeds 15 characters
206 // BSD archive format
207 // First member : __.SYMDEF (points to the symbol table)
208 // There is no string table, if the filename exceeds 15 characters or has a
209 // embedded space, the filename has #1/<size>, The size represents the size
210 // of the filename that needs to be read after the archive header
211 // COFF archive format
212 // First member : /
213 // Second member : / (provides a directory of symbols)
Rui Ueyama891c0cd2013-06-03 00:27:03 +0000214 // Third member : // (may exist, if it exists, contains the string table)
215 // Note: Microsoft PE/COFF Spec 8.3 says that the third member is present
216 // even if the string table is empty. However, lib.exe does not in fact
217 // seem to create the third member if there's no member whose filename
218 // exceeds 15 characters. So the third member is optional.
Rafael Espindola09a7f602013-07-03 15:57:14 +0000219
Rafael Espindola4a0bf542013-07-05 03:35:15 +0000220 if (Name == "__.SYMDEF") {
Rafael Espindola09a7f602013-07-03 15:57:14 +0000221 Format = K_BSD;
222 SymbolTable = i;
223 ec = object_error::success;
224 return;
225 }
226
Rafael Espindola4a0bf542013-07-05 03:35:15 +0000227 if (Name == "/") {
Shankar Easwaran206252c2012-11-13 18:38:42 +0000228 SymbolTable = i;
Rafael Espindola09a7f602013-07-03 15:57:14 +0000229
230 ++i;
Michael J. Spencerbf82b072013-01-10 00:07:38 +0000231 if (i == e) {
232 ec = object_error::parse_failed;
233 return;
234 }
Rafael Espindola4a0bf542013-07-05 03:35:15 +0000235 Name = i->getRawName();
Rafael Espindola09a7f602013-07-03 15:57:14 +0000236 }
237
Rafael Espindola4a0bf542013-07-05 03:35:15 +0000238 if (Name == "//") {
Rafael Espindola09a7f602013-07-03 15:57:14 +0000239 Format = K_GNU;
240 StringTable = i;
241 ec = object_error::success;
242 return;
243 }
244
Rafael Espindola4a0bf542013-07-05 03:35:15 +0000245 if (Name[0] != '/') {
Rafael Espindola09a7f602013-07-03 15:57:14 +0000246 Format = K_GNU;
247 ec = object_error::success;
248 return;
249 }
250
Rafael Espindola4a0bf542013-07-05 03:35:15 +0000251 if (Name != "/") {
Rafael Espindola09a7f602013-07-03 15:57:14 +0000252 ec = object_error::parse_failed;
253 return;
254 }
255
256 Format = K_COFF;
257 SymbolTable = i;
258
259 ++i;
260 if (i == e) {
261 ec = object_error::success;
262 return;
263 }
264
Rafael Espindola4a0bf542013-07-05 03:35:15 +0000265 Name = i->getRawName();
Rafael Espindola09a7f602013-07-03 15:57:14 +0000266
Rafael Espindola4a0bf542013-07-05 03:35:15 +0000267 if (Name == "//")
Rafael Espindola09a7f602013-07-03 15:57:14 +0000268 StringTable = i;
269
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000270 ec = object_error::success;
271}
272
Michael J. Spenceraaf98ea2011-10-25 22:30:42 +0000273Archive::child_iterator Archive::begin_children(bool skip_internal) const {
Benjamin Kramere1a44272012-02-22 13:42:11 +0000274 const char *Loc = Data->getBufferStart() + strlen(Magic);
Rafael Espindolabe6b9102013-07-09 05:26:25 +0000275 Child c(this, Loc);
Michael J. Spenceraaf98ea2011-10-25 22:30:42 +0000276 // Skip internals at the beginning of an archive.
Rafael Espindolac5f87572013-07-09 12:22:05 +0000277 if (skip_internal && isInternalMember(*toHeader(Loc)))
Michael J. Spenceraaf98ea2011-10-25 22:30:42 +0000278 return c.getNext();
279 return c;
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000280}
281
Michael J. Spencer58618932011-10-08 00:17:45 +0000282Archive::child_iterator Archive::end_children() const {
Rafael Espindolabe6b9102013-07-09 05:26:25 +0000283 return Child(this, NULL);
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000284}
Michael J. Spencerc8a55a62011-11-02 19:33:12 +0000285
286error_code Archive::Symbol::getName(StringRef &Result) const {
Michael J. Spencer0f76e642013-02-03 10:48:50 +0000287 Result = StringRef(Parent->SymbolTable->getBuffer().begin() + StringIndex);
Michael J. Spencerc8a55a62011-11-02 19:33:12 +0000288 return object_error::success;
289}
290
291error_code Archive::Symbol::getMember(child_iterator &Result) const {
Michael J. Spencer0f76e642013-02-03 10:48:50 +0000292 const char *Buf = Parent->SymbolTable->getBuffer().begin();
Michael J. Spencer768a7072012-11-14 00:04:13 +0000293 const char *Offsets = Buf + 4;
294 uint32_t Offset = 0;
Shankar Easwaran206252c2012-11-13 18:38:42 +0000295 if (Parent->kind() == K_GNU) {
Michael J. Spencer768a7072012-11-14 00:04:13 +0000296 Offset = *(reinterpret_cast<const support::ubig32_t*>(Offsets)
297 + SymbolIndex);
Shankar Easwaran206252c2012-11-13 18:38:42 +0000298 } else if (Parent->kind() == K_BSD) {
Matt Beaumont-Gay7af4b9b2012-11-14 17:58:11 +0000299 llvm_unreachable("BSD format is not supported");
Shankar Easwaran206252c2012-11-13 18:38:42 +0000300 } else {
Michael J. Spencer768a7072012-11-14 00:04:13 +0000301 uint32_t MemberCount = *reinterpret_cast<const support::ulittle32_t*>(Buf);
302
303 // Skip offsets.
304 Buf += sizeof(support::ulittle32_t)
305 + (MemberCount * sizeof(support::ulittle32_t));
306
307 uint32_t SymbolCount = *reinterpret_cast<const support::ulittle32_t*>(Buf);
308
309 if (SymbolIndex >= SymbolCount)
310 return object_error::parse_failed;
311
Matt Beaumont-Gayf1c2a6b2012-11-14 00:21:27 +0000312 // Skip SymbolCount to get to the indices table.
313 const char *Indices = Buf + sizeof(support::ulittle32_t);
Michael J. Spencer768a7072012-11-14 00:04:13 +0000314
315 // Get the index of the offset in the file member offset table for this
316 // symbol.
317 uint16_t OffsetIndex =
Matt Beaumont-Gayf1c2a6b2012-11-14 00:21:27 +0000318 *(reinterpret_cast<const support::ulittle16_t*>(Indices)
Shankar Easwaran206252c2012-11-13 18:38:42 +0000319 + SymbolIndex);
Michael J. Spencer768a7072012-11-14 00:04:13 +0000320 // Subtract 1 since OffsetIndex is 1 based.
321 --OffsetIndex;
322
323 if (OffsetIndex >= MemberCount)
324 return object_error::parse_failed;
325
326 Offset = *(reinterpret_cast<const support::ulittle32_t*>(Offsets)
327 + OffsetIndex);
Shankar Easwaran206252c2012-11-13 18:38:42 +0000328 }
Michael J. Spencerc8a55a62011-11-02 19:33:12 +0000329
Michael J. Spencer768a7072012-11-14 00:04:13 +0000330 const char *Loc = Parent->getData().begin() + Offset;
Rafael Espindolabe6b9102013-07-09 05:26:25 +0000331 Result = Child(Parent, Loc);
Michael J. Spencerc8a55a62011-11-02 19:33:12 +0000332
333 return object_error::success;
334}
335
336Archive::Symbol Archive::Symbol::getNext() const {
337 Symbol t(*this);
Benjamin Kramerefd2d5e2011-11-04 13:52:17 +0000338 // Go to one past next null.
339 t.StringIndex =
Michael J. Spencer0f76e642013-02-03 10:48:50 +0000340 Parent->SymbolTable->getBuffer().find('\0', t.StringIndex) + 1;
Michael J. Spencerc8a55a62011-11-02 19:33:12 +0000341 ++t.SymbolIndex;
342 return t;
343}
344
345Archive::symbol_iterator Archive::begin_symbols() const {
Michael J. Spencer0f76e642013-02-03 10:48:50 +0000346 const char *buf = SymbolTable->getBuffer().begin();
Shankar Easwaran206252c2012-11-13 18:38:42 +0000347 if (kind() == K_GNU) {
348 uint32_t symbol_count = 0;
349 symbol_count = *reinterpret_cast<const support::ubig32_t*>(buf);
350 buf += sizeof(uint32_t) + (symbol_count * (sizeof(uint32_t)));
351 } else if (kind() == K_BSD) {
Matt Beaumont-Gay7af4b9b2012-11-14 17:58:11 +0000352 llvm_unreachable("BSD archive format is not supported");
Shankar Easwaran206252c2012-11-13 18:38:42 +0000353 } else {
354 uint32_t member_count = 0;
355 uint32_t symbol_count = 0;
356 member_count = *reinterpret_cast<const support::ulittle32_t*>(buf);
357 buf += 4 + (member_count * 4); // Skip offsets.
358 symbol_count = *reinterpret_cast<const support::ulittle32_t*>(buf);
359 buf += 4 + (symbol_count * 2); // Skip indices.
360 }
Michael J. Spencer0f76e642013-02-03 10:48:50 +0000361 uint32_t string_start_offset = buf - SymbolTable->getBuffer().begin();
Michael J. Spencerc8a55a62011-11-02 19:33:12 +0000362 return symbol_iterator(Symbol(this, 0, string_start_offset));
363}
364
365Archive::symbol_iterator Archive::end_symbols() const {
Michael J. Spencer0f76e642013-02-03 10:48:50 +0000366 const char *buf = SymbolTable->getBuffer().begin();
Shankar Easwaran206252c2012-11-13 18:38:42 +0000367 uint32_t symbol_count = 0;
368 if (kind() == K_GNU) {
369 symbol_count = *reinterpret_cast<const support::ubig32_t*>(buf);
Shankar Easwaran206252c2012-11-13 18:38:42 +0000370 } else if (kind() == K_BSD) {
Matt Beaumont-Gay7af4b9b2012-11-14 17:58:11 +0000371 llvm_unreachable("BSD archive format is not supported");
Shankar Easwaran206252c2012-11-13 18:38:42 +0000372 } else {
373 uint32_t member_count = 0;
374 member_count = *reinterpret_cast<const support::ulittle32_t*>(buf);
375 buf += 4 + (member_count * 4); // Skip offsets.
376 symbol_count = *reinterpret_cast<const support::ulittle32_t*>(buf);
377 }
Michael J. Spencerc8a55a62011-11-02 19:33:12 +0000378 return symbol_iterator(
379 Symbol(this, symbol_count, 0));
380}
Shankar Easwaran206252c2012-11-13 18:38:42 +0000381
382Archive::child_iterator Archive::findSym(StringRef name) const {
383 Archive::symbol_iterator bs = begin_symbols();
384 Archive::symbol_iterator es = end_symbols();
385 Archive::child_iterator result;
386
387 StringRef symname;
388 for (; bs != es; ++bs) {
389 if (bs->getName(symname))
390 return end_children();
391 if (symname == name) {
392 if (bs->getMember(result))
393 return end_children();
394 return result;
395 }
396 }
397 return end_children();
398}