blob: 99bc4a5a3b33cdc43e0fe5d9ae6e22ea9f0362cd [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"
Rafael Espindola747bc072013-07-09 03:39:35 +000016#include "llvm/ADT/SmallString.h"
17#include "llvm/ADT/Twine.h"
Michael J. Spencere03ea9c2011-11-02 19:33:12 +000018#include "llvm/Support/Endian.h"
Michael J. Spencerd3b7b122011-09-27 19:36:55 +000019#include "llvm/Support/MemoryBuffer.h"
20
21using namespace llvm;
22using namespace object;
23
Craig Topperd3a34f82013-07-16 01:17:10 +000024static const char *const Magic = "!<arch>\n";
Michael J. Spencerd3b7b122011-09-27 19:36:55 +000025
David Blaikiea379b1812011-12-20 02:50:00 +000026void Archive::anchor() { }
27
Rafael Espindola747bc072013-07-09 03:39:35 +000028StringRef ArchiveMemberHeader::getName() const {
29 char EndCond;
30 if (Name[0] == '/' || Name[0] == '#')
31 EndCond = ' ';
32 else
33 EndCond = '/';
34 llvm::StringRef::size_type end =
35 llvm::StringRef(Name, sizeof(Name)).find(EndCond);
36 if (end == llvm::StringRef::npos)
37 end = sizeof(Name);
38 assert(end <= sizeof(Name) && end > 0);
39 // Don't include the EndCond if there is one.
40 return llvm::StringRef(Name, end);
41}
42
Rafael Espindola8e9385e2013-07-09 12:45:11 +000043uint32_t ArchiveMemberHeader::getSize() const {
44 uint32_t Ret;
45 if (llvm::StringRef(Size, sizeof(Size)).rtrim(" ").getAsInteger(10, Ret))
46 llvm_unreachable("Size is not a decimal number.");
47 return Ret;
Rafael Espindola747bc072013-07-09 03:39:35 +000048}
49
Rafael Espindola8115e1d2013-07-09 12:49:24 +000050sys::fs::perms ArchiveMemberHeader::getAccessMode() const {
51 unsigned Ret;
52 if (StringRef(AccessMode, sizeof(AccessMode)).rtrim(" ").getAsInteger(8, Ret))
53 llvm_unreachable("Access mode is not an octal number.");
54 return static_cast<sys::fs::perms>(Ret);
55}
56
57sys::TimeValue ArchiveMemberHeader::getLastModified() const {
58 unsigned Seconds;
59 if (StringRef(LastModified, sizeof(LastModified)).rtrim(" ")
60 .getAsInteger(10, Seconds))
61 llvm_unreachable("Last modified time not a decimal number.");
62
63 sys::TimeValue Ret;
64 Ret.fromEpochTime(Seconds);
65 return Ret;
66}
67
68unsigned ArchiveMemberHeader::getUID() const {
69 unsigned Ret;
70 if (StringRef(UID, sizeof(UID)).rtrim(" ").getAsInteger(10, Ret))
71 llvm_unreachable("UID time not a decimal number.");
72 return Ret;
73}
74
75unsigned ArchiveMemberHeader::getGID() const {
76 unsigned Ret;
77 if (StringRef(GID, sizeof(GID)).rtrim(" ").getAsInteger(10, Ret))
78 llvm_unreachable("GID time not a decimal number.");
79 return Ret;
80}
81
Rafael Espindola0f3de642013-07-09 05:26:25 +000082Archive::Child::Child(const Archive *Parent, const char *Start)
83 : Parent(Parent) {
84 if (!Start)
Rafael Espindola747bc072013-07-09 03:39:35 +000085 return;
Rafael Espindola0f3de642013-07-09 05:26:25 +000086
Rafael Espindola3e2b21c2013-07-12 20:21:39 +000087 const ArchiveMemberHeader *Header =
88 reinterpret_cast<const ArchiveMemberHeader *>(Start);
Rafael Espindola0f3de642013-07-09 05:26:25 +000089 Data = StringRef(Start, sizeof(ArchiveMemberHeader) + Header->getSize());
90
Rafael Espindola747bc072013-07-09 03:39:35 +000091 // Setup StartOfFile and PaddingBytes.
92 StartOfFile = sizeof(ArchiveMemberHeader);
93 // Don't include attached name.
Rafael Espindola0f3de642013-07-09 05:26:25 +000094 StringRef Name = Header->getName();
Rafael Espindola747bc072013-07-09 03:39:35 +000095 if (Name.startswith("#1/")) {
96 uint64_t NameSize;
97 if (Name.substr(3).rtrim(" ").getAsInteger(10, NameSize))
98 llvm_unreachable("Long name length is not an integer");
99 StartOfFile += NameSize;
100 }
101}
102
103Archive::Child Archive::Child::getNext() const {
104 size_t SpaceToSkip = Data.size();
105 // If it's odd, add 1 to make it even.
106 if (SpaceToSkip & 1)
107 ++SpaceToSkip;
108
109 const char *NextLoc = Data.data() + SpaceToSkip;
110
111 // Check to see if this is past the end of the archive.
112 if (NextLoc >= Parent->Data->getBufferEnd())
Rafael Espindola0f3de642013-07-09 05:26:25 +0000113 return Child(Parent, NULL);
Rafael Espindola747bc072013-07-09 03:39:35 +0000114
Rafael Espindola0f3de642013-07-09 05:26:25 +0000115 return Child(Parent, NextLoc);
Rafael Espindola747bc072013-07-09 03:39:35 +0000116}
117
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000118error_code Archive::Child::getName(StringRef &Result) const {
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000119 StringRef name = getRawName();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000120 // Check if it's a special name.
121 if (name[0] == '/') {
122 if (name.size() == 1) { // Linker member.
123 Result = name;
124 return object_error::success;
125 }
126 if (name.size() == 2 && name[1] == '/') { // String table.
127 Result = name;
128 return object_error::success;
129 }
130 // It's a long name.
131 // Get the offset.
Michael J. Spencer04614ff2013-01-10 00:07:38 +0000132 std::size_t offset;
Michael J. Spencer8c71e922013-01-10 01:05:34 +0000133 if (name.substr(1).rtrim(" ").getAsInteger(10, offset))
134 llvm_unreachable("Long name offset is not an integer");
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000135 const char *addr = Parent->StringTable->Data.begin()
136 + sizeof(ArchiveMemberHeader)
Michael J. Spencer751fd882013-01-09 22:58:43 +0000137 + offset;
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000138 // Verify it.
Rafael Espindola23a97502014-01-21 16:09:45 +0000139 if (Parent->StringTable == Parent->child_end()
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000140 || addr < (Parent->StringTable->Data.begin()
141 + sizeof(ArchiveMemberHeader))
142 || addr > (Parent->StringTable->Data.begin()
143 + sizeof(ArchiveMemberHeader)
144 + Parent->StringTable->getSize()))
145 return object_error::parse_failed;
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000146
147 // GNU long file names end with a /.
148 if (Parent->kind() == K_GNU) {
149 StringRef::size_type End = StringRef(addr).find('/');
150 Result = StringRef(addr, End);
151 } else {
152 Result = addr;
153 }
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000154 return object_error::success;
Michael J. Spencer9aaa8522011-10-25 22:30:42 +0000155 } else if (name.startswith("#1/")) {
Michael J. Spencer751fd882013-01-09 22:58:43 +0000156 uint64_t name_size;
Michael J. Spencer8c71e922013-01-10 01:05:34 +0000157 if (name.substr(3).rtrim(" ").getAsInteger(10, name_size))
158 llvm_unreachable("Long name length is not an ingeter");
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000159 Result = Data.substr(sizeof(ArchiveMemberHeader), name_size)
160 .rtrim(StringRef("\0", 1));
Michael J. Spencer9aaa8522011-10-25 22:30:42 +0000161 return object_error::success;
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000162 }
163 // It's a simple name.
164 if (name[name.size() - 1] == '/')
165 Result = name.substr(0, name.size() - 1);
166 else
167 Result = name;
168 return object_error::success;
169}
170
Rafael Espindola747bc072013-07-09 03:39:35 +0000171error_code Archive::Child::getMemoryBuffer(OwningPtr<MemoryBuffer> &Result,
172 bool FullPath) const {
173 StringRef Name;
174 if (error_code ec = getName(Name))
175 return ec;
176 SmallString<128> Path;
177 Result.reset(MemoryBuffer::getMemBuffer(
178 getBuffer(), FullPath ? (Twine(Parent->getFileName()) + "(" + Name + ")")
179 .toStringRef(Path)
180 : Name,
181 false));
182 return error_code::success();
183}
184
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000185error_code Archive::Child::getAsBinary(OwningPtr<Binary> &Result,
186 LLVMContext *Context) const {
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000187 OwningPtr<Binary> ret;
Michael J. Spencer9718f452013-02-03 10:48:50 +0000188 OwningPtr<MemoryBuffer> Buff;
189 if (error_code ec = getMemoryBuffer(Buff))
190 return ec;
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000191 ErrorOr<Binary *> BinaryOrErr = createBinary(Buff.take(), Context);
Rafael Espindola63da2952014-01-15 19:37:43 +0000192 if (error_code EC = BinaryOrErr.getError())
193 return EC;
194 Result.reset(BinaryOrErr.get());
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000195 return object_error::success;
196}
197
Rafael Espindola692410e2014-01-21 23:06:54 +0000198ErrorOr<Archive*> Archive::create(MemoryBuffer *Source) {
199 error_code EC;
200 OwningPtr<Archive> Ret(new Archive(Source, EC));
201 if (EC)
202 return EC;
203 return Ret.take();
204}
205
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000206Archive::Archive(MemoryBuffer *source, error_code &ec)
Rafael Espindola23a97502014-01-21 16:09:45 +0000207 : Binary(Binary::ID_Archive, source), SymbolTable(child_end()) {
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000208 // Check for sufficient magic.
Rafael Espindolaf0c61722013-07-12 13:32:28 +0000209 assert(source);
210 if (source->getBufferSize() < 8 ||
211 StringRef(source->getBufferStart(), 8) != Magic) {
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000212 ec = object_error::invalid_file_type;
213 return;
214 }
215
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000216 // Get the special members.
Rafael Espindola23a97502014-01-21 16:09:45 +0000217 child_iterator i = child_begin(false);
218 child_iterator e = child_end();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000219
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000220 if (i == e) {
Rafael Espindolaf0c61722013-07-12 13:32:28 +0000221 ec = object_error::success;
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000222 return;
223 }
224
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000225 StringRef Name = i->getRawName();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000226
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000227 // Below is the pattern that is used to figure out the archive format
228 // GNU archive format
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000229 // First member : / (may exist, if it exists, points to the symbol table )
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000230 // Second member : // (may exist, if it exists, points to the string table)
231 // Note : The string table is used if the filename exceeds 15 characters
232 // BSD archive format
Rafael Espindola55509922013-07-10 22:07:59 +0000233 // First member : __.SYMDEF or "__.SYMDEF SORTED" (the symbol table)
234 // There is no string table, if the filename exceeds 15 characters or has a
235 // embedded space, the filename has #1/<size>, The size represents the size
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000236 // of the filename that needs to be read after the archive header
237 // COFF archive format
238 // First member : /
239 // Second member : / (provides a directory of symbols)
Rui Ueyamaf4d0a8c2013-06-03 00:27:03 +0000240 // Third member : // (may exist, if it exists, contains the string table)
241 // Note: Microsoft PE/COFF Spec 8.3 says that the third member is present
242 // even if the string table is empty. However, lib.exe does not in fact
243 // seem to create the third member if there's no member whose filename
244 // exceeds 15 characters. So the third member is optional.
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000245
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000246 if (Name == "__.SYMDEF") {
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000247 Format = K_BSD;
248 SymbolTable = i;
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000249 ++i;
250 FirstRegular = i;
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000251 ec = object_error::success;
252 return;
253 }
254
Rafael Espindola55509922013-07-10 22:07:59 +0000255 if (Name.startswith("#1/")) {
256 Format = K_BSD;
257 // We know this is BSD, so getName will work since there is no string table.
258 ec = i->getName(Name);
259 if (ec)
260 return;
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000261 if (Name == "__.SYMDEF SORTED") {
Rafael Espindola55509922013-07-10 22:07:59 +0000262 SymbolTable = i;
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000263 ++i;
264 }
265 FirstRegular = i;
Rafael Espindola55509922013-07-10 22:07:59 +0000266 return;
267 }
268
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000269 if (Name == "/") {
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000270 SymbolTable = i;
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000271
272 ++i;
Michael J. Spencer04614ff2013-01-10 00:07:38 +0000273 if (i == e) {
274 ec = object_error::parse_failed;
275 return;
276 }
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000277 Name = i->getRawName();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000278 }
279
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000280 if (Name == "//") {
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000281 Format = K_GNU;
282 StringTable = i;
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000283 ++i;
284 FirstRegular = i;
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000285 ec = object_error::success;
286 return;
287 }
288
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000289 if (Name[0] != '/') {
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000290 Format = K_GNU;
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000291 FirstRegular = i;
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000292 ec = object_error::success;
293 return;
294 }
295
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000296 if (Name != "/") {
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000297 ec = object_error::parse_failed;
298 return;
299 }
300
301 Format = K_COFF;
302 SymbolTable = i;
303
304 ++i;
305 if (i == e) {
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000306 FirstRegular = i;
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000307 ec = object_error::success;
308 return;
309 }
310
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000311 Name = i->getRawName();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000312
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000313 if (Name == "//") {
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000314 StringTable = i;
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000315 ++i;
316 }
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000317
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000318 FirstRegular = i;
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000319 ec = object_error::success;
320}
321
Rafael Espindola23a97502014-01-21 16:09:45 +0000322Archive::child_iterator Archive::child_begin(bool SkipInternal) const {
Rafael Espindolaf0c61722013-07-12 13:32:28 +0000323 if (Data->getBufferSize() == 8) // empty archive.
Rafael Espindola23a97502014-01-21 16:09:45 +0000324 return child_end();
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000325
326 if (SkipInternal)
327 return FirstRegular;
328
Benjamin Kramer453173f2012-02-22 13:42:11 +0000329 const char *Loc = Data->getBufferStart() + strlen(Magic);
Rafael Espindola0f3de642013-07-09 05:26:25 +0000330 Child c(this, Loc);
Michael J. Spencer9aaa8522011-10-25 22:30:42 +0000331 return c;
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000332}
333
Rafael Espindola23a97502014-01-21 16:09:45 +0000334Archive::child_iterator Archive::child_end() const {
Rafael Espindola0f3de642013-07-09 05:26:25 +0000335 return Child(this, NULL);
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000336}
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000337
338error_code Archive::Symbol::getName(StringRef &Result) const {
Michael J. Spencer9718f452013-02-03 10:48:50 +0000339 Result = StringRef(Parent->SymbolTable->getBuffer().begin() + StringIndex);
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000340 return object_error::success;
341}
342
343error_code Archive::Symbol::getMember(child_iterator &Result) const {
Michael J. Spencer9718f452013-02-03 10:48:50 +0000344 const char *Buf = Parent->SymbolTable->getBuffer().begin();
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000345 const char *Offsets = Buf + 4;
346 uint32_t Offset = 0;
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000347 if (Parent->kind() == K_GNU) {
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000348 Offset = *(reinterpret_cast<const support::ubig32_t*>(Offsets)
349 + SymbolIndex);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000350 } else if (Parent->kind() == K_BSD) {
Matt Beaumont-Gay1fc20022012-11-14 17:58:11 +0000351 llvm_unreachable("BSD format is not supported");
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000352 } else {
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000353 uint32_t MemberCount = *reinterpret_cast<const support::ulittle32_t*>(Buf);
354
355 // Skip offsets.
356 Buf += sizeof(support::ulittle32_t)
357 + (MemberCount * sizeof(support::ulittle32_t));
358
359 uint32_t SymbolCount = *reinterpret_cast<const support::ulittle32_t*>(Buf);
360
361 if (SymbolIndex >= SymbolCount)
362 return object_error::parse_failed;
363
Matt Beaumont-Gay68e0b6a2012-11-14 00:21:27 +0000364 // Skip SymbolCount to get to the indices table.
365 const char *Indices = Buf + sizeof(support::ulittle32_t);
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000366
367 // Get the index of the offset in the file member offset table for this
368 // symbol.
369 uint16_t OffsetIndex =
Matt Beaumont-Gay68e0b6a2012-11-14 00:21:27 +0000370 *(reinterpret_cast<const support::ulittle16_t*>(Indices)
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000371 + SymbolIndex);
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000372 // Subtract 1 since OffsetIndex is 1 based.
373 --OffsetIndex;
374
375 if (OffsetIndex >= MemberCount)
376 return object_error::parse_failed;
377
378 Offset = *(reinterpret_cast<const support::ulittle32_t*>(Offsets)
379 + OffsetIndex);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000380 }
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000381
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000382 const char *Loc = Parent->getData().begin() + Offset;
Rafael Espindola0f3de642013-07-09 05:26:25 +0000383 Result = Child(Parent, Loc);
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000384
385 return object_error::success;
386}
387
388Archive::Symbol Archive::Symbol::getNext() const {
389 Symbol t(*this);
Benjamin Kramer4246ca42011-11-04 13:52:17 +0000390 // Go to one past next null.
391 t.StringIndex =
Michael J. Spencer9718f452013-02-03 10:48:50 +0000392 Parent->SymbolTable->getBuffer().find('\0', t.StringIndex) + 1;
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000393 ++t.SymbolIndex;
394 return t;
395}
396
Rafael Espindola23a97502014-01-21 16:09:45 +0000397Archive::symbol_iterator Archive::symbol_begin() const {
Rafael Espindolab6b5f52e2013-07-29 12:40:31 +0000398 if (!hasSymbolTable())
Rafael Espindolafbcafc02013-07-10 20:14:22 +0000399 return symbol_iterator(Symbol(this, 0, 0));
400
Michael J. Spencer9718f452013-02-03 10:48:50 +0000401 const char *buf = SymbolTable->getBuffer().begin();
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000402 if (kind() == K_GNU) {
403 uint32_t symbol_count = 0;
404 symbol_count = *reinterpret_cast<const support::ubig32_t*>(buf);
405 buf += sizeof(uint32_t) + (symbol_count * (sizeof(uint32_t)));
406 } else if (kind() == K_BSD) {
Matt Beaumont-Gay1fc20022012-11-14 17:58:11 +0000407 llvm_unreachable("BSD archive format is not supported");
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000408 } else {
409 uint32_t member_count = 0;
410 uint32_t symbol_count = 0;
411 member_count = *reinterpret_cast<const support::ulittle32_t*>(buf);
412 buf += 4 + (member_count * 4); // Skip offsets.
413 symbol_count = *reinterpret_cast<const support::ulittle32_t*>(buf);
414 buf += 4 + (symbol_count * 2); // Skip indices.
415 }
Michael J. Spencer9718f452013-02-03 10:48:50 +0000416 uint32_t string_start_offset = buf - SymbolTable->getBuffer().begin();
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000417 return symbol_iterator(Symbol(this, 0, string_start_offset));
418}
419
Rafael Espindola23a97502014-01-21 16:09:45 +0000420Archive::symbol_iterator Archive::symbol_end() const {
Rafael Espindolab6b5f52e2013-07-29 12:40:31 +0000421 if (!hasSymbolTable())
Rafael Espindolafbcafc02013-07-10 20:14:22 +0000422 return symbol_iterator(Symbol(this, 0, 0));
423
Michael J. Spencer9718f452013-02-03 10:48:50 +0000424 const char *buf = SymbolTable->getBuffer().begin();
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000425 uint32_t symbol_count = 0;
426 if (kind() == K_GNU) {
427 symbol_count = *reinterpret_cast<const support::ubig32_t*>(buf);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000428 } else if (kind() == K_BSD) {
Matt Beaumont-Gay1fc20022012-11-14 17:58:11 +0000429 llvm_unreachable("BSD archive format is not supported");
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000430 } else {
431 uint32_t member_count = 0;
432 member_count = *reinterpret_cast<const support::ulittle32_t*>(buf);
433 buf += 4 + (member_count * 4); // Skip offsets.
434 symbol_count = *reinterpret_cast<const support::ulittle32_t*>(buf);
435 }
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000436 return symbol_iterator(
437 Symbol(this, symbol_count, 0));
438}
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000439
440Archive::child_iterator Archive::findSym(StringRef name) const {
Rafael Espindola23a97502014-01-21 16:09:45 +0000441 Archive::symbol_iterator bs = symbol_begin();
442 Archive::symbol_iterator es = symbol_end();
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000443 Archive::child_iterator result;
444
445 StringRef symname;
446 for (; bs != es; ++bs) {
447 if (bs->getName(symname))
Rafael Espindola23a97502014-01-21 16:09:45 +0000448 return child_end();
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000449 if (symname == name) {
450 if (bs->getMember(result))
Rafael Espindola23a97502014-01-21 16:09:45 +0000451 return child_end();
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000452 return result;
453 }
454 }
Rafael Espindola23a97502014-01-21 16:09:45 +0000455 return child_end();
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000456}
Rafael Espindolab6b5f52e2013-07-29 12:40:31 +0000457
458bool Archive::hasSymbolTable() const {
Rafael Espindola23a97502014-01-21 16:09:45 +0000459 return SymbolTable != child_end();
Rafael Espindolab6b5f52e2013-07-29 12:40:31 +0000460}