blob: 5585bc44f7e7141fd63f5d934ef907cf27d7ad87 [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
64Archive::Child::Child(const Archive *p, StringRef d) : Parent(p), Data(d) {
65 if (!p || d.empty())
66 return;
67 // Setup StartOfFile and PaddingBytes.
68 StartOfFile = sizeof(ArchiveMemberHeader);
69 // Don't include attached name.
70 StringRef Name = ToHeader(Data.data())->getName();
71 if (Name.startswith("#1/")) {
72 uint64_t NameSize;
73 if (Name.substr(3).rtrim(" ").getAsInteger(10, NameSize))
74 llvm_unreachable("Long name length is not an integer");
75 StartOfFile += NameSize;
76 }
77}
78
79Archive::Child Archive::Child::getNext() const {
80 size_t SpaceToSkip = Data.size();
81 // If it's odd, add 1 to make it even.
82 if (SpaceToSkip & 1)
83 ++SpaceToSkip;
84
85 const char *NextLoc = Data.data() + SpaceToSkip;
86
87 // Check to see if this is past the end of the archive.
88 if (NextLoc >= Parent->Data->getBufferEnd())
89 return Child(Parent, StringRef(0, 0));
90
91 size_t NextSize = sizeof(ArchiveMemberHeader) + ToHeader(NextLoc)->getSize();
92
93 return Child(Parent, StringRef(NextLoc, NextSize));
94}
95
Michael J. Spencera51d7d92011-09-27 19:36:55 +000096error_code Archive::Child::getName(StringRef &Result) const {
Rafael Espindola4a0bf542013-07-05 03:35:15 +000097 StringRef name = getRawName();
Michael J. Spencera51d7d92011-09-27 19:36:55 +000098 // Check if it's a special name.
99 if (name[0] == '/') {
100 if (name.size() == 1) { // Linker member.
101 Result = name;
102 return object_error::success;
103 }
104 if (name.size() == 2 && name[1] == '/') { // String table.
105 Result = name;
106 return object_error::success;
107 }
108 // It's a long name.
109 // Get the offset.
Michael J. Spencerbf82b072013-01-10 00:07:38 +0000110 std::size_t offset;
Michael J. Spencer7932c412013-01-10 01:05:34 +0000111 if (name.substr(1).rtrim(" ").getAsInteger(10, offset))
112 llvm_unreachable("Long name offset is not an integer");
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000113 const char *addr = Parent->StringTable->Data.begin()
114 + sizeof(ArchiveMemberHeader)
Michael J. Spencere92800d2013-01-09 22:58:43 +0000115 + offset;
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000116 // Verify it.
117 if (Parent->StringTable == Parent->end_children()
118 || addr < (Parent->StringTable->Data.begin()
119 + sizeof(ArchiveMemberHeader))
120 || addr > (Parent->StringTable->Data.begin()
121 + sizeof(ArchiveMemberHeader)
122 + Parent->StringTable->getSize()))
123 return object_error::parse_failed;
Shankar Easwaran206252c2012-11-13 18:38:42 +0000124
125 // GNU long file names end with a /.
126 if (Parent->kind() == K_GNU) {
127 StringRef::size_type End = StringRef(addr).find('/');
128 Result = StringRef(addr, End);
129 } else {
130 Result = addr;
131 }
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000132 return object_error::success;
Michael J. Spenceraaf98ea2011-10-25 22:30:42 +0000133 } else if (name.startswith("#1/")) {
Michael J. Spencere92800d2013-01-09 22:58:43 +0000134 uint64_t name_size;
Michael J. Spencer7932c412013-01-10 01:05:34 +0000135 if (name.substr(3).rtrim(" ").getAsInteger(10, name_size))
136 llvm_unreachable("Long name length is not an ingeter");
137 Result = Data.substr(sizeof(ArchiveMemberHeader), name_size);
Michael J. Spenceraaf98ea2011-10-25 22:30:42 +0000138 return object_error::success;
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000139 }
140 // It's a simple name.
141 if (name[name.size() - 1] == '/')
142 Result = name.substr(0, name.size() - 1);
143 else
144 Result = name;
145 return object_error::success;
146}
147
Rafael Espindola5263d0a2013-07-09 03:39:35 +0000148error_code Archive::Child::getMemoryBuffer(OwningPtr<MemoryBuffer> &Result,
149 bool FullPath) const {
150 StringRef Name;
151 if (error_code ec = getName(Name))
152 return ec;
153 SmallString<128> Path;
154 Result.reset(MemoryBuffer::getMemBuffer(
155 getBuffer(), FullPath ? (Twine(Parent->getFileName()) + "(" + Name + ")")
156 .toStringRef(Path)
157 : Name,
158 false));
159 return error_code::success();
160}
161
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000162error_code Archive::Child::getAsBinary(OwningPtr<Binary> &Result) const {
163 OwningPtr<Binary> ret;
Michael J. Spencer0f76e642013-02-03 10:48:50 +0000164 OwningPtr<MemoryBuffer> Buff;
165 if (error_code ec = getMemoryBuffer(Buff))
166 return ec;
167 if (error_code ec = createBinary(Buff.take(), ret))
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000168 return ec;
169 Result.swap(ret);
170 return object_error::success;
171}
172
173Archive::Archive(MemoryBuffer *source, error_code &ec)
David Meyer6f9489a2012-03-09 20:41:57 +0000174 : Binary(Binary::ID_Archive, source) {
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000175 // Check for sufficient magic.
176 if (!source || source->getBufferSize()
Rafael Espindola6d88f9b2013-07-04 19:40:23 +0000177 < (8 + sizeof(ArchiveMemberHeader)) // Smallest archive.
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000178 || StringRef(source->getBufferStart(), 8) != Magic) {
179 ec = object_error::invalid_file_type;
180 return;
181 }
182
Michael J. Spencerc8a55a62011-11-02 19:33:12 +0000183 // Get the special members.
184 child_iterator i = begin_children(false);
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000185 child_iterator e = end_children();
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000186
Rafael Espindola09a7f602013-07-03 15:57:14 +0000187 if (i == e) {
188 ec = object_error::parse_failed;
189 return;
190 }
191
Rafael Espindola4a0bf542013-07-05 03:35:15 +0000192 StringRef Name = i->getRawName();
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000193
Shankar Easwaran206252c2012-11-13 18:38:42 +0000194 // Below is the pattern that is used to figure out the archive format
195 // GNU archive format
Rafael Espindola09a7f602013-07-03 15:57:14 +0000196 // First member : / (may exist, if it exists, points to the symbol table )
Shankar Easwaran206252c2012-11-13 18:38:42 +0000197 // Second member : // (may exist, if it exists, points to the string table)
198 // Note : The string table is used if the filename exceeds 15 characters
199 // BSD archive format
200 // First member : __.SYMDEF (points to the symbol table)
201 // There is no string table, if the filename exceeds 15 characters or has a
202 // embedded space, the filename has #1/<size>, The size represents the size
203 // of the filename that needs to be read after the archive header
204 // COFF archive format
205 // First member : /
206 // Second member : / (provides a directory of symbols)
Rui Ueyama891c0cd2013-06-03 00:27:03 +0000207 // Third member : // (may exist, if it exists, contains the string table)
208 // Note: Microsoft PE/COFF Spec 8.3 says that the third member is present
209 // even if the string table is empty. However, lib.exe does not in fact
210 // seem to create the third member if there's no member whose filename
211 // exceeds 15 characters. So the third member is optional.
Rafael Espindola09a7f602013-07-03 15:57:14 +0000212
Rafael Espindola4a0bf542013-07-05 03:35:15 +0000213 if (Name == "__.SYMDEF") {
Rafael Espindola09a7f602013-07-03 15:57:14 +0000214 Format = K_BSD;
215 SymbolTable = i;
216 ec = object_error::success;
217 return;
218 }
219
Rafael Espindola4a0bf542013-07-05 03:35:15 +0000220 if (Name == "/") {
Shankar Easwaran206252c2012-11-13 18:38:42 +0000221 SymbolTable = i;
Rafael Espindola09a7f602013-07-03 15:57:14 +0000222
223 ++i;
Michael J. Spencerbf82b072013-01-10 00:07:38 +0000224 if (i == e) {
225 ec = object_error::parse_failed;
226 return;
227 }
Rafael Espindola4a0bf542013-07-05 03:35:15 +0000228 Name = i->getRawName();
Rafael Espindola09a7f602013-07-03 15:57:14 +0000229 }
230
Rafael Espindola4a0bf542013-07-05 03:35:15 +0000231 if (Name == "//") {
Rafael Espindola09a7f602013-07-03 15:57:14 +0000232 Format = K_GNU;
233 StringTable = i;
234 ec = object_error::success;
235 return;
236 }
237
Rafael Espindola4a0bf542013-07-05 03:35:15 +0000238 if (Name[0] != '/') {
Rafael Espindola09a7f602013-07-03 15:57:14 +0000239 Format = K_GNU;
240 ec = object_error::success;
241 return;
242 }
243
Rafael Espindola4a0bf542013-07-05 03:35:15 +0000244 if (Name != "/") {
Rafael Espindola09a7f602013-07-03 15:57:14 +0000245 ec = object_error::parse_failed;
246 return;
247 }
248
249 Format = K_COFF;
250 SymbolTable = i;
251
252 ++i;
253 if (i == e) {
254 ec = object_error::success;
255 return;
256 }
257
Rafael Espindola4a0bf542013-07-05 03:35:15 +0000258 Name = i->getRawName();
Rafael Espindola09a7f602013-07-03 15:57:14 +0000259
Rafael Espindola4a0bf542013-07-05 03:35:15 +0000260 if (Name == "//")
Rafael Espindola09a7f602013-07-03 15:57:14 +0000261 StringTable = i;
262
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000263 ec = object_error::success;
264}
265
Michael J. Spenceraaf98ea2011-10-25 22:30:42 +0000266Archive::child_iterator Archive::begin_children(bool skip_internal) const {
Benjamin Kramere1a44272012-02-22 13:42:11 +0000267 const char *Loc = Data->getBufferStart() + strlen(Magic);
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000268 size_t Size = sizeof(ArchiveMemberHeader) +
269 ToHeader(Loc)->getSize();
Michael J. Spenceraaf98ea2011-10-25 22:30:42 +0000270 Child c(this, StringRef(Loc, Size));
271 // Skip internals at the beginning of an archive.
272 if (skip_internal && isInternalMember(*ToHeader(Loc)))
273 return c.getNext();
274 return c;
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000275}
276
Michael J. Spencer58618932011-10-08 00:17:45 +0000277Archive::child_iterator Archive::end_children() const {
Michael J. Spencera51d7d92011-09-27 19:36:55 +0000278 return Child(this, StringRef(0, 0));
279}
Michael J. Spencerc8a55a62011-11-02 19:33:12 +0000280
281error_code Archive::Symbol::getName(StringRef &Result) const {
Michael J. Spencer0f76e642013-02-03 10:48:50 +0000282 Result = StringRef(Parent->SymbolTable->getBuffer().begin() + StringIndex);
Michael J. Spencerc8a55a62011-11-02 19:33:12 +0000283 return object_error::success;
284}
285
286error_code Archive::Symbol::getMember(child_iterator &Result) const {
Michael J. Spencer0f76e642013-02-03 10:48:50 +0000287 const char *Buf = Parent->SymbolTable->getBuffer().begin();
Michael J. Spencer768a7072012-11-14 00:04:13 +0000288 const char *Offsets = Buf + 4;
289 uint32_t Offset = 0;
Shankar Easwaran206252c2012-11-13 18:38:42 +0000290 if (Parent->kind() == K_GNU) {
Michael J. Spencer768a7072012-11-14 00:04:13 +0000291 Offset = *(reinterpret_cast<const support::ubig32_t*>(Offsets)
292 + SymbolIndex);
Shankar Easwaran206252c2012-11-13 18:38:42 +0000293 } else if (Parent->kind() == K_BSD) {
Matt Beaumont-Gay7af4b9b2012-11-14 17:58:11 +0000294 llvm_unreachable("BSD format is not supported");
Shankar Easwaran206252c2012-11-13 18:38:42 +0000295 } else {
Michael J. Spencer768a7072012-11-14 00:04:13 +0000296 uint32_t MemberCount = *reinterpret_cast<const support::ulittle32_t*>(Buf);
297
298 // Skip offsets.
299 Buf += sizeof(support::ulittle32_t)
300 + (MemberCount * sizeof(support::ulittle32_t));
301
302 uint32_t SymbolCount = *reinterpret_cast<const support::ulittle32_t*>(Buf);
303
304 if (SymbolIndex >= SymbolCount)
305 return object_error::parse_failed;
306
Matt Beaumont-Gayf1c2a6b2012-11-14 00:21:27 +0000307 // Skip SymbolCount to get to the indices table.
308 const char *Indices = Buf + sizeof(support::ulittle32_t);
Michael J. Spencer768a7072012-11-14 00:04:13 +0000309
310 // Get the index of the offset in the file member offset table for this
311 // symbol.
312 uint16_t OffsetIndex =
Matt Beaumont-Gayf1c2a6b2012-11-14 00:21:27 +0000313 *(reinterpret_cast<const support::ulittle16_t*>(Indices)
Shankar Easwaran206252c2012-11-13 18:38:42 +0000314 + SymbolIndex);
Michael J. Spencer768a7072012-11-14 00:04:13 +0000315 // Subtract 1 since OffsetIndex is 1 based.
316 --OffsetIndex;
317
318 if (OffsetIndex >= MemberCount)
319 return object_error::parse_failed;
320
321 Offset = *(reinterpret_cast<const support::ulittle32_t*>(Offsets)
322 + OffsetIndex);
Shankar Easwaran206252c2012-11-13 18:38:42 +0000323 }
Michael J. Spencerc8a55a62011-11-02 19:33:12 +0000324
Michael J. Spencer768a7072012-11-14 00:04:13 +0000325 const char *Loc = Parent->getData().begin() + Offset;
Michael J. Spencerc8a55a62011-11-02 19:33:12 +0000326 size_t Size = sizeof(ArchiveMemberHeader) +
327 ToHeader(Loc)->getSize();
328 Result = Child(Parent, StringRef(Loc, Size));
329
330 return object_error::success;
331}
332
333Archive::Symbol Archive::Symbol::getNext() const {
334 Symbol t(*this);
Benjamin Kramerefd2d5e2011-11-04 13:52:17 +0000335 // Go to one past next null.
336 t.StringIndex =
Michael J. Spencer0f76e642013-02-03 10:48:50 +0000337 Parent->SymbolTable->getBuffer().find('\0', t.StringIndex) + 1;
Michael J. Spencerc8a55a62011-11-02 19:33:12 +0000338 ++t.SymbolIndex;
339 return t;
340}
341
342Archive::symbol_iterator Archive::begin_symbols() const {
Michael J. Spencer0f76e642013-02-03 10:48:50 +0000343 const char *buf = SymbolTable->getBuffer().begin();
Shankar Easwaran206252c2012-11-13 18:38:42 +0000344 if (kind() == K_GNU) {
345 uint32_t symbol_count = 0;
346 symbol_count = *reinterpret_cast<const support::ubig32_t*>(buf);
347 buf += sizeof(uint32_t) + (symbol_count * (sizeof(uint32_t)));
348 } else if (kind() == K_BSD) {
Matt Beaumont-Gay7af4b9b2012-11-14 17:58:11 +0000349 llvm_unreachable("BSD archive format is not supported");
Shankar Easwaran206252c2012-11-13 18:38:42 +0000350 } else {
351 uint32_t member_count = 0;
352 uint32_t symbol_count = 0;
353 member_count = *reinterpret_cast<const support::ulittle32_t*>(buf);
354 buf += 4 + (member_count * 4); // Skip offsets.
355 symbol_count = *reinterpret_cast<const support::ulittle32_t*>(buf);
356 buf += 4 + (symbol_count * 2); // Skip indices.
357 }
Michael J. Spencer0f76e642013-02-03 10:48:50 +0000358 uint32_t string_start_offset = buf - SymbolTable->getBuffer().begin();
Michael J. Spencerc8a55a62011-11-02 19:33:12 +0000359 return symbol_iterator(Symbol(this, 0, string_start_offset));
360}
361
362Archive::symbol_iterator Archive::end_symbols() const {
Michael J. Spencer0f76e642013-02-03 10:48:50 +0000363 const char *buf = SymbolTable->getBuffer().begin();
Shankar Easwaran206252c2012-11-13 18:38:42 +0000364 uint32_t symbol_count = 0;
365 if (kind() == K_GNU) {
366 symbol_count = *reinterpret_cast<const support::ubig32_t*>(buf);
Shankar Easwaran206252c2012-11-13 18:38:42 +0000367 } else if (kind() == K_BSD) {
Matt Beaumont-Gay7af4b9b2012-11-14 17:58:11 +0000368 llvm_unreachable("BSD archive format is not supported");
Shankar Easwaran206252c2012-11-13 18:38:42 +0000369 } else {
370 uint32_t member_count = 0;
371 member_count = *reinterpret_cast<const support::ulittle32_t*>(buf);
372 buf += 4 + (member_count * 4); // Skip offsets.
373 symbol_count = *reinterpret_cast<const support::ulittle32_t*>(buf);
374 }
Michael J. Spencerc8a55a62011-11-02 19:33:12 +0000375 return symbol_iterator(
376 Symbol(this, symbol_count, 0));
377}
Shankar Easwaran206252c2012-11-13 18:38:42 +0000378
379Archive::child_iterator Archive::findSym(StringRef name) const {
380 Archive::symbol_iterator bs = begin_symbols();
381 Archive::symbol_iterator es = end_symbols();
382 Archive::child_iterator result;
383
384 StringRef symname;
385 for (; bs != es; ++bs) {
386 if (bs->getName(symname))
387 return end_children();
388 if (symname == name) {
389 if (bs->getMember(result))
390 return end_children();
391 return result;
392 }
393 }
394 return end_children();
395}