blob: d53704fa799188a58af95ec12c3a54637b8302ac [file] [log] [blame]
Michael J. Spencerd3b7b122011-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"
Ahmed Charlesfba06642014-03-05 10:27:34 +000016#include "llvm/ADT/OwningPtr.h"
Rafael Espindola747bc072013-07-09 03:39:35 +000017#include "llvm/ADT/SmallString.h"
18#include "llvm/ADT/Twine.h"
Michael J. Spencere03ea9c2011-11-02 19:33:12 +000019#include "llvm/Support/Endian.h"
Michael J. Spencerd3b7b122011-09-27 19:36:55 +000020#include "llvm/Support/MemoryBuffer.h"
21
22using namespace llvm;
23using namespace object;
24
Craig Topperd3a34f82013-07-16 01:17:10 +000025static const char *const Magic = "!<arch>\n";
Michael J. Spencerd3b7b122011-09-27 19:36:55 +000026
David Blaikiea379b1812011-12-20 02:50:00 +000027void Archive::anchor() { }
28
Rafael Espindola747bc072013-07-09 03:39:35 +000029StringRef ArchiveMemberHeader::getName() const {
30 char EndCond;
31 if (Name[0] == '/' || Name[0] == '#')
32 EndCond = ' ';
33 else
34 EndCond = '/';
35 llvm::StringRef::size_type end =
36 llvm::StringRef(Name, sizeof(Name)).find(EndCond);
37 if (end == llvm::StringRef::npos)
38 end = sizeof(Name);
39 assert(end <= sizeof(Name) && end > 0);
40 // Don't include the EndCond if there is one.
41 return llvm::StringRef(Name, end);
42}
43
Rafael Espindola8e9385e2013-07-09 12:45:11 +000044uint32_t ArchiveMemberHeader::getSize() const {
45 uint32_t Ret;
46 if (llvm::StringRef(Size, sizeof(Size)).rtrim(" ").getAsInteger(10, Ret))
47 llvm_unreachable("Size is not a decimal number.");
48 return Ret;
Rafael Espindola747bc072013-07-09 03:39:35 +000049}
50
Rafael Espindola8115e1d2013-07-09 12:49:24 +000051sys::fs::perms ArchiveMemberHeader::getAccessMode() const {
52 unsigned Ret;
53 if (StringRef(AccessMode, sizeof(AccessMode)).rtrim(" ").getAsInteger(8, Ret))
54 llvm_unreachable("Access mode is not an octal number.");
55 return static_cast<sys::fs::perms>(Ret);
56}
57
58sys::TimeValue ArchiveMemberHeader::getLastModified() const {
59 unsigned Seconds;
60 if (StringRef(LastModified, sizeof(LastModified)).rtrim(" ")
61 .getAsInteger(10, Seconds))
62 llvm_unreachable("Last modified time not a decimal number.");
63
64 sys::TimeValue Ret;
65 Ret.fromEpochTime(Seconds);
66 return Ret;
67}
68
69unsigned ArchiveMemberHeader::getUID() const {
70 unsigned Ret;
71 if (StringRef(UID, sizeof(UID)).rtrim(" ").getAsInteger(10, Ret))
72 llvm_unreachable("UID time not a decimal number.");
73 return Ret;
74}
75
76unsigned ArchiveMemberHeader::getGID() const {
77 unsigned Ret;
78 if (StringRef(GID, sizeof(GID)).rtrim(" ").getAsInteger(10, Ret))
79 llvm_unreachable("GID time not a decimal number.");
80 return Ret;
81}
82
Rafael Espindola0f3de642013-07-09 05:26:25 +000083Archive::Child::Child(const Archive *Parent, const char *Start)
84 : Parent(Parent) {
85 if (!Start)
Rafael Espindola747bc072013-07-09 03:39:35 +000086 return;
Rafael Espindola0f3de642013-07-09 05:26:25 +000087
Rafael Espindola3e2b21c2013-07-12 20:21:39 +000088 const ArchiveMemberHeader *Header =
89 reinterpret_cast<const ArchiveMemberHeader *>(Start);
Rafael Espindola0f3de642013-07-09 05:26:25 +000090 Data = StringRef(Start, sizeof(ArchiveMemberHeader) + Header->getSize());
91
Rafael Espindola747bc072013-07-09 03:39:35 +000092 // Setup StartOfFile and PaddingBytes.
93 StartOfFile = sizeof(ArchiveMemberHeader);
94 // Don't include attached name.
Rafael Espindola0f3de642013-07-09 05:26:25 +000095 StringRef Name = Header->getName();
Rafael Espindola747bc072013-07-09 03:39:35 +000096 if (Name.startswith("#1/")) {
97 uint64_t NameSize;
98 if (Name.substr(3).rtrim(" ").getAsInteger(10, NameSize))
99 llvm_unreachable("Long name length is not an integer");
100 StartOfFile += NameSize;
101 }
102}
103
104Archive::Child Archive::Child::getNext() const {
105 size_t SpaceToSkip = Data.size();
106 // If it's odd, add 1 to make it even.
107 if (SpaceToSkip & 1)
108 ++SpaceToSkip;
109
110 const char *NextLoc = Data.data() + SpaceToSkip;
111
112 // Check to see if this is past the end of the archive.
113 if (NextLoc >= Parent->Data->getBufferEnd())
Craig Topper2617dcc2014-04-15 06:32:26 +0000114 return Child(Parent, nullptr);
Rafael Espindola747bc072013-07-09 03:39:35 +0000115
Rafael Espindola0f3de642013-07-09 05:26:25 +0000116 return Child(Parent, NextLoc);
Rafael Espindola747bc072013-07-09 03:39:35 +0000117}
118
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000119error_code Archive::Child::getName(StringRef &Result) const {
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000120 StringRef name = getRawName();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000121 // Check if it's a special name.
122 if (name[0] == '/') {
123 if (name.size() == 1) { // Linker member.
124 Result = name;
125 return object_error::success;
126 }
127 if (name.size() == 2 && name[1] == '/') { // String table.
128 Result = name;
129 return object_error::success;
130 }
131 // It's a long name.
132 // Get the offset.
Michael J. Spencer04614ff2013-01-10 00:07:38 +0000133 std::size_t offset;
Michael J. Spencer8c71e922013-01-10 01:05:34 +0000134 if (name.substr(1).rtrim(" ").getAsInteger(10, offset))
135 llvm_unreachable("Long name offset is not an integer");
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000136 const char *addr = Parent->StringTable->Data.begin()
137 + sizeof(ArchiveMemberHeader)
Michael J. Spencer751fd882013-01-09 22:58:43 +0000138 + offset;
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000139 // Verify it.
Rafael Espindola23a97502014-01-21 16:09:45 +0000140 if (Parent->StringTable == Parent->child_end()
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000141 || addr < (Parent->StringTable->Data.begin()
142 + sizeof(ArchiveMemberHeader))
143 || addr > (Parent->StringTable->Data.begin()
144 + sizeof(ArchiveMemberHeader)
145 + Parent->StringTable->getSize()))
146 return object_error::parse_failed;
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000147
148 // GNU long file names end with a /.
149 if (Parent->kind() == K_GNU) {
150 StringRef::size_type End = StringRef(addr).find('/');
151 Result = StringRef(addr, End);
152 } else {
153 Result = addr;
154 }
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000155 return object_error::success;
Michael J. Spencer9aaa8522011-10-25 22:30:42 +0000156 } else if (name.startswith("#1/")) {
Michael J. Spencer751fd882013-01-09 22:58:43 +0000157 uint64_t name_size;
Michael J. Spencer8c71e922013-01-10 01:05:34 +0000158 if (name.substr(3).rtrim(" ").getAsInteger(10, name_size))
159 llvm_unreachable("Long name length is not an ingeter");
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000160 Result = Data.substr(sizeof(ArchiveMemberHeader), name_size)
161 .rtrim(StringRef("\0", 1));
Michael J. Spencer9aaa8522011-10-25 22:30:42 +0000162 return object_error::success;
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000163 }
164 // It's a simple name.
165 if (name[name.size() - 1] == '/')
166 Result = name.substr(0, name.size() - 1);
167 else
168 Result = name;
169 return object_error::success;
170}
171
Ahmed Charlesfba06642014-03-05 10:27:34 +0000172error_code Archive::Child::getMemoryBuffer(std::unique_ptr<MemoryBuffer> &Result,
Rafael Espindola747bc072013-07-09 03:39:35 +0000173 bool FullPath) const {
174 StringRef Name;
175 if (error_code ec = getName(Name))
176 return ec;
177 SmallString<128> Path;
178 Result.reset(MemoryBuffer::getMemBuffer(
179 getBuffer(), FullPath ? (Twine(Parent->getFileName()) + "(" + Name + ")")
180 .toStringRef(Path)
181 : Name,
182 false));
183 return error_code::success();
184}
185
Ahmed Charlesfba06642014-03-05 10:27:34 +0000186error_code Archive::Child::getMemoryBuffer(OwningPtr<MemoryBuffer> &Result,
187 bool FullPath) const {
188 std::unique_ptr<MemoryBuffer> MB;
189 error_code ec = getMemoryBuffer(MB, FullPath);
190 Result = std::move(MB);
191 return ec;
192}
193
194error_code Archive::Child::getAsBinary(std::unique_ptr<Binary> &Result,
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000195 LLVMContext *Context) const {
Ahmed Charlesfba06642014-03-05 10:27:34 +0000196 std::unique_ptr<Binary> ret;
197 std::unique_ptr<MemoryBuffer> Buff;
Michael J. Spencer9718f452013-02-03 10:48:50 +0000198 if (error_code ec = getMemoryBuffer(Buff))
199 return ec;
Ahmed Charlesfba06642014-03-05 10:27:34 +0000200 ErrorOr<Binary *> BinaryOrErr = createBinary(Buff.release(), Context);
Rafael Espindola63da2952014-01-15 19:37:43 +0000201 if (error_code EC = BinaryOrErr.getError())
202 return EC;
203 Result.reset(BinaryOrErr.get());
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000204 return object_error::success;
205}
206
Ahmed Charlesfba06642014-03-05 10:27:34 +0000207error_code Archive::Child::getAsBinary(OwningPtr<Binary> &Result,
208 LLVMContext *Context) const {
209 std::unique_ptr<Binary> B;
210 error_code ec = getAsBinary(B, Context);
211 Result = std::move(B);
212 return ec;
213}
214
Rafael Espindola692410e2014-01-21 23:06:54 +0000215ErrorOr<Archive*> Archive::create(MemoryBuffer *Source) {
216 error_code EC;
Ahmed Charlesfba06642014-03-05 10:27:34 +0000217 std::unique_ptr<Archive> Ret(new Archive(Source, EC));
Rafael Espindola692410e2014-01-21 23:06:54 +0000218 if (EC)
219 return EC;
Ahmed Charlesfba06642014-03-05 10:27:34 +0000220 return Ret.release();
Rafael Espindola692410e2014-01-21 23:06:54 +0000221}
222
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000223Archive::Archive(MemoryBuffer *source, error_code &ec)
Rafael Espindola23a97502014-01-21 16:09:45 +0000224 : Binary(Binary::ID_Archive, source), SymbolTable(child_end()) {
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000225 // Check for sufficient magic.
Rafael Espindolaf0c61722013-07-12 13:32:28 +0000226 assert(source);
227 if (source->getBufferSize() < 8 ||
228 StringRef(source->getBufferStart(), 8) != Magic) {
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000229 ec = object_error::invalid_file_type;
230 return;
231 }
232
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000233 // Get the special members.
Rafael Espindola23a97502014-01-21 16:09:45 +0000234 child_iterator i = child_begin(false);
235 child_iterator e = child_end();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000236
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000237 if (i == e) {
Rafael Espindolaf0c61722013-07-12 13:32:28 +0000238 ec = object_error::success;
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000239 return;
240 }
241
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000242 StringRef Name = i->getRawName();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000243
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000244 // Below is the pattern that is used to figure out the archive format
245 // GNU archive format
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000246 // First member : / (may exist, if it exists, points to the symbol table )
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000247 // Second member : // (may exist, if it exists, points to the string table)
248 // Note : The string table is used if the filename exceeds 15 characters
249 // BSD archive format
Rafael Espindola55509922013-07-10 22:07:59 +0000250 // First member : __.SYMDEF or "__.SYMDEF SORTED" (the symbol table)
251 // There is no string table, if the filename exceeds 15 characters or has a
252 // embedded space, the filename has #1/<size>, The size represents the size
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000253 // of the filename that needs to be read after the archive header
254 // COFF archive format
255 // First member : /
256 // Second member : / (provides a directory of symbols)
Rui Ueyamaf4d0a8c2013-06-03 00:27:03 +0000257 // Third member : // (may exist, if it exists, contains the string table)
258 // Note: Microsoft PE/COFF Spec 8.3 says that the third member is present
259 // even if the string table is empty. However, lib.exe does not in fact
260 // seem to create the third member if there's no member whose filename
261 // exceeds 15 characters. So the third member is optional.
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000262
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000263 if (Name == "__.SYMDEF") {
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000264 Format = K_BSD;
265 SymbolTable = i;
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000266 ++i;
267 FirstRegular = i;
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000268 ec = object_error::success;
269 return;
270 }
271
Rafael Espindola55509922013-07-10 22:07:59 +0000272 if (Name.startswith("#1/")) {
273 Format = K_BSD;
274 // We know this is BSD, so getName will work since there is no string table.
275 ec = i->getName(Name);
276 if (ec)
277 return;
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000278 if (Name == "__.SYMDEF SORTED") {
Rafael Espindola55509922013-07-10 22:07:59 +0000279 SymbolTable = i;
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000280 ++i;
281 }
282 FirstRegular = i;
Rafael Espindola55509922013-07-10 22:07:59 +0000283 return;
284 }
285
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000286 if (Name == "/") {
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000287 SymbolTable = i;
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000288
289 ++i;
Michael J. Spencer04614ff2013-01-10 00:07:38 +0000290 if (i == e) {
291 ec = object_error::parse_failed;
292 return;
293 }
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000294 Name = i->getRawName();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000295 }
296
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000297 if (Name == "//") {
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000298 Format = K_GNU;
299 StringTable = i;
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000300 ++i;
301 FirstRegular = i;
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000302 ec = object_error::success;
303 return;
304 }
305
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000306 if (Name[0] != '/') {
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000307 Format = K_GNU;
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000308 FirstRegular = i;
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000309 ec = object_error::success;
310 return;
311 }
312
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000313 if (Name != "/") {
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000314 ec = object_error::parse_failed;
315 return;
316 }
317
318 Format = K_COFF;
319 SymbolTable = i;
320
321 ++i;
322 if (i == e) {
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000323 FirstRegular = i;
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000324 ec = object_error::success;
325 return;
326 }
327
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000328 Name = i->getRawName();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000329
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000330 if (Name == "//") {
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000331 StringTable = i;
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000332 ++i;
333 }
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000334
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000335 FirstRegular = i;
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000336 ec = object_error::success;
337}
338
Rafael Espindola23a97502014-01-21 16:09:45 +0000339Archive::child_iterator Archive::child_begin(bool SkipInternal) const {
Rafael Espindolaf0c61722013-07-12 13:32:28 +0000340 if (Data->getBufferSize() == 8) // empty archive.
Rafael Espindola23a97502014-01-21 16:09:45 +0000341 return child_end();
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000342
343 if (SkipInternal)
344 return FirstRegular;
345
Benjamin Kramer453173f2012-02-22 13:42:11 +0000346 const char *Loc = Data->getBufferStart() + strlen(Magic);
Rafael Espindola0f3de642013-07-09 05:26:25 +0000347 Child c(this, Loc);
Michael J. Spencer9aaa8522011-10-25 22:30:42 +0000348 return c;
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000349}
350
Rafael Espindola23a97502014-01-21 16:09:45 +0000351Archive::child_iterator Archive::child_end() const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000352 return Child(this, nullptr);
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000353}
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000354
355error_code Archive::Symbol::getName(StringRef &Result) const {
Michael J. Spencer9718f452013-02-03 10:48:50 +0000356 Result = StringRef(Parent->SymbolTable->getBuffer().begin() + StringIndex);
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000357 return object_error::success;
358}
359
360error_code Archive::Symbol::getMember(child_iterator &Result) const {
Michael J. Spencer9718f452013-02-03 10:48:50 +0000361 const char *Buf = Parent->SymbolTable->getBuffer().begin();
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000362 const char *Offsets = Buf + 4;
363 uint32_t Offset = 0;
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000364 if (Parent->kind() == K_GNU) {
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000365 Offset = *(reinterpret_cast<const support::ubig32_t*>(Offsets)
366 + SymbolIndex);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000367 } else if (Parent->kind() == K_BSD) {
Matt Beaumont-Gay1fc20022012-11-14 17:58:11 +0000368 llvm_unreachable("BSD format is not supported");
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000369 } else {
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000370 uint32_t MemberCount = *reinterpret_cast<const support::ulittle32_t*>(Buf);
371
372 // Skip offsets.
373 Buf += sizeof(support::ulittle32_t)
374 + (MemberCount * sizeof(support::ulittle32_t));
375
376 uint32_t SymbolCount = *reinterpret_cast<const support::ulittle32_t*>(Buf);
377
378 if (SymbolIndex >= SymbolCount)
379 return object_error::parse_failed;
380
Matt Beaumont-Gay68e0b6a2012-11-14 00:21:27 +0000381 // Skip SymbolCount to get to the indices table.
382 const char *Indices = Buf + sizeof(support::ulittle32_t);
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000383
384 // Get the index of the offset in the file member offset table for this
385 // symbol.
386 uint16_t OffsetIndex =
Matt Beaumont-Gay68e0b6a2012-11-14 00:21:27 +0000387 *(reinterpret_cast<const support::ulittle16_t*>(Indices)
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000388 + SymbolIndex);
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000389 // Subtract 1 since OffsetIndex is 1 based.
390 --OffsetIndex;
391
392 if (OffsetIndex >= MemberCount)
393 return object_error::parse_failed;
394
395 Offset = *(reinterpret_cast<const support::ulittle32_t*>(Offsets)
396 + OffsetIndex);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000397 }
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000398
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000399 const char *Loc = Parent->getData().begin() + Offset;
Rafael Espindola0f3de642013-07-09 05:26:25 +0000400 Result = Child(Parent, Loc);
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000401
402 return object_error::success;
403}
404
405Archive::Symbol Archive::Symbol::getNext() const {
406 Symbol t(*this);
Benjamin Kramer4246ca42011-11-04 13:52:17 +0000407 // Go to one past next null.
408 t.StringIndex =
Michael J. Spencer9718f452013-02-03 10:48:50 +0000409 Parent->SymbolTable->getBuffer().find('\0', t.StringIndex) + 1;
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000410 ++t.SymbolIndex;
411 return t;
412}
413
Rafael Espindola23a97502014-01-21 16:09:45 +0000414Archive::symbol_iterator Archive::symbol_begin() const {
Rafael Espindolab6b5f52e2013-07-29 12:40:31 +0000415 if (!hasSymbolTable())
Rafael Espindolafbcafc02013-07-10 20:14:22 +0000416 return symbol_iterator(Symbol(this, 0, 0));
417
Michael J. Spencer9718f452013-02-03 10:48:50 +0000418 const char *buf = SymbolTable->getBuffer().begin();
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000419 if (kind() == K_GNU) {
420 uint32_t symbol_count = 0;
421 symbol_count = *reinterpret_cast<const support::ubig32_t*>(buf);
422 buf += sizeof(uint32_t) + (symbol_count * (sizeof(uint32_t)));
423 } else if (kind() == K_BSD) {
Matt Beaumont-Gay1fc20022012-11-14 17:58:11 +0000424 llvm_unreachable("BSD archive format is not supported");
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000425 } else {
426 uint32_t member_count = 0;
427 uint32_t symbol_count = 0;
428 member_count = *reinterpret_cast<const support::ulittle32_t*>(buf);
429 buf += 4 + (member_count * 4); // Skip offsets.
430 symbol_count = *reinterpret_cast<const support::ulittle32_t*>(buf);
431 buf += 4 + (symbol_count * 2); // Skip indices.
432 }
Michael J. Spencer9718f452013-02-03 10:48:50 +0000433 uint32_t string_start_offset = buf - SymbolTable->getBuffer().begin();
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000434 return symbol_iterator(Symbol(this, 0, string_start_offset));
435}
436
Rafael Espindola23a97502014-01-21 16:09:45 +0000437Archive::symbol_iterator Archive::symbol_end() const {
Rafael Espindolab6b5f52e2013-07-29 12:40:31 +0000438 if (!hasSymbolTable())
Rafael Espindolafbcafc02013-07-10 20:14:22 +0000439 return symbol_iterator(Symbol(this, 0, 0));
440
Michael J. Spencer9718f452013-02-03 10:48:50 +0000441 const char *buf = SymbolTable->getBuffer().begin();
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000442 uint32_t symbol_count = 0;
443 if (kind() == K_GNU) {
444 symbol_count = *reinterpret_cast<const support::ubig32_t*>(buf);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000445 } else if (kind() == K_BSD) {
Matt Beaumont-Gay1fc20022012-11-14 17:58:11 +0000446 llvm_unreachable("BSD archive format is not supported");
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000447 } else {
448 uint32_t member_count = 0;
449 member_count = *reinterpret_cast<const support::ulittle32_t*>(buf);
450 buf += 4 + (member_count * 4); // Skip offsets.
451 symbol_count = *reinterpret_cast<const support::ulittle32_t*>(buf);
452 }
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000453 return symbol_iterator(
454 Symbol(this, symbol_count, 0));
455}
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000456
457Archive::child_iterator Archive::findSym(StringRef name) const {
Rafael Espindola23a97502014-01-21 16:09:45 +0000458 Archive::symbol_iterator bs = symbol_begin();
459 Archive::symbol_iterator es = symbol_end();
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000460 Archive::child_iterator result;
461
462 StringRef symname;
463 for (; bs != es; ++bs) {
464 if (bs->getName(symname))
Rafael Espindola23a97502014-01-21 16:09:45 +0000465 return child_end();
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000466 if (symname == name) {
467 if (bs->getMember(result))
Rafael Espindola23a97502014-01-21 16:09:45 +0000468 return child_end();
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000469 return result;
470 }
471 }
Rafael Espindola23a97502014-01-21 16:09:45 +0000472 return child_end();
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000473}
Rafael Espindolab6b5f52e2013-07-29 12:40:31 +0000474
475bool Archive::hasSymbolTable() const {
Rafael Espindola23a97502014-01-21 16:09:45 +0000476 return SymbolTable != child_end();
Rafael Espindolab6b5f52e2013-07-29 12:40:31 +0000477}