blob: 8b225f00c6269cff189f3b9fd973276dcf82c8dc [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"
Rafael Espindola747bc072013-07-09 03:39:35 +000015#include "llvm/ADT/SmallString.h"
16#include "llvm/ADT/Twine.h"
Michael J. Spencere03ea9c2011-11-02 19:33:12 +000017#include "llvm/Support/Endian.h"
Michael J. Spencerd3b7b122011-09-27 19:36:55 +000018#include "llvm/Support/MemoryBuffer.h"
Rafael Espindola4b83cb52015-07-14 22:18:43 +000019#include "llvm/Support/Path.h"
Michael J. Spencerd3b7b122011-09-27 19:36:55 +000020
21using namespace llvm;
22using namespace object;
Rui Ueyama3206b792015-03-02 21:19:12 +000023using namespace llvm::support::endian;
Michael J. Spencerd3b7b122011-09-27 19:36:55 +000024
Craig Topperd3a34f82013-07-16 01:17:10 +000025static const char *const Magic = "!<arch>\n";
Rafael Espindola9d102062014-12-16 01:43:41 +000026static const char *const ThinMagic = "!<thin>\n";
Michael J. Spencerd3b7b122011-09-27 19:36:55 +000027
David Blaikiea379b1812011-12-20 02:50:00 +000028void Archive::anchor() { }
29
Kevin Enderby6524bd82016-07-19 20:47:07 +000030static Error
31malformedError(Twine Msg) {
32 std::string StringMsg = "truncated or malformed archive (" + Msg.str() + ")";
33 return make_error<GenericBinaryError>(std::move(StringMsg),
34 object_error::parse_failed);
35}
36
Kevin Enderby95b08422016-07-25 20:36:36 +000037ArchiveMemberHeader::ArchiveMemberHeader(const Archive *Parent,
38 const char *RawHeaderPtr,
39 uint64_t Size, Error *Err)
40 : Parent(Parent),
41 ArMemHdr(reinterpret_cast<const ArMemHdrType *>(RawHeaderPtr)) {
42 if (RawHeaderPtr == nullptr)
43 return;
44 ErrorAsOutParameter ErrAsOutParam(Err);
45
46 // TODO: For errors messages with the ArchiveMemberHeader class use the
47 // archive member name instead of the the offset to the archive member header.
48 // When there is also error getting the member name then use the offset to
49 // the member in the message.
50
51 if (Size < sizeof(ArMemHdrType)) {
52 if (Err) {
53 uint64_t Offset = RawHeaderPtr - Parent->getData().data();
54 *Err = malformedError("remaining size of archive too small for next "
55 "archive member header at offset " +
56 Twine(Offset));
57 }
58 return;
59 }
60 if (ArMemHdr->Terminator[0] != '`' || ArMemHdr->Terminator[1] != '\n') {
61 if (Err) {
62 std::string Buf;
63 raw_string_ostream OS(Buf);
64 OS.write_escaped(llvm::StringRef(ArMemHdr->Terminator,
65 sizeof(ArMemHdr->Terminator)));
66 OS.flush();
67 uint64_t Offset = RawHeaderPtr - Parent->getData().data();
68 *Err = malformedError("terminator characters in archive member \"" + Buf +
69 "\" not the correct \"`\\n\" values for the "
70 "archive member header at offset " + Twine(Offset));
71 }
72 return;
73 }
74}
75
Rafael Espindola747bc072013-07-09 03:39:35 +000076StringRef ArchiveMemberHeader::getName() const {
77 char EndCond;
Kevin Enderby95b08422016-07-25 20:36:36 +000078 if (ArMemHdr->Name[0] == '/' || ArMemHdr->Name[0] == '#')
Rafael Espindola747bc072013-07-09 03:39:35 +000079 EndCond = ' ';
80 else
81 EndCond = '/';
82 llvm::StringRef::size_type end =
Kevin Enderby95b08422016-07-25 20:36:36 +000083 llvm::StringRef(ArMemHdr->Name, sizeof(ArMemHdr->Name)).find(EndCond);
Rafael Espindola747bc072013-07-09 03:39:35 +000084 if (end == llvm::StringRef::npos)
Kevin Enderby95b08422016-07-25 20:36:36 +000085 end = sizeof(ArMemHdr->Name);
86 assert(end <= sizeof(ArMemHdr->Name) && end > 0);
Rafael Espindola747bc072013-07-09 03:39:35 +000087 // Don't include the EndCond if there is one.
Kevin Enderby95b08422016-07-25 20:36:36 +000088 return llvm::StringRef(ArMemHdr->Name, end);
Rafael Espindola747bc072013-07-09 03:39:35 +000089}
90
Kevin Enderby6524bd82016-07-19 20:47:07 +000091Expected<uint32_t> ArchiveMemberHeader::getSize() const {
Rafael Espindola8e9385e2013-07-09 12:45:11 +000092 uint32_t Ret;
Kevin Enderby95b08422016-07-25 20:36:36 +000093 if (llvm::StringRef(ArMemHdr->Size,
94 sizeof(ArMemHdr->Size)).rtrim(" ").getAsInteger(10, Ret)) {
Kevin Enderby6524bd82016-07-19 20:47:07 +000095 std::string Buf;
96 raw_string_ostream OS(Buf);
Kevin Enderby95b08422016-07-25 20:36:36 +000097 OS.write_escaped(llvm::StringRef(ArMemHdr->Size,
98 sizeof(ArMemHdr->Size)).rtrim(" "));
Kevin Enderby6524bd82016-07-19 20:47:07 +000099 OS.flush();
Kevin Enderby95b08422016-07-25 20:36:36 +0000100 uint64_t Offset = reinterpret_cast<const char *>(ArMemHdr) -
101 Parent->getData().data();
Kevin Enderby6524bd82016-07-19 20:47:07 +0000102 return malformedError("characters in size field in archive header are not "
Kevin Enderby95b08422016-07-25 20:36:36 +0000103 "all decimal numbers: '" + Buf + "' for archive "
104 "member header at offset " + Twine(Offset));
Kevin Enderby6524bd82016-07-19 20:47:07 +0000105 }
Rafael Espindola8e9385e2013-07-09 12:45:11 +0000106 return Ret;
Rafael Espindola747bc072013-07-09 03:39:35 +0000107}
108
Rafael Espindola8115e1d2013-07-09 12:49:24 +0000109sys::fs::perms ArchiveMemberHeader::getAccessMode() const {
110 unsigned Ret;
Kevin Enderby95b08422016-07-25 20:36:36 +0000111 if (StringRef(ArMemHdr->AccessMode,
112 sizeof(ArMemHdr->AccessMode)).rtrim(' ').getAsInteger(8, Ret))
Rafael Espindola8115e1d2013-07-09 12:49:24 +0000113 llvm_unreachable("Access mode is not an octal number.");
114 return static_cast<sys::fs::perms>(Ret);
115}
116
117sys::TimeValue ArchiveMemberHeader::getLastModified() const {
118 unsigned Seconds;
Kevin Enderby95b08422016-07-25 20:36:36 +0000119 if (StringRef(ArMemHdr->LastModified,
120 sizeof(ArMemHdr->LastModified)).rtrim(' ')
Rafael Espindola8115e1d2013-07-09 12:49:24 +0000121 .getAsInteger(10, Seconds))
122 llvm_unreachable("Last modified time not a decimal number.");
123
124 sys::TimeValue Ret;
125 Ret.fromEpochTime(Seconds);
126 return Ret;
127}
128
129unsigned ArchiveMemberHeader::getUID() const {
130 unsigned Ret;
Kevin Enderby95b08422016-07-25 20:36:36 +0000131 StringRef User = StringRef(ArMemHdr->UID, sizeof(ArMemHdr->UID)).rtrim(' ');
Saleem Abdulrasoolaecbdf72016-07-05 00:23:05 +0000132 if (User.empty())
133 return 0;
134 if (User.getAsInteger(10, Ret))
Rafael Espindola8115e1d2013-07-09 12:49:24 +0000135 llvm_unreachable("UID time not a decimal number.");
136 return Ret;
137}
138
139unsigned ArchiveMemberHeader::getGID() const {
140 unsigned Ret;
Kevin Enderby95b08422016-07-25 20:36:36 +0000141 StringRef Group = StringRef(ArMemHdr->GID, sizeof(ArMemHdr->GID)).rtrim(' ');
Saleem Abdulrasoolaecbdf72016-07-05 00:23:05 +0000142 if (Group.empty())
143 return 0;
144 if (Group.getAsInteger(10, Ret))
Rafael Espindola8115e1d2013-07-09 12:49:24 +0000145 llvm_unreachable("GID time not a decimal number.");
146 return Ret;
147}
148
Rafael Espindola43358762015-10-31 21:44:42 +0000149Archive::Child::Child(const Archive *Parent, StringRef Data,
150 uint16_t StartOfFile)
Kevin Enderby95b08422016-07-25 20:36:36 +0000151 : Parent(Parent), Header(Parent, Data.data(), Data.size(), nullptr),
152 Data(Data), StartOfFile(StartOfFile) {
153}
Rafael Espindola43358762015-10-31 21:44:42 +0000154
Kevin Enderby6524bd82016-07-19 20:47:07 +0000155Archive::Child::Child(const Archive *Parent, const char *Start, Error *Err)
Kevin Enderby95b08422016-07-25 20:36:36 +0000156 : Parent(Parent), Header(Parent, Start, Parent->getData().size() -
157 (Start - Parent->getData().data()), Err) {
Rafael Espindola0f3de642013-07-09 05:26:25 +0000158 if (!Start)
Rafael Espindola747bc072013-07-09 03:39:35 +0000159 return;
Lang Hames5e51a2e2016-07-22 16:11:25 +0000160 ErrorAsOutParameter ErrAsOutParam(Err);
Rafael Espindola0f3de642013-07-09 05:26:25 +0000161
Kevin Enderby95b08422016-07-25 20:36:36 +0000162 // If there was an error in the construction of the Header and we were passed
163 // Err that is not nullptr then just return with the error now set.
164 if (Err && *Err)
165 return;
166
167 uint64_t Size = Header.getSizeOf();
Rafael Espindola9d102062014-12-16 01:43:41 +0000168 Data = StringRef(Start, Size);
Rafael Espindolabe9ab262015-07-22 19:34:26 +0000169 if (!isThinMember()) {
Kevin Enderby6524bd82016-07-19 20:47:07 +0000170 Expected<uint64_t> MemberSize = getRawSize();
171 if (!MemberSize) {
Lang Hames5e51a2e2016-07-22 16:11:25 +0000172 if (Err)
Kevin Enderby6524bd82016-07-19 20:47:07 +0000173 *Err = MemberSize.takeError();
Kevin Enderby7a969422015-11-05 19:24:56 +0000174 return;
Kevin Enderby6524bd82016-07-19 20:47:07 +0000175 }
Kevin Enderby7a969422015-11-05 19:24:56 +0000176 Size += MemberSize.get();
Rafael Espindolabe9ab262015-07-22 19:34:26 +0000177 Data = StringRef(Start, Size);
178 }
Rafael Espindola0f3de642013-07-09 05:26:25 +0000179
Rafael Espindola747bc072013-07-09 03:39:35 +0000180 // Setup StartOfFile and PaddingBytes.
Kevin Enderby95b08422016-07-25 20:36:36 +0000181 StartOfFile = Header.getSizeOf();
Rafael Espindola747bc072013-07-09 03:39:35 +0000182 // Don't include attached name.
Rafael Espindolabe9ab262015-07-22 19:34:26 +0000183 StringRef Name = getRawName();
Rafael Espindola747bc072013-07-09 03:39:35 +0000184 if (Name.startswith("#1/")) {
185 uint64_t NameSize;
Vedant Kumar98372e32016-02-16 02:06:01 +0000186 if (Name.substr(3).rtrim(' ').getAsInteger(10, NameSize))
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000187 llvm_unreachable("Long name length is not an integer");
Rafael Espindola747bc072013-07-09 03:39:35 +0000188 StartOfFile += NameSize;
189 }
190}
191
Kevin Enderby6524bd82016-07-19 20:47:07 +0000192Expected<uint64_t> Archive::Child::getSize() const {
Kevin Enderby1c1add42015-10-13 20:48:04 +0000193 if (Parent->IsThin) {
Kevin Enderby95b08422016-07-25 20:36:36 +0000194 Expected<uint32_t> Size = Header.getSize();
Kevin Enderby6524bd82016-07-19 20:47:07 +0000195 if (!Size)
196 return Size.takeError();
Kevin Enderby1c1add42015-10-13 20:48:04 +0000197 return Size.get();
198 }
Rafael Espindola9d102062014-12-16 01:43:41 +0000199 return Data.size() - StartOfFile;
200}
201
Kevin Enderby6524bd82016-07-19 20:47:07 +0000202Expected<uint64_t> Archive::Child::getRawSize() const {
Kevin Enderby95b08422016-07-25 20:36:36 +0000203 return Header.getSize();
Kevin Enderby13023a12015-01-15 23:19:11 +0000204}
205
Rafael Espindolabe9ab262015-07-22 19:34:26 +0000206bool Archive::Child::isThinMember() const {
Kevin Enderby95b08422016-07-25 20:36:36 +0000207 StringRef Name = Header.getName();
Rafael Espindolabe9ab262015-07-22 19:34:26 +0000208 return Parent->IsThin && Name != "/" && Name != "//";
209}
210
Rafael Espindola694210c2016-05-02 13:45:06 +0000211ErrorOr<std::string> Archive::Child::getFullName() const {
212 assert(isThinMember());
213 ErrorOr<StringRef> NameOrErr = getName();
214 if (std::error_code EC = NameOrErr.getError())
215 return EC;
216 StringRef Name = *NameOrErr;
217 if (sys::path::is_absolute(Name))
218 return Name;
219
220 SmallString<128> FullName = sys::path::parent_path(
221 Parent->getMemoryBufferRef().getBufferIdentifier());
222 sys::path::append(FullName, Name);
223 return StringRef(FullName);
224}
225
Rafael Espindola4b83cb52015-07-14 22:18:43 +0000226ErrorOr<StringRef> Archive::Child::getBuffer() const {
Kevin Enderby7a969422015-11-05 19:24:56 +0000227 if (!isThinMember()) {
Kevin Enderby6524bd82016-07-19 20:47:07 +0000228 Expected<uint32_t> Size = getSize();
229 if (!Size)
230 return errorToErrorCode(Size.takeError());
Kevin Enderby7a969422015-11-05 19:24:56 +0000231 return StringRef(Data.data() + StartOfFile, Size.get());
232 }
Rafael Espindola694210c2016-05-02 13:45:06 +0000233 ErrorOr<std::string> FullNameOrEr = getFullName();
234 if (std::error_code EC = FullNameOrEr.getError())
Rafael Espindola4b83cb52015-07-14 22:18:43 +0000235 return EC;
Rafael Espindola694210c2016-05-02 13:45:06 +0000236 const std::string &FullName = *FullNameOrEr;
Rafael Espindola4b83cb52015-07-14 22:18:43 +0000237 ErrorOr<std::unique_ptr<MemoryBuffer>> Buf = MemoryBuffer::getFile(FullName);
238 if (std::error_code EC = Buf.getError())
239 return EC;
240 Parent->ThinBuffers.push_back(std::move(*Buf));
241 return Parent->ThinBuffers.back()->getBuffer();
242}
243
Kevin Enderby6524bd82016-07-19 20:47:07 +0000244Expected<Archive::Child> Archive::Child::getNext() const {
Rafael Espindola747bc072013-07-09 03:39:35 +0000245 size_t SpaceToSkip = Data.size();
246 // If it's odd, add 1 to make it even.
247 if (SpaceToSkip & 1)
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000248 ++SpaceToSkip;
Rafael Espindola747bc072013-07-09 03:39:35 +0000249
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000250 const char *NextLoc = Data.data() + SpaceToSkip;
Rafael Espindola747bc072013-07-09 03:39:35 +0000251
Kevin Enderby7a969422015-11-05 19:24:56 +0000252 // Check to see if this is at the end of the archive.
253 if (NextLoc == Parent->Data.getBufferEnd())
254 return Child(Parent, nullptr, nullptr);
Rafael Espindola747bc072013-07-09 03:39:35 +0000255
Kevin Enderby7a969422015-11-05 19:24:56 +0000256 // Check to see if this is past the end of the archive.
Kevin Enderby95b08422016-07-25 20:36:36 +0000257 if (NextLoc > Parent->Data.getBufferEnd()) {
258 Twine Msg("offset to next archive member past the end of the archive after "
259 "member ");
260 ErrorOr<StringRef> NameOrErr = getName();
261 if (NameOrErr.getError()) {
262 uint64_t Offset = Data.data() - Parent->getData().data();
263 return malformedError(Msg + "at offset " + Twine(Offset));
264 } else
265 return malformedError(Msg + Twine(NameOrErr.get()));
266 }
Kevin Enderby7a969422015-11-05 19:24:56 +0000267
Kevin Enderby6524bd82016-07-19 20:47:07 +0000268 Error Err;
269 Child Ret(Parent, NextLoc, &Err);
270 if (Err)
271 return std::move(Err);
Kevin Enderby7a969422015-11-05 19:24:56 +0000272 return Ret;
Rafael Espindola747bc072013-07-09 03:39:35 +0000273}
274
Kevin Enderby13023a12015-01-15 23:19:11 +0000275uint64_t Archive::Child::getChildOffset() const {
276 const char *a = Parent->Data.getBuffer().data();
277 const char *c = Data.data();
278 uint64_t offset = c - a;
279 return offset;
280}
281
Rafael Espindolaae460022014-06-16 16:08:36 +0000282ErrorOr<StringRef> Archive::Child::getName() const {
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000283 StringRef name = getRawName();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000284 // Check if it's a special name.
285 if (name[0] == '/') {
Rafael Espindolaae460022014-06-16 16:08:36 +0000286 if (name.size() == 1) // Linker member.
287 return name;
288 if (name.size() == 2 && name[1] == '/') // String table.
289 return name;
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000290 // It's a long name.
291 // Get the offset.
Michael J. Spencer04614ff2013-01-10 00:07:38 +0000292 std::size_t offset;
Vedant Kumar98372e32016-02-16 02:06:01 +0000293 if (name.substr(1).rtrim(' ').getAsInteger(10, offset))
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000294 llvm_unreachable("Long name offset is not an integer");
Rafael Espindola8f238822015-10-31 20:06:13 +0000295
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000296 // Verify it.
Rafael Espindola8f238822015-10-31 20:06:13 +0000297 if (offset >= Parent->StringTable.size())
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000298 return object_error::parse_failed;
Rafael Espindola8f238822015-10-31 20:06:13 +0000299 const char *addr = Parent->StringTable.begin() + offset;
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000300
Rafael Espindolac60d0d22015-07-13 23:07:05 +0000301 // GNU long file names end with a "/\n".
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000302 if (Parent->kind() == K_GNU || Parent->kind() == K_MIPS64) {
Rafael Espindolac60d0d22015-07-13 23:07:05 +0000303 StringRef::size_type End = StringRef(addr).find('\n');
304 return StringRef(addr, End - 1);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000305 }
Rafael Espindolaae460022014-06-16 16:08:36 +0000306 return StringRef(addr);
Michael J. Spencer9aaa8522011-10-25 22:30:42 +0000307 } else if (name.startswith("#1/")) {
Michael J. Spencer751fd882013-01-09 22:58:43 +0000308 uint64_t name_size;
Vedant Kumar98372e32016-02-16 02:06:01 +0000309 if (name.substr(3).rtrim(' ').getAsInteger(10, name_size))
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000310 llvm_unreachable("Long name length is not an ingeter");
Kevin Enderby95b08422016-07-25 20:36:36 +0000311 return Data.substr(Header.getSizeOf(), name_size).rtrim('\0');
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000312 } else {
313 // It is not a long name so trim the blanks at the end of the name.
314 if (name[name.size() - 1] != '/') {
315 return name.rtrim(' ');
316 }
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000317 }
318 // It's a simple name.
319 if (name[name.size() - 1] == '/')
Rafael Espindolaae460022014-06-16 16:08:36 +0000320 return name.substr(0, name.size() - 1);
321 return name;
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000322}
323
Rafael Espindola48af1c22014-08-19 18:44:46 +0000324ErrorOr<MemoryBufferRef> Archive::Child::getMemoryBufferRef() const {
Rafael Espindolaae460022014-06-16 16:08:36 +0000325 ErrorOr<StringRef> NameOrErr = getName();
326 if (std::error_code EC = NameOrErr.getError())
Rafael Espindola63da2952014-01-15 19:37:43 +0000327 return EC;
Rafael Espindolaae460022014-06-16 16:08:36 +0000328 StringRef Name = NameOrErr.get();
Rafael Espindola4b83cb52015-07-14 22:18:43 +0000329 ErrorOr<StringRef> Buf = getBuffer();
330 if (std::error_code EC = Buf.getError())
331 return EC;
332 return MemoryBufferRef(*Buf, Name);
Rafael Espindolaae460022014-06-16 16:08:36 +0000333}
334
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000335Expected<std::unique_ptr<Binary>>
Rafael Espindolaae460022014-06-16 16:08:36 +0000336Archive::Child::getAsBinary(LLVMContext *Context) const {
Rafael Espindola48af1c22014-08-19 18:44:46 +0000337 ErrorOr<MemoryBufferRef> BuffOrErr = getMemoryBufferRef();
Rafael Espindolaae460022014-06-16 16:08:36 +0000338 if (std::error_code EC = BuffOrErr.getError())
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000339 return errorCodeToError(EC);
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +0000340
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +0000341 auto BinaryOrErr = createBinary(BuffOrErr.get(), Context);
342 if (BinaryOrErr)
343 return std::move(*BinaryOrErr);
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000344 return BinaryOrErr.takeError();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000345}
346
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000347Expected<std::unique_ptr<Archive>> Archive::create(MemoryBufferRef Source) {
348 Error Err;
349 std::unique_ptr<Archive> Ret(new Archive(Source, Err));
350 if (Err)
351 return std::move(Err);
Rafael Espindolaf5577132014-07-31 03:36:00 +0000352 return std::move(Ret);
Rafael Espindola692410e2014-01-21 23:06:54 +0000353}
354
Rafael Espindola43358762015-10-31 21:44:42 +0000355void Archive::setFirstRegular(const Child &C) {
356 FirstRegularData = C.Data;
357 FirstRegularStartOfFile = C.StartOfFile;
358}
359
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000360Archive::Archive(MemoryBufferRef Source, Error &Err)
Rafael Espindola43358762015-10-31 21:44:42 +0000361 : Binary(Binary::ID_Archive, Source) {
Lang Hames5e51a2e2016-07-22 16:11:25 +0000362 ErrorAsOutParameter ErrAsOutParam(&Err);
Rafael Espindola9d102062014-12-16 01:43:41 +0000363 StringRef Buffer = Data.getBuffer();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000364 // Check for sufficient magic.
Rafael Espindola9d102062014-12-16 01:43:41 +0000365 if (Buffer.startswith(ThinMagic)) {
366 IsThin = true;
367 } else if (Buffer.startswith(Magic)) {
368 IsThin = false;
369 } else {
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000370 Err = make_error<GenericBinaryError>("File too small to be an archive",
371 object_error::invalid_file_type);
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000372 return;
373 }
374
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000375 // Get the special members.
Lang Hamesfc209622016-07-14 02:24:01 +0000376 child_iterator I = child_begin(Err, false);
377 if (Err)
Kevin Enderby7a969422015-11-05 19:24:56 +0000378 return;
379 child_iterator E = child_end();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000380
Rafael Espindolacc371202016-06-24 13:47:29 +0000381 // This is at least a valid empty archive. Since an empty archive is the
382 // same in all formats, just claim it to be gnu to make sure Format is
383 // initialized.
384 Format = K_GNU;
385
Kevin Enderby7a969422015-11-05 19:24:56 +0000386 if (I == E) {
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000387 Err = Error::success();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000388 return;
389 }
Lang Hamesfc209622016-07-14 02:24:01 +0000390 const Child *C = &*I;
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000391
Kevin Enderby7a969422015-11-05 19:24:56 +0000392 auto Increment = [&]() {
393 ++I;
Lang Hamesfc209622016-07-14 02:24:01 +0000394 if (Err)
Kevin Enderby7a969422015-11-05 19:24:56 +0000395 return true;
Lang Hamesfc209622016-07-14 02:24:01 +0000396 C = &*I;
Kevin Enderby7a969422015-11-05 19:24:56 +0000397 return false;
398 };
399
400 StringRef Name = C->getRawName();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000401
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000402 // Below is the pattern that is used to figure out the archive format
403 // GNU archive format
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000404 // First member : / (may exist, if it exists, points to the symbol table )
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000405 // Second member : // (may exist, if it exists, points to the string table)
406 // Note : The string table is used if the filename exceeds 15 characters
407 // BSD archive format
Rafael Espindola55509922013-07-10 22:07:59 +0000408 // First member : __.SYMDEF or "__.SYMDEF SORTED" (the symbol table)
409 // There is no string table, if the filename exceeds 15 characters or has a
410 // embedded space, the filename has #1/<size>, The size represents the size
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000411 // of the filename that needs to be read after the archive header
412 // COFF archive format
413 // First member : /
414 // Second member : / (provides a directory of symbols)
Rui Ueyamaf4d0a8c2013-06-03 00:27:03 +0000415 // Third member : // (may exist, if it exists, contains the string table)
416 // Note: Microsoft PE/COFF Spec 8.3 says that the third member is present
417 // even if the string table is empty. However, lib.exe does not in fact
418 // seem to create the third member if there's no member whose filename
419 // exceeds 15 characters. So the third member is optional.
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000420
Kevin Enderbyae108ff2016-06-17 22:16:06 +0000421 if (Name == "__.SYMDEF" || Name == "__.SYMDEF_64") {
422 if (Name == "__.SYMDEF")
423 Format = K_BSD;
424 else // Name == "__.SYMDEF_64"
425 Format = K_DARWIN64;
Rafael Espindola4a782fb2015-10-31 21:03:29 +0000426 // We know that the symbol table is not an external file, so we just assert
427 // there is no error.
Kevin Enderby7a969422015-11-05 19:24:56 +0000428 SymbolTable = *C->getBuffer();
429 if (Increment())
430 return;
431 setFirstRegular(*C);
432
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000433 Err = Error::success();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000434 return;
435 }
436
Rafael Espindola55509922013-07-10 22:07:59 +0000437 if (Name.startswith("#1/")) {
438 Format = K_BSD;
439 // We know this is BSD, so getName will work since there is no string table.
Kevin Enderby7a969422015-11-05 19:24:56 +0000440 ErrorOr<StringRef> NameOrErr = C->getName();
Lang Hamesfc209622016-07-14 02:24:01 +0000441 if (auto ec = NameOrErr.getError()) {
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000442 Err = errorCodeToError(ec);
Rafael Espindola55509922013-07-10 22:07:59 +0000443 return;
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000444 }
Rafael Espindolaae460022014-06-16 16:08:36 +0000445 Name = NameOrErr.get();
Nick Kledzikf44dbda2014-11-12 01:37:45 +0000446 if (Name == "__.SYMDEF SORTED" || Name == "__.SYMDEF") {
Rafael Espindola4a782fb2015-10-31 21:03:29 +0000447 // We know that the symbol table is not an external file, so we just
448 // assert there is no error.
Kevin Enderby7a969422015-11-05 19:24:56 +0000449 SymbolTable = *C->getBuffer();
450 if (Increment())
451 return;
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000452 }
Kevin Enderbyae108ff2016-06-17 22:16:06 +0000453 else if (Name == "__.SYMDEF_64 SORTED" || Name == "__.SYMDEF_64") {
454 Format = K_DARWIN64;
455 // We know that the symbol table is not an external file, so we just
456 // assert there is no error.
457 SymbolTable = *C->getBuffer();
458 if (Increment())
459 return;
460 }
Kevin Enderby7a969422015-11-05 19:24:56 +0000461 setFirstRegular(*C);
Rafael Espindola55509922013-07-10 22:07:59 +0000462 return;
463 }
464
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000465 // MIPS 64-bit ELF archives use a special format of a symbol table.
466 // This format is marked by `ar_name` field equals to "/SYM64/".
467 // For detailed description see page 96 in the following document:
468 // http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf
469
470 bool has64SymTable = false;
471 if (Name == "/" || Name == "/SYM64/") {
Rafael Espindola4a782fb2015-10-31 21:03:29 +0000472 // We know that the symbol table is not an external file, so we just assert
473 // there is no error.
Kevin Enderby7a969422015-11-05 19:24:56 +0000474 SymbolTable = *C->getBuffer();
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000475 if (Name == "/SYM64/")
476 has64SymTable = true;
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000477
Kevin Enderby7a969422015-11-05 19:24:56 +0000478 if (Increment())
479 return;
480 if (I == E) {
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000481 Err = Error::success();
Michael J. Spencer04614ff2013-01-10 00:07:38 +0000482 return;
483 }
Kevin Enderby7a969422015-11-05 19:24:56 +0000484 Name = C->getRawName();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000485 }
486
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000487 if (Name == "//") {
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000488 Format = has64SymTable ? K_MIPS64 : K_GNU;
Rafael Espindola8f238822015-10-31 20:06:13 +0000489 // The string table is never an external member, so we just assert on the
490 // ErrorOr.
Kevin Enderby7a969422015-11-05 19:24:56 +0000491 StringTable = *C->getBuffer();
492 if (Increment())
493 return;
494 setFirstRegular(*C);
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000495 Err = Error::success();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000496 return;
497 }
498
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000499 if (Name[0] != '/') {
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000500 Format = has64SymTable ? K_MIPS64 : K_GNU;
Kevin Enderby7a969422015-11-05 19:24:56 +0000501 setFirstRegular(*C);
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000502 Err = Error::success();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000503 return;
504 }
505
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000506 if (Name != "/") {
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000507 Err = errorCodeToError(object_error::parse_failed);
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000508 return;
509 }
510
511 Format = K_COFF;
Rafael Espindola4a782fb2015-10-31 21:03:29 +0000512 // We know that the symbol table is not an external file, so we just assert
513 // there is no error.
Kevin Enderby7a969422015-11-05 19:24:56 +0000514 SymbolTable = *C->getBuffer();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000515
Kevin Enderby7a969422015-11-05 19:24:56 +0000516 if (Increment())
517 return;
518
519 if (I == E) {
520 setFirstRegular(*C);
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000521 Err = Error::success();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000522 return;
523 }
524
Kevin Enderby7a969422015-11-05 19:24:56 +0000525 Name = C->getRawName();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000526
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000527 if (Name == "//") {
Rafael Espindola8f238822015-10-31 20:06:13 +0000528 // The string table is never an external member, so we just assert on the
529 // ErrorOr.
Kevin Enderby7a969422015-11-05 19:24:56 +0000530 StringTable = *C->getBuffer();
531 if (Increment())
532 return;
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000533 }
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000534
Kevin Enderby7a969422015-11-05 19:24:56 +0000535 setFirstRegular(*C);
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000536 Err = Error::success();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000537}
538
Lang Hamesfc209622016-07-14 02:24:01 +0000539Archive::child_iterator Archive::child_begin(Error &Err,
540 bool SkipInternal) const {
Rafael Espindola48af1c22014-08-19 18:44:46 +0000541 if (Data.getBufferSize() == 8) // empty archive.
Rafael Espindola23a97502014-01-21 16:09:45 +0000542 return child_end();
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000543
544 if (SkipInternal)
Lang Hamesfc209622016-07-14 02:24:01 +0000545 return child_iterator(Child(this, FirstRegularData,
546 FirstRegularStartOfFile),
547 &Err);
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000548
Rafael Espindola48af1c22014-08-19 18:44:46 +0000549 const char *Loc = Data.getBufferStart() + strlen(Magic);
Kevin Enderby6524bd82016-07-19 20:47:07 +0000550 Child C(this, Loc, &Err);
551 if (Err)
Lang Hamesfc209622016-07-14 02:24:01 +0000552 return child_end();
Lang Hamesfc209622016-07-14 02:24:01 +0000553 return child_iterator(C, &Err);
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000554}
555
Rafael Espindola23a97502014-01-21 16:09:45 +0000556Archive::child_iterator Archive::child_end() const {
Lang Hamesfc209622016-07-14 02:24:01 +0000557 return child_iterator(Child(this, nullptr, nullptr), nullptr);
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000558}
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000559
Rafael Espindolaae460022014-06-16 16:08:36 +0000560StringRef Archive::Symbol::getName() const {
Rafael Espindola2b054162015-07-14 01:06:16 +0000561 return Parent->getSymbolTable().begin() + StringIndex;
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000562}
563
Rafael Espindolacc86d822015-11-03 01:20:44 +0000564ErrorOr<Archive::Child> Archive::Symbol::getMember() const {
Rafael Espindola2b054162015-07-14 01:06:16 +0000565 const char *Buf = Parent->getSymbolTable().begin();
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000566 const char *Offsets = Buf;
Kevin Enderbyae108ff2016-06-17 22:16:06 +0000567 if (Parent->kind() == K_MIPS64 || Parent->kind() == K_DARWIN64)
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000568 Offsets += sizeof(uint64_t);
569 else
570 Offsets += sizeof(uint32_t);
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000571 uint32_t Offset = 0;
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000572 if (Parent->kind() == K_GNU) {
Rui Ueyama3206b792015-03-02 21:19:12 +0000573 Offset = read32be(Offsets + SymbolIndex * 4);
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000574 } else if (Parent->kind() == K_MIPS64) {
Rui Ueyama3206b792015-03-02 21:19:12 +0000575 Offset = read64be(Offsets + SymbolIndex * 8);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000576 } else if (Parent->kind() == K_BSD) {
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000577 // The SymbolIndex is an index into the ranlib structs that start at
578 // Offsets (the first uint32_t is the number of bytes of the ranlib
579 // structs). The ranlib structs are a pair of uint32_t's the first
580 // being a string table offset and the second being the offset into
581 // the archive of the member that defines the symbol. Which is what
582 // is needed here.
Rui Ueyama3206b792015-03-02 21:19:12 +0000583 Offset = read32le(Offsets + SymbolIndex * 8 + 4);
Kevin Enderbyae108ff2016-06-17 22:16:06 +0000584 } else if (Parent->kind() == K_DARWIN64) {
585 // The SymbolIndex is an index into the ranlib_64 structs that start at
586 // Offsets (the first uint64_t is the number of bytes of the ranlib_64
587 // structs). The ranlib_64 structs are a pair of uint64_t's the first
588 // being a string table offset and the second being the offset into
589 // the archive of the member that defines the symbol. Which is what
590 // is needed here.
591 Offset = read64le(Offsets + SymbolIndex * 16 + 8);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000592 } else {
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000593 // Skip offsets.
Rui Ueyama3206b792015-03-02 21:19:12 +0000594 uint32_t MemberCount = read32le(Buf);
595 Buf += MemberCount * 4 + 4;
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000596
Rui Ueyama3206b792015-03-02 21:19:12 +0000597 uint32_t SymbolCount = read32le(Buf);
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000598 if (SymbolIndex >= SymbolCount)
599 return object_error::parse_failed;
600
Matt Beaumont-Gay68e0b6a2012-11-14 00:21:27 +0000601 // Skip SymbolCount to get to the indices table.
Rui Ueyama3206b792015-03-02 21:19:12 +0000602 const char *Indices = Buf + 4;
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000603
604 // Get the index of the offset in the file member offset table for this
605 // symbol.
Rui Ueyama3206b792015-03-02 21:19:12 +0000606 uint16_t OffsetIndex = read16le(Indices + SymbolIndex * 2);
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000607 // Subtract 1 since OffsetIndex is 1 based.
608 --OffsetIndex;
609
610 if (OffsetIndex >= MemberCount)
611 return object_error::parse_failed;
612
Rui Ueyama3206b792015-03-02 21:19:12 +0000613 Offset = read32le(Offsets + OffsetIndex * 4);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000614 }
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000615
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000616 const char *Loc = Parent->getData().begin() + Offset;
Kevin Enderby6524bd82016-07-19 20:47:07 +0000617 Error Err;
618 Child C(Parent, Loc, &Err);
619 if (Err)
620 return errorToErrorCode(std::move(Err));
Kevin Enderby7a969422015-11-05 19:24:56 +0000621 return C;
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000622}
623
624Archive::Symbol Archive::Symbol::getNext() const {
625 Symbol t(*this);
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000626 if (Parent->kind() == K_BSD) {
627 // t.StringIndex is an offset from the start of the __.SYMDEF or
628 // "__.SYMDEF SORTED" member into the string table for the ranlib
629 // struct indexed by t.SymbolIndex . To change t.StringIndex to the
630 // offset in the string table for t.SymbolIndex+1 we subtract the
631 // its offset from the start of the string table for t.SymbolIndex
632 // and add the offset of the string table for t.SymbolIndex+1.
633
634 // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t
635 // which is the number of bytes of ranlib structs that follow. The ranlib
636 // structs are a pair of uint32_t's the first being a string table offset
637 // and the second being the offset into the archive of the member that
638 // define the symbol. After that the next uint32_t is the byte count of
639 // the string table followed by the string table.
Rafael Espindola2b054162015-07-14 01:06:16 +0000640 const char *Buf = Parent->getSymbolTable().begin();
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000641 uint32_t RanlibCount = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000642 RanlibCount = read32le(Buf) / 8;
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000643 // If t.SymbolIndex + 1 will be past the count of symbols (the RanlibCount)
644 // don't change the t.StringIndex as we don't want to reference a ranlib
645 // past RanlibCount.
646 if (t.SymbolIndex + 1 < RanlibCount) {
647 const char *Ranlibs = Buf + 4;
648 uint32_t CurRanStrx = 0;
649 uint32_t NextRanStrx = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000650 CurRanStrx = read32le(Ranlibs + t.SymbolIndex * 8);
651 NextRanStrx = read32le(Ranlibs + (t.SymbolIndex + 1) * 8);
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000652 t.StringIndex -= CurRanStrx;
653 t.StringIndex += NextRanStrx;
654 }
655 } else {
656 // Go to one past next null.
Rafael Espindola2b054162015-07-14 01:06:16 +0000657 t.StringIndex = Parent->getSymbolTable().find('\0', t.StringIndex) + 1;
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000658 }
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000659 ++t.SymbolIndex;
660 return t;
661}
662
Rafael Espindola23a97502014-01-21 16:09:45 +0000663Archive::symbol_iterator Archive::symbol_begin() const {
Rafael Espindolab6b5f52e2013-07-29 12:40:31 +0000664 if (!hasSymbolTable())
Rafael Espindolafbcafc02013-07-10 20:14:22 +0000665 return symbol_iterator(Symbol(this, 0, 0));
666
Rafael Espindola2b054162015-07-14 01:06:16 +0000667 const char *buf = getSymbolTable().begin();
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000668 if (kind() == K_GNU) {
669 uint32_t symbol_count = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000670 symbol_count = read32be(buf);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000671 buf += sizeof(uint32_t) + (symbol_count * (sizeof(uint32_t)));
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000672 } else if (kind() == K_MIPS64) {
Rui Ueyama3206b792015-03-02 21:19:12 +0000673 uint64_t symbol_count = read64be(buf);
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000674 buf += sizeof(uint64_t) + (symbol_count * (sizeof(uint64_t)));
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000675 } else if (kind() == K_BSD) {
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000676 // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t
677 // which is the number of bytes of ranlib structs that follow. The ranlib
678 // structs are a pair of uint32_t's the first being a string table offset
679 // and the second being the offset into the archive of the member that
680 // define the symbol. After that the next uint32_t is the byte count of
681 // the string table followed by the string table.
682 uint32_t ranlib_count = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000683 ranlib_count = read32le(buf) / 8;
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000684 const char *ranlibs = buf + 4;
685 uint32_t ran_strx = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000686 ran_strx = read32le(ranlibs);
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000687 buf += sizeof(uint32_t) + (ranlib_count * (2 * (sizeof(uint32_t))));
688 // Skip the byte count of the string table.
689 buf += sizeof(uint32_t);
690 buf += ran_strx;
Kevin Enderbyae108ff2016-06-17 22:16:06 +0000691 } else if (kind() == K_DARWIN64) {
692 // The __.SYMDEF_64 or "__.SYMDEF_64 SORTED" member starts with a uint64_t
693 // which is the number of bytes of ranlib_64 structs that follow. The
694 // ranlib_64 structs are a pair of uint64_t's the first being a string
695 // table offset and the second being the offset into the archive of the
696 // member that define the symbol. After that the next uint64_t is the byte
697 // count of the string table followed by the string table.
698 uint64_t ranlib_count = 0;
699 ranlib_count = read64le(buf) / 16;
700 const char *ranlibs = buf + 8;
701 uint64_t ran_strx = 0;
702 ran_strx = read64le(ranlibs);
703 buf += sizeof(uint64_t) + (ranlib_count * (2 * (sizeof(uint64_t))));
704 // Skip the byte count of the string table.
705 buf += sizeof(uint64_t);
706 buf += ran_strx;
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000707 } else {
708 uint32_t member_count = 0;
709 uint32_t symbol_count = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000710 member_count = read32le(buf);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000711 buf += 4 + (member_count * 4); // Skip offsets.
Rui Ueyama3206b792015-03-02 21:19:12 +0000712 symbol_count = read32le(buf);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000713 buf += 4 + (symbol_count * 2); // Skip indices.
714 }
Rafael Espindola2b054162015-07-14 01:06:16 +0000715 uint32_t string_start_offset = buf - getSymbolTable().begin();
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000716 return symbol_iterator(Symbol(this, 0, string_start_offset));
717}
718
Rafael Espindola23a97502014-01-21 16:09:45 +0000719Archive::symbol_iterator Archive::symbol_end() const {
Rui Ueyama407e0972015-05-26 16:20:40 +0000720 return symbol_iterator(Symbol(this, getNumberOfSymbols(), 0));
721}
Rafael Espindolafbcafc02013-07-10 20:14:22 +0000722
Rui Ueyama407e0972015-05-26 16:20:40 +0000723uint32_t Archive::getNumberOfSymbols() const {
Rafael Espindola483ad202015-10-08 18:06:20 +0000724 if (!hasSymbolTable())
725 return 0;
Rafael Espindola2b054162015-07-14 01:06:16 +0000726 const char *buf = getSymbolTable().begin();
Rui Ueyama407e0972015-05-26 16:20:40 +0000727 if (kind() == K_GNU)
728 return read32be(buf);
729 if (kind() == K_MIPS64)
730 return read64be(buf);
731 if (kind() == K_BSD)
732 return read32le(buf) / 8;
Kevin Enderbyae108ff2016-06-17 22:16:06 +0000733 if (kind() == K_DARWIN64)
734 return read64le(buf) / 16;
Rui Ueyama407e0972015-05-26 16:20:40 +0000735 uint32_t member_count = 0;
736 member_count = read32le(buf);
737 buf += 4 + (member_count * 4); // Skip offsets.
738 return read32le(buf);
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000739}
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000740
Lang Hames69f49022016-07-14 20:44:27 +0000741Expected<Optional<Archive::Child>> Archive::findSym(StringRef name) const {
Rafael Espindola23a97502014-01-21 16:09:45 +0000742 Archive::symbol_iterator bs = symbol_begin();
743 Archive::symbol_iterator es = symbol_end();
Rafael Espindolaae460022014-06-16 16:08:36 +0000744
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000745 for (; bs != es; ++bs) {
Rafael Espindolaae460022014-06-16 16:08:36 +0000746 StringRef SymName = bs->getName();
747 if (SymName == name) {
Lang Hames69f49022016-07-14 20:44:27 +0000748 if (auto MemberOrErr = bs->getMember())
749 return Child(*MemberOrErr);
750 else
751 return errorCodeToError(MemberOrErr.getError());
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000752 }
753 }
Lang Hames69f49022016-07-14 20:44:27 +0000754 return Optional<Child>();
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000755}
Rafael Espindolab6b5f52e2013-07-29 12:40:31 +0000756
Rafael Espindola4a782fb2015-10-31 21:03:29 +0000757bool Archive::hasSymbolTable() const { return !SymbolTable.empty(); }