blob: 91cc4efde22d4ffb8a6a16442b38690b02409d8d [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
Rafael Espindola20125932013-07-09 12:45:11 +000057uint32_t ArchiveMemberHeader::getSize() const {
58 uint32_t Ret;
59 if (llvm::StringRef(Size, sizeof(Size)).rtrim(" ").getAsInteger(10, Ret))
60 llvm_unreachable("Size is not a decimal number.");
61 return Ret;
Rafael Espindola5263d0a2013-07-09 03:39:35 +000062}
63
Rafael Espindola9941bdd2013-07-09 12:49:24 +000064sys::fs::perms ArchiveMemberHeader::getAccessMode() const {
65 unsigned Ret;
66 if (StringRef(AccessMode, sizeof(AccessMode)).rtrim(" ").getAsInteger(8, Ret))
67 llvm_unreachable("Access mode is not an octal number.");
68 return static_cast<sys::fs::perms>(Ret);
69}
70
71sys::TimeValue ArchiveMemberHeader::getLastModified() const {
72 unsigned Seconds;
73 if (StringRef(LastModified, sizeof(LastModified)).rtrim(" ")
74 .getAsInteger(10, Seconds))
75 llvm_unreachable("Last modified time not a decimal number.");
76
77 sys::TimeValue Ret;
78 Ret.fromEpochTime(Seconds);
79 return Ret;
80}
81
82unsigned ArchiveMemberHeader::getUID() const {
83 unsigned Ret;
84 if (StringRef(UID, sizeof(UID)).rtrim(" ").getAsInteger(10, Ret))
85 llvm_unreachable("UID time not a decimal number.");
86 return Ret;
87}
88
89unsigned ArchiveMemberHeader::getGID() const {
90 unsigned Ret;
91 if (StringRef(GID, sizeof(GID)).rtrim(" ").getAsInteger(10, Ret))
92 llvm_unreachable("GID time not a decimal number.");
93 return Ret;
94}
95
Rafael Espindolac5f87572013-07-09 12:22:05 +000096static const ArchiveMemberHeader *toHeader(const char *base) {
97 return reinterpret_cast<const ArchiveMemberHeader *>(base);
98}
99
Rafael Espindolabe6b9102013-07-09 05:26:25 +0000100Archive::Child::Child(const Archive *Parent, const char *Start)
101 : Parent(Parent) {
102 if (!Start)
Rafael Espindola5263d0a2013-07-09 03:39:35 +0000103 return;
Rafael Espindolabe6b9102013-07-09 05:26:25 +0000104
Rafael Espindolac5f87572013-07-09 12:22:05 +0000105 const ArchiveMemberHeader *Header = toHeader(Start);
Rafael Espindolabe6b9102013-07-09 05:26:25 +0000106 Data = StringRef(Start, sizeof(ArchiveMemberHeader) + Header->getSize());
107
Rafael Espindola5263d0a2013-07-09 03:39:35 +0000108 // Setup StartOfFile and PaddingBytes.
109 StartOfFile = sizeof(ArchiveMemberHeader);
110 // Don't include attached name.
Rafael Espindolabe6b9102013-07-09 05:26:25 +0000111 StringRef Name = Header->getName();
Rafael Espindola5263d0a2013-07-09 03:39:35 +0000112 if (Name.startswith("#1/")) {
113 uint64_t NameSize;
114 if (Name.substr(3).rtrim(" ").getAsInteger(10, NameSize))
115 llvm_unreachable("Long name length is not an integer");
116 StartOfFile += NameSize;
117 }
118}
119
120Archive::Child Archive::Child::getNext() const {
121 size_t SpaceToSkip = Data.size();
122 // If it's odd, add 1 to make it even.
123 if (SpaceToSkip & 1)
124 ++SpaceToSkip;
125
126 const char *NextLoc = Data.data() + SpaceToSkip;
127
128 // Check to see if this is past the end of the archive.
129 if (NextLoc >= Parent->Data->getBufferEnd())
Rafael Espindolabe6b9102013-07-09 05:26:25 +0000130 return Child(Parent, NULL);
Rafael Espindola5263d0a2013-07-09 03:39:35 +0000131
Rafael Espindolabe6b9102013-07-09 05:26:25 +0000132 return Child(Parent, NextLoc);
Rafael Espindola5263d0a2013-07-09 03:39:35 +0000133}
134
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000135error_code Archive::Child::getName(StringRef &Result) const {
Rafael Espindola4a0bf542013-07-05 03:35:15 +0000136 StringRef name = getRawName();
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000137 // Check if it's a special name.
138 if (name[0] == '/') {
139 if (name.size() == 1) { // Linker member.
140 Result = name;
141 return object_error::success;
142 }
143 if (name.size() == 2 && name[1] == '/') { // String table.
144 Result = name;
145 return object_error::success;
146 }
147 // It's a long name.
148 // Get the offset.
Michael J. Spencerbf82b072013-01-10 00:07:38 +0000149 std::size_t offset;
Michael J. Spencer7932c412013-01-10 01:05:34 +0000150 if (name.substr(1).rtrim(" ").getAsInteger(10, offset))
151 llvm_unreachable("Long name offset is not an integer");
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000152 const char *addr = Parent->StringTable->Data.begin()
153 + sizeof(ArchiveMemberHeader)
Michael J. Spencere92800d2013-01-09 22:58:43 +0000154 + offset;
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000155 // Verify it.
156 if (Parent->StringTable == Parent->end_children()
157 || addr < (Parent->StringTable->Data.begin()
158 + sizeof(ArchiveMemberHeader))
159 || addr > (Parent->StringTable->Data.begin()
160 + sizeof(ArchiveMemberHeader)
161 + Parent->StringTable->getSize()))
162 return object_error::parse_failed;
Shankar Easwaran206252c2012-11-13 18:38:42 +0000163
164 // GNU long file names end with a /.
165 if (Parent->kind() == K_GNU) {
166 StringRef::size_type End = StringRef(addr).find('/');
167 Result = StringRef(addr, End);
168 } else {
169 Result = addr;
170 }
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000171 return object_error::success;
Michael J. Spenceraaf98ea2011-10-25 22:30:42 +0000172 } else if (name.startswith("#1/")) {
Michael J. Spencere92800d2013-01-09 22:58:43 +0000173 uint64_t name_size;
Michael J. Spencer7932c412013-01-10 01:05:34 +0000174 if (name.substr(3).rtrim(" ").getAsInteger(10, name_size))
175 llvm_unreachable("Long name length is not an ingeter");
176 Result = Data.substr(sizeof(ArchiveMemberHeader), name_size);
Michael J. Spenceraaf98ea2011-10-25 22:30:42 +0000177 return object_error::success;
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000178 }
179 // It's a simple name.
180 if (name[name.size() - 1] == '/')
181 Result = name.substr(0, name.size() - 1);
182 else
183 Result = name;
184 return object_error::success;
185}
186
Rafael Espindola5263d0a2013-07-09 03:39:35 +0000187error_code Archive::Child::getMemoryBuffer(OwningPtr<MemoryBuffer> &Result,
188 bool FullPath) const {
189 StringRef Name;
190 if (error_code ec = getName(Name))
191 return ec;
192 SmallString<128> Path;
193 Result.reset(MemoryBuffer::getMemBuffer(
194 getBuffer(), FullPath ? (Twine(Parent->getFileName()) + "(" + Name + ")")
195 .toStringRef(Path)
196 : Name,
197 false));
198 return error_code::success();
199}
200
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000201error_code Archive::Child::getAsBinary(OwningPtr<Binary> &Result) const {
202 OwningPtr<Binary> ret;
Michael J. Spencer0f76e642013-02-03 10:48:50 +0000203 OwningPtr<MemoryBuffer> Buff;
204 if (error_code ec = getMemoryBuffer(Buff))
205 return ec;
206 if (error_code ec = createBinary(Buff.take(), ret))
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000207 return ec;
208 Result.swap(ret);
209 return object_error::success;
210}
211
212Archive::Archive(MemoryBuffer *source, error_code &ec)
Rafael Espindola51597182013-07-10 20:14:22 +0000213 : Binary(Binary::ID_Archive, source), SymbolTable(end_children()) {
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000214 // Check for sufficient magic.
215 if (!source || source->getBufferSize()
Rafael Espindola6d88f9b2013-07-04 19:40:23 +0000216 < (8 + sizeof(ArchiveMemberHeader)) // Smallest archive.
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000217 || StringRef(source->getBufferStart(), 8) != Magic) {
218 ec = object_error::invalid_file_type;
219 return;
220 }
221
Michael J. Spencerc8a55a62011-11-02 19:33:12 +0000222 // Get the special members.
223 child_iterator i = begin_children(false);
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000224 child_iterator e = end_children();
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000225
Rafael Espindola09a7f602013-07-03 15:57:14 +0000226 if (i == e) {
227 ec = object_error::parse_failed;
228 return;
229 }
230
Rafael Espindola4a0bf542013-07-05 03:35:15 +0000231 StringRef Name = i->getRawName();
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000232
Shankar Easwaran206252c2012-11-13 18:38:42 +0000233 // Below is the pattern that is used to figure out the archive format
234 // GNU archive format
Rafael Espindola09a7f602013-07-03 15:57:14 +0000235 // First member : / (may exist, if it exists, points to the symbol table )
Shankar Easwaran206252c2012-11-13 18:38:42 +0000236 // Second member : // (may exist, if it exists, points to the string table)
237 // Note : The string table is used if the filename exceeds 15 characters
238 // BSD archive format
239 // First member : __.SYMDEF (points to the symbol table)
240 // There is no string table, if the filename exceeds 15 characters or has a
241 // embedded space, the filename has #1/<size>, The size represents the size
242 // of the filename that needs to be read after the archive header
243 // COFF archive format
244 // First member : /
245 // Second member : / (provides a directory of symbols)
Rui Ueyama891c0cd2013-06-03 00:27:03 +0000246 // Third member : // (may exist, if it exists, contains the string table)
247 // Note: Microsoft PE/COFF Spec 8.3 says that the third member is present
248 // even if the string table is empty. However, lib.exe does not in fact
249 // seem to create the third member if there's no member whose filename
250 // exceeds 15 characters. So the third member is optional.
Rafael Espindola09a7f602013-07-03 15:57:14 +0000251
Rafael Espindola4a0bf542013-07-05 03:35:15 +0000252 if (Name == "__.SYMDEF") {
Rafael Espindola09a7f602013-07-03 15:57:14 +0000253 Format = K_BSD;
254 SymbolTable = i;
255 ec = object_error::success;
256 return;
257 }
258
Rafael Espindola4a0bf542013-07-05 03:35:15 +0000259 if (Name == "/") {
Shankar Easwaran206252c2012-11-13 18:38:42 +0000260 SymbolTable = i;
Rafael Espindola09a7f602013-07-03 15:57:14 +0000261
262 ++i;
Michael J. Spencerbf82b072013-01-10 00:07:38 +0000263 if (i == e) {
264 ec = object_error::parse_failed;
265 return;
266 }
Rafael Espindola4a0bf542013-07-05 03:35:15 +0000267 Name = i->getRawName();
Rafael Espindola09a7f602013-07-03 15:57:14 +0000268 }
269
Rafael Espindola4a0bf542013-07-05 03:35:15 +0000270 if (Name == "//") {
Rafael Espindola09a7f602013-07-03 15:57:14 +0000271 Format = K_GNU;
272 StringTable = i;
273 ec = object_error::success;
274 return;
275 }
276
Rafael Espindola4a0bf542013-07-05 03:35:15 +0000277 if (Name[0] != '/') {
Rafael Espindola09a7f602013-07-03 15:57:14 +0000278 Format = K_GNU;
279 ec = object_error::success;
280 return;
281 }
282
Rafael Espindola4a0bf542013-07-05 03:35:15 +0000283 if (Name != "/") {
Rafael Espindola09a7f602013-07-03 15:57:14 +0000284 ec = object_error::parse_failed;
285 return;
286 }
287
288 Format = K_COFF;
289 SymbolTable = i;
290
291 ++i;
292 if (i == e) {
293 ec = object_error::success;
294 return;
295 }
296
Rafael Espindola4a0bf542013-07-05 03:35:15 +0000297 Name = i->getRawName();
Rafael Espindola09a7f602013-07-03 15:57:14 +0000298
Rafael Espindola4a0bf542013-07-05 03:35:15 +0000299 if (Name == "//")
Rafael Espindola09a7f602013-07-03 15:57:14 +0000300 StringTable = i;
301
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000302 ec = object_error::success;
303}
304
Michael J. Spenceraaf98ea2011-10-25 22:30:42 +0000305Archive::child_iterator Archive::begin_children(bool skip_internal) const {
Benjamin Kramere1a44272012-02-22 13:42:11 +0000306 const char *Loc = Data->getBufferStart() + strlen(Magic);
Rafael Espindolabe6b9102013-07-09 05:26:25 +0000307 Child c(this, Loc);
Michael J. Spenceraaf98ea2011-10-25 22:30:42 +0000308 // Skip internals at the beginning of an archive.
Rafael Espindolac5f87572013-07-09 12:22:05 +0000309 if (skip_internal && isInternalMember(*toHeader(Loc)))
Michael J. Spenceraaf98ea2011-10-25 22:30:42 +0000310 return c.getNext();
311 return c;
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000312}
313
Michael J. Spencer58618932011-10-08 00:17:45 +0000314Archive::child_iterator Archive::end_children() const {
Rafael Espindolabe6b9102013-07-09 05:26:25 +0000315 return Child(this, NULL);
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000316}
Michael J. Spencerc8a55a62011-11-02 19:33:12 +0000317
318error_code Archive::Symbol::getName(StringRef &Result) const {
Michael J. Spencer0f76e642013-02-03 10:48:50 +0000319 Result = StringRef(Parent->SymbolTable->getBuffer().begin() + StringIndex);
Michael J. Spencerc8a55a62011-11-02 19:33:12 +0000320 return object_error::success;
321}
322
323error_code Archive::Symbol::getMember(child_iterator &Result) const {
Michael J. Spencer0f76e642013-02-03 10:48:50 +0000324 const char *Buf = Parent->SymbolTable->getBuffer().begin();
Michael J. Spencer768a7072012-11-14 00:04:13 +0000325 const char *Offsets = Buf + 4;
326 uint32_t Offset = 0;
Shankar Easwaran206252c2012-11-13 18:38:42 +0000327 if (Parent->kind() == K_GNU) {
Michael J. Spencer768a7072012-11-14 00:04:13 +0000328 Offset = *(reinterpret_cast<const support::ubig32_t*>(Offsets)
329 + SymbolIndex);
Shankar Easwaran206252c2012-11-13 18:38:42 +0000330 } else if (Parent->kind() == K_BSD) {
Matt Beaumont-Gay7af4b9b2012-11-14 17:58:11 +0000331 llvm_unreachable("BSD format is not supported");
Shankar Easwaran206252c2012-11-13 18:38:42 +0000332 } else {
Michael J. Spencer768a7072012-11-14 00:04:13 +0000333 uint32_t MemberCount = *reinterpret_cast<const support::ulittle32_t*>(Buf);
334
335 // Skip offsets.
336 Buf += sizeof(support::ulittle32_t)
337 + (MemberCount * sizeof(support::ulittle32_t));
338
339 uint32_t SymbolCount = *reinterpret_cast<const support::ulittle32_t*>(Buf);
340
341 if (SymbolIndex >= SymbolCount)
342 return object_error::parse_failed;
343
Matt Beaumont-Gayf1c2a6b2012-11-14 00:21:27 +0000344 // Skip SymbolCount to get to the indices table.
345 const char *Indices = Buf + sizeof(support::ulittle32_t);
Michael J. Spencer768a7072012-11-14 00:04:13 +0000346
347 // Get the index of the offset in the file member offset table for this
348 // symbol.
349 uint16_t OffsetIndex =
Matt Beaumont-Gayf1c2a6b2012-11-14 00:21:27 +0000350 *(reinterpret_cast<const support::ulittle16_t*>(Indices)
Shankar Easwaran206252c2012-11-13 18:38:42 +0000351 + SymbolIndex);
Michael J. Spencer768a7072012-11-14 00:04:13 +0000352 // Subtract 1 since OffsetIndex is 1 based.
353 --OffsetIndex;
354
355 if (OffsetIndex >= MemberCount)
356 return object_error::parse_failed;
357
358 Offset = *(reinterpret_cast<const support::ulittle32_t*>(Offsets)
359 + OffsetIndex);
Shankar Easwaran206252c2012-11-13 18:38:42 +0000360 }
Michael J. Spencerc8a55a62011-11-02 19:33:12 +0000361
Michael J. Spencer768a7072012-11-14 00:04:13 +0000362 const char *Loc = Parent->getData().begin() + Offset;
Rafael Espindolabe6b9102013-07-09 05:26:25 +0000363 Result = Child(Parent, Loc);
Michael J. Spencerc8a55a62011-11-02 19:33:12 +0000364
365 return object_error::success;
366}
367
368Archive::Symbol Archive::Symbol::getNext() const {
369 Symbol t(*this);
Benjamin Kramerefd2d5e2011-11-04 13:52:17 +0000370 // Go to one past next null.
371 t.StringIndex =
Michael J. Spencer0f76e642013-02-03 10:48:50 +0000372 Parent->SymbolTable->getBuffer().find('\0', t.StringIndex) + 1;
Michael J. Spencerc8a55a62011-11-02 19:33:12 +0000373 ++t.SymbolIndex;
374 return t;
375}
376
377Archive::symbol_iterator Archive::begin_symbols() const {
Rafael Espindola51597182013-07-10 20:14:22 +0000378 if (SymbolTable == end_children())
379 return symbol_iterator(Symbol(this, 0, 0));
380
Michael J. Spencer0f76e642013-02-03 10:48:50 +0000381 const char *buf = SymbolTable->getBuffer().begin();
Shankar Easwaran206252c2012-11-13 18:38:42 +0000382 if (kind() == K_GNU) {
383 uint32_t symbol_count = 0;
384 symbol_count = *reinterpret_cast<const support::ubig32_t*>(buf);
385 buf += sizeof(uint32_t) + (symbol_count * (sizeof(uint32_t)));
386 } else if (kind() == K_BSD) {
Matt Beaumont-Gay7af4b9b2012-11-14 17:58:11 +0000387 llvm_unreachable("BSD archive format is not supported");
Shankar Easwaran206252c2012-11-13 18:38:42 +0000388 } else {
389 uint32_t member_count = 0;
390 uint32_t symbol_count = 0;
391 member_count = *reinterpret_cast<const support::ulittle32_t*>(buf);
392 buf += 4 + (member_count * 4); // Skip offsets.
393 symbol_count = *reinterpret_cast<const support::ulittle32_t*>(buf);
394 buf += 4 + (symbol_count * 2); // Skip indices.
395 }
Michael J. Spencer0f76e642013-02-03 10:48:50 +0000396 uint32_t string_start_offset = buf - SymbolTable->getBuffer().begin();
Michael J. Spencerc8a55a62011-11-02 19:33:12 +0000397 return symbol_iterator(Symbol(this, 0, string_start_offset));
398}
399
400Archive::symbol_iterator Archive::end_symbols() const {
Rafael Espindola51597182013-07-10 20:14:22 +0000401 if (SymbolTable == end_children())
402 return symbol_iterator(Symbol(this, 0, 0));
403
Michael J. Spencer0f76e642013-02-03 10:48:50 +0000404 const char *buf = SymbolTable->getBuffer().begin();
Shankar Easwaran206252c2012-11-13 18:38:42 +0000405 uint32_t symbol_count = 0;
406 if (kind() == K_GNU) {
407 symbol_count = *reinterpret_cast<const support::ubig32_t*>(buf);
Shankar Easwaran206252c2012-11-13 18:38:42 +0000408 } else if (kind() == K_BSD) {
Matt Beaumont-Gay7af4b9b2012-11-14 17:58:11 +0000409 llvm_unreachable("BSD archive format is not supported");
Shankar Easwaran206252c2012-11-13 18:38:42 +0000410 } else {
411 uint32_t member_count = 0;
412 member_count = *reinterpret_cast<const support::ulittle32_t*>(buf);
413 buf += 4 + (member_count * 4); // Skip offsets.
414 symbol_count = *reinterpret_cast<const support::ulittle32_t*>(buf);
415 }
Michael J. Spencerc8a55a62011-11-02 19:33:12 +0000416 return symbol_iterator(
417 Symbol(this, symbol_count, 0));
418}
Shankar Easwaran206252c2012-11-13 18:38:42 +0000419
420Archive::child_iterator Archive::findSym(StringRef name) const {
421 Archive::symbol_iterator bs = begin_symbols();
422 Archive::symbol_iterator es = end_symbols();
423 Archive::child_iterator result;
424
425 StringRef symname;
426 for (; bs != es; ++bs) {
427 if (bs->getName(symname))
428 return end_children();
429 if (symname == name) {
430 if (bs->getMember(result))
431 return end_children();
432 return result;
433 }
434 }
435 return end_children();
436}