1 //===- Archive.cpp - ar File Format implementation ------------------------===//
2 //
3 // 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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the ArchiveObjectFile class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/Object/Archive.h"
14 #include "llvm/ADT/Optional.h"
15 #include "llvm/ADT/SmallString.h"
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/ADT/Twine.h"
18 #include "llvm/Object/Binary.h"
19 #include "llvm/Object/Error.h"
20 #include "llvm/Support/Chrono.h"
21 #include "llvm/Support/Endian.h"
22 #include "llvm/Support/Error.h"
23 #include "llvm/Support/ErrorOr.h"
24 #include "llvm/Support/FileSystem.h"
25 #include "llvm/Support/MemoryBuffer.h"
26 #include "llvm/Support/Path.h"
27 #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>
36 
37 using namespace llvm;
38 using namespace object;
39 using namespace llvm::support::endian;
40 
41 static const char *const Magic = "!<arch>\n";
42 static const char *const ThinMagic = "!<thin>\n";
43 
44 void Archive::anchor() {}
45 
46 static Error
47 malformedError(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 
53 ArchiveMemberHeader::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 
62   if (Size < sizeof(ArMemHdrType)) {
63     if (Err) {
64       std::string Msg("remaining size of archive too small for next archive "
65                       "member header ");
66       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
72         *Err = malformedError(Msg + "for " + NameOrErr.get());
73     }
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);
80       OS.write_escaped(StringRef(ArMemHdr->Terminator,
81                                  sizeof(ArMemHdr->Terminator)));
82       OS.flush();
83       std::string Msg("terminator characters in archive member \"" + Buf +
84                       "\" not the correct \"`\\n\" values for the archive "
85                       "member header ");
86       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
92         *Err = malformedError(Msg + "for " + NameOrErr.get());
93     }
94     return;
95   }
96 }
97 
98 // 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.
100 Expected<StringRef> ArchiveMemberHeader::getRawName() const {
101   char EndCond;
102   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] == '#')
113     EndCond = ' ';
114   else
115     EndCond = '/';
116   StringRef::size_type end =
117       StringRef(ArMemHdr->Name, sizeof(ArMemHdr->Name)).find(EndCond);
118   if (end == StringRef::npos)
119     end = sizeof(ArMemHdr->Name);
120   assert(end <= sizeof(ArMemHdr->Name) && end > 0);
121   // Don't include the EndCond if there is one.
122   return StringRef(ArMemHdr->Name, end);
123 }
124 
125 // 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.
128 Expected<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);
159       OS.write_escaped(Name.substr(1).rtrim(' '));
160       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     }
177 
178     // GNU long file names end with a "/\n".
179     if (Parent->kind() == Archive::K_GNU ||
180         Parent->kind() == Archive::K_GNU64) {
181       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);
188     }
189     return Parent->getStringTable().begin() + StringOffset;
190   }
191 
192   if (Name.startswith("#1/")) {
193     uint64_t NameLength;
194     if (Name.substr(3).rtrim(' ').getAsInteger(10, NameLength)) {
195       std::string Buf;
196       raw_string_ostream OS(Buf);
197       OS.write_escaped(Name.substr(3).rtrim(' '));
198       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');
216   }
217 
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 
222   // It's a simple name.
223   return Name.drop_back(1);
224 }
225 
226 Expected<uint32_t> ArchiveMemberHeader::getSize() const {
227   uint32_t Ret;
228   if (StringRef(ArMemHdr->Size,
229                 sizeof(ArMemHdr->Size)).rtrim(" ").getAsInteger(10, Ret)) {
230     std::string Buf;
231     raw_string_ostream OS(Buf);
232     OS.write_escaped(StringRef(ArMemHdr->Size,
233                                sizeof(ArMemHdr->Size)).rtrim(" "));
234     OS.flush();
235     uint64_t Offset = reinterpret_cast<const char *>(ArMemHdr) -
236                       Parent->getData().data();
237     return malformedError("characters in size field in archive header are not "
238                           "all decimal numbers: '" + Buf + "' for archive "
239                           "member header at offset " + Twine(Offset));
240   }
241   return Ret;
242 }
243 
244 Expected<sys::fs::perms> ArchiveMemberHeader::getAccessMode() const {
245   unsigned Ret;
246   if (StringRef(ArMemHdr->AccessMode,
247                 sizeof(ArMemHdr->AccessMode)).rtrim(' ').getAsInteger(8, Ret)) {
248     std::string Buf;
249     raw_string_ostream OS(Buf);
250     OS.write_escaped(StringRef(ArMemHdr->AccessMode,
251                                sizeof(ArMemHdr->AccessMode)).rtrim(" "));
252     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   }
259   return static_cast<sys::fs::perms>(Ret);
260 }
261 
262 Expected<sys::TimePoint<std::chrono::seconds>>
263 ArchiveMemberHeader::getLastModified() const {
264   unsigned Seconds;
265   if (StringRef(ArMemHdr->LastModified,
266                 sizeof(ArMemHdr->LastModified)).rtrim(' ')
267           .getAsInteger(10, Seconds)) {
268     std::string Buf;
269     raw_string_ostream OS(Buf);
270     OS.write_escaped(StringRef(ArMemHdr->LastModified,
271                                sizeof(ArMemHdr->LastModified)).rtrim(" "));
272     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   }
279 
280   return sys::toTimePoint(Seconds);
281 }
282 
283 Expected<unsigned> ArchiveMemberHeader::getUID() const {
284   unsigned Ret;
285   StringRef User = StringRef(ArMemHdr->UID, sizeof(ArMemHdr->UID)).rtrim(' ');
286   if (User.empty())
287     return 0;
288   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   }
299   return Ret;
300 }
301 
302 Expected<unsigned> ArchiveMemberHeader::getGID() const {
303   unsigned Ret;
304   StringRef Group = StringRef(ArMemHdr->GID, sizeof(ArMemHdr->GID)).rtrim(' ');
305   if (Group.empty())
306     return 0;
307   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   }
318   return Ret;
319 }
320 
321 Archive::Child::Child(const Archive *Parent, StringRef Data,
322                       uint16_t StartOfFile)
323     : Parent(Parent), Header(Parent, Data.data(), Data.size(), nullptr),
324       Data(Data), StartOfFile(StartOfFile) {
325 }
326 
327 Archive::Child::Child(const Archive *Parent, const char *Start, Error *Err)
328     : Parent(Parent),
329       Header(Parent, Start,
330              Parent
331                ? Parent->getData().size() - (Start - Parent->getData().data())
332                : 0, Err) {
333   if (!Start)
334     return;
335 
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 
342   ErrorAsOutParameter ErrAsOutParam(Err);
343 
344   // If there was an error in the construction of the Header
345   // then just return with the error now set.
346   if (*Err)
347     return;
348 
349   uint64_t Size = Header.getSizeOf();
350   Data = StringRef(Start, Size);
351   Expected<bool> isThinOrErr = isThinMember();
352   if (!isThinOrErr) {
353     *Err = isThinOrErr.takeError();
354     return;
355   }
356   bool isThin = isThinOrErr.get();
357   if (!isThin) {
358     Expected<uint64_t> MemberSize = getRawSize();
359     if (!MemberSize) {
360       *Err = MemberSize.takeError();
361       return;
362     }
363     Size += MemberSize.get();
364     Data = StringRef(Start, Size);
365   }
366 
367   // Setup StartOfFile and PaddingBytes.
368   StartOfFile = Header.getSizeOf();
369   // Don't include attached name.
370   Expected<StringRef> NameOrErr = getRawName();
371   if (!NameOrErr){
372     *Err = NameOrErr.takeError();
373     return;
374   }
375   StringRef Name = NameOrErr.get();
376   if (Name.startswith("#1/")) {
377     uint64_t NameSize;
378     if (Name.substr(3).rtrim(' ').getAsInteger(10, NameSize)) {
379       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;
389     }
390     StartOfFile += NameSize;
391   }
392 }
393 
394 Expected<uint64_t> Archive::Child::getSize() const {
395   if (Parent->IsThin) {
396     Expected<uint32_t> Size = Header.getSize();
397     if (!Size)
398       return Size.takeError();
399     return Size.get();
400   }
401   return Data.size() - StartOfFile;
402 }
403 
404 Expected<uint64_t> Archive::Child::getRawSize() const {
405   return Header.getSize();
406 }
407 
408 Expected<bool> Archive::Child::isThinMember() const {
409   Expected<StringRef> NameOrErr = Header.getRawName();
410   if (!NameOrErr)
411     return NameOrErr.takeError();
412   StringRef Name = NameOrErr.get();
413   return Parent->IsThin && Name != "/" && Name != "//";
414 }
415 
416 Expected<std::string> Archive::Child::getFullName() const {
417   Expected<bool> isThin = isThinMember();
418   if (!isThin)
419     return isThin.takeError();
420   assert(isThin.get());
421   Expected<StringRef> NameOrErr = getName();
422   if (!NameOrErr)
423     return NameOrErr.takeError();
424   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 
434 Expected<StringRef> Archive::Child::getBuffer() const {
435   Expected<bool> isThinOrErr = isThinMember();
436   if (!isThinOrErr)
437     return isThinOrErr.takeError();
438   bool isThin = isThinOrErr.get();
439   if (!isThin) {
440     Expected<uint32_t> Size = getSize();
441     if (!Size)
442       return Size.takeError();
443     return StringRef(Data.data() + StartOfFile, Size.get());
444   }
445   Expected<std::string> FullNameOrErr = getFullName();
446   if (!FullNameOrErr)
447     return FullNameOrErr.takeError();
448   const std::string &FullName = *FullNameOrErr;
449   ErrorOr<std::unique_ptr<MemoryBuffer>> Buf = MemoryBuffer::getFile(FullName);
450   if (std::error_code EC = Buf.getError())
451     return errorCodeToError(EC);
452   Parent->ThinBuffers.push_back(std::move(*Buf));
453   return Parent->ThinBuffers.back()->getBuffer();
454 }
455 
456 Expected<Archive::Child> Archive::Child::getNext() const {
457   size_t SpaceToSkip = Data.size();
458   // If it's odd, add 1 to make it even.
459   if (SpaceToSkip & 1)
460     ++SpaceToSkip;
461 
462   const char *NextLoc = Data.data() + SpaceToSkip;
463 
464   // Check to see if this is at the end of the archive.
465   if (NextLoc == Parent->Data.getBufferEnd())
466     return Child(nullptr, nullptr, nullptr);
467 
468   // Check to see if this is past the end of the archive.
469   if (NextLoc > Parent->Data.getBufferEnd()) {
470     std::string Msg("offset to next archive member past the end of the archive "
471                     "after member ");
472     Expected<StringRef> NameOrErr = getName();
473     if (!NameOrErr) {
474       consumeError(NameOrErr.takeError());
475       uint64_t Offset = Data.data() - Parent->getData().data();
476       return malformedError(Msg + "at offset " + Twine(Offset));
477     } else
478       return malformedError(Msg + NameOrErr.get());
479   }
480 
481   Error Err = Error::success();
482   Child Ret(Parent, NextLoc, &Err);
483   if (Err)
484     return std::move(Err);
485   return Ret;
486 }
487 
488 uint64_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 
495 Expected<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;
505 }
506 
507 Expected<MemoryBufferRef> Archive::Child::getMemoryBufferRef() const {
508   Expected<StringRef> NameOrErr = getName();
509   if (!NameOrErr)
510     return NameOrErr.takeError();
511   StringRef Name = NameOrErr.get();
512   Expected<StringRef> Buf = getBuffer();
513   if (!Buf)
514     return Buf.takeError();
515   return MemoryBufferRef(*Buf, Name);
516 }
517 
518 Expected<std::unique_ptr<Binary>>
519 Archive::Child::getAsBinary(LLVMContext *Context) const {
520   Expected<MemoryBufferRef> BuffOrErr = getMemoryBufferRef();
521   if (!BuffOrErr)
522     return BuffOrErr.takeError();
523 
524   auto BinaryOrErr = createBinary(BuffOrErr.get(), Context);
525   if (BinaryOrErr)
526     return std::move(*BinaryOrErr);
527   return BinaryOrErr.takeError();
528 }
529 
530 Expected<std::unique_ptr<Archive>> Archive::create(MemoryBufferRef Source) {
531   Error Err = Error::success();
532   std::unique_ptr<Archive> Ret(new Archive(Source, Err));
533   if (Err)
534     return std::move(Err);
535   return std::move(Ret);
536 }
537 
538 void Archive::setFirstRegular(const Child &C) {
539   FirstRegularData = C.Data;
540   FirstRegularStartOfFile = C.StartOfFile;
541 }
542 
543 Archive::Archive(MemoryBufferRef Source, Error &Err)
544     : Binary(Binary::ID_Archive, Source) {
545   ErrorAsOutParameter ErrAsOutParam(&Err);
546   StringRef Buffer = Data.getBuffer();
547   // Check for sufficient magic.
548   if (Buffer.startswith(ThinMagic)) {
549     IsThin = true;
550   } else if (Buffer.startswith(Magic)) {
551     IsThin = false;
552   } else {
553     Err = make_error<GenericBinaryError>("File too small to be an archive",
554                                          object_error::invalid_file_type);
555     return;
556   }
557 
558   // 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 
565   // Get the special members.
566   child_iterator I = child_begin(Err, false);
567   if (Err)
568     return;
569   child_iterator E = child_end();
570 
571   // See if this is a valid empty archive and if so return.
572   if (I == E) {
573     Err = Error::success();
574     return;
575   }
576   const Child *C = &*I;
577 
578   auto Increment = [&]() {
579     ++I;
580     if (Err)
581       return true;
582     C = &*I;
583     return false;
584   };
585 
586   Expected<StringRef> NameOrErr = C->getRawName();
587   if (!NameOrErr) {
588     Err = NameOrErr.takeError();
589     return;
590   }
591   StringRef Name = NameOrErr.get();
592 
593   // Below is the pattern that is used to figure out the archive format
594   // GNU archive format
595   //  First member : / (may exist, if it exists, points to the symbol table )
596   //  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
599   //  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
602   //  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)
606   //  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.
611 
612   if (Name == "__.SYMDEF" || Name == "__.SYMDEF_64") {
613     if (Name == "__.SYMDEF")
614       Format = K_BSD;
615     else // Name == "__.SYMDEF_64"
616       Format = K_DARWIN64;
617     // 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();
625     if (Increment())
626       return;
627     setFirstRegular(*C);
628 
629     Err = Error::success();
630     return;
631   }
632 
633   if (Name.startswith("#1/")) {
634     Format = K_BSD;
635     // We know this is BSD, so getName will work since there is no string table.
636     Expected<StringRef> NameOrErr = C->getName();
637     if (!NameOrErr) {
638       Err = NameOrErr.takeError();
639       return;
640     }
641     Name = NameOrErr.get();
642     if (Name == "__.SYMDEF SORTED" || Name == "__.SYMDEF") {
643       // 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();
651       if (Increment())
652         return;
653     }
654     else if (Name == "__.SYMDEF_64 SORTED" || Name == "__.SYMDEF_64") {
655       Format = K_DARWIN64;
656       // 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();
664       if (Increment())
665         return;
666     }
667     setFirstRegular(*C);
668     return;
669   }
670 
671   // 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/") {
678     // 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();
686     if (Name == "/SYM64/")
687       has64SymTable = true;
688 
689     if (Increment())
690       return;
691     if (I == E) {
692       Err = Error::success();
693       return;
694     }
695     Expected<StringRef> NameOrErr = C->getRawName();
696     if (!NameOrErr) {
697       Err = NameOrErr.takeError();
698       return;
699     }
700     Name = NameOrErr.get();
701   }
702 
703   if (Name == "//") {
704     Format = has64SymTable ? K_GNU64 : K_GNU;
705     // 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();
713     if (Increment())
714       return;
715     setFirstRegular(*C);
716     Err = Error::success();
717     return;
718   }
719 
720   if (Name[0] != '/') {
721     Format = has64SymTable ? K_GNU64 : K_GNU;
722     setFirstRegular(*C);
723     Err = Error::success();
724     return;
725   }
726 
727   if (Name != "/") {
728     Err = errorCodeToError(object_error::parse_failed);
729     return;
730   }
731 
732   Format = K_COFF;
733   // 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();
741 
742   if (Increment())
743     return;
744 
745   if (I == E) {
746     setFirstRegular(*C);
747     Err = Error::success();
748     return;
749   }
750 
751   NameOrErr = C->getRawName();
752   if (!NameOrErr) {
753     Err = NameOrErr.takeError();
754     return;
755   }
756   Name = NameOrErr.get();
757 
758   if (Name == "//") {
759     // 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();
767     if (Increment())
768       return;
769   }
770 
771   setFirstRegular(*C);
772   Err = Error::success();
773 }
774 
775 Archive::child_iterator Archive::child_begin(Error &Err,
776                                              bool SkipInternal) const {
777   if (isEmpty())
778     return child_end();
779 
780   if (SkipInternal)
781     return child_iterator(Child(this, FirstRegularData,
782                                 FirstRegularStartOfFile),
783                           &Err);
784 
785   const char *Loc = Data.getBufferStart() + strlen(Magic);
786   Child C(this, Loc, &Err);
787   if (Err)
788     return child_end();
789   return child_iterator(C, &Err);
790 }
791 
792 Archive::child_iterator Archive::child_end() const {
793   return child_iterator(Child(nullptr, nullptr, nullptr), nullptr);
794 }
795 
796 StringRef Archive::Symbol::getName() const {
797   return Parent->getSymbolTable().begin() + StringIndex;
798 }
799 
800 Expected<Archive::Child> Archive::Symbol::getMember() const {
801   const char *Buf = Parent->getSymbolTable().begin();
802   const char *Offsets = Buf;
803   if (Parent->kind() == K_GNU64 || Parent->kind() == K_DARWIN64)
804     Offsets += sizeof(uint64_t);
805   else
806     Offsets += sizeof(uint32_t);
807   uint64_t Offset = 0;
808   if (Parent->kind() == K_GNU) {
809     Offset = read32be(Offsets + SymbolIndex * 4);
810   } else if (Parent->kind() == K_GNU64) {
811     Offset = read64be(Offsets + SymbolIndex * 8);
812   } else if (Parent->kind() == K_BSD) {
813     // The SymbolIndex is an index into the ranlib structs that start at
814     // Offsets (the first uint32_t is the number of bytes of the ranlib
815     // structs).  The ranlib structs are a pair of uint32_t's the first
816     // being a string table offset and the second being the offset into
817     // the archive of the member that defines the symbol.  Which is what
818     // is needed here.
819     Offset = read32le(Offsets + SymbolIndex * 8 + 4);
820   } else if (Parent->kind() == K_DARWIN64) {
821     // The SymbolIndex is an index into the ranlib_64 structs that start at
822     // Offsets (the first uint64_t is the number of bytes of the ranlib_64
823     // structs).  The ranlib_64 structs are a pair of uint64_t's the first
824     // being a string table offset and the second being the offset into
825     // the archive of the member that defines the symbol.  Which is what
826     // is needed here.
827     Offset = read64le(Offsets + SymbolIndex * 16 + 8);
828   } else {
829     // Skip offsets.
830     uint32_t MemberCount = read32le(Buf);
831     Buf += MemberCount * 4 + 4;
832 
833     uint32_t SymbolCount = read32le(Buf);
834     if (SymbolIndex >= SymbolCount)
835       return errorCodeToError(object_error::parse_failed);
836 
837     // Skip SymbolCount to get to the indices table.
838     const char *Indices = Buf + 4;
839 
840     // Get the index of the offset in the file member offset table for this
841     // symbol.
842     uint16_t OffsetIndex = read16le(Indices + SymbolIndex * 2);
843     // Subtract 1 since OffsetIndex is 1 based.
844     --OffsetIndex;
845 
846     if (OffsetIndex >= MemberCount)
847       return errorCodeToError(object_error::parse_failed);
848 
849     Offset = read32le(Offsets + OffsetIndex * 4);
850   }
851 
852   const char *Loc = Parent->getData().begin() + Offset;
853   Error Err = Error::success();
854   Child C(Parent, Loc, &Err);
855   if (Err)
856     return std::move(Err);
857   return C;
858 }
859 
860 Archive::Symbol Archive::Symbol::getNext() const {
861   Symbol t(*this);
862   if (Parent->kind() == K_BSD) {
863     // t.StringIndex is an offset from the start of the __.SYMDEF or
864     // "__.SYMDEF SORTED" member into the string table for the ranlib
865     // struct indexed by t.SymbolIndex .  To change t.StringIndex to the
866     // offset in the string table for t.SymbolIndex+1 we subtract the
867     // its offset from the start of the string table for t.SymbolIndex
868     // and add the offset of the string table for t.SymbolIndex+1.
869 
870     // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t
871     // which is the number of bytes of ranlib structs that follow.  The ranlib
872     // structs are a pair of uint32_t's the first being a string table offset
873     // and the second being the offset into the archive of the member that
874     // define the symbol. After that the next uint32_t is the byte count of
875     // the string table followed by the string table.
876     const char *Buf = Parent->getSymbolTable().begin();
877     uint32_t RanlibCount = 0;
878     RanlibCount = read32le(Buf) / 8;
879     // If t.SymbolIndex + 1 will be past the count of symbols (the RanlibCount)
880     // don't change the t.StringIndex as we don't want to reference a ranlib
881     // past RanlibCount.
882     if (t.SymbolIndex + 1 < RanlibCount) {
883       const char *Ranlibs = Buf + 4;
884       uint32_t CurRanStrx = 0;
885       uint32_t NextRanStrx = 0;
886       CurRanStrx = read32le(Ranlibs + t.SymbolIndex * 8);
887       NextRanStrx = read32le(Ranlibs + (t.SymbolIndex + 1) * 8);
888       t.StringIndex -= CurRanStrx;
889       t.StringIndex += NextRanStrx;
890     }
891   } else {
892     // Go to one past next null.
893     t.StringIndex = Parent->getSymbolTable().find('\0', t.StringIndex) + 1;
894   }
895   ++t.SymbolIndex;
896   return t;
897 }
898 
899 Archive::symbol_iterator Archive::symbol_begin() const {
900   if (!hasSymbolTable())
901     return symbol_iterator(Symbol(this, 0, 0));
902 
903   const char *buf = getSymbolTable().begin();
904   if (kind() == K_GNU) {
905     uint32_t symbol_count = 0;
906     symbol_count = read32be(buf);
907     buf += sizeof(uint32_t) + (symbol_count * (sizeof(uint32_t)));
908   } else if (kind() == K_GNU64) {
909     uint64_t symbol_count = read64be(buf);
910     buf += sizeof(uint64_t) + (symbol_count * (sizeof(uint64_t)));
911   } else if (kind() == K_BSD) {
912     // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t
913     // which is the number of bytes of ranlib structs that follow.  The ranlib
914     // structs are a pair of uint32_t's the first being a string table offset
915     // and the second being the offset into the archive of the member that
916     // define the symbol. After that the next uint32_t is the byte count of
917     // the string table followed by the string table.
918     uint32_t ranlib_count = 0;
919     ranlib_count = read32le(buf) / 8;
920     const char *ranlibs = buf + 4;
921     uint32_t ran_strx = 0;
922     ran_strx = read32le(ranlibs);
923     buf += sizeof(uint32_t) + (ranlib_count * (2 * (sizeof(uint32_t))));
924     // Skip the byte count of the string table.
925     buf += sizeof(uint32_t);
926     buf += ran_strx;
927   } else if (kind() == K_DARWIN64) {
928     // The __.SYMDEF_64 or "__.SYMDEF_64 SORTED" member starts with a uint64_t
929     // which is the number of bytes of ranlib_64 structs that follow.  The
930     // ranlib_64 structs are a pair of uint64_t's the first being a string
931     // table offset and the second being the offset into the archive of the
932     // member that define the symbol. After that the next uint64_t is the byte
933     // count of the string table followed by the string table.
934     uint64_t ranlib_count = 0;
935     ranlib_count = read64le(buf) / 16;
936     const char *ranlibs = buf + 8;
937     uint64_t ran_strx = 0;
938     ran_strx = read64le(ranlibs);
939     buf += sizeof(uint64_t) + (ranlib_count * (2 * (sizeof(uint64_t))));
940     // Skip the byte count of the string table.
941     buf += sizeof(uint64_t);
942     buf += ran_strx;
943   } else {
944     uint32_t member_count = 0;
945     uint32_t symbol_count = 0;
946     member_count = read32le(buf);
947     buf += 4 + (member_count * 4); // Skip offsets.
948     symbol_count = read32le(buf);
949     buf += 4 + (symbol_count * 2); // Skip indices.
950   }
951   uint32_t string_start_offset = buf - getSymbolTable().begin();
952   return symbol_iterator(Symbol(this, 0, string_start_offset));
953 }
954 
955 Archive::symbol_iterator Archive::symbol_end() const {
956   return symbol_iterator(Symbol(this, getNumberOfSymbols(), 0));
957 }
958 
959 uint32_t Archive::getNumberOfSymbols() const {
960   if (!hasSymbolTable())
961     return 0;
962   const char *buf = getSymbolTable().begin();
963   if (kind() == K_GNU)
964     return read32be(buf);
965   if (kind() == K_GNU64)
966     return read64be(buf);
967   if (kind() == K_BSD)
968     return read32le(buf) / 8;
969   if (kind() == K_DARWIN64)
970     return read64le(buf) / 16;
971   uint32_t member_count = 0;
972   member_count = read32le(buf);
973   buf += 4 + (member_count * 4); // Skip offsets.
974   return read32le(buf);
975 }
976 
977 Expected<Optional<Archive::Child>> Archive::findSym(StringRef name) const {
978   Archive::symbol_iterator bs = symbol_begin();
979   Archive::symbol_iterator es = symbol_end();
980 
981   for (; bs != es; ++bs) {
982     StringRef SymName = bs->getName();
983     if (SymName == name) {
984       if (auto MemberOrErr = bs->getMember())
985         return Child(*MemberOrErr);
986       else
987         return MemberOrErr.takeError();
988     }
989   }
990   return Optional<Child>();
991 }
992 
993 // Returns true if archive file contains no member file.
994 bool Archive::isEmpty() const { return Data.getBufferSize() == 8; }
995 
996 bool Archive::hasSymbolTable() const { return !SymbolTable.empty(); }
997