blob: f2021f796d1250258d077405d9f3c644d1c5a229 [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
Kevin Enderby95b08422016-07-25 20:36:36 +000046 if (Size < sizeof(ArMemHdrType)) {
47 if (Err) {
Kevin Enderby31b07f12016-07-29 22:32:02 +000048 std::string Msg("remaining size of archive too small for next archive "
49 "member header ");
Kevin Enderbyf4586032016-07-29 17:44:13 +000050 Expected<StringRef> NameOrErr = getName(Size);
51 if (!NameOrErr) {
52 consumeError(NameOrErr.takeError());
53 uint64_t Offset = RawHeaderPtr - Parent->getData().data();
54 *Err = malformedError(Msg + "at offset " + Twine(Offset));
55 } else
Kevin Enderby31b07f12016-07-29 22:32:02 +000056 *Err = malformedError(Msg + "for " + NameOrErr.get());
Kevin Enderby95b08422016-07-25 20:36:36 +000057 }
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();
Kevin Enderby31b07f12016-07-29 22:32:02 +000067 std::string Msg("terminator characters in archive member \"" + Buf +
68 "\" not the correct \"`\\n\" values for the archive "
69 "member header ");
Kevin Enderbyf4586032016-07-29 17:44:13 +000070 Expected<StringRef> NameOrErr = getName(Size);
71 if (!NameOrErr) {
72 consumeError(NameOrErr.takeError());
73 uint64_t Offset = RawHeaderPtr - Parent->getData().data();
74 *Err = malformedError(Msg + "at offset " + Twine(Offset));
75 } else
Kevin Enderby31b07f12016-07-29 22:32:02 +000076 *Err = malformedError(Msg + "for " + NameOrErr.get());
Kevin Enderby95b08422016-07-25 20:36:36 +000077 }
78 return;
79 }
80}
81
Kevin Enderbyf4586032016-07-29 17:44:13 +000082// This gets the raw name from the ArMemHdr->Name field and checks that it is
83// valid for the kind of archive. If it is not valid it returns an Error.
84Expected<StringRef> ArchiveMemberHeader::getRawName() const {
Rafael Espindola747bc072013-07-09 03:39:35 +000085 char EndCond;
Kevin Enderbyf4586032016-07-29 17:44:13 +000086 auto Kind = Parent->kind();
87 if (Kind == Archive::K_BSD || Kind == Archive::K_DARWIN64) {
88 if (ArMemHdr->Name[0] == ' ') {
89 uint64_t Offset = reinterpret_cast<const char *>(ArMemHdr) -
90 Parent->getData().data();
91 return malformedError("name contains a leading space for archive member "
92 "header at offset " + Twine(Offset));
93 }
94 EndCond = ' ';
95 }
96 else if (ArMemHdr->Name[0] == '/' || ArMemHdr->Name[0] == '#')
Rafael Espindola747bc072013-07-09 03:39:35 +000097 EndCond = ' ';
98 else
99 EndCond = '/';
100 llvm::StringRef::size_type end =
Kevin Enderby95b08422016-07-25 20:36:36 +0000101 llvm::StringRef(ArMemHdr->Name, sizeof(ArMemHdr->Name)).find(EndCond);
Rafael Espindola747bc072013-07-09 03:39:35 +0000102 if (end == llvm::StringRef::npos)
Kevin Enderby95b08422016-07-25 20:36:36 +0000103 end = sizeof(ArMemHdr->Name);
104 assert(end <= sizeof(ArMemHdr->Name) && end > 0);
Rafael Espindola747bc072013-07-09 03:39:35 +0000105 // Don't include the EndCond if there is one.
Kevin Enderby95b08422016-07-25 20:36:36 +0000106 return llvm::StringRef(ArMemHdr->Name, end);
Rafael Espindola747bc072013-07-09 03:39:35 +0000107}
108
Kevin Enderbyf4586032016-07-29 17:44:13 +0000109// This gets the name looking up long names. Size is the size of the archive
110// member including the header, so the size of any name following the header
111// is checked to make sure it does not overflow.
112Expected<StringRef> ArchiveMemberHeader::getName(uint64_t Size) const {
113
114 // This can be called from the ArchiveMemberHeader constructor when the
115 // archive header is truncated to produce an error message with the name.
116 // Make sure the name field is not truncated.
117 if (Size < offsetof(ArMemHdrType, Name) + sizeof(ArMemHdr->Name)) {
118 uint64_t ArchiveOffset = reinterpret_cast<const char *>(ArMemHdr) -
119 Parent->getData().data();
120 return malformedError("archive header truncated before the name field "
121 "for archive member header at offset " +
122 Twine(ArchiveOffset));
123 }
124
125 // The raw name itself can be invalid.
126 Expected<StringRef> NameOrErr = getRawName();
127 if (!NameOrErr)
128 return NameOrErr.takeError();
129 StringRef Name = NameOrErr.get();
130
131 // Check if it's a special name.
132 if (Name[0] == '/') {
133 if (Name.size() == 1) // Linker member.
134 return Name;
135 if (Name.size() == 2 && Name[1] == '/') // String table.
136 return Name;
137 // It's a long name.
138 // Get the string table offset.
139 std::size_t StringOffset;
140 if (Name.substr(1).rtrim(' ').getAsInteger(10, StringOffset)) {
141 std::string Buf;
142 raw_string_ostream OS(Buf);
Kevin Enderby31b07f12016-07-29 22:32:02 +0000143 OS.write_escaped(Name.substr(1).rtrim(' '));
Kevin Enderbyf4586032016-07-29 17:44:13 +0000144 OS.flush();
145 uint64_t ArchiveOffset = reinterpret_cast<const char *>(ArMemHdr) -
146 Parent->getData().data();
147 return malformedError("long name offset characters after the '/' are "
148 "not all decimal numbers: '" + Buf + "' for "
149 "archive member header at offset " +
150 Twine(ArchiveOffset));
151 }
152
153 // Verify it.
154 if (StringOffset >= Parent->getStringTable().size()) {
155 uint64_t ArchiveOffset = reinterpret_cast<const char *>(ArMemHdr) -
156 Parent->getData().data();
157 return malformedError("long name offset " + Twine(StringOffset) + " past "
158 "the end of the string table for archive member "
159 "header at offset " + Twine(ArchiveOffset));
160 }
161 const char *addr = Parent->getStringTable().begin() + StringOffset;
162
163 // GNU long file names end with a "/\n".
164 if (Parent->kind() == Archive::K_GNU ||
165 Parent->kind() == Archive::K_MIPS64) {
166 StringRef::size_type End = StringRef(addr).find('\n');
167 return StringRef(addr, End - 1);
168 }
David Blaikiecd842ec2016-08-01 21:50:43 +0000169 return addr;
170 }
171
172 if (Name.startswith("#1/")) {
Kevin Enderbyf4586032016-07-29 17:44:13 +0000173 uint64_t NameLength;
174 if (Name.substr(3).rtrim(' ').getAsInteger(10, NameLength)) {
175 std::string Buf;
176 raw_string_ostream OS(Buf);
Kevin Enderby31b07f12016-07-29 22:32:02 +0000177 OS.write_escaped(Name.substr(3).rtrim(' '));
Kevin Enderbyf4586032016-07-29 17:44:13 +0000178 OS.flush();
179 uint64_t ArchiveOffset = reinterpret_cast<const char *>(ArMemHdr) -
180 Parent->getData().data();
181 return malformedError("long name length characters after the #1/ are "
182 "not all decimal numbers: '" + Buf + "' for "
183 "archive member header at offset " +
184 Twine(ArchiveOffset));
185 }
186 if (getSizeOf() + NameLength > Size) {
187 uint64_t ArchiveOffset = reinterpret_cast<const char *>(ArMemHdr) -
188 Parent->getData().data();
189 return malformedError("long name length: " + Twine(NameLength) +
190 " extends past the end of the member or archive "
191 "for archive member header at offset " +
192 Twine(ArchiveOffset));
193 }
194 return StringRef(reinterpret_cast<const char *>(ArMemHdr) + getSizeOf(),
195 NameLength).rtrim('\0');
Kevin Enderbyf4586032016-07-29 17:44:13 +0000196 }
David Blaikiecd842ec2016-08-01 21:50:43 +0000197
198 // It is not a long name so trim the blanks at the end of the name.
199 if (Name[Name.size() - 1] != '/')
200 return Name.rtrim(' ');
201
Kevin Enderbyf4586032016-07-29 17:44:13 +0000202 // It's a simple name.
David Blaikiecd842ec2016-08-01 21:50:43 +0000203 return Name.drop_back(1);
Kevin Enderbyf4586032016-07-29 17:44:13 +0000204}
205
Kevin Enderby6524bd82016-07-19 20:47:07 +0000206Expected<uint32_t> ArchiveMemberHeader::getSize() const {
Rafael Espindola8e9385e2013-07-09 12:45:11 +0000207 uint32_t Ret;
Kevin Enderby95b08422016-07-25 20:36:36 +0000208 if (llvm::StringRef(ArMemHdr->Size,
209 sizeof(ArMemHdr->Size)).rtrim(" ").getAsInteger(10, Ret)) {
Kevin Enderby6524bd82016-07-19 20:47:07 +0000210 std::string Buf;
211 raw_string_ostream OS(Buf);
Kevin Enderby95b08422016-07-25 20:36:36 +0000212 OS.write_escaped(llvm::StringRef(ArMemHdr->Size,
213 sizeof(ArMemHdr->Size)).rtrim(" "));
Kevin Enderby6524bd82016-07-19 20:47:07 +0000214 OS.flush();
Kevin Enderby95b08422016-07-25 20:36:36 +0000215 uint64_t Offset = reinterpret_cast<const char *>(ArMemHdr) -
216 Parent->getData().data();
Kevin Enderby6524bd82016-07-19 20:47:07 +0000217 return malformedError("characters in size field in archive header are not "
Kevin Enderby95b08422016-07-25 20:36:36 +0000218 "all decimal numbers: '" + Buf + "' for archive "
219 "member header at offset " + Twine(Offset));
Kevin Enderby6524bd82016-07-19 20:47:07 +0000220 }
Rafael Espindola8e9385e2013-07-09 12:45:11 +0000221 return Ret;
Rafael Espindola747bc072013-07-09 03:39:35 +0000222}
223
Vedant Kumar4031d9f2016-08-03 19:02:50 +0000224Expected<sys::fs::perms> ArchiveMemberHeader::getAccessMode() const {
Rafael Espindola8115e1d2013-07-09 12:49:24 +0000225 unsigned Ret;
Kevin Enderby95b08422016-07-25 20:36:36 +0000226 if (StringRef(ArMemHdr->AccessMode,
Vedant Kumar4031d9f2016-08-03 19:02:50 +0000227 sizeof(ArMemHdr->AccessMode)).rtrim(' ').getAsInteger(8, Ret)) {
228 std::string Buf;
229 raw_string_ostream OS(Buf);
230 OS.write_escaped(llvm::StringRef(ArMemHdr->AccessMode,
231 sizeof(ArMemHdr->AccessMode)).rtrim(" "));
232 OS.flush();
233 uint64_t Offset = reinterpret_cast<const char *>(ArMemHdr) -
234 Parent->getData().data();
235 return malformedError("characters in AccessMode field in archive header "
236 "are not all decimal numbers: '" + Buf + "' for the "
237 "archive member header at offset " + Twine(Offset));
238 }
Rafael Espindola8115e1d2013-07-09 12:49:24 +0000239 return static_cast<sys::fs::perms>(Ret);
240}
241
Pavel Labathbff47b52016-10-24 13:38:27 +0000242Expected<sys::TimePoint<std::chrono::seconds>>
243ArchiveMemberHeader::getLastModified() const {
Rafael Espindola8115e1d2013-07-09 12:49:24 +0000244 unsigned Seconds;
Kevin Enderby95b08422016-07-25 20:36:36 +0000245 if (StringRef(ArMemHdr->LastModified,
246 sizeof(ArMemHdr->LastModified)).rtrim(' ')
Vedant Kumar4031d9f2016-08-03 19:02:50 +0000247 .getAsInteger(10, Seconds)) {
248 std::string Buf;
249 raw_string_ostream OS(Buf);
250 OS.write_escaped(llvm::StringRef(ArMemHdr->LastModified,
251 sizeof(ArMemHdr->LastModified)).rtrim(" "));
252 OS.flush();
253 uint64_t Offset = reinterpret_cast<const char *>(ArMemHdr) -
254 Parent->getData().data();
255 return malformedError("characters in LastModified field in archive header "
256 "are not all decimal numbers: '" + Buf + "' for the "
257 "archive member header at offset " + Twine(Offset));
258 }
Rafael Espindola8115e1d2013-07-09 12:49:24 +0000259
Pavel Labathbff47b52016-10-24 13:38:27 +0000260 return sys::toTimePoint(Seconds);
Rafael Espindola8115e1d2013-07-09 12:49:24 +0000261}
262
Vedant Kumar4031d9f2016-08-03 19:02:50 +0000263Expected<unsigned> ArchiveMemberHeader::getUID() const {
Rafael Espindola8115e1d2013-07-09 12:49:24 +0000264 unsigned Ret;
Kevin Enderby95b08422016-07-25 20:36:36 +0000265 StringRef User = StringRef(ArMemHdr->UID, sizeof(ArMemHdr->UID)).rtrim(' ');
Saleem Abdulrasoolaecbdf72016-07-05 00:23:05 +0000266 if (User.empty())
267 return 0;
Vedant Kumar4031d9f2016-08-03 19:02:50 +0000268 if (User.getAsInteger(10, Ret)) {
269 std::string Buf;
270 raw_string_ostream OS(Buf);
271 OS.write_escaped(User);
272 OS.flush();
273 uint64_t Offset = reinterpret_cast<const char *>(ArMemHdr) -
274 Parent->getData().data();
275 return malformedError("characters in UID field in archive header "
276 "are not all decimal numbers: '" + Buf + "' for the "
277 "archive member header at offset " + Twine(Offset));
278 }
Rafael Espindola8115e1d2013-07-09 12:49:24 +0000279 return Ret;
280}
281
Vedant Kumar4031d9f2016-08-03 19:02:50 +0000282Expected<unsigned> ArchiveMemberHeader::getGID() const {
Rafael Espindola8115e1d2013-07-09 12:49:24 +0000283 unsigned Ret;
Kevin Enderby95b08422016-07-25 20:36:36 +0000284 StringRef Group = StringRef(ArMemHdr->GID, sizeof(ArMemHdr->GID)).rtrim(' ');
Saleem Abdulrasoolaecbdf72016-07-05 00:23:05 +0000285 if (Group.empty())
286 return 0;
Vedant Kumar4031d9f2016-08-03 19:02:50 +0000287 if (Group.getAsInteger(10, Ret)) {
288 std::string Buf;
289 raw_string_ostream OS(Buf);
290 OS.write_escaped(Group);
291 OS.flush();
292 uint64_t Offset = reinterpret_cast<const char *>(ArMemHdr) -
293 Parent->getData().data();
294 return malformedError("characters in GID field in archive header "
295 "are not all decimal numbers: '" + Buf + "' for the "
296 "archive member header at offset " + Twine(Offset));
297 }
Rafael Espindola8115e1d2013-07-09 12:49:24 +0000298 return Ret;
299}
300
Rafael Espindola43358762015-10-31 21:44:42 +0000301Archive::Child::Child(const Archive *Parent, StringRef Data,
302 uint16_t StartOfFile)
Kevin Enderby95b08422016-07-25 20:36:36 +0000303 : Parent(Parent), Header(Parent, Data.data(), Data.size(), nullptr),
304 Data(Data), StartOfFile(StartOfFile) {
305}
Rafael Espindola43358762015-10-31 21:44:42 +0000306
Kevin Enderby6524bd82016-07-19 20:47:07 +0000307Archive::Child::Child(const Archive *Parent, const char *Start, Error *Err)
Lang Hamesa5e873e2016-10-05 21:20:00 +0000308 : Parent(Parent),
309 Header(Parent, Start,
310 Parent
311 ? Parent->getData().size() - (Start - Parent->getData().data())
312 : 0, Err) {
Rafael Espindola0f3de642013-07-09 05:26:25 +0000313 if (!Start)
Rafael Espindola747bc072013-07-09 03:39:35 +0000314 return;
Kevin Enderby2c182702016-08-04 21:54:19 +0000315
316 // If we are pointed to real data, Start is not a nullptr, then there must be
317 // a non-null Err pointer available to report malformed data on. Only in
318 // the case sentinel value is being constructed is Err is permitted to be a
319 // nullptr.
320 assert(Err && "Err can't be nullptr if Start is not a nullptr");
321
Lang Hames5e51a2e2016-07-22 16:11:25 +0000322 ErrorAsOutParameter ErrAsOutParam(Err);
Rafael Espindola0f3de642013-07-09 05:26:25 +0000323
Kevin Enderby2c182702016-08-04 21:54:19 +0000324 // If there was an error in the construction of the Header
325 // then just return with the error now set.
326 if (*Err)
Kevin Enderby95b08422016-07-25 20:36:36 +0000327 return;
328
329 uint64_t Size = Header.getSizeOf();
Rafael Espindola9d102062014-12-16 01:43:41 +0000330 Data = StringRef(Start, Size);
Kevin Enderbyf4586032016-07-29 17:44:13 +0000331 Expected<bool> isThinOrErr = isThinMember();
332 if (!isThinOrErr) {
Kevin Enderby2c182702016-08-04 21:54:19 +0000333 *Err = isThinOrErr.takeError();
Kevin Enderbyf4586032016-07-29 17:44:13 +0000334 return;
335 }
336 bool isThin = isThinOrErr.get();
337 if (!isThin) {
Kevin Enderby6524bd82016-07-19 20:47:07 +0000338 Expected<uint64_t> MemberSize = getRawSize();
339 if (!MemberSize) {
Kevin Enderby2c182702016-08-04 21:54:19 +0000340 *Err = MemberSize.takeError();
Kevin Enderby7a969422015-11-05 19:24:56 +0000341 return;
Kevin Enderby6524bd82016-07-19 20:47:07 +0000342 }
Kevin Enderby7a969422015-11-05 19:24:56 +0000343 Size += MemberSize.get();
Rafael Espindolabe9ab262015-07-22 19:34:26 +0000344 Data = StringRef(Start, Size);
345 }
Rafael Espindola0f3de642013-07-09 05:26:25 +0000346
Rafael Espindola747bc072013-07-09 03:39:35 +0000347 // Setup StartOfFile and PaddingBytes.
Kevin Enderby95b08422016-07-25 20:36:36 +0000348 StartOfFile = Header.getSizeOf();
Rafael Espindola747bc072013-07-09 03:39:35 +0000349 // Don't include attached name.
Kevin Enderbyf4586032016-07-29 17:44:13 +0000350 Expected<StringRef> NameOrErr = getRawName();
351 if (!NameOrErr){
Kevin Enderby2c182702016-08-04 21:54:19 +0000352 *Err = NameOrErr.takeError();
Kevin Enderbyf4586032016-07-29 17:44:13 +0000353 return;
354 }
355 StringRef Name = NameOrErr.get();
Rafael Espindola747bc072013-07-09 03:39:35 +0000356 if (Name.startswith("#1/")) {
357 uint64_t NameSize;
Kevin Enderbyf4586032016-07-29 17:44:13 +0000358 if (Name.substr(3).rtrim(' ').getAsInteger(10, NameSize)) {
Kevin Enderby2c182702016-08-04 21:54:19 +0000359 std::string Buf;
360 raw_string_ostream OS(Buf);
361 OS.write_escaped(Name.substr(3).rtrim(' '));
362 OS.flush();
363 uint64_t Offset = Start - Parent->getData().data();
364 *Err = malformedError("long name length characters after the #1/ are "
365 "not all decimal numbers: '" + Buf + "' for "
366 "archive member header at offset " +
367 Twine(Offset));
368 return;
Kevin Enderbyf4586032016-07-29 17:44:13 +0000369 }
Rafael Espindola747bc072013-07-09 03:39:35 +0000370 StartOfFile += NameSize;
371 }
372}
373
Kevin Enderby6524bd82016-07-19 20:47:07 +0000374Expected<uint64_t> Archive::Child::getSize() const {
Kevin Enderby1c1add42015-10-13 20:48:04 +0000375 if (Parent->IsThin) {
Kevin Enderby95b08422016-07-25 20:36:36 +0000376 Expected<uint32_t> Size = Header.getSize();
Kevin Enderby6524bd82016-07-19 20:47:07 +0000377 if (!Size)
378 return Size.takeError();
Kevin Enderby1c1add42015-10-13 20:48:04 +0000379 return Size.get();
380 }
Rafael Espindola9d102062014-12-16 01:43:41 +0000381 return Data.size() - StartOfFile;
382}
383
Kevin Enderby6524bd82016-07-19 20:47:07 +0000384Expected<uint64_t> Archive::Child::getRawSize() const {
Kevin Enderby95b08422016-07-25 20:36:36 +0000385 return Header.getSize();
Kevin Enderby13023a12015-01-15 23:19:11 +0000386}
387
Kevin Enderbyf4586032016-07-29 17:44:13 +0000388Expected<bool> Archive::Child::isThinMember() const {
389 Expected<StringRef> NameOrErr = Header.getRawName();
390 if (!NameOrErr)
391 return NameOrErr.takeError();
392 StringRef Name = NameOrErr.get();
Rafael Espindolabe9ab262015-07-22 19:34:26 +0000393 return Parent->IsThin && Name != "/" && Name != "//";
394}
395
Kevin Enderby27e85bd2016-08-03 21:57:47 +0000396Expected<std::string> Archive::Child::getFullName() const {
Kevin Enderbyf4586032016-07-29 17:44:13 +0000397 Expected<bool> isThin = isThinMember();
398 if (!isThin)
Kevin Enderby27e85bd2016-08-03 21:57:47 +0000399 return isThin.takeError();
Kevin Enderbyf4586032016-07-29 17:44:13 +0000400 assert(isThin.get());
401 Expected<StringRef> NameOrErr = getName();
402 if (!NameOrErr)
Kevin Enderby27e85bd2016-08-03 21:57:47 +0000403 return NameOrErr.takeError();
Rafael Espindola694210c2016-05-02 13:45:06 +0000404 StringRef Name = *NameOrErr;
405 if (sys::path::is_absolute(Name))
406 return Name;
407
408 SmallString<128> FullName = sys::path::parent_path(
409 Parent->getMemoryBufferRef().getBufferIdentifier());
410 sys::path::append(FullName, Name);
411 return StringRef(FullName);
412}
413
Kevin Enderby27e85bd2016-08-03 21:57:47 +0000414Expected<StringRef> Archive::Child::getBuffer() const {
Kevin Enderbyf4586032016-07-29 17:44:13 +0000415 Expected<bool> isThinOrErr = isThinMember();
416 if (!isThinOrErr)
Kevin Enderby27e85bd2016-08-03 21:57:47 +0000417 return isThinOrErr.takeError();
Kevin Enderbyf4586032016-07-29 17:44:13 +0000418 bool isThin = isThinOrErr.get();
419 if (!isThin) {
Kevin Enderby6524bd82016-07-19 20:47:07 +0000420 Expected<uint32_t> Size = getSize();
421 if (!Size)
Kevin Enderby27e85bd2016-08-03 21:57:47 +0000422 return Size.takeError();
Kevin Enderby7a969422015-11-05 19:24:56 +0000423 return StringRef(Data.data() + StartOfFile, Size.get());
424 }
Kevin Enderby27e85bd2016-08-03 21:57:47 +0000425 Expected<std::string> FullNameOrErr = getFullName();
426 if (!FullNameOrErr)
427 return FullNameOrErr.takeError();
428 const std::string &FullName = *FullNameOrErr;
Rafael Espindola4b83cb52015-07-14 22:18:43 +0000429 ErrorOr<std::unique_ptr<MemoryBuffer>> Buf = MemoryBuffer::getFile(FullName);
430 if (std::error_code EC = Buf.getError())
Kevin Enderby27e85bd2016-08-03 21:57:47 +0000431 return errorCodeToError(EC);
Rafael Espindola4b83cb52015-07-14 22:18:43 +0000432 Parent->ThinBuffers.push_back(std::move(*Buf));
433 return Parent->ThinBuffers.back()->getBuffer();
434}
435
Kevin Enderby6524bd82016-07-19 20:47:07 +0000436Expected<Archive::Child> Archive::Child::getNext() const {
Rafael Espindola747bc072013-07-09 03:39:35 +0000437 size_t SpaceToSkip = Data.size();
438 // If it's odd, add 1 to make it even.
439 if (SpaceToSkip & 1)
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000440 ++SpaceToSkip;
Rafael Espindola747bc072013-07-09 03:39:35 +0000441
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000442 const char *NextLoc = Data.data() + SpaceToSkip;
Rafael Espindola747bc072013-07-09 03:39:35 +0000443
Kevin Enderby7a969422015-11-05 19:24:56 +0000444 // Check to see if this is at the end of the archive.
445 if (NextLoc == Parent->Data.getBufferEnd())
Lang Hamesa5e873e2016-10-05 21:20:00 +0000446 return Child(nullptr, nullptr, nullptr);
Rafael Espindola747bc072013-07-09 03:39:35 +0000447
Kevin Enderby7a969422015-11-05 19:24:56 +0000448 // Check to see if this is past the end of the archive.
Kevin Enderby95b08422016-07-25 20:36:36 +0000449 if (NextLoc > Parent->Data.getBufferEnd()) {
Kevin Enderby31b07f12016-07-29 22:32:02 +0000450 std::string Msg("offset to next archive member past the end of the archive "
451 "after member ");
Kevin Enderbyf4586032016-07-29 17:44:13 +0000452 Expected<StringRef> NameOrErr = getName();
453 if (!NameOrErr) {
454 consumeError(NameOrErr.takeError());
Kevin Enderby95b08422016-07-25 20:36:36 +0000455 uint64_t Offset = Data.data() - Parent->getData().data();
456 return malformedError(Msg + "at offset " + Twine(Offset));
457 } else
Kevin Enderby31b07f12016-07-29 22:32:02 +0000458 return malformedError(Msg + NameOrErr.get());
Kevin Enderby95b08422016-07-25 20:36:36 +0000459 }
Kevin Enderby7a969422015-11-05 19:24:56 +0000460
Mehdi Amini41af4302016-11-11 04:28:40 +0000461 Error Err = Error::success();
Kevin Enderby6524bd82016-07-19 20:47:07 +0000462 Child Ret(Parent, NextLoc, &Err);
463 if (Err)
464 return std::move(Err);
Kevin Enderby7a969422015-11-05 19:24:56 +0000465 return Ret;
Rafael Espindola747bc072013-07-09 03:39:35 +0000466}
467
Kevin Enderby13023a12015-01-15 23:19:11 +0000468uint64_t Archive::Child::getChildOffset() const {
469 const char *a = Parent->Data.getBuffer().data();
470 const char *c = Data.data();
471 uint64_t offset = c - a;
472 return offset;
473}
474
Kevin Enderbyf4586032016-07-29 17:44:13 +0000475Expected<StringRef> Archive::Child::getName() const {
476 Expected<uint64_t> RawSizeOrErr = getRawSize();
477 if (!RawSizeOrErr)
478 return RawSizeOrErr.takeError();
479 uint64_t RawSize = RawSizeOrErr.get();
480 Expected<StringRef> NameOrErr = Header.getName(Header.getSizeOf() + RawSize);
481 if (!NameOrErr)
482 return NameOrErr.takeError();
483 StringRef Name = NameOrErr.get();
484 return Name;
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000485}
486
Kevin Enderbyf4586032016-07-29 17:44:13 +0000487Expected<MemoryBufferRef> Archive::Child::getMemoryBufferRef() const {
488 Expected<StringRef> NameOrErr = getName();
489 if (!NameOrErr)
490 return NameOrErr.takeError();
Rafael Espindolaae460022014-06-16 16:08:36 +0000491 StringRef Name = NameOrErr.get();
Kevin Enderby27e85bd2016-08-03 21:57:47 +0000492 Expected<StringRef> Buf = getBuffer();
493 if (!Buf)
494 return Buf.takeError();
Rafael Espindola4b83cb52015-07-14 22:18:43 +0000495 return MemoryBufferRef(*Buf, Name);
Rafael Espindolaae460022014-06-16 16:08:36 +0000496}
497
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000498Expected<std::unique_ptr<Binary>>
Rafael Espindolaae460022014-06-16 16:08:36 +0000499Archive::Child::getAsBinary(LLVMContext *Context) const {
Kevin Enderbyf4586032016-07-29 17:44:13 +0000500 Expected<MemoryBufferRef> BuffOrErr = getMemoryBufferRef();
501 if (!BuffOrErr)
502 return BuffOrErr.takeError();
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +0000503
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +0000504 auto BinaryOrErr = createBinary(BuffOrErr.get(), Context);
505 if (BinaryOrErr)
506 return std::move(*BinaryOrErr);
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000507 return BinaryOrErr.takeError();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000508}
509
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000510Expected<std::unique_ptr<Archive>> Archive::create(MemoryBufferRef Source) {
Mehdi Amini41af4302016-11-11 04:28:40 +0000511 Error Err = Error::success();
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000512 std::unique_ptr<Archive> Ret(new Archive(Source, Err));
513 if (Err)
514 return std::move(Err);
Rafael Espindolaf5577132014-07-31 03:36:00 +0000515 return std::move(Ret);
Rafael Espindola692410e2014-01-21 23:06:54 +0000516}
517
Rafael Espindola43358762015-10-31 21:44:42 +0000518void Archive::setFirstRegular(const Child &C) {
519 FirstRegularData = C.Data;
520 FirstRegularStartOfFile = C.StartOfFile;
521}
522
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000523Archive::Archive(MemoryBufferRef Source, Error &Err)
Rafael Espindola43358762015-10-31 21:44:42 +0000524 : Binary(Binary::ID_Archive, Source) {
Lang Hames5e51a2e2016-07-22 16:11:25 +0000525 ErrorAsOutParameter ErrAsOutParam(&Err);
Rafael Espindola9d102062014-12-16 01:43:41 +0000526 StringRef Buffer = Data.getBuffer();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000527 // Check for sufficient magic.
Rafael Espindola9d102062014-12-16 01:43:41 +0000528 if (Buffer.startswith(ThinMagic)) {
529 IsThin = true;
530 } else if (Buffer.startswith(Magic)) {
531 IsThin = false;
532 } else {
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000533 Err = make_error<GenericBinaryError>("File too small to be an archive",
534 object_error::invalid_file_type);
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000535 return;
536 }
537
Kevin Enderbyf4586032016-07-29 17:44:13 +0000538 // Make sure Format is initialized before any call to
539 // ArchiveMemberHeader::getName() is made. This could be a valid empty
540 // archive which is the same in all formats. So claiming it to be gnu to is
541 // fine if not totally correct before we look for a string table or table of
542 // contents.
543 Format = K_GNU;
544
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000545 // Get the special members.
Lang Hamesfc209622016-07-14 02:24:01 +0000546 child_iterator I = child_begin(Err, false);
547 if (Err)
Kevin Enderby7a969422015-11-05 19:24:56 +0000548 return;
549 child_iterator E = child_end();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000550
Kevin Enderbyf4586032016-07-29 17:44:13 +0000551 // See if this is a valid empty archive and if so return.
Kevin Enderby7a969422015-11-05 19:24:56 +0000552 if (I == E) {
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000553 Err = Error::success();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000554 return;
555 }
Lang Hamesfc209622016-07-14 02:24:01 +0000556 const Child *C = &*I;
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000557
Kevin Enderby7a969422015-11-05 19:24:56 +0000558 auto Increment = [&]() {
559 ++I;
Lang Hamesfc209622016-07-14 02:24:01 +0000560 if (Err)
Kevin Enderby7a969422015-11-05 19:24:56 +0000561 return true;
Lang Hamesfc209622016-07-14 02:24:01 +0000562 C = &*I;
Kevin Enderby7a969422015-11-05 19:24:56 +0000563 return false;
564 };
565
Kevin Enderbyf4586032016-07-29 17:44:13 +0000566 Expected<StringRef> NameOrErr = C->getRawName();
567 if (!NameOrErr) {
568 Err = NameOrErr.takeError();
569 return;
570 }
571 StringRef Name = NameOrErr.get();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000572
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000573 // Below is the pattern that is used to figure out the archive format
574 // GNU archive format
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000575 // First member : / (may exist, if it exists, points to the symbol table )
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000576 // Second member : // (may exist, if it exists, points to the string table)
577 // Note : The string table is used if the filename exceeds 15 characters
578 // BSD archive format
Rafael Espindola55509922013-07-10 22:07:59 +0000579 // First member : __.SYMDEF or "__.SYMDEF SORTED" (the symbol table)
580 // There is no string table, if the filename exceeds 15 characters or has a
581 // embedded space, the filename has #1/<size>, The size represents the size
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000582 // of the filename that needs to be read after the archive header
583 // COFF archive format
584 // First member : /
585 // Second member : / (provides a directory of symbols)
Rui Ueyamaf4d0a8c2013-06-03 00:27:03 +0000586 // Third member : // (may exist, if it exists, contains the string table)
587 // Note: Microsoft PE/COFF Spec 8.3 says that the third member is present
588 // even if the string table is empty. However, lib.exe does not in fact
589 // seem to create the third member if there's no member whose filename
590 // exceeds 15 characters. So the third member is optional.
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000591
Kevin Enderbyae108ff2016-06-17 22:16:06 +0000592 if (Name == "__.SYMDEF" || Name == "__.SYMDEF_64") {
593 if (Name == "__.SYMDEF")
594 Format = K_BSD;
595 else // Name == "__.SYMDEF_64"
596 Format = K_DARWIN64;
Kevin Enderby27e85bd2016-08-03 21:57:47 +0000597 // We know that the symbol table is not an external file, but we still must
598 // check any Expected<> return value.
599 Expected<StringRef> BufOrErr = C->getBuffer();
600 if (!BufOrErr) {
601 Err = BufOrErr.takeError();
602 return;
603 }
604 SymbolTable = BufOrErr.get();
Kevin Enderby7a969422015-11-05 19:24:56 +0000605 if (Increment())
606 return;
607 setFirstRegular(*C);
608
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000609 Err = Error::success();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000610 return;
611 }
612
Rafael Espindola55509922013-07-10 22:07:59 +0000613 if (Name.startswith("#1/")) {
614 Format = K_BSD;
615 // We know this is BSD, so getName will work since there is no string table.
Kevin Enderbyf4586032016-07-29 17:44:13 +0000616 Expected<StringRef> NameOrErr = C->getName();
617 if (!NameOrErr) {
618 Err = NameOrErr.takeError();
Rafael Espindola55509922013-07-10 22:07:59 +0000619 return;
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000620 }
Rafael Espindolaae460022014-06-16 16:08:36 +0000621 Name = NameOrErr.get();
Nick Kledzikf44dbda2014-11-12 01:37:45 +0000622 if (Name == "__.SYMDEF SORTED" || Name == "__.SYMDEF") {
Kevin Enderby27e85bd2016-08-03 21:57:47 +0000623 // We know that the symbol table is not an external file, but we still
624 // must check any Expected<> return value.
625 Expected<StringRef> BufOrErr = C->getBuffer();
626 if (!BufOrErr) {
627 Err = BufOrErr.takeError();
628 return;
629 }
630 SymbolTable = BufOrErr.get();
Kevin Enderby7a969422015-11-05 19:24:56 +0000631 if (Increment())
632 return;
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000633 }
Kevin Enderbyae108ff2016-06-17 22:16:06 +0000634 else if (Name == "__.SYMDEF_64 SORTED" || Name == "__.SYMDEF_64") {
635 Format = K_DARWIN64;
Kevin Enderby27e85bd2016-08-03 21:57:47 +0000636 // We know that the symbol table is not an external file, but we still
637 // must check any Expected<> return value.
638 Expected<StringRef> BufOrErr = C->getBuffer();
639 if (!BufOrErr) {
640 Err = BufOrErr.takeError();
641 return;
642 }
643 SymbolTable = BufOrErr.get();
Kevin Enderbyae108ff2016-06-17 22:16:06 +0000644 if (Increment())
645 return;
646 }
Kevin Enderby7a969422015-11-05 19:24:56 +0000647 setFirstRegular(*C);
Rafael Espindola55509922013-07-10 22:07:59 +0000648 return;
649 }
650
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000651 // MIPS 64-bit ELF archives use a special format of a symbol table.
652 // This format is marked by `ar_name` field equals to "/SYM64/".
653 // For detailed description see page 96 in the following document:
654 // http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf
655
656 bool has64SymTable = false;
657 if (Name == "/" || Name == "/SYM64/") {
Kevin Enderby27e85bd2016-08-03 21:57:47 +0000658 // We know that the symbol table is not an external file, but we still
659 // must check any Expected<> return value.
660 Expected<StringRef> BufOrErr = C->getBuffer();
661 if (!BufOrErr) {
662 Err = BufOrErr.takeError();
663 return;
664 }
665 SymbolTable = BufOrErr.get();
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000666 if (Name == "/SYM64/")
667 has64SymTable = true;
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000668
Kevin Enderby7a969422015-11-05 19:24:56 +0000669 if (Increment())
670 return;
671 if (I == E) {
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000672 Err = Error::success();
Michael J. Spencer04614ff2013-01-10 00:07:38 +0000673 return;
674 }
Kevin Enderbyf4586032016-07-29 17:44:13 +0000675 Expected<StringRef> NameOrErr = C->getRawName();
676 if (!NameOrErr) {
677 Err = NameOrErr.takeError();
678 return;
679 }
680 Name = NameOrErr.get();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000681 }
682
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000683 if (Name == "//") {
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000684 Format = has64SymTable ? K_MIPS64 : K_GNU;
Kevin Enderby27e85bd2016-08-03 21:57:47 +0000685 // The string table is never an external member, but we still
686 // must check any Expected<> return value.
687 Expected<StringRef> BufOrErr = C->getBuffer();
688 if (!BufOrErr) {
689 Err = BufOrErr.takeError();
690 return;
691 }
692 StringTable = BufOrErr.get();
Kevin Enderby7a969422015-11-05 19:24:56 +0000693 if (Increment())
694 return;
695 setFirstRegular(*C);
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000696 Err = Error::success();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000697 return;
698 }
699
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000700 if (Name[0] != '/') {
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000701 Format = has64SymTable ? K_MIPS64 : K_GNU;
Kevin Enderby7a969422015-11-05 19:24:56 +0000702 setFirstRegular(*C);
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000703 Err = Error::success();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000704 return;
705 }
706
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000707 if (Name != "/") {
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000708 Err = errorCodeToError(object_error::parse_failed);
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000709 return;
710 }
711
712 Format = K_COFF;
Kevin Enderby27e85bd2016-08-03 21:57:47 +0000713 // We know that the symbol table is not an external file, but we still
714 // must check any Expected<> return value.
715 Expected<StringRef> BufOrErr = C->getBuffer();
716 if (!BufOrErr) {
717 Err = BufOrErr.takeError();
718 return;
719 }
720 SymbolTable = BufOrErr.get();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000721
Kevin Enderby7a969422015-11-05 19:24:56 +0000722 if (Increment())
723 return;
724
725 if (I == E) {
726 setFirstRegular(*C);
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000727 Err = Error::success();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000728 return;
729 }
730
Kevin Enderbyf4586032016-07-29 17:44:13 +0000731 NameOrErr = C->getRawName();
732 if (!NameOrErr) {
733 Err = NameOrErr.takeError();
734 return;
735 }
736 Name = NameOrErr.get();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000737
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000738 if (Name == "//") {
Kevin Enderby27e85bd2016-08-03 21:57:47 +0000739 // The string table is never an external member, but we still
740 // must check any Expected<> return value.
741 Expected<StringRef> BufOrErr = C->getBuffer();
742 if (!BufOrErr) {
743 Err = BufOrErr.takeError();
744 return;
745 }
746 StringTable = BufOrErr.get();
Kevin Enderby7a969422015-11-05 19:24:56 +0000747 if (Increment())
748 return;
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000749 }
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000750
Kevin Enderby7a969422015-11-05 19:24:56 +0000751 setFirstRegular(*C);
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000752 Err = Error::success();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000753}
754
Lang Hamesfc209622016-07-14 02:24:01 +0000755Archive::child_iterator Archive::child_begin(Error &Err,
756 bool SkipInternal) const {
Rui Ueyama14a5ca02016-09-30 17:54:31 +0000757 if (isEmpty())
Rafael Espindola23a97502014-01-21 16:09:45 +0000758 return child_end();
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000759
760 if (SkipInternal)
Lang Hamesfc209622016-07-14 02:24:01 +0000761 return child_iterator(Child(this, FirstRegularData,
762 FirstRegularStartOfFile),
763 &Err);
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000764
Rafael Espindola48af1c22014-08-19 18:44:46 +0000765 const char *Loc = Data.getBufferStart() + strlen(Magic);
Kevin Enderby6524bd82016-07-19 20:47:07 +0000766 Child C(this, Loc, &Err);
767 if (Err)
Lang Hamesfc209622016-07-14 02:24:01 +0000768 return child_end();
Lang Hamesfc209622016-07-14 02:24:01 +0000769 return child_iterator(C, &Err);
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000770}
771
Rafael Espindola23a97502014-01-21 16:09:45 +0000772Archive::child_iterator Archive::child_end() const {
Lang Hamesa5e873e2016-10-05 21:20:00 +0000773 return child_iterator(Child(nullptr, nullptr, nullptr), nullptr);
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000774}
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000775
Rafael Espindolaae460022014-06-16 16:08:36 +0000776StringRef Archive::Symbol::getName() const {
Rafael Espindola2b054162015-07-14 01:06:16 +0000777 return Parent->getSymbolTable().begin() + StringIndex;
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000778}
779
Kevin Enderby27e85bd2016-08-03 21:57:47 +0000780Expected<Archive::Child> Archive::Symbol::getMember() const {
Rafael Espindola2b054162015-07-14 01:06:16 +0000781 const char *Buf = Parent->getSymbolTable().begin();
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000782 const char *Offsets = Buf;
Kevin Enderbyae108ff2016-06-17 22:16:06 +0000783 if (Parent->kind() == K_MIPS64 || Parent->kind() == K_DARWIN64)
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000784 Offsets += sizeof(uint64_t);
785 else
786 Offsets += sizeof(uint32_t);
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000787 uint32_t Offset = 0;
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000788 if (Parent->kind() == K_GNU) {
Rui Ueyama3206b792015-03-02 21:19:12 +0000789 Offset = read32be(Offsets + SymbolIndex * 4);
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000790 } else if (Parent->kind() == K_MIPS64) {
Rui Ueyama3206b792015-03-02 21:19:12 +0000791 Offset = read64be(Offsets + SymbolIndex * 8);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000792 } else if (Parent->kind() == K_BSD) {
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000793 // The SymbolIndex is an index into the ranlib structs that start at
794 // Offsets (the first uint32_t is the number of bytes of the ranlib
795 // structs). The ranlib structs are a pair of uint32_t's the first
796 // being a string table offset and the second being the offset into
797 // the archive of the member that defines the symbol. Which is what
798 // is needed here.
Rui Ueyama3206b792015-03-02 21:19:12 +0000799 Offset = read32le(Offsets + SymbolIndex * 8 + 4);
Kevin Enderbyae108ff2016-06-17 22:16:06 +0000800 } else if (Parent->kind() == K_DARWIN64) {
801 // The SymbolIndex is an index into the ranlib_64 structs that start at
802 // Offsets (the first uint64_t is the number of bytes of the ranlib_64
803 // structs). The ranlib_64 structs are a pair of uint64_t's the first
804 // being a string table offset and the second being the offset into
805 // the archive of the member that defines the symbol. Which is what
806 // is needed here.
807 Offset = read64le(Offsets + SymbolIndex * 16 + 8);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000808 } else {
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000809 // Skip offsets.
Rui Ueyama3206b792015-03-02 21:19:12 +0000810 uint32_t MemberCount = read32le(Buf);
811 Buf += MemberCount * 4 + 4;
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000812
Rui Ueyama3206b792015-03-02 21:19:12 +0000813 uint32_t SymbolCount = read32le(Buf);
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000814 if (SymbolIndex >= SymbolCount)
Kevin Enderby27e85bd2016-08-03 21:57:47 +0000815 return errorCodeToError(object_error::parse_failed);
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000816
Matt Beaumont-Gay68e0b6a2012-11-14 00:21:27 +0000817 // Skip SymbolCount to get to the indices table.
Rui Ueyama3206b792015-03-02 21:19:12 +0000818 const char *Indices = Buf + 4;
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000819
820 // Get the index of the offset in the file member offset table for this
821 // symbol.
Rui Ueyama3206b792015-03-02 21:19:12 +0000822 uint16_t OffsetIndex = read16le(Indices + SymbolIndex * 2);
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000823 // Subtract 1 since OffsetIndex is 1 based.
824 --OffsetIndex;
825
826 if (OffsetIndex >= MemberCount)
Kevin Enderby27e85bd2016-08-03 21:57:47 +0000827 return errorCodeToError(object_error::parse_failed);
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000828
Rui Ueyama3206b792015-03-02 21:19:12 +0000829 Offset = read32le(Offsets + OffsetIndex * 4);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000830 }
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000831
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000832 const char *Loc = Parent->getData().begin() + Offset;
Mehdi Amini41af4302016-11-11 04:28:40 +0000833 Error Err = Error::success();
Kevin Enderby6524bd82016-07-19 20:47:07 +0000834 Child C(Parent, Loc, &Err);
835 if (Err)
Kevin Enderby27e85bd2016-08-03 21:57:47 +0000836 return std::move(Err);
Kevin Enderby7a969422015-11-05 19:24:56 +0000837 return C;
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000838}
839
840Archive::Symbol Archive::Symbol::getNext() const {
841 Symbol t(*this);
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000842 if (Parent->kind() == K_BSD) {
843 // t.StringIndex is an offset from the start of the __.SYMDEF or
844 // "__.SYMDEF SORTED" member into the string table for the ranlib
845 // struct indexed by t.SymbolIndex . To change t.StringIndex to the
846 // offset in the string table for t.SymbolIndex+1 we subtract the
847 // its offset from the start of the string table for t.SymbolIndex
848 // and add the offset of the string table for t.SymbolIndex+1.
849
850 // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t
851 // which is the number of bytes of ranlib structs that follow. The ranlib
852 // structs are a pair of uint32_t's the first being a string table offset
853 // and the second being the offset into the archive of the member that
854 // define the symbol. After that the next uint32_t is the byte count of
855 // the string table followed by the string table.
Rafael Espindola2b054162015-07-14 01:06:16 +0000856 const char *Buf = Parent->getSymbolTable().begin();
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000857 uint32_t RanlibCount = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000858 RanlibCount = read32le(Buf) / 8;
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000859 // If t.SymbolIndex + 1 will be past the count of symbols (the RanlibCount)
860 // don't change the t.StringIndex as we don't want to reference a ranlib
861 // past RanlibCount.
862 if (t.SymbolIndex + 1 < RanlibCount) {
863 const char *Ranlibs = Buf + 4;
864 uint32_t CurRanStrx = 0;
865 uint32_t NextRanStrx = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000866 CurRanStrx = read32le(Ranlibs + t.SymbolIndex * 8);
867 NextRanStrx = read32le(Ranlibs + (t.SymbolIndex + 1) * 8);
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000868 t.StringIndex -= CurRanStrx;
869 t.StringIndex += NextRanStrx;
870 }
871 } else {
872 // Go to one past next null.
Rafael Espindola2b054162015-07-14 01:06:16 +0000873 t.StringIndex = Parent->getSymbolTable().find('\0', t.StringIndex) + 1;
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000874 }
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000875 ++t.SymbolIndex;
876 return t;
877}
878
Rafael Espindola23a97502014-01-21 16:09:45 +0000879Archive::symbol_iterator Archive::symbol_begin() const {
Rafael Espindolab6b5f52e2013-07-29 12:40:31 +0000880 if (!hasSymbolTable())
Rafael Espindolafbcafc02013-07-10 20:14:22 +0000881 return symbol_iterator(Symbol(this, 0, 0));
882
Rafael Espindola2b054162015-07-14 01:06:16 +0000883 const char *buf = getSymbolTable().begin();
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000884 if (kind() == K_GNU) {
885 uint32_t symbol_count = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000886 symbol_count = read32be(buf);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000887 buf += sizeof(uint32_t) + (symbol_count * (sizeof(uint32_t)));
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000888 } else if (kind() == K_MIPS64) {
Rui Ueyama3206b792015-03-02 21:19:12 +0000889 uint64_t symbol_count = read64be(buf);
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000890 buf += sizeof(uint64_t) + (symbol_count * (sizeof(uint64_t)));
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000891 } else if (kind() == K_BSD) {
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000892 // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t
893 // which is the number of bytes of ranlib structs that follow. The ranlib
894 // structs are a pair of uint32_t's the first being a string table offset
895 // and the second being the offset into the archive of the member that
896 // define the symbol. After that the next uint32_t is the byte count of
897 // the string table followed by the string table.
898 uint32_t ranlib_count = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000899 ranlib_count = read32le(buf) / 8;
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000900 const char *ranlibs = buf + 4;
901 uint32_t ran_strx = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000902 ran_strx = read32le(ranlibs);
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000903 buf += sizeof(uint32_t) + (ranlib_count * (2 * (sizeof(uint32_t))));
904 // Skip the byte count of the string table.
905 buf += sizeof(uint32_t);
906 buf += ran_strx;
Kevin Enderbyae108ff2016-06-17 22:16:06 +0000907 } else if (kind() == K_DARWIN64) {
908 // The __.SYMDEF_64 or "__.SYMDEF_64 SORTED" member starts with a uint64_t
909 // which is the number of bytes of ranlib_64 structs that follow. The
910 // ranlib_64 structs are a pair of uint64_t's the first being a string
911 // table offset and the second being the offset into the archive of the
912 // member that define the symbol. After that the next uint64_t is the byte
913 // count of the string table followed by the string table.
914 uint64_t ranlib_count = 0;
915 ranlib_count = read64le(buf) / 16;
916 const char *ranlibs = buf + 8;
917 uint64_t ran_strx = 0;
918 ran_strx = read64le(ranlibs);
919 buf += sizeof(uint64_t) + (ranlib_count * (2 * (sizeof(uint64_t))));
920 // Skip the byte count of the string table.
921 buf += sizeof(uint64_t);
922 buf += ran_strx;
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000923 } else {
924 uint32_t member_count = 0;
925 uint32_t symbol_count = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000926 member_count = read32le(buf);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000927 buf += 4 + (member_count * 4); // Skip offsets.
Rui Ueyama3206b792015-03-02 21:19:12 +0000928 symbol_count = read32le(buf);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000929 buf += 4 + (symbol_count * 2); // Skip indices.
930 }
Rafael Espindola2b054162015-07-14 01:06:16 +0000931 uint32_t string_start_offset = buf - getSymbolTable().begin();
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000932 return symbol_iterator(Symbol(this, 0, string_start_offset));
933}
934
Rafael Espindola23a97502014-01-21 16:09:45 +0000935Archive::symbol_iterator Archive::symbol_end() const {
Rui Ueyama407e0972015-05-26 16:20:40 +0000936 return symbol_iterator(Symbol(this, getNumberOfSymbols(), 0));
937}
Rafael Espindolafbcafc02013-07-10 20:14:22 +0000938
Rui Ueyama407e0972015-05-26 16:20:40 +0000939uint32_t Archive::getNumberOfSymbols() const {
Rafael Espindola483ad202015-10-08 18:06:20 +0000940 if (!hasSymbolTable())
941 return 0;
Rafael Espindola2b054162015-07-14 01:06:16 +0000942 const char *buf = getSymbolTable().begin();
Rui Ueyama407e0972015-05-26 16:20:40 +0000943 if (kind() == K_GNU)
944 return read32be(buf);
945 if (kind() == K_MIPS64)
946 return read64be(buf);
947 if (kind() == K_BSD)
948 return read32le(buf) / 8;
Kevin Enderbyae108ff2016-06-17 22:16:06 +0000949 if (kind() == K_DARWIN64)
950 return read64le(buf) / 16;
Rui Ueyama407e0972015-05-26 16:20:40 +0000951 uint32_t member_count = 0;
952 member_count = read32le(buf);
953 buf += 4 + (member_count * 4); // Skip offsets.
954 return read32le(buf);
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000955}
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000956
Lang Hames69f49022016-07-14 20:44:27 +0000957Expected<Optional<Archive::Child>> Archive::findSym(StringRef name) const {
Rafael Espindola23a97502014-01-21 16:09:45 +0000958 Archive::symbol_iterator bs = symbol_begin();
959 Archive::symbol_iterator es = symbol_end();
Rafael Espindolaae460022014-06-16 16:08:36 +0000960
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000961 for (; bs != es; ++bs) {
Rafael Espindolaae460022014-06-16 16:08:36 +0000962 StringRef SymName = bs->getName();
963 if (SymName == name) {
Lang Hames69f49022016-07-14 20:44:27 +0000964 if (auto MemberOrErr = bs->getMember())
965 return Child(*MemberOrErr);
966 else
Kevin Enderby27e85bd2016-08-03 21:57:47 +0000967 return MemberOrErr.takeError();
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000968 }
969 }
Lang Hames69f49022016-07-14 20:44:27 +0000970 return Optional<Child>();
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000971}
Rafael Espindolab6b5f52e2013-07-29 12:40:31 +0000972
Rui Ueyama14a5ca02016-09-30 17:54:31 +0000973// Returns true if archive file contains no member file.
974bool Archive::isEmpty() const { return Data.getBufferSize() == 8; }
975
Rafael Espindola4a782fb2015-10-31 21:03:29 +0000976bool Archive::hasSymbolTable() const { return !SymbolTable.empty(); }