blob: 49e66f46ab3f249eff64ff27233297d9201c5dd3 [file] [log] [blame]
Eugene Zelenkod341c932017-04-19 23:02:10 +00001//===- Archive.cpp - ar File Format implementation ------------------------===//
Michael J. Spencerd3b7b122011-09-27 19:36:55 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Michael J. Spencerd3b7b122011-09-27 19:36:55 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the ArchiveObjectFile class.
10//
11//===----------------------------------------------------------------------===//
12
Chandler Carruth6bda14b2017-06-06 11:49:48 +000013#include "llvm/Object/Archive.h"
Eugene Zelenkod341c932017-04-19 23:02:10 +000014#include "llvm/ADT/Optional.h"
Rafael Espindola747bc072013-07-09 03:39:35 +000015#include "llvm/ADT/SmallString.h"
Eugene Zelenkod341c932017-04-19 23:02:10 +000016#include "llvm/ADT/StringRef.h"
Rafael Espindola747bc072013-07-09 03:39:35 +000017#include "llvm/ADT/Twine.h"
Eugene Zelenkod341c932017-04-19 23:02:10 +000018#include "llvm/Object/Binary.h"
19#include "llvm/Object/Error.h"
20#include "llvm/Support/Chrono.h"
Michael J. Spencere03ea9c2011-11-02 19:33:12 +000021#include "llvm/Support/Endian.h"
Eugene Zelenkod341c932017-04-19 23:02:10 +000022#include "llvm/Support/Error.h"
23#include "llvm/Support/ErrorOr.h"
24#include "llvm/Support/FileSystem.h"
Michael J. Spencerd3b7b122011-09-27 19:36:55 +000025#include "llvm/Support/MemoryBuffer.h"
Rafael Espindola4b83cb52015-07-14 22:18:43 +000026#include "llvm/Support/Path.h"
Eugene Zelenkod341c932017-04-19 23:02:10 +000027#include "llvm/Support/raw_ostream.h"
28#include <algorithm>
29#include <cassert>
30#include <cstddef>
31#include <cstdint>
32#include <cstring>
33#include <memory>
34#include <string>
35#include <system_error>
Michael J. Spencerd3b7b122011-09-27 19:36:55 +000036
37using namespace llvm;
38using namespace object;
Rui Ueyama3206b792015-03-02 21:19:12 +000039using namespace llvm::support::endian;
Michael J. Spencerd3b7b122011-09-27 19:36:55 +000040
Craig Topperd3a34f82013-07-16 01:17:10 +000041static const char *const Magic = "!<arch>\n";
Rafael Espindola9d102062014-12-16 01:43:41 +000042static const char *const ThinMagic = "!<thin>\n";
Michael J. Spencerd3b7b122011-09-27 19:36:55 +000043
Eugene Zelenkod341c932017-04-19 23:02:10 +000044void Archive::anchor() {}
David Blaikiea379b1812011-12-20 02:50:00 +000045
Kevin Enderby6524bd82016-07-19 20:47:07 +000046static Error
47malformedError(Twine Msg) {
48 std::string StringMsg = "truncated or malformed archive (" + Msg.str() + ")";
49 return make_error<GenericBinaryError>(std::move(StringMsg),
50 object_error::parse_failed);
51}
52
Kevin Enderby95b08422016-07-25 20:36:36 +000053ArchiveMemberHeader::ArchiveMemberHeader(const Archive *Parent,
54 const char *RawHeaderPtr,
55 uint64_t Size, Error *Err)
56 : Parent(Parent),
57 ArMemHdr(reinterpret_cast<const ArMemHdrType *>(RawHeaderPtr)) {
58 if (RawHeaderPtr == nullptr)
59 return;
60 ErrorAsOutParameter ErrAsOutParam(Err);
61
Kevin Enderby95b08422016-07-25 20:36:36 +000062 if (Size < sizeof(ArMemHdrType)) {
63 if (Err) {
Kevin Enderby31b07f12016-07-29 22:32:02 +000064 std::string Msg("remaining size of archive too small for next archive "
65 "member header ");
Kevin Enderbyf4586032016-07-29 17:44:13 +000066 Expected<StringRef> NameOrErr = getName(Size);
67 if (!NameOrErr) {
68 consumeError(NameOrErr.takeError());
69 uint64_t Offset = RawHeaderPtr - Parent->getData().data();
70 *Err = malformedError(Msg + "at offset " + Twine(Offset));
71 } else
Kevin Enderby31b07f12016-07-29 22:32:02 +000072 *Err = malformedError(Msg + "for " + NameOrErr.get());
Kevin Enderby95b08422016-07-25 20:36:36 +000073 }
74 return;
75 }
76 if (ArMemHdr->Terminator[0] != '`' || ArMemHdr->Terminator[1] != '\n') {
77 if (Err) {
78 std::string Buf;
79 raw_string_ostream OS(Buf);
Eugene Zelenkod341c932017-04-19 23:02:10 +000080 OS.write_escaped(StringRef(ArMemHdr->Terminator,
81 sizeof(ArMemHdr->Terminator)));
Kevin Enderby95b08422016-07-25 20:36:36 +000082 OS.flush();
Kevin Enderby31b07f12016-07-29 22:32:02 +000083 std::string Msg("terminator characters in archive member \"" + Buf +
84 "\" not the correct \"`\\n\" values for the archive "
85 "member header ");
Kevin Enderbyf4586032016-07-29 17:44:13 +000086 Expected<StringRef> NameOrErr = getName(Size);
87 if (!NameOrErr) {
88 consumeError(NameOrErr.takeError());
89 uint64_t Offset = RawHeaderPtr - Parent->getData().data();
90 *Err = malformedError(Msg + "at offset " + Twine(Offset));
91 } else
Kevin Enderby31b07f12016-07-29 22:32:02 +000092 *Err = malformedError(Msg + "for " + NameOrErr.get());
Kevin Enderby95b08422016-07-25 20:36:36 +000093 }
94 return;
95 }
96}
97
Kevin Enderbyf4586032016-07-29 17:44:13 +000098// This gets the raw name from the ArMemHdr->Name field and checks that it is
99// valid for the kind of archive. If it is not valid it returns an Error.
100Expected<StringRef> ArchiveMemberHeader::getRawName() const {
Rafael Espindola747bc072013-07-09 03:39:35 +0000101 char EndCond;
Kevin Enderbyf4586032016-07-29 17:44:13 +0000102 auto Kind = Parent->kind();
103 if (Kind == Archive::K_BSD || Kind == Archive::K_DARWIN64) {
104 if (ArMemHdr->Name[0] == ' ') {
105 uint64_t Offset = reinterpret_cast<const char *>(ArMemHdr) -
106 Parent->getData().data();
107 return malformedError("name contains a leading space for archive member "
108 "header at offset " + Twine(Offset));
109 }
110 EndCond = ' ';
111 }
112 else if (ArMemHdr->Name[0] == '/' || ArMemHdr->Name[0] == '#')
Rafael Espindola747bc072013-07-09 03:39:35 +0000113 EndCond = ' ';
114 else
115 EndCond = '/';
Eugene Zelenkod341c932017-04-19 23:02:10 +0000116 StringRef::size_type end =
117 StringRef(ArMemHdr->Name, sizeof(ArMemHdr->Name)).find(EndCond);
118 if (end == StringRef::npos)
Kevin Enderby95b08422016-07-25 20:36:36 +0000119 end = sizeof(ArMemHdr->Name);
120 assert(end <= sizeof(ArMemHdr->Name) && end > 0);
Rafael Espindola747bc072013-07-09 03:39:35 +0000121 // Don't include the EndCond if there is one.
Eugene Zelenkod341c932017-04-19 23:02:10 +0000122 return StringRef(ArMemHdr->Name, end);
Rafael Espindola747bc072013-07-09 03:39:35 +0000123}
124
Kevin Enderbyf4586032016-07-29 17:44:13 +0000125// This gets the name looking up long names. Size is the size of the archive
126// member including the header, so the size of any name following the header
127// is checked to make sure it does not overflow.
128Expected<StringRef> ArchiveMemberHeader::getName(uint64_t Size) const {
129
130 // This can be called from the ArchiveMemberHeader constructor when the
131 // archive header is truncated to produce an error message with the name.
132 // Make sure the name field is not truncated.
133 if (Size < offsetof(ArMemHdrType, Name) + sizeof(ArMemHdr->Name)) {
134 uint64_t ArchiveOffset = reinterpret_cast<const char *>(ArMemHdr) -
135 Parent->getData().data();
136 return malformedError("archive header truncated before the name field "
137 "for archive member header at offset " +
138 Twine(ArchiveOffset));
139 }
140
141 // The raw name itself can be invalid.
142 Expected<StringRef> NameOrErr = getRawName();
143 if (!NameOrErr)
144 return NameOrErr.takeError();
145 StringRef Name = NameOrErr.get();
146
147 // Check if it's a special name.
148 if (Name[0] == '/') {
149 if (Name.size() == 1) // Linker member.
150 return Name;
151 if (Name.size() == 2 && Name[1] == '/') // String table.
152 return Name;
153 // It's a long name.
154 // Get the string table offset.
155 std::size_t StringOffset;
156 if (Name.substr(1).rtrim(' ').getAsInteger(10, StringOffset)) {
157 std::string Buf;
158 raw_string_ostream OS(Buf);
Kevin Enderby31b07f12016-07-29 22:32:02 +0000159 OS.write_escaped(Name.substr(1).rtrim(' '));
Kevin Enderbyf4586032016-07-29 17:44:13 +0000160 OS.flush();
161 uint64_t ArchiveOffset = reinterpret_cast<const char *>(ArMemHdr) -
162 Parent->getData().data();
163 return malformedError("long name offset characters after the '/' are "
164 "not all decimal numbers: '" + Buf + "' for "
165 "archive member header at offset " +
166 Twine(ArchiveOffset));
167 }
168
169 // Verify it.
170 if (StringOffset >= Parent->getStringTable().size()) {
171 uint64_t ArchiveOffset = reinterpret_cast<const char *>(ArMemHdr) -
172 Parent->getData().data();
173 return malformedError("long name offset " + Twine(StringOffset) + " past "
174 "the end of the string table for archive member "
175 "header at offset " + Twine(ArchiveOffset));
176 }
Kevin Enderbyf4586032016-07-29 17:44:13 +0000177
178 // GNU long file names end with a "/\n".
179 if (Parent->kind() == Archive::K_GNU ||
Jake Ehrlich1b30d632017-09-20 18:23:01 +0000180 Parent->kind() == Archive::K_GNU64) {
Hans Wennborg5e6e6cc2018-05-08 08:22:58 +0000181 size_t End = Parent->getStringTable().find('\n', /*From=*/StringOffset);
182 if (End == StringRef::npos || End < 1 ||
183 Parent->getStringTable()[End - 1] != '/') {
184 return malformedError("string table at long name offset " +
185 Twine(StringOffset) + "not terminated");
186 }
187 return Parent->getStringTable().slice(StringOffset, End - 1);
Kevin Enderbyf4586032016-07-29 17:44:13 +0000188 }
Hans Wennborg5e6e6cc2018-05-08 08:22:58 +0000189 return Parent->getStringTable().begin() + StringOffset;
David Blaikiecd842ec2016-08-01 21:50:43 +0000190 }
191
192 if (Name.startswith("#1/")) {
Kevin Enderbyf4586032016-07-29 17:44:13 +0000193 uint64_t NameLength;
194 if (Name.substr(3).rtrim(' ').getAsInteger(10, NameLength)) {
195 std::string Buf;
196 raw_string_ostream OS(Buf);
Kevin Enderby31b07f12016-07-29 22:32:02 +0000197 OS.write_escaped(Name.substr(3).rtrim(' '));
Kevin Enderbyf4586032016-07-29 17:44:13 +0000198 OS.flush();
199 uint64_t ArchiveOffset = reinterpret_cast<const char *>(ArMemHdr) -
200 Parent->getData().data();
201 return malformedError("long name length characters after the #1/ are "
202 "not all decimal numbers: '" + Buf + "' for "
203 "archive member header at offset " +
204 Twine(ArchiveOffset));
205 }
206 if (getSizeOf() + NameLength > Size) {
207 uint64_t ArchiveOffset = reinterpret_cast<const char *>(ArMemHdr) -
208 Parent->getData().data();
209 return malformedError("long name length: " + Twine(NameLength) +
210 " extends past the end of the member or archive "
211 "for archive member header at offset " +
212 Twine(ArchiveOffset));
213 }
214 return StringRef(reinterpret_cast<const char *>(ArMemHdr) + getSizeOf(),
215 NameLength).rtrim('\0');
Kevin Enderbyf4586032016-07-29 17:44:13 +0000216 }
David Blaikiecd842ec2016-08-01 21:50:43 +0000217
218 // It is not a long name so trim the blanks at the end of the name.
219 if (Name[Name.size() - 1] != '/')
220 return Name.rtrim(' ');
221
Kevin Enderbyf4586032016-07-29 17:44:13 +0000222 // It's a simple name.
David Blaikiecd842ec2016-08-01 21:50:43 +0000223 return Name.drop_back(1);
Kevin Enderbyf4586032016-07-29 17:44:13 +0000224}
225
Kevin Enderby6524bd82016-07-19 20:47:07 +0000226Expected<uint32_t> ArchiveMemberHeader::getSize() const {
Rafael Espindola8e9385e2013-07-09 12:45:11 +0000227 uint32_t Ret;
Eugene Zelenkod341c932017-04-19 23:02:10 +0000228 if (StringRef(ArMemHdr->Size,
229 sizeof(ArMemHdr->Size)).rtrim(" ").getAsInteger(10, Ret)) {
Kevin Enderby6524bd82016-07-19 20:47:07 +0000230 std::string Buf;
231 raw_string_ostream OS(Buf);
Eugene Zelenkod341c932017-04-19 23:02:10 +0000232 OS.write_escaped(StringRef(ArMemHdr->Size,
233 sizeof(ArMemHdr->Size)).rtrim(" "));
Kevin Enderby6524bd82016-07-19 20:47:07 +0000234 OS.flush();
Kevin Enderby95b08422016-07-25 20:36:36 +0000235 uint64_t Offset = reinterpret_cast<const char *>(ArMemHdr) -
236 Parent->getData().data();
Kevin Enderby6524bd82016-07-19 20:47:07 +0000237 return malformedError("characters in size field in archive header are not "
Kevin Enderby95b08422016-07-25 20:36:36 +0000238 "all decimal numbers: '" + Buf + "' for archive "
239 "member header at offset " + Twine(Offset));
Kevin Enderby6524bd82016-07-19 20:47:07 +0000240 }
Rafael Espindola8e9385e2013-07-09 12:45:11 +0000241 return Ret;
Rafael Espindola747bc072013-07-09 03:39:35 +0000242}
243
Vedant Kumar4031d9f2016-08-03 19:02:50 +0000244Expected<sys::fs::perms> ArchiveMemberHeader::getAccessMode() const {
Rafael Espindola8115e1d2013-07-09 12:49:24 +0000245 unsigned Ret;
Kevin Enderby95b08422016-07-25 20:36:36 +0000246 if (StringRef(ArMemHdr->AccessMode,
Vedant Kumar4031d9f2016-08-03 19:02:50 +0000247 sizeof(ArMemHdr->AccessMode)).rtrim(' ').getAsInteger(8, Ret)) {
248 std::string Buf;
249 raw_string_ostream OS(Buf);
Eugene Zelenkod341c932017-04-19 23:02:10 +0000250 OS.write_escaped(StringRef(ArMemHdr->AccessMode,
251 sizeof(ArMemHdr->AccessMode)).rtrim(" "));
Vedant Kumar4031d9f2016-08-03 19:02:50 +0000252 OS.flush();
253 uint64_t Offset = reinterpret_cast<const char *>(ArMemHdr) -
254 Parent->getData().data();
255 return malformedError("characters in AccessMode 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 return static_cast<sys::fs::perms>(Ret);
260}
261
Pavel Labathbff47b52016-10-24 13:38:27 +0000262Expected<sys::TimePoint<std::chrono::seconds>>
263ArchiveMemberHeader::getLastModified() const {
Rafael Espindola8115e1d2013-07-09 12:49:24 +0000264 unsigned Seconds;
Kevin Enderby95b08422016-07-25 20:36:36 +0000265 if (StringRef(ArMemHdr->LastModified,
266 sizeof(ArMemHdr->LastModified)).rtrim(' ')
Vedant Kumar4031d9f2016-08-03 19:02:50 +0000267 .getAsInteger(10, Seconds)) {
268 std::string Buf;
269 raw_string_ostream OS(Buf);
Eugene Zelenkod341c932017-04-19 23:02:10 +0000270 OS.write_escaped(StringRef(ArMemHdr->LastModified,
271 sizeof(ArMemHdr->LastModified)).rtrim(" "));
Vedant Kumar4031d9f2016-08-03 19:02:50 +0000272 OS.flush();
273 uint64_t Offset = reinterpret_cast<const char *>(ArMemHdr) -
274 Parent->getData().data();
275 return malformedError("characters in LastModified 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
Pavel Labathbff47b52016-10-24 13:38:27 +0000280 return sys::toTimePoint(Seconds);
Rafael Espindola8115e1d2013-07-09 12:49:24 +0000281}
282
Vedant Kumar4031d9f2016-08-03 19:02:50 +0000283Expected<unsigned> ArchiveMemberHeader::getUID() const {
Rafael Espindola8115e1d2013-07-09 12:49:24 +0000284 unsigned Ret;
Kevin Enderby95b08422016-07-25 20:36:36 +0000285 StringRef User = StringRef(ArMemHdr->UID, sizeof(ArMemHdr->UID)).rtrim(' ');
Saleem Abdulrasoolaecbdf72016-07-05 00:23:05 +0000286 if (User.empty())
287 return 0;
Vedant Kumar4031d9f2016-08-03 19:02:50 +0000288 if (User.getAsInteger(10, Ret)) {
289 std::string Buf;
290 raw_string_ostream OS(Buf);
291 OS.write_escaped(User);
292 OS.flush();
293 uint64_t Offset = reinterpret_cast<const char *>(ArMemHdr) -
294 Parent->getData().data();
295 return malformedError("characters in UID field in archive header "
296 "are not all decimal numbers: '" + Buf + "' for the "
297 "archive member header at offset " + Twine(Offset));
298 }
Rafael Espindola8115e1d2013-07-09 12:49:24 +0000299 return Ret;
300}
301
Vedant Kumar4031d9f2016-08-03 19:02:50 +0000302Expected<unsigned> ArchiveMemberHeader::getGID() const {
Rafael Espindola8115e1d2013-07-09 12:49:24 +0000303 unsigned Ret;
Kevin Enderby95b08422016-07-25 20:36:36 +0000304 StringRef Group = StringRef(ArMemHdr->GID, sizeof(ArMemHdr->GID)).rtrim(' ');
Saleem Abdulrasoolaecbdf72016-07-05 00:23:05 +0000305 if (Group.empty())
306 return 0;
Vedant Kumar4031d9f2016-08-03 19:02:50 +0000307 if (Group.getAsInteger(10, Ret)) {
308 std::string Buf;
309 raw_string_ostream OS(Buf);
310 OS.write_escaped(Group);
311 OS.flush();
312 uint64_t Offset = reinterpret_cast<const char *>(ArMemHdr) -
313 Parent->getData().data();
314 return malformedError("characters in GID field in archive header "
315 "are not all decimal numbers: '" + Buf + "' for the "
316 "archive member header at offset " + Twine(Offset));
317 }
Rafael Espindola8115e1d2013-07-09 12:49:24 +0000318 return Ret;
319}
320
Rafael Espindola43358762015-10-31 21:44:42 +0000321Archive::Child::Child(const Archive *Parent, StringRef Data,
322 uint16_t StartOfFile)
Kevin Enderby95b08422016-07-25 20:36:36 +0000323 : Parent(Parent), Header(Parent, Data.data(), Data.size(), nullptr),
324 Data(Data), StartOfFile(StartOfFile) {
325}
Rafael Espindola43358762015-10-31 21:44:42 +0000326
Kevin Enderby6524bd82016-07-19 20:47:07 +0000327Archive::Child::Child(const Archive *Parent, const char *Start, Error *Err)
Lang Hamesa5e873e2016-10-05 21:20:00 +0000328 : Parent(Parent),
329 Header(Parent, Start,
330 Parent
331 ? Parent->getData().size() - (Start - Parent->getData().data())
332 : 0, Err) {
Rafael Espindola0f3de642013-07-09 05:26:25 +0000333 if (!Start)
Rafael Espindola747bc072013-07-09 03:39:35 +0000334 return;
Kevin Enderby2c182702016-08-04 21:54:19 +0000335
336 // If we are pointed to real data, Start is not a nullptr, then there must be
337 // a non-null Err pointer available to report malformed data on. Only in
338 // the case sentinel value is being constructed is Err is permitted to be a
339 // nullptr.
340 assert(Err && "Err can't be nullptr if Start is not a nullptr");
341
Lang Hames5e51a2e2016-07-22 16:11:25 +0000342 ErrorAsOutParameter ErrAsOutParam(Err);
Rafael Espindola0f3de642013-07-09 05:26:25 +0000343
Jake Ehrlich1b30d632017-09-20 18:23:01 +0000344 // If there was an error in the construction of the Header
Kevin Enderby2c182702016-08-04 21:54:19 +0000345 // then just return with the error now set.
346 if (*Err)
Kevin Enderby95b08422016-07-25 20:36:36 +0000347 return;
348
349 uint64_t Size = Header.getSizeOf();
Rafael Espindola9d102062014-12-16 01:43:41 +0000350 Data = StringRef(Start, Size);
Kevin Enderbyf4586032016-07-29 17:44:13 +0000351 Expected<bool> isThinOrErr = isThinMember();
352 if (!isThinOrErr) {
Kevin Enderby2c182702016-08-04 21:54:19 +0000353 *Err = isThinOrErr.takeError();
Kevin Enderbyf4586032016-07-29 17:44:13 +0000354 return;
355 }
356 bool isThin = isThinOrErr.get();
357 if (!isThin) {
Kevin Enderby6524bd82016-07-19 20:47:07 +0000358 Expected<uint64_t> MemberSize = getRawSize();
359 if (!MemberSize) {
Kevin Enderby2c182702016-08-04 21:54:19 +0000360 *Err = MemberSize.takeError();
Kevin Enderby7a969422015-11-05 19:24:56 +0000361 return;
Kevin Enderby6524bd82016-07-19 20:47:07 +0000362 }
Kevin Enderby7a969422015-11-05 19:24:56 +0000363 Size += MemberSize.get();
Rafael Espindolabe9ab262015-07-22 19:34:26 +0000364 Data = StringRef(Start, Size);
365 }
Rafael Espindola0f3de642013-07-09 05:26:25 +0000366
Rafael Espindola747bc072013-07-09 03:39:35 +0000367 // Setup StartOfFile and PaddingBytes.
Kevin Enderby95b08422016-07-25 20:36:36 +0000368 StartOfFile = Header.getSizeOf();
Rafael Espindola747bc072013-07-09 03:39:35 +0000369 // Don't include attached name.
Kevin Enderbyf4586032016-07-29 17:44:13 +0000370 Expected<StringRef> NameOrErr = getRawName();
371 if (!NameOrErr){
Kevin Enderby2c182702016-08-04 21:54:19 +0000372 *Err = NameOrErr.takeError();
Kevin Enderbyf4586032016-07-29 17:44:13 +0000373 return;
374 }
375 StringRef Name = NameOrErr.get();
Rafael Espindola747bc072013-07-09 03:39:35 +0000376 if (Name.startswith("#1/")) {
377 uint64_t NameSize;
Kevin Enderbyf4586032016-07-29 17:44:13 +0000378 if (Name.substr(3).rtrim(' ').getAsInteger(10, NameSize)) {
Kevin Enderby2c182702016-08-04 21:54:19 +0000379 std::string Buf;
380 raw_string_ostream OS(Buf);
381 OS.write_escaped(Name.substr(3).rtrim(' '));
382 OS.flush();
383 uint64_t Offset = Start - Parent->getData().data();
384 *Err = malformedError("long name length characters after the #1/ are "
385 "not all decimal numbers: '" + Buf + "' for "
386 "archive member header at offset " +
387 Twine(Offset));
388 return;
Kevin Enderbyf4586032016-07-29 17:44:13 +0000389 }
Rafael Espindola747bc072013-07-09 03:39:35 +0000390 StartOfFile += NameSize;
391 }
392}
393
Kevin Enderby6524bd82016-07-19 20:47:07 +0000394Expected<uint64_t> Archive::Child::getSize() const {
Kevin Enderby1c1add42015-10-13 20:48:04 +0000395 if (Parent->IsThin) {
Kevin Enderby95b08422016-07-25 20:36:36 +0000396 Expected<uint32_t> Size = Header.getSize();
Kevin Enderby6524bd82016-07-19 20:47:07 +0000397 if (!Size)
398 return Size.takeError();
Kevin Enderby1c1add42015-10-13 20:48:04 +0000399 return Size.get();
400 }
Rafael Espindola9d102062014-12-16 01:43:41 +0000401 return Data.size() - StartOfFile;
402}
403
Kevin Enderby6524bd82016-07-19 20:47:07 +0000404Expected<uint64_t> Archive::Child::getRawSize() const {
Kevin Enderby95b08422016-07-25 20:36:36 +0000405 return Header.getSize();
Kevin Enderby13023a12015-01-15 23:19:11 +0000406}
407
Kevin Enderbyf4586032016-07-29 17:44:13 +0000408Expected<bool> Archive::Child::isThinMember() const {
409 Expected<StringRef> NameOrErr = Header.getRawName();
410 if (!NameOrErr)
411 return NameOrErr.takeError();
412 StringRef Name = NameOrErr.get();
Rafael Espindolabe9ab262015-07-22 19:34:26 +0000413 return Parent->IsThin && Name != "/" && Name != "//";
414}
415
Kevin Enderby27e85bd2016-08-03 21:57:47 +0000416Expected<std::string> Archive::Child::getFullName() const {
Kevin Enderbyf4586032016-07-29 17:44:13 +0000417 Expected<bool> isThin = isThinMember();
418 if (!isThin)
Kevin Enderby27e85bd2016-08-03 21:57:47 +0000419 return isThin.takeError();
Kevin Enderbyf4586032016-07-29 17:44:13 +0000420 assert(isThin.get());
421 Expected<StringRef> NameOrErr = getName();
422 if (!NameOrErr)
Kevin Enderby27e85bd2016-08-03 21:57:47 +0000423 return NameOrErr.takeError();
Rafael Espindola694210c2016-05-02 13:45:06 +0000424 StringRef Name = *NameOrErr;
425 if (sys::path::is_absolute(Name))
426 return Name;
427
428 SmallString<128> FullName = sys::path::parent_path(
429 Parent->getMemoryBufferRef().getBufferIdentifier());
430 sys::path::append(FullName, Name);
431 return StringRef(FullName);
432}
433
Kevin Enderby27e85bd2016-08-03 21:57:47 +0000434Expected<StringRef> Archive::Child::getBuffer() const {
Kevin Enderbyf4586032016-07-29 17:44:13 +0000435 Expected<bool> isThinOrErr = isThinMember();
436 if (!isThinOrErr)
Kevin Enderby27e85bd2016-08-03 21:57:47 +0000437 return isThinOrErr.takeError();
Kevin Enderbyf4586032016-07-29 17:44:13 +0000438 bool isThin = isThinOrErr.get();
439 if (!isThin) {
Kevin Enderby6524bd82016-07-19 20:47:07 +0000440 Expected<uint32_t> Size = getSize();
441 if (!Size)
Kevin Enderby27e85bd2016-08-03 21:57:47 +0000442 return Size.takeError();
Kevin Enderby7a969422015-11-05 19:24:56 +0000443 return StringRef(Data.data() + StartOfFile, Size.get());
444 }
Kevin Enderby27e85bd2016-08-03 21:57:47 +0000445 Expected<std::string> FullNameOrErr = getFullName();
446 if (!FullNameOrErr)
447 return FullNameOrErr.takeError();
448 const std::string &FullName = *FullNameOrErr;
Rafael Espindola4b83cb52015-07-14 22:18:43 +0000449 ErrorOr<std::unique_ptr<MemoryBuffer>> Buf = MemoryBuffer::getFile(FullName);
450 if (std::error_code EC = Buf.getError())
Kevin Enderby27e85bd2016-08-03 21:57:47 +0000451 return errorCodeToError(EC);
Rafael Espindola4b83cb52015-07-14 22:18:43 +0000452 Parent->ThinBuffers.push_back(std::move(*Buf));
453 return Parent->ThinBuffers.back()->getBuffer();
454}
455
Kevin Enderby6524bd82016-07-19 20:47:07 +0000456Expected<Archive::Child> Archive::Child::getNext() const {
Rafael Espindola747bc072013-07-09 03:39:35 +0000457 size_t SpaceToSkip = Data.size();
458 // If it's odd, add 1 to make it even.
459 if (SpaceToSkip & 1)
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000460 ++SpaceToSkip;
Rafael Espindola747bc072013-07-09 03:39:35 +0000461
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000462 const char *NextLoc = Data.data() + SpaceToSkip;
Rafael Espindola747bc072013-07-09 03:39:35 +0000463
Kevin Enderby7a969422015-11-05 19:24:56 +0000464 // Check to see if this is at the end of the archive.
465 if (NextLoc == Parent->Data.getBufferEnd())
Lang Hamesa5e873e2016-10-05 21:20:00 +0000466 return Child(nullptr, nullptr, nullptr);
Rafael Espindola747bc072013-07-09 03:39:35 +0000467
Kevin Enderby7a969422015-11-05 19:24:56 +0000468 // Check to see if this is past the end of the archive.
Kevin Enderby95b08422016-07-25 20:36:36 +0000469 if (NextLoc > Parent->Data.getBufferEnd()) {
Kevin Enderby31b07f12016-07-29 22:32:02 +0000470 std::string Msg("offset to next archive member past the end of the archive "
471 "after member ");
Kevin Enderbyf4586032016-07-29 17:44:13 +0000472 Expected<StringRef> NameOrErr = getName();
473 if (!NameOrErr) {
474 consumeError(NameOrErr.takeError());
Kevin Enderby95b08422016-07-25 20:36:36 +0000475 uint64_t Offset = Data.data() - Parent->getData().data();
476 return malformedError(Msg + "at offset " + Twine(Offset));
477 } else
Kevin Enderby31b07f12016-07-29 22:32:02 +0000478 return malformedError(Msg + NameOrErr.get());
Kevin Enderby95b08422016-07-25 20:36:36 +0000479 }
Kevin Enderby7a969422015-11-05 19:24:56 +0000480
Mehdi Amini41af4302016-11-11 04:28:40 +0000481 Error Err = Error::success();
Kevin Enderby6524bd82016-07-19 20:47:07 +0000482 Child Ret(Parent, NextLoc, &Err);
483 if (Err)
484 return std::move(Err);
Kevin Enderby7a969422015-11-05 19:24:56 +0000485 return Ret;
Rafael Espindola747bc072013-07-09 03:39:35 +0000486}
487
Kevin Enderby13023a12015-01-15 23:19:11 +0000488uint64_t Archive::Child::getChildOffset() const {
489 const char *a = Parent->Data.getBuffer().data();
490 const char *c = Data.data();
491 uint64_t offset = c - a;
492 return offset;
493}
494
Kevin Enderbyf4586032016-07-29 17:44:13 +0000495Expected<StringRef> Archive::Child::getName() const {
496 Expected<uint64_t> RawSizeOrErr = getRawSize();
497 if (!RawSizeOrErr)
498 return RawSizeOrErr.takeError();
499 uint64_t RawSize = RawSizeOrErr.get();
500 Expected<StringRef> NameOrErr = Header.getName(Header.getSizeOf() + RawSize);
501 if (!NameOrErr)
502 return NameOrErr.takeError();
503 StringRef Name = NameOrErr.get();
504 return Name;
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000505}
506
Kevin Enderbyf4586032016-07-29 17:44:13 +0000507Expected<MemoryBufferRef> Archive::Child::getMemoryBufferRef() const {
508 Expected<StringRef> NameOrErr = getName();
509 if (!NameOrErr)
510 return NameOrErr.takeError();
Rafael Espindolaae460022014-06-16 16:08:36 +0000511 StringRef Name = NameOrErr.get();
Kevin Enderby27e85bd2016-08-03 21:57:47 +0000512 Expected<StringRef> Buf = getBuffer();
513 if (!Buf)
Jordan Rupprechtd3a7e9d2019-02-06 20:51:04 +0000514 return createFileError(Name, Buf.takeError());
Rafael Espindola4b83cb52015-07-14 22:18:43 +0000515 return MemoryBufferRef(*Buf, Name);
Rafael Espindolaae460022014-06-16 16:08:36 +0000516}
517
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000518Expected<std::unique_ptr<Binary>>
Rafael Espindolaae460022014-06-16 16:08:36 +0000519Archive::Child::getAsBinary(LLVMContext *Context) const {
Kevin Enderbyf4586032016-07-29 17:44:13 +0000520 Expected<MemoryBufferRef> BuffOrErr = getMemoryBufferRef();
521 if (!BuffOrErr)
522 return BuffOrErr.takeError();
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +0000523
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +0000524 auto BinaryOrErr = createBinary(BuffOrErr.get(), Context);
525 if (BinaryOrErr)
526 return std::move(*BinaryOrErr);
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000527 return BinaryOrErr.takeError();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000528}
529
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000530Expected<std::unique_ptr<Archive>> Archive::create(MemoryBufferRef Source) {
Mehdi Amini41af4302016-11-11 04:28:40 +0000531 Error Err = Error::success();
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000532 std::unique_ptr<Archive> Ret(new Archive(Source, Err));
533 if (Err)
534 return std::move(Err);
Rafael Espindolaf5577132014-07-31 03:36:00 +0000535 return std::move(Ret);
Rafael Espindola692410e2014-01-21 23:06:54 +0000536}
537
Rafael Espindola43358762015-10-31 21:44:42 +0000538void Archive::setFirstRegular(const Child &C) {
539 FirstRegularData = C.Data;
540 FirstRegularStartOfFile = C.StartOfFile;
541}
542
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000543Archive::Archive(MemoryBufferRef Source, Error &Err)
Rafael Espindola43358762015-10-31 21:44:42 +0000544 : Binary(Binary::ID_Archive, Source) {
Lang Hames5e51a2e2016-07-22 16:11:25 +0000545 ErrorAsOutParameter ErrAsOutParam(&Err);
Rafael Espindola9d102062014-12-16 01:43:41 +0000546 StringRef Buffer = Data.getBuffer();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000547 // Check for sufficient magic.
Rafael Espindola9d102062014-12-16 01:43:41 +0000548 if (Buffer.startswith(ThinMagic)) {
549 IsThin = true;
550 } else if (Buffer.startswith(Magic)) {
551 IsThin = false;
552 } else {
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000553 Err = make_error<GenericBinaryError>("File too small to be an archive",
554 object_error::invalid_file_type);
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000555 return;
556 }
557
Kevin Enderbyf4586032016-07-29 17:44:13 +0000558 // Make sure Format is initialized before any call to
559 // ArchiveMemberHeader::getName() is made. This could be a valid empty
560 // archive which is the same in all formats. So claiming it to be gnu to is
561 // fine if not totally correct before we look for a string table or table of
562 // contents.
563 Format = K_GNU;
564
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000565 // Get the special members.
Lang Hamesfc209622016-07-14 02:24:01 +0000566 child_iterator I = child_begin(Err, false);
567 if (Err)
Kevin Enderby7a969422015-11-05 19:24:56 +0000568 return;
569 child_iterator E = child_end();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000570
Kevin Enderbyf4586032016-07-29 17:44:13 +0000571 // See if this is a valid empty archive and if so return.
Kevin Enderby7a969422015-11-05 19:24:56 +0000572 if (I == E) {
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000573 Err = Error::success();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000574 return;
575 }
Lang Hamesfc209622016-07-14 02:24:01 +0000576 const Child *C = &*I;
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000577
Kevin Enderby7a969422015-11-05 19:24:56 +0000578 auto Increment = [&]() {
579 ++I;
Lang Hamesfc209622016-07-14 02:24:01 +0000580 if (Err)
Kevin Enderby7a969422015-11-05 19:24:56 +0000581 return true;
Lang Hamesfc209622016-07-14 02:24:01 +0000582 C = &*I;
Kevin Enderby7a969422015-11-05 19:24:56 +0000583 return false;
584 };
585
Kevin Enderbyf4586032016-07-29 17:44:13 +0000586 Expected<StringRef> NameOrErr = C->getRawName();
587 if (!NameOrErr) {
588 Err = NameOrErr.takeError();
589 return;
590 }
591 StringRef Name = NameOrErr.get();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000592
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000593 // Below is the pattern that is used to figure out the archive format
594 // GNU archive format
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000595 // First member : / (may exist, if it exists, points to the symbol table )
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000596 // Second member : // (may exist, if it exists, points to the string table)
597 // Note : The string table is used if the filename exceeds 15 characters
598 // BSD archive format
Rafael Espindola55509922013-07-10 22:07:59 +0000599 // First member : __.SYMDEF or "__.SYMDEF SORTED" (the symbol table)
600 // There is no string table, if the filename exceeds 15 characters or has a
601 // embedded space, the filename has #1/<size>, The size represents the size
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000602 // of the filename that needs to be read after the archive header
603 // COFF archive format
604 // First member : /
605 // Second member : / (provides a directory of symbols)
Rui Ueyamaf4d0a8c2013-06-03 00:27:03 +0000606 // Third member : // (may exist, if it exists, contains the string table)
607 // Note: Microsoft PE/COFF Spec 8.3 says that the third member is present
608 // even if the string table is empty. However, lib.exe does not in fact
609 // seem to create the third member if there's no member whose filename
610 // exceeds 15 characters. So the third member is optional.
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000611
Kevin Enderbyae108ff2016-06-17 22:16:06 +0000612 if (Name == "__.SYMDEF" || Name == "__.SYMDEF_64") {
613 if (Name == "__.SYMDEF")
614 Format = K_BSD;
615 else // Name == "__.SYMDEF_64"
616 Format = K_DARWIN64;
Kevin Enderby27e85bd2016-08-03 21:57:47 +0000617 // We know that the symbol table is not an external file, but we still must
618 // check any Expected<> return value.
619 Expected<StringRef> BufOrErr = C->getBuffer();
620 if (!BufOrErr) {
621 Err = BufOrErr.takeError();
622 return;
623 }
624 SymbolTable = BufOrErr.get();
Kevin Enderby7a969422015-11-05 19:24:56 +0000625 if (Increment())
626 return;
627 setFirstRegular(*C);
628
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000629 Err = Error::success();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000630 return;
631 }
632
Rafael Espindola55509922013-07-10 22:07:59 +0000633 if (Name.startswith("#1/")) {
634 Format = K_BSD;
635 // We know this is BSD, so getName will work since there is no string table.
Kevin Enderbyf4586032016-07-29 17:44:13 +0000636 Expected<StringRef> NameOrErr = C->getName();
637 if (!NameOrErr) {
638 Err = NameOrErr.takeError();
Rafael Espindola55509922013-07-10 22:07:59 +0000639 return;
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000640 }
Rafael Espindolaae460022014-06-16 16:08:36 +0000641 Name = NameOrErr.get();
Nick Kledzikf44dbda2014-11-12 01:37:45 +0000642 if (Name == "__.SYMDEF SORTED" || Name == "__.SYMDEF") {
Kevin Enderby27e85bd2016-08-03 21:57:47 +0000643 // We know that the symbol table is not an external file, but we still
644 // must check any Expected<> return value.
645 Expected<StringRef> BufOrErr = C->getBuffer();
646 if (!BufOrErr) {
647 Err = BufOrErr.takeError();
648 return;
649 }
650 SymbolTable = BufOrErr.get();
Kevin Enderby7a969422015-11-05 19:24:56 +0000651 if (Increment())
652 return;
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000653 }
Kevin Enderbyae108ff2016-06-17 22:16:06 +0000654 else if (Name == "__.SYMDEF_64 SORTED" || Name == "__.SYMDEF_64") {
655 Format = K_DARWIN64;
Kevin Enderby27e85bd2016-08-03 21:57:47 +0000656 // We know that the symbol table is not an external file, but we still
657 // must check any Expected<> return value.
658 Expected<StringRef> BufOrErr = C->getBuffer();
659 if (!BufOrErr) {
660 Err = BufOrErr.takeError();
661 return;
662 }
663 SymbolTable = BufOrErr.get();
Kevin Enderbyae108ff2016-06-17 22:16:06 +0000664 if (Increment())
665 return;
666 }
Kevin Enderby7a969422015-11-05 19:24:56 +0000667 setFirstRegular(*C);
Rafael Espindola55509922013-07-10 22:07:59 +0000668 return;
669 }
670
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000671 // MIPS 64-bit ELF archives use a special format of a symbol table.
672 // This format is marked by `ar_name` field equals to "/SYM64/".
673 // For detailed description see page 96 in the following document:
674 // http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf
675
676 bool has64SymTable = false;
677 if (Name == "/" || Name == "/SYM64/") {
Kevin Enderby27e85bd2016-08-03 21:57:47 +0000678 // We know that the symbol table is not an external file, but we still
679 // must check any Expected<> return value.
680 Expected<StringRef> BufOrErr = C->getBuffer();
681 if (!BufOrErr) {
682 Err = BufOrErr.takeError();
683 return;
684 }
685 SymbolTable = BufOrErr.get();
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000686 if (Name == "/SYM64/")
687 has64SymTable = true;
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000688
Kevin Enderby7a969422015-11-05 19:24:56 +0000689 if (Increment())
690 return;
691 if (I == E) {
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000692 Err = Error::success();
Michael J. Spencer04614ff2013-01-10 00:07:38 +0000693 return;
694 }
Kevin Enderbyf4586032016-07-29 17:44:13 +0000695 Expected<StringRef> NameOrErr = C->getRawName();
696 if (!NameOrErr) {
697 Err = NameOrErr.takeError();
698 return;
699 }
700 Name = NameOrErr.get();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000701 }
702
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000703 if (Name == "//") {
Jake Ehrlich1b30d632017-09-20 18:23:01 +0000704 Format = has64SymTable ? K_GNU64 : K_GNU;
Kevin Enderby27e85bd2016-08-03 21:57:47 +0000705 // The string table is never an external member, but we still
706 // must check any Expected<> return value.
707 Expected<StringRef> BufOrErr = C->getBuffer();
708 if (!BufOrErr) {
709 Err = BufOrErr.takeError();
710 return;
711 }
712 StringTable = BufOrErr.get();
Kevin Enderby7a969422015-11-05 19:24:56 +0000713 if (Increment())
714 return;
715 setFirstRegular(*C);
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000716 Err = Error::success();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000717 return;
718 }
719
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000720 if (Name[0] != '/') {
Jake Ehrlich1b30d632017-09-20 18:23:01 +0000721 Format = has64SymTable ? K_GNU64 : K_GNU;
Kevin Enderby7a969422015-11-05 19:24:56 +0000722 setFirstRegular(*C);
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000723 Err = Error::success();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000724 return;
725 }
726
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000727 if (Name != "/") {
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000728 Err = errorCodeToError(object_error::parse_failed);
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000729 return;
730 }
731
732 Format = K_COFF;
Kevin Enderby27e85bd2016-08-03 21:57:47 +0000733 // We know that the symbol table is not an external file, but we still
734 // must check any Expected<> return value.
735 Expected<StringRef> BufOrErr = C->getBuffer();
736 if (!BufOrErr) {
737 Err = BufOrErr.takeError();
738 return;
739 }
740 SymbolTable = BufOrErr.get();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000741
Kevin Enderby7a969422015-11-05 19:24:56 +0000742 if (Increment())
743 return;
744
745 if (I == E) {
746 setFirstRegular(*C);
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000747 Err = Error::success();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000748 return;
749 }
750
Kevin Enderbyf4586032016-07-29 17:44:13 +0000751 NameOrErr = C->getRawName();
752 if (!NameOrErr) {
753 Err = NameOrErr.takeError();
754 return;
755 }
756 Name = NameOrErr.get();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000757
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000758 if (Name == "//") {
Kevin Enderby27e85bd2016-08-03 21:57:47 +0000759 // The string table is never an external member, but we still
760 // must check any Expected<> return value.
761 Expected<StringRef> BufOrErr = C->getBuffer();
762 if (!BufOrErr) {
763 Err = BufOrErr.takeError();
764 return;
765 }
766 StringTable = BufOrErr.get();
Kevin Enderby7a969422015-11-05 19:24:56 +0000767 if (Increment())
768 return;
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000769 }
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000770
Kevin Enderby7a969422015-11-05 19:24:56 +0000771 setFirstRegular(*C);
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000772 Err = Error::success();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000773}
774
Lang Hamesfc209622016-07-14 02:24:01 +0000775Archive::child_iterator Archive::child_begin(Error &Err,
776 bool SkipInternal) const {
Rui Ueyama14a5ca02016-09-30 17:54:31 +0000777 if (isEmpty())
Rafael Espindola23a97502014-01-21 16:09:45 +0000778 return child_end();
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000779
780 if (SkipInternal)
Lang Hames3e040e02019-02-05 23:17:11 +0000781 return child_iterator::itr(
782 Child(this, FirstRegularData, FirstRegularStartOfFile), Err);
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000783
Rafael Espindola48af1c22014-08-19 18:44:46 +0000784 const char *Loc = Data.getBufferStart() + strlen(Magic);
Kevin Enderby6524bd82016-07-19 20:47:07 +0000785 Child C(this, Loc, &Err);
786 if (Err)
Lang Hamesfc209622016-07-14 02:24:01 +0000787 return child_end();
Lang Hames3e040e02019-02-05 23:17:11 +0000788 return child_iterator::itr(C, Err);
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000789}
790
Rafael Espindola23a97502014-01-21 16:09:45 +0000791Archive::child_iterator Archive::child_end() const {
Lang Hames3e040e02019-02-05 23:17:11 +0000792 return child_iterator::end(Child(nullptr, nullptr, nullptr));
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000793}
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000794
Rafael Espindolaae460022014-06-16 16:08:36 +0000795StringRef Archive::Symbol::getName() const {
Rafael Espindola2b054162015-07-14 01:06:16 +0000796 return Parent->getSymbolTable().begin() + StringIndex;
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000797}
798
Kevin Enderby27e85bd2016-08-03 21:57:47 +0000799Expected<Archive::Child> Archive::Symbol::getMember() const {
Rafael Espindola2b054162015-07-14 01:06:16 +0000800 const char *Buf = Parent->getSymbolTable().begin();
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000801 const char *Offsets = Buf;
Jake Ehrlich1b30d632017-09-20 18:23:01 +0000802 if (Parent->kind() == K_GNU64 || Parent->kind() == K_DARWIN64)
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000803 Offsets += sizeof(uint64_t);
804 else
805 Offsets += sizeof(uint32_t);
Jake Ehrlichde370412017-10-27 21:47:38 +0000806 uint64_t Offset = 0;
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000807 if (Parent->kind() == K_GNU) {
Rui Ueyama3206b792015-03-02 21:19:12 +0000808 Offset = read32be(Offsets + SymbolIndex * 4);
Jake Ehrlich1b30d632017-09-20 18:23:01 +0000809 } else if (Parent->kind() == K_GNU64) {
Rui Ueyama3206b792015-03-02 21:19:12 +0000810 Offset = read64be(Offsets + SymbolIndex * 8);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000811 } else if (Parent->kind() == K_BSD) {
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000812 // The SymbolIndex is an index into the ranlib structs that start at
813 // Offsets (the first uint32_t is the number of bytes of the ranlib
814 // structs). The ranlib structs are a pair of uint32_t's the first
815 // being a string table offset and the second being the offset into
816 // the archive of the member that defines the symbol. Which is what
817 // is needed here.
Rui Ueyama3206b792015-03-02 21:19:12 +0000818 Offset = read32le(Offsets + SymbolIndex * 8 + 4);
Kevin Enderbyae108ff2016-06-17 22:16:06 +0000819 } else if (Parent->kind() == K_DARWIN64) {
820 // The SymbolIndex is an index into the ranlib_64 structs that start at
821 // Offsets (the first uint64_t is the number of bytes of the ranlib_64
822 // structs). The ranlib_64 structs are a pair of uint64_t's the first
823 // being a string table offset and the second being the offset into
824 // the archive of the member that defines the symbol. Which is what
825 // is needed here.
826 Offset = read64le(Offsets + SymbolIndex * 16 + 8);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000827 } else {
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000828 // Skip offsets.
Rui Ueyama3206b792015-03-02 21:19:12 +0000829 uint32_t MemberCount = read32le(Buf);
830 Buf += MemberCount * 4 + 4;
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000831
Rui Ueyama3206b792015-03-02 21:19:12 +0000832 uint32_t SymbolCount = read32le(Buf);
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000833 if (SymbolIndex >= SymbolCount)
Kevin Enderby27e85bd2016-08-03 21:57:47 +0000834 return errorCodeToError(object_error::parse_failed);
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000835
Matt Beaumont-Gay68e0b6a2012-11-14 00:21:27 +0000836 // Skip SymbolCount to get to the indices table.
Rui Ueyama3206b792015-03-02 21:19:12 +0000837 const char *Indices = Buf + 4;
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000838
839 // Get the index of the offset in the file member offset table for this
840 // symbol.
Rui Ueyama3206b792015-03-02 21:19:12 +0000841 uint16_t OffsetIndex = read16le(Indices + SymbolIndex * 2);
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000842 // Subtract 1 since OffsetIndex is 1 based.
843 --OffsetIndex;
844
845 if (OffsetIndex >= MemberCount)
Kevin Enderby27e85bd2016-08-03 21:57:47 +0000846 return errorCodeToError(object_error::parse_failed);
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000847
Rui Ueyama3206b792015-03-02 21:19:12 +0000848 Offset = read32le(Offsets + OffsetIndex * 4);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000849 }
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000850
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000851 const char *Loc = Parent->getData().begin() + Offset;
Mehdi Amini41af4302016-11-11 04:28:40 +0000852 Error Err = Error::success();
Kevin Enderby6524bd82016-07-19 20:47:07 +0000853 Child C(Parent, Loc, &Err);
854 if (Err)
Kevin Enderby27e85bd2016-08-03 21:57:47 +0000855 return std::move(Err);
Kevin Enderby7a969422015-11-05 19:24:56 +0000856 return C;
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000857}
858
859Archive::Symbol Archive::Symbol::getNext() const {
860 Symbol t(*this);
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000861 if (Parent->kind() == K_BSD) {
862 // t.StringIndex is an offset from the start of the __.SYMDEF or
863 // "__.SYMDEF SORTED" member into the string table for the ranlib
864 // struct indexed by t.SymbolIndex . To change t.StringIndex to the
865 // offset in the string table for t.SymbolIndex+1 we subtract the
866 // its offset from the start of the string table for t.SymbolIndex
867 // and add the offset of the string table for t.SymbolIndex+1.
868
869 // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t
870 // which is the number of bytes of ranlib structs that follow. The ranlib
871 // structs are a pair of uint32_t's the first being a string table offset
872 // and the second being the offset into the archive of the member that
873 // define the symbol. After that the next uint32_t is the byte count of
874 // the string table followed by the string table.
Rafael Espindola2b054162015-07-14 01:06:16 +0000875 const char *Buf = Parent->getSymbolTable().begin();
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000876 uint32_t RanlibCount = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000877 RanlibCount = read32le(Buf) / 8;
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000878 // If t.SymbolIndex + 1 will be past the count of symbols (the RanlibCount)
879 // don't change the t.StringIndex as we don't want to reference a ranlib
880 // past RanlibCount.
881 if (t.SymbolIndex + 1 < RanlibCount) {
882 const char *Ranlibs = Buf + 4;
883 uint32_t CurRanStrx = 0;
884 uint32_t NextRanStrx = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000885 CurRanStrx = read32le(Ranlibs + t.SymbolIndex * 8);
886 NextRanStrx = read32le(Ranlibs + (t.SymbolIndex + 1) * 8);
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000887 t.StringIndex -= CurRanStrx;
888 t.StringIndex += NextRanStrx;
889 }
890 } else {
891 // Go to one past next null.
Rafael Espindola2b054162015-07-14 01:06:16 +0000892 t.StringIndex = Parent->getSymbolTable().find('\0', t.StringIndex) + 1;
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000893 }
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000894 ++t.SymbolIndex;
895 return t;
896}
897
Rafael Espindola23a97502014-01-21 16:09:45 +0000898Archive::symbol_iterator Archive::symbol_begin() const {
Rafael Espindolab6b5f52e2013-07-29 12:40:31 +0000899 if (!hasSymbolTable())
Rafael Espindolafbcafc02013-07-10 20:14:22 +0000900 return symbol_iterator(Symbol(this, 0, 0));
901
Rafael Espindola2b054162015-07-14 01:06:16 +0000902 const char *buf = getSymbolTable().begin();
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000903 if (kind() == K_GNU) {
904 uint32_t symbol_count = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000905 symbol_count = read32be(buf);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000906 buf += sizeof(uint32_t) + (symbol_count * (sizeof(uint32_t)));
Jake Ehrlich1b30d632017-09-20 18:23:01 +0000907 } else if (kind() == K_GNU64) {
Rui Ueyama3206b792015-03-02 21:19:12 +0000908 uint64_t symbol_count = read64be(buf);
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000909 buf += sizeof(uint64_t) + (symbol_count * (sizeof(uint64_t)));
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000910 } else if (kind() == K_BSD) {
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000911 // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t
912 // which is the number of bytes of ranlib structs that follow. The ranlib
913 // structs are a pair of uint32_t's the first being a string table offset
914 // and the second being the offset into the archive of the member that
915 // define the symbol. After that the next uint32_t is the byte count of
916 // the string table followed by the string table.
917 uint32_t ranlib_count = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000918 ranlib_count = read32le(buf) / 8;
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000919 const char *ranlibs = buf + 4;
920 uint32_t ran_strx = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000921 ran_strx = read32le(ranlibs);
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000922 buf += sizeof(uint32_t) + (ranlib_count * (2 * (sizeof(uint32_t))));
923 // Skip the byte count of the string table.
924 buf += sizeof(uint32_t);
925 buf += ran_strx;
Kevin Enderbyae108ff2016-06-17 22:16:06 +0000926 } else if (kind() == K_DARWIN64) {
927 // The __.SYMDEF_64 or "__.SYMDEF_64 SORTED" member starts with a uint64_t
928 // which is the number of bytes of ranlib_64 structs that follow. The
929 // ranlib_64 structs are a pair of uint64_t's the first being a string
930 // table offset and the second being the offset into the archive of the
931 // member that define the symbol. After that the next uint64_t is the byte
932 // count of the string table followed by the string table.
933 uint64_t ranlib_count = 0;
934 ranlib_count = read64le(buf) / 16;
935 const char *ranlibs = buf + 8;
936 uint64_t ran_strx = 0;
937 ran_strx = read64le(ranlibs);
938 buf += sizeof(uint64_t) + (ranlib_count * (2 * (sizeof(uint64_t))));
939 // Skip the byte count of the string table.
940 buf += sizeof(uint64_t);
941 buf += ran_strx;
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000942 } else {
943 uint32_t member_count = 0;
944 uint32_t symbol_count = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000945 member_count = read32le(buf);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000946 buf += 4 + (member_count * 4); // Skip offsets.
Rui Ueyama3206b792015-03-02 21:19:12 +0000947 symbol_count = read32le(buf);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000948 buf += 4 + (symbol_count * 2); // Skip indices.
949 }
Rafael Espindola2b054162015-07-14 01:06:16 +0000950 uint32_t string_start_offset = buf - getSymbolTable().begin();
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000951 return symbol_iterator(Symbol(this, 0, string_start_offset));
952}
953
Rafael Espindola23a97502014-01-21 16:09:45 +0000954Archive::symbol_iterator Archive::symbol_end() const {
Rui Ueyama407e0972015-05-26 16:20:40 +0000955 return symbol_iterator(Symbol(this, getNumberOfSymbols(), 0));
956}
Rafael Espindolafbcafc02013-07-10 20:14:22 +0000957
Rui Ueyama407e0972015-05-26 16:20:40 +0000958uint32_t Archive::getNumberOfSymbols() const {
Rafael Espindola483ad202015-10-08 18:06:20 +0000959 if (!hasSymbolTable())
960 return 0;
Rafael Espindola2b054162015-07-14 01:06:16 +0000961 const char *buf = getSymbolTable().begin();
Rui Ueyama407e0972015-05-26 16:20:40 +0000962 if (kind() == K_GNU)
963 return read32be(buf);
Jake Ehrlich1b30d632017-09-20 18:23:01 +0000964 if (kind() == K_GNU64)
Rui Ueyama407e0972015-05-26 16:20:40 +0000965 return read64be(buf);
966 if (kind() == K_BSD)
967 return read32le(buf) / 8;
Kevin Enderbyae108ff2016-06-17 22:16:06 +0000968 if (kind() == K_DARWIN64)
969 return read64le(buf) / 16;
Rui Ueyama407e0972015-05-26 16:20:40 +0000970 uint32_t member_count = 0;
971 member_count = read32le(buf);
972 buf += 4 + (member_count * 4); // Skip offsets.
973 return read32le(buf);
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000974}
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000975
Lang Hames69f49022016-07-14 20:44:27 +0000976Expected<Optional<Archive::Child>> Archive::findSym(StringRef name) const {
Rafael Espindola23a97502014-01-21 16:09:45 +0000977 Archive::symbol_iterator bs = symbol_begin();
978 Archive::symbol_iterator es = symbol_end();
Rafael Espindolaae460022014-06-16 16:08:36 +0000979
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000980 for (; bs != es; ++bs) {
Rafael Espindolaae460022014-06-16 16:08:36 +0000981 StringRef SymName = bs->getName();
982 if (SymName == name) {
Lang Hames69f49022016-07-14 20:44:27 +0000983 if (auto MemberOrErr = bs->getMember())
984 return Child(*MemberOrErr);
985 else
Kevin Enderby27e85bd2016-08-03 21:57:47 +0000986 return MemberOrErr.takeError();
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000987 }
988 }
Lang Hames69f49022016-07-14 20:44:27 +0000989 return Optional<Child>();
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000990}
Rafael Espindolab6b5f52e2013-07-29 12:40:31 +0000991
Rui Ueyama14a5ca02016-09-30 17:54:31 +0000992// Returns true if archive file contains no member file.
993bool Archive::isEmpty() const { return Data.getBufferSize() == 8; }
994
Rafael Espindola4a782fb2015-10-31 21:03:29 +0000995bool Archive::hasSymbolTable() const { return !SymbolTable.empty(); }