blob: 586f9c1da81cbaa190a23f8c8460d008dbd7585f [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 Kumarbfb60722016-08-03 18:44:32 +0000224sys::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 Kumarbfb60722016-08-03 18:44:32 +0000227 sizeof(ArMemHdr->AccessMode)).rtrim(' ').getAsInteger(8, Ret))
228 llvm_unreachable("Access mode is not an octal number.");
Rafael Espindola8115e1d2013-07-09 12:49:24 +0000229 return static_cast<sys::fs::perms>(Ret);
230}
231
Vedant Kumarbfb60722016-08-03 18:44:32 +0000232sys::TimeValue ArchiveMemberHeader::getLastModified() const {
Rafael Espindola8115e1d2013-07-09 12:49:24 +0000233 unsigned Seconds;
Kevin Enderby95b08422016-07-25 20:36:36 +0000234 if (StringRef(ArMemHdr->LastModified,
235 sizeof(ArMemHdr->LastModified)).rtrim(' ')
Vedant Kumarbfb60722016-08-03 18:44:32 +0000236 .getAsInteger(10, Seconds))
237 llvm_unreachable("Last modified time not a decimal number.");
Rafael Espindola8115e1d2013-07-09 12:49:24 +0000238
239 sys::TimeValue Ret;
240 Ret.fromEpochTime(Seconds);
241 return Ret;
242}
243
Vedant Kumarbfb60722016-08-03 18:44:32 +0000244unsigned ArchiveMemberHeader::getUID() const {
Rafael Espindola8115e1d2013-07-09 12:49:24 +0000245 unsigned Ret;
Kevin Enderby95b08422016-07-25 20:36:36 +0000246 StringRef User = StringRef(ArMemHdr->UID, sizeof(ArMemHdr->UID)).rtrim(' ');
Saleem Abdulrasoolaecbdf72016-07-05 00:23:05 +0000247 if (User.empty())
248 return 0;
Vedant Kumarbfb60722016-08-03 18:44:32 +0000249 if (User.getAsInteger(10, Ret))
250 llvm_unreachable("UID time not a decimal number.");
Rafael Espindola8115e1d2013-07-09 12:49:24 +0000251 return Ret;
252}
253
Vedant Kumarbfb60722016-08-03 18:44:32 +0000254unsigned ArchiveMemberHeader::getGID() const {
Rafael Espindola8115e1d2013-07-09 12:49:24 +0000255 unsigned Ret;
Kevin Enderby95b08422016-07-25 20:36:36 +0000256 StringRef Group = StringRef(ArMemHdr->GID, sizeof(ArMemHdr->GID)).rtrim(' ');
Saleem Abdulrasoolaecbdf72016-07-05 00:23:05 +0000257 if (Group.empty())
258 return 0;
Vedant Kumarbfb60722016-08-03 18:44:32 +0000259 if (Group.getAsInteger(10, Ret))
260 llvm_unreachable("GID time not a decimal number.");
Rafael Espindola8115e1d2013-07-09 12:49:24 +0000261 return Ret;
262}
263
Rafael Espindola43358762015-10-31 21:44:42 +0000264Archive::Child::Child(const Archive *Parent, StringRef Data,
265 uint16_t StartOfFile)
Kevin Enderby95b08422016-07-25 20:36:36 +0000266 : Parent(Parent), Header(Parent, Data.data(), Data.size(), nullptr),
267 Data(Data), StartOfFile(StartOfFile) {
268}
Rafael Espindola43358762015-10-31 21:44:42 +0000269
Kevin Enderby6524bd82016-07-19 20:47:07 +0000270Archive::Child::Child(const Archive *Parent, const char *Start, Error *Err)
Kevin Enderby95b08422016-07-25 20:36:36 +0000271 : Parent(Parent), Header(Parent, Start, Parent->getData().size() -
272 (Start - Parent->getData().data()), Err) {
Rafael Espindola0f3de642013-07-09 05:26:25 +0000273 if (!Start)
Rafael Espindola747bc072013-07-09 03:39:35 +0000274 return;
Lang Hames5e51a2e2016-07-22 16:11:25 +0000275 ErrorAsOutParameter ErrAsOutParam(Err);
Rafael Espindola0f3de642013-07-09 05:26:25 +0000276
Kevin Enderby95b08422016-07-25 20:36:36 +0000277 // If there was an error in the construction of the Header and we were passed
278 // Err that is not nullptr then just return with the error now set.
279 if (Err && *Err)
280 return;
281
282 uint64_t Size = Header.getSizeOf();
Rafael Espindola9d102062014-12-16 01:43:41 +0000283 Data = StringRef(Start, Size);
Kevin Enderbyf4586032016-07-29 17:44:13 +0000284 Expected<bool> isThinOrErr = isThinMember();
285 if (!isThinOrErr) {
286 if (Err)
287 *Err = isThinOrErr.takeError();
288 return;
289 }
290 bool isThin = isThinOrErr.get();
291 if (!isThin) {
Kevin Enderby6524bd82016-07-19 20:47:07 +0000292 Expected<uint64_t> MemberSize = getRawSize();
293 if (!MemberSize) {
Lang Hames5e51a2e2016-07-22 16:11:25 +0000294 if (Err)
Kevin Enderby6524bd82016-07-19 20:47:07 +0000295 *Err = MemberSize.takeError();
Kevin Enderby7a969422015-11-05 19:24:56 +0000296 return;
Kevin Enderby6524bd82016-07-19 20:47:07 +0000297 }
Kevin Enderby7a969422015-11-05 19:24:56 +0000298 Size += MemberSize.get();
Rafael Espindolabe9ab262015-07-22 19:34:26 +0000299 Data = StringRef(Start, Size);
300 }
Rafael Espindola0f3de642013-07-09 05:26:25 +0000301
Rafael Espindola747bc072013-07-09 03:39:35 +0000302 // Setup StartOfFile and PaddingBytes.
Kevin Enderby95b08422016-07-25 20:36:36 +0000303 StartOfFile = Header.getSizeOf();
Rafael Espindola747bc072013-07-09 03:39:35 +0000304 // Don't include attached name.
Kevin Enderbyf4586032016-07-29 17:44:13 +0000305 Expected<StringRef> NameOrErr = getRawName();
306 if (!NameOrErr){
307 if (Err)
308 *Err = NameOrErr.takeError();
309 return;
310 }
311 StringRef Name = NameOrErr.get();
Rafael Espindola747bc072013-07-09 03:39:35 +0000312 if (Name.startswith("#1/")) {
313 uint64_t NameSize;
Kevin Enderbyf4586032016-07-29 17:44:13 +0000314 if (Name.substr(3).rtrim(' ').getAsInteger(10, NameSize)) {
315 if (Err) {
316 std::string Buf;
317 raw_string_ostream OS(Buf);
Kevin Enderby31b07f12016-07-29 22:32:02 +0000318 OS.write_escaped(Name.substr(3).rtrim(' '));
Kevin Enderbyf4586032016-07-29 17:44:13 +0000319 OS.flush();
320 uint64_t Offset = Start - Parent->getData().data();
321 *Err = malformedError("long name length characters after the #1/ are "
322 "not all decimal numbers: '" + Buf + "' for "
323 "archive member header at offset " +
324 Twine(Offset));
325 return;
326 }
327 }
Rafael Espindola747bc072013-07-09 03:39:35 +0000328 StartOfFile += NameSize;
329 }
330}
331
Kevin Enderby6524bd82016-07-19 20:47:07 +0000332Expected<uint64_t> Archive::Child::getSize() const {
Kevin Enderby1c1add42015-10-13 20:48:04 +0000333 if (Parent->IsThin) {
Kevin Enderby95b08422016-07-25 20:36:36 +0000334 Expected<uint32_t> Size = Header.getSize();
Kevin Enderby6524bd82016-07-19 20:47:07 +0000335 if (!Size)
336 return Size.takeError();
Kevin Enderby1c1add42015-10-13 20:48:04 +0000337 return Size.get();
338 }
Rafael Espindola9d102062014-12-16 01:43:41 +0000339 return Data.size() - StartOfFile;
340}
341
Kevin Enderby6524bd82016-07-19 20:47:07 +0000342Expected<uint64_t> Archive::Child::getRawSize() const {
Kevin Enderby95b08422016-07-25 20:36:36 +0000343 return Header.getSize();
Kevin Enderby13023a12015-01-15 23:19:11 +0000344}
345
Kevin Enderbyf4586032016-07-29 17:44:13 +0000346Expected<bool> Archive::Child::isThinMember() const {
347 Expected<StringRef> NameOrErr = Header.getRawName();
348 if (!NameOrErr)
349 return NameOrErr.takeError();
350 StringRef Name = NameOrErr.get();
Rafael Espindolabe9ab262015-07-22 19:34:26 +0000351 return Parent->IsThin && Name != "/" && Name != "//";
352}
353
Rafael Espindola694210c2016-05-02 13:45:06 +0000354ErrorOr<std::string> Archive::Child::getFullName() const {
Kevin Enderbyf4586032016-07-29 17:44:13 +0000355 Expected<bool> isThin = isThinMember();
356 if (!isThin)
357 return errorToErrorCode(isThin.takeError());
358 assert(isThin.get());
359 Expected<StringRef> NameOrErr = getName();
360 if (!NameOrErr)
361 return errorToErrorCode(NameOrErr.takeError());
Rafael Espindola694210c2016-05-02 13:45:06 +0000362 StringRef Name = *NameOrErr;
363 if (sys::path::is_absolute(Name))
364 return Name;
365
366 SmallString<128> FullName = sys::path::parent_path(
367 Parent->getMemoryBufferRef().getBufferIdentifier());
368 sys::path::append(FullName, Name);
369 return StringRef(FullName);
370}
371
Rafael Espindola4b83cb52015-07-14 22:18:43 +0000372ErrorOr<StringRef> Archive::Child::getBuffer() const {
Kevin Enderbyf4586032016-07-29 17:44:13 +0000373 Expected<bool> isThinOrErr = isThinMember();
374 if (!isThinOrErr)
375 return errorToErrorCode(isThinOrErr.takeError());
376 bool isThin = isThinOrErr.get();
377 if (!isThin) {
Kevin Enderby6524bd82016-07-19 20:47:07 +0000378 Expected<uint32_t> Size = getSize();
379 if (!Size)
380 return errorToErrorCode(Size.takeError());
Kevin Enderby7a969422015-11-05 19:24:56 +0000381 return StringRef(Data.data() + StartOfFile, Size.get());
382 }
Rafael Espindola694210c2016-05-02 13:45:06 +0000383 ErrorOr<std::string> FullNameOrEr = getFullName();
384 if (std::error_code EC = FullNameOrEr.getError())
Rafael Espindola4b83cb52015-07-14 22:18:43 +0000385 return EC;
Rafael Espindola694210c2016-05-02 13:45:06 +0000386 const std::string &FullName = *FullNameOrEr;
Rafael Espindola4b83cb52015-07-14 22:18:43 +0000387 ErrorOr<std::unique_ptr<MemoryBuffer>> Buf = MemoryBuffer::getFile(FullName);
388 if (std::error_code EC = Buf.getError())
389 return EC;
390 Parent->ThinBuffers.push_back(std::move(*Buf));
391 return Parent->ThinBuffers.back()->getBuffer();
392}
393
Kevin Enderby6524bd82016-07-19 20:47:07 +0000394Expected<Archive::Child> Archive::Child::getNext() const {
Rafael Espindola747bc072013-07-09 03:39:35 +0000395 size_t SpaceToSkip = Data.size();
396 // If it's odd, add 1 to make it even.
397 if (SpaceToSkip & 1)
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000398 ++SpaceToSkip;
Rafael Espindola747bc072013-07-09 03:39:35 +0000399
Kevin Enderbyda9dd052015-10-21 17:13:20 +0000400 const char *NextLoc = Data.data() + SpaceToSkip;
Rafael Espindola747bc072013-07-09 03:39:35 +0000401
Kevin Enderby7a969422015-11-05 19:24:56 +0000402 // Check to see if this is at the end of the archive.
403 if (NextLoc == Parent->Data.getBufferEnd())
404 return Child(Parent, nullptr, nullptr);
Rafael Espindola747bc072013-07-09 03:39:35 +0000405
Kevin Enderby7a969422015-11-05 19:24:56 +0000406 // Check to see if this is past the end of the archive.
Kevin Enderby95b08422016-07-25 20:36:36 +0000407 if (NextLoc > Parent->Data.getBufferEnd()) {
Kevin Enderby31b07f12016-07-29 22:32:02 +0000408 std::string Msg("offset to next archive member past the end of the archive "
409 "after member ");
Kevin Enderbyf4586032016-07-29 17:44:13 +0000410 Expected<StringRef> NameOrErr = getName();
411 if (!NameOrErr) {
412 consumeError(NameOrErr.takeError());
Kevin Enderby95b08422016-07-25 20:36:36 +0000413 uint64_t Offset = Data.data() - Parent->getData().data();
414 return malformedError(Msg + "at offset " + Twine(Offset));
415 } else
Kevin Enderby31b07f12016-07-29 22:32:02 +0000416 return malformedError(Msg + NameOrErr.get());
Kevin Enderby95b08422016-07-25 20:36:36 +0000417 }
Kevin Enderby7a969422015-11-05 19:24:56 +0000418
Kevin Enderby6524bd82016-07-19 20:47:07 +0000419 Error Err;
420 Child Ret(Parent, NextLoc, &Err);
421 if (Err)
422 return std::move(Err);
Kevin Enderby7a969422015-11-05 19:24:56 +0000423 return Ret;
Rafael Espindola747bc072013-07-09 03:39:35 +0000424}
425
Kevin Enderby13023a12015-01-15 23:19:11 +0000426uint64_t Archive::Child::getChildOffset() const {
427 const char *a = Parent->Data.getBuffer().data();
428 const char *c = Data.data();
429 uint64_t offset = c - a;
430 return offset;
431}
432
Kevin Enderbyf4586032016-07-29 17:44:13 +0000433Expected<StringRef> Archive::Child::getName() const {
434 Expected<uint64_t> RawSizeOrErr = getRawSize();
435 if (!RawSizeOrErr)
436 return RawSizeOrErr.takeError();
437 uint64_t RawSize = RawSizeOrErr.get();
438 Expected<StringRef> NameOrErr = Header.getName(Header.getSizeOf() + RawSize);
439 if (!NameOrErr)
440 return NameOrErr.takeError();
441 StringRef Name = NameOrErr.get();
442 return Name;
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000443}
444
Kevin Enderbyf4586032016-07-29 17:44:13 +0000445Expected<MemoryBufferRef> Archive::Child::getMemoryBufferRef() const {
446 Expected<StringRef> NameOrErr = getName();
447 if (!NameOrErr)
448 return NameOrErr.takeError();
Rafael Espindolaae460022014-06-16 16:08:36 +0000449 StringRef Name = NameOrErr.get();
Rafael Espindola4b83cb52015-07-14 22:18:43 +0000450 ErrorOr<StringRef> Buf = getBuffer();
451 if (std::error_code EC = Buf.getError())
Kevin Enderbyf4586032016-07-29 17:44:13 +0000452 return errorCodeToError(EC);
Rafael Espindola4b83cb52015-07-14 22:18:43 +0000453 return MemoryBufferRef(*Buf, Name);
Rafael Espindolaae460022014-06-16 16:08:36 +0000454}
455
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000456Expected<std::unique_ptr<Binary>>
Rafael Espindolaae460022014-06-16 16:08:36 +0000457Archive::Child::getAsBinary(LLVMContext *Context) const {
Kevin Enderbyf4586032016-07-29 17:44:13 +0000458 Expected<MemoryBufferRef> BuffOrErr = getMemoryBufferRef();
459 if (!BuffOrErr)
460 return BuffOrErr.takeError();
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +0000461
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +0000462 auto BinaryOrErr = createBinary(BuffOrErr.get(), Context);
463 if (BinaryOrErr)
464 return std::move(*BinaryOrErr);
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000465 return BinaryOrErr.takeError();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000466}
467
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000468Expected<std::unique_ptr<Archive>> Archive::create(MemoryBufferRef Source) {
469 Error Err;
470 std::unique_ptr<Archive> Ret(new Archive(Source, Err));
471 if (Err)
472 return std::move(Err);
Rafael Espindolaf5577132014-07-31 03:36:00 +0000473 return std::move(Ret);
Rafael Espindola692410e2014-01-21 23:06:54 +0000474}
475
Rafael Espindola43358762015-10-31 21:44:42 +0000476void Archive::setFirstRegular(const Child &C) {
477 FirstRegularData = C.Data;
478 FirstRegularStartOfFile = C.StartOfFile;
479}
480
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000481Archive::Archive(MemoryBufferRef Source, Error &Err)
Rafael Espindola43358762015-10-31 21:44:42 +0000482 : Binary(Binary::ID_Archive, Source) {
Lang Hames5e51a2e2016-07-22 16:11:25 +0000483 ErrorAsOutParameter ErrAsOutParam(&Err);
Rafael Espindola9d102062014-12-16 01:43:41 +0000484 StringRef Buffer = Data.getBuffer();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000485 // Check for sufficient magic.
Rafael Espindola9d102062014-12-16 01:43:41 +0000486 if (Buffer.startswith(ThinMagic)) {
487 IsThin = true;
488 } else if (Buffer.startswith(Magic)) {
489 IsThin = false;
490 } else {
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000491 Err = make_error<GenericBinaryError>("File too small to be an archive",
492 object_error::invalid_file_type);
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000493 return;
494 }
495
Kevin Enderbyf4586032016-07-29 17:44:13 +0000496 // Make sure Format is initialized before any call to
497 // ArchiveMemberHeader::getName() is made. This could be a valid empty
498 // archive which is the same in all formats. So claiming it to be gnu to is
499 // fine if not totally correct before we look for a string table or table of
500 // contents.
501 Format = K_GNU;
502
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000503 // Get the special members.
Lang Hamesfc209622016-07-14 02:24:01 +0000504 child_iterator I = child_begin(Err, false);
505 if (Err)
Kevin Enderby7a969422015-11-05 19:24:56 +0000506 return;
507 child_iterator E = child_end();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000508
Kevin Enderbyf4586032016-07-29 17:44:13 +0000509 // See if this is a valid empty archive and if so return.
Kevin Enderby7a969422015-11-05 19:24:56 +0000510 if (I == E) {
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000511 Err = Error::success();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000512 return;
513 }
Lang Hamesfc209622016-07-14 02:24:01 +0000514 const Child *C = &*I;
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000515
Kevin Enderby7a969422015-11-05 19:24:56 +0000516 auto Increment = [&]() {
517 ++I;
Lang Hamesfc209622016-07-14 02:24:01 +0000518 if (Err)
Kevin Enderby7a969422015-11-05 19:24:56 +0000519 return true;
Lang Hamesfc209622016-07-14 02:24:01 +0000520 C = &*I;
Kevin Enderby7a969422015-11-05 19:24:56 +0000521 return false;
522 };
523
Kevin Enderbyf4586032016-07-29 17:44:13 +0000524 Expected<StringRef> NameOrErr = C->getRawName();
525 if (!NameOrErr) {
526 Err = NameOrErr.takeError();
527 return;
528 }
529 StringRef Name = NameOrErr.get();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000530
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000531 // Below is the pattern that is used to figure out the archive format
532 // GNU archive format
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000533 // First member : / (may exist, if it exists, points to the symbol table )
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000534 // Second member : // (may exist, if it exists, points to the string table)
535 // Note : The string table is used if the filename exceeds 15 characters
536 // BSD archive format
Rafael Espindola55509922013-07-10 22:07:59 +0000537 // First member : __.SYMDEF or "__.SYMDEF SORTED" (the symbol table)
538 // There is no string table, if the filename exceeds 15 characters or has a
539 // embedded space, the filename has #1/<size>, The size represents the size
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000540 // of the filename that needs to be read after the archive header
541 // COFF archive format
542 // First member : /
543 // Second member : / (provides a directory of symbols)
Rui Ueyamaf4d0a8c2013-06-03 00:27:03 +0000544 // Third member : // (may exist, if it exists, contains the string table)
545 // Note: Microsoft PE/COFF Spec 8.3 says that the third member is present
546 // even if the string table is empty. However, lib.exe does not in fact
547 // seem to create the third member if there's no member whose filename
548 // exceeds 15 characters. So the third member is optional.
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000549
Kevin Enderbyae108ff2016-06-17 22:16:06 +0000550 if (Name == "__.SYMDEF" || Name == "__.SYMDEF_64") {
551 if (Name == "__.SYMDEF")
552 Format = K_BSD;
553 else // Name == "__.SYMDEF_64"
554 Format = K_DARWIN64;
Rafael Espindola4a782fb2015-10-31 21:03:29 +0000555 // We know that the symbol table is not an external file, so we just assert
556 // there is no error.
Kevin Enderby7a969422015-11-05 19:24:56 +0000557 SymbolTable = *C->getBuffer();
558 if (Increment())
559 return;
560 setFirstRegular(*C);
561
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000562 Err = Error::success();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000563 return;
564 }
565
Rafael Espindola55509922013-07-10 22:07:59 +0000566 if (Name.startswith("#1/")) {
567 Format = K_BSD;
568 // We know this is BSD, so getName will work since there is no string table.
Kevin Enderbyf4586032016-07-29 17:44:13 +0000569 Expected<StringRef> NameOrErr = C->getName();
570 if (!NameOrErr) {
571 Err = NameOrErr.takeError();
Rafael Espindola55509922013-07-10 22:07:59 +0000572 return;
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000573 }
Rafael Espindolaae460022014-06-16 16:08:36 +0000574 Name = NameOrErr.get();
Nick Kledzikf44dbda2014-11-12 01:37:45 +0000575 if (Name == "__.SYMDEF SORTED" || Name == "__.SYMDEF") {
Rafael Espindola4a782fb2015-10-31 21:03:29 +0000576 // We know that the symbol table is not an external file, so we just
577 // assert there is no error.
Kevin Enderby7a969422015-11-05 19:24:56 +0000578 SymbolTable = *C->getBuffer();
579 if (Increment())
580 return;
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000581 }
Kevin Enderbyae108ff2016-06-17 22:16:06 +0000582 else if (Name == "__.SYMDEF_64 SORTED" || Name == "__.SYMDEF_64") {
583 Format = K_DARWIN64;
584 // We know that the symbol table is not an external file, so we just
585 // assert there is no error.
586 SymbolTable = *C->getBuffer();
587 if (Increment())
588 return;
589 }
Kevin Enderby7a969422015-11-05 19:24:56 +0000590 setFirstRegular(*C);
Rafael Espindola55509922013-07-10 22:07:59 +0000591 return;
592 }
593
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000594 // MIPS 64-bit ELF archives use a special format of a symbol table.
595 // This format is marked by `ar_name` field equals to "/SYM64/".
596 // For detailed description see page 96 in the following document:
597 // http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf
598
599 bool has64SymTable = false;
600 if (Name == "/" || Name == "/SYM64/") {
Rafael Espindola4a782fb2015-10-31 21:03:29 +0000601 // We know that the symbol table is not an external file, so we just assert
602 // there is no error.
Kevin Enderby7a969422015-11-05 19:24:56 +0000603 SymbolTable = *C->getBuffer();
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000604 if (Name == "/SYM64/")
605 has64SymTable = true;
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000606
Kevin Enderby7a969422015-11-05 19:24:56 +0000607 if (Increment())
608 return;
609 if (I == E) {
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000610 Err = Error::success();
Michael J. Spencer04614ff2013-01-10 00:07:38 +0000611 return;
612 }
Kevin Enderbyf4586032016-07-29 17:44:13 +0000613 Expected<StringRef> NameOrErr = C->getRawName();
614 if (!NameOrErr) {
615 Err = NameOrErr.takeError();
616 return;
617 }
618 Name = NameOrErr.get();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000619 }
620
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000621 if (Name == "//") {
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000622 Format = has64SymTable ? K_MIPS64 : K_GNU;
Rafael Espindola8f238822015-10-31 20:06:13 +0000623 // The string table is never an external member, so we just assert on the
624 // ErrorOr.
Kevin Enderby7a969422015-11-05 19:24:56 +0000625 StringTable = *C->getBuffer();
626 if (Increment())
627 return;
628 setFirstRegular(*C);
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000629 Err = Error::success();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000630 return;
631 }
632
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000633 if (Name[0] != '/') {
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000634 Format = has64SymTable ? K_MIPS64 : K_GNU;
Kevin Enderby7a969422015-11-05 19:24:56 +0000635 setFirstRegular(*C);
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000636 Err = Error::success();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000637 return;
638 }
639
Rafael Espindola6cc2dc72013-07-05 03:35:15 +0000640 if (Name != "/") {
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000641 Err = errorCodeToError(object_error::parse_failed);
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000642 return;
643 }
644
645 Format = K_COFF;
Rafael Espindola4a782fb2015-10-31 21:03:29 +0000646 // We know that the symbol table is not an external file, so we just assert
647 // there is no error.
Kevin Enderby7a969422015-11-05 19:24:56 +0000648 SymbolTable = *C->getBuffer();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000649
Kevin Enderby7a969422015-11-05 19:24:56 +0000650 if (Increment())
651 return;
652
653 if (I == E) {
654 setFirstRegular(*C);
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000655 Err = Error::success();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000656 return;
657 }
658
Kevin Enderbyf4586032016-07-29 17:44:13 +0000659 NameOrErr = C->getRawName();
660 if (!NameOrErr) {
661 Err = NameOrErr.takeError();
662 return;
663 }
664 Name = NameOrErr.get();
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000665
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000666 if (Name == "//") {
Rafael Espindola8f238822015-10-31 20:06:13 +0000667 // The string table is never an external member, so we just assert on the
668 // ErrorOr.
Kevin Enderby7a969422015-11-05 19:24:56 +0000669 StringTable = *C->getBuffer();
670 if (Increment())
671 return;
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000672 }
Rafael Espindola88ae7dd2013-07-03 15:57:14 +0000673
Kevin Enderby7a969422015-11-05 19:24:56 +0000674 setFirstRegular(*C);
Kevin Enderbyc60a3212016-06-29 20:35:44 +0000675 Err = Error::success();
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000676}
677
Lang Hamesfc209622016-07-14 02:24:01 +0000678Archive::child_iterator Archive::child_begin(Error &Err,
679 bool SkipInternal) const {
Rafael Espindola48af1c22014-08-19 18:44:46 +0000680 if (Data.getBufferSize() == 8) // empty archive.
Rafael Espindola23a97502014-01-21 16:09:45 +0000681 return child_end();
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000682
683 if (SkipInternal)
Lang Hamesfc209622016-07-14 02:24:01 +0000684 return child_iterator(Child(this, FirstRegularData,
685 FirstRegularStartOfFile),
686 &Err);
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000687
Rafael Espindola48af1c22014-08-19 18:44:46 +0000688 const char *Loc = Data.getBufferStart() + strlen(Magic);
Kevin Enderby6524bd82016-07-19 20:47:07 +0000689 Child C(this, Loc, &Err);
690 if (Err)
Lang Hamesfc209622016-07-14 02:24:01 +0000691 return child_end();
Lang Hamesfc209622016-07-14 02:24:01 +0000692 return child_iterator(C, &Err);
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000693}
694
Rafael Espindola23a97502014-01-21 16:09:45 +0000695Archive::child_iterator Archive::child_end() const {
Lang Hamesfc209622016-07-14 02:24:01 +0000696 return child_iterator(Child(this, nullptr, nullptr), nullptr);
Michael J. Spencerd3b7b122011-09-27 19:36:55 +0000697}
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000698
Rafael Espindolaae460022014-06-16 16:08:36 +0000699StringRef Archive::Symbol::getName() const {
Rafael Espindola2b054162015-07-14 01:06:16 +0000700 return Parent->getSymbolTable().begin() + StringIndex;
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000701}
702
Rafael Espindolacc86d822015-11-03 01:20:44 +0000703ErrorOr<Archive::Child> Archive::Symbol::getMember() const {
Rafael Espindola2b054162015-07-14 01:06:16 +0000704 const char *Buf = Parent->getSymbolTable().begin();
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000705 const char *Offsets = Buf;
Kevin Enderbyae108ff2016-06-17 22:16:06 +0000706 if (Parent->kind() == K_MIPS64 || Parent->kind() == K_DARWIN64)
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000707 Offsets += sizeof(uint64_t);
708 else
709 Offsets += sizeof(uint32_t);
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000710 uint32_t Offset = 0;
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000711 if (Parent->kind() == K_GNU) {
Rui Ueyama3206b792015-03-02 21:19:12 +0000712 Offset = read32be(Offsets + SymbolIndex * 4);
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000713 } else if (Parent->kind() == K_MIPS64) {
Rui Ueyama3206b792015-03-02 21:19:12 +0000714 Offset = read64be(Offsets + SymbolIndex * 8);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000715 } else if (Parent->kind() == K_BSD) {
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000716 // The SymbolIndex is an index into the ranlib structs that start at
717 // Offsets (the first uint32_t is the number of bytes of the ranlib
718 // structs). The ranlib structs are a pair of uint32_t's the first
719 // being a string table offset and the second being the offset into
720 // the archive of the member that defines the symbol. Which is what
721 // is needed here.
Rui Ueyama3206b792015-03-02 21:19:12 +0000722 Offset = read32le(Offsets + SymbolIndex * 8 + 4);
Kevin Enderbyae108ff2016-06-17 22:16:06 +0000723 } else if (Parent->kind() == K_DARWIN64) {
724 // The SymbolIndex is an index into the ranlib_64 structs that start at
725 // Offsets (the first uint64_t is the number of bytes of the ranlib_64
726 // structs). The ranlib_64 structs are a pair of uint64_t's the first
727 // being a string table offset and the second being the offset into
728 // the archive of the member that defines the symbol. Which is what
729 // is needed here.
730 Offset = read64le(Offsets + SymbolIndex * 16 + 8);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000731 } else {
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000732 // Skip offsets.
Rui Ueyama3206b792015-03-02 21:19:12 +0000733 uint32_t MemberCount = read32le(Buf);
734 Buf += MemberCount * 4 + 4;
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000735
Rui Ueyama3206b792015-03-02 21:19:12 +0000736 uint32_t SymbolCount = read32le(Buf);
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000737 if (SymbolIndex >= SymbolCount)
738 return object_error::parse_failed;
739
Matt Beaumont-Gay68e0b6a2012-11-14 00:21:27 +0000740 // Skip SymbolCount to get to the indices table.
Rui Ueyama3206b792015-03-02 21:19:12 +0000741 const char *Indices = Buf + 4;
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000742
743 // Get the index of the offset in the file member offset table for this
744 // symbol.
Rui Ueyama3206b792015-03-02 21:19:12 +0000745 uint16_t OffsetIndex = read16le(Indices + SymbolIndex * 2);
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000746 // Subtract 1 since OffsetIndex is 1 based.
747 --OffsetIndex;
748
749 if (OffsetIndex >= MemberCount)
750 return object_error::parse_failed;
751
Rui Ueyama3206b792015-03-02 21:19:12 +0000752 Offset = read32le(Offsets + OffsetIndex * 4);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000753 }
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000754
Michael J. Spencer4e92d5b2012-11-14 00:04:13 +0000755 const char *Loc = Parent->getData().begin() + Offset;
Kevin Enderby6524bd82016-07-19 20:47:07 +0000756 Error Err;
757 Child C(Parent, Loc, &Err);
758 if (Err)
759 return errorToErrorCode(std::move(Err));
Kevin Enderby7a969422015-11-05 19:24:56 +0000760 return C;
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000761}
762
763Archive::Symbol Archive::Symbol::getNext() const {
764 Symbol t(*this);
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000765 if (Parent->kind() == K_BSD) {
766 // t.StringIndex is an offset from the start of the __.SYMDEF or
767 // "__.SYMDEF SORTED" member into the string table for the ranlib
768 // struct indexed by t.SymbolIndex . To change t.StringIndex to the
769 // offset in the string table for t.SymbolIndex+1 we subtract the
770 // its offset from the start of the string table for t.SymbolIndex
771 // and add the offset of the string table for t.SymbolIndex+1.
772
773 // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t
774 // which is the number of bytes of ranlib structs that follow. The ranlib
775 // structs are a pair of uint32_t's the first being a string table offset
776 // and the second being the offset into the archive of the member that
777 // define the symbol. After that the next uint32_t is the byte count of
778 // the string table followed by the string table.
Rafael Espindola2b054162015-07-14 01:06:16 +0000779 const char *Buf = Parent->getSymbolTable().begin();
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000780 uint32_t RanlibCount = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000781 RanlibCount = read32le(Buf) / 8;
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000782 // If t.SymbolIndex + 1 will be past the count of symbols (the RanlibCount)
783 // don't change the t.StringIndex as we don't want to reference a ranlib
784 // past RanlibCount.
785 if (t.SymbolIndex + 1 < RanlibCount) {
786 const char *Ranlibs = Buf + 4;
787 uint32_t CurRanStrx = 0;
788 uint32_t NextRanStrx = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000789 CurRanStrx = read32le(Ranlibs + t.SymbolIndex * 8);
790 NextRanStrx = read32le(Ranlibs + (t.SymbolIndex + 1) * 8);
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000791 t.StringIndex -= CurRanStrx;
792 t.StringIndex += NextRanStrx;
793 }
794 } else {
795 // Go to one past next null.
Rafael Espindola2b054162015-07-14 01:06:16 +0000796 t.StringIndex = Parent->getSymbolTable().find('\0', t.StringIndex) + 1;
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000797 }
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000798 ++t.SymbolIndex;
799 return t;
800}
801
Rafael Espindola23a97502014-01-21 16:09:45 +0000802Archive::symbol_iterator Archive::symbol_begin() const {
Rafael Espindolab6b5f52e2013-07-29 12:40:31 +0000803 if (!hasSymbolTable())
Rafael Espindolafbcafc02013-07-10 20:14:22 +0000804 return symbol_iterator(Symbol(this, 0, 0));
805
Rafael Espindola2b054162015-07-14 01:06:16 +0000806 const char *buf = getSymbolTable().begin();
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000807 if (kind() == K_GNU) {
808 uint32_t symbol_count = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000809 symbol_count = read32be(buf);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000810 buf += sizeof(uint32_t) + (symbol_count * (sizeof(uint32_t)));
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000811 } else if (kind() == K_MIPS64) {
Rui Ueyama3206b792015-03-02 21:19:12 +0000812 uint64_t symbol_count = read64be(buf);
Simon Atanasyan1d902b72015-02-17 18:54:22 +0000813 buf += sizeof(uint64_t) + (symbol_count * (sizeof(uint64_t)));
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000814 } else if (kind() == K_BSD) {
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000815 // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t
816 // which is the number of bytes of ranlib structs that follow. The ranlib
817 // structs are a pair of uint32_t's the first being a string table offset
818 // and the second being the offset into the archive of the member that
819 // define the symbol. After that the next uint32_t is the byte count of
820 // the string table followed by the string table.
821 uint32_t ranlib_count = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000822 ranlib_count = read32le(buf) / 8;
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000823 const char *ranlibs = buf + 4;
824 uint32_t ran_strx = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000825 ran_strx = read32le(ranlibs);
Kevin Enderby8c50dbb2014-07-08 22:10:02 +0000826 buf += sizeof(uint32_t) + (ranlib_count * (2 * (sizeof(uint32_t))));
827 // Skip the byte count of the string table.
828 buf += sizeof(uint32_t);
829 buf += ran_strx;
Kevin Enderbyae108ff2016-06-17 22:16:06 +0000830 } else if (kind() == K_DARWIN64) {
831 // The __.SYMDEF_64 or "__.SYMDEF_64 SORTED" member starts with a uint64_t
832 // which is the number of bytes of ranlib_64 structs that follow. The
833 // ranlib_64 structs are a pair of uint64_t's the first being a string
834 // table offset and the second being the offset into the archive of the
835 // member that define the symbol. After that the next uint64_t is the byte
836 // count of the string table followed by the string table.
837 uint64_t ranlib_count = 0;
838 ranlib_count = read64le(buf) / 16;
839 const char *ranlibs = buf + 8;
840 uint64_t ran_strx = 0;
841 ran_strx = read64le(ranlibs);
842 buf += sizeof(uint64_t) + (ranlib_count * (2 * (sizeof(uint64_t))));
843 // Skip the byte count of the string table.
844 buf += sizeof(uint64_t);
845 buf += ran_strx;
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000846 } else {
847 uint32_t member_count = 0;
848 uint32_t symbol_count = 0;
Rui Ueyama3206b792015-03-02 21:19:12 +0000849 member_count = read32le(buf);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000850 buf += 4 + (member_count * 4); // Skip offsets.
Rui Ueyama3206b792015-03-02 21:19:12 +0000851 symbol_count = read32le(buf);
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000852 buf += 4 + (symbol_count * 2); // Skip indices.
853 }
Rafael Espindola2b054162015-07-14 01:06:16 +0000854 uint32_t string_start_offset = buf - getSymbolTable().begin();
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000855 return symbol_iterator(Symbol(this, 0, string_start_offset));
856}
857
Rafael Espindola23a97502014-01-21 16:09:45 +0000858Archive::symbol_iterator Archive::symbol_end() const {
Rui Ueyama407e0972015-05-26 16:20:40 +0000859 return symbol_iterator(Symbol(this, getNumberOfSymbols(), 0));
860}
Rafael Espindolafbcafc02013-07-10 20:14:22 +0000861
Rui Ueyama407e0972015-05-26 16:20:40 +0000862uint32_t Archive::getNumberOfSymbols() const {
Rafael Espindola483ad202015-10-08 18:06:20 +0000863 if (!hasSymbolTable())
864 return 0;
Rafael Espindola2b054162015-07-14 01:06:16 +0000865 const char *buf = getSymbolTable().begin();
Rui Ueyama407e0972015-05-26 16:20:40 +0000866 if (kind() == K_GNU)
867 return read32be(buf);
868 if (kind() == K_MIPS64)
869 return read64be(buf);
870 if (kind() == K_BSD)
871 return read32le(buf) / 8;
Kevin Enderbyae108ff2016-06-17 22:16:06 +0000872 if (kind() == K_DARWIN64)
873 return read64le(buf) / 16;
Rui Ueyama407e0972015-05-26 16:20:40 +0000874 uint32_t member_count = 0;
875 member_count = read32le(buf);
876 buf += 4 + (member_count * 4); // Skip offsets.
877 return read32le(buf);
Michael J. Spencere03ea9c2011-11-02 19:33:12 +0000878}
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000879
Lang Hames69f49022016-07-14 20:44:27 +0000880Expected<Optional<Archive::Child>> Archive::findSym(StringRef name) const {
Rafael Espindola23a97502014-01-21 16:09:45 +0000881 Archive::symbol_iterator bs = symbol_begin();
882 Archive::symbol_iterator es = symbol_end();
Rafael Espindolaae460022014-06-16 16:08:36 +0000883
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000884 for (; bs != es; ++bs) {
Rafael Espindolaae460022014-06-16 16:08:36 +0000885 StringRef SymName = bs->getName();
886 if (SymName == name) {
Lang Hames69f49022016-07-14 20:44:27 +0000887 if (auto MemberOrErr = bs->getMember())
888 return Child(*MemberOrErr);
889 else
890 return errorCodeToError(MemberOrErr.getError());
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000891 }
892 }
Lang Hames69f49022016-07-14 20:44:27 +0000893 return Optional<Child>();
Shankar Easwaran15b28be2012-11-13 18:38:42 +0000894}
Rafael Espindolab6b5f52e2013-07-29 12:40:31 +0000895
Rafael Espindola4a782fb2015-10-31 21:03:29 +0000896bool Archive::hasSymbolTable() const { return !SymbolTable.empty(); }