1 //===- 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"
15 #include "llvm/ADT/SmallString.h"
16 #include "llvm/ADT/Twine.h"
17 #include "llvm/Support/Endian.h"
18 #include "llvm/Support/MemoryBuffer.h"
19 #include "llvm/Support/Path.h"
20 
21 using namespace llvm;
22 using namespace object;
23 using namespace llvm::support::endian;
24 
25 static const char *const Magic = "!<arch>\n";
26 static const char *const ThinMagic = "!<thin>\n";
27 
28 void Archive::anchor() { }
29 
30 static Error
31 malformedError(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 
37 ArchiveMemberHeader::ArchiveMemberHeader(const Archive *Parent,
38                                          const char *RawHeaderPtr,
39                                          uint64_t Size, Error *Err)
40     : Parent(Parent),
41       ArMemHdr(reinterpret_cast<const ArMemHdrType *>(RawHeaderPtr)) {
42   if (RawHeaderPtr == nullptr)
43     return;
44   ErrorAsOutParameter ErrAsOutParam(Err);
45 
46   if (Size < sizeof(ArMemHdrType)) {
47     if (Err) {
48       std::string Msg("remaining size of archive too small for next archive "
49                       "member header ");
50       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
56         *Err = malformedError(Msg + "for " + NameOrErr.get());
57     }
58     return;
59   }
60   if (ArMemHdr->Terminator[0] != '`' || ArMemHdr->Terminator[1] != '\n') {
61     if (Err) {
62       std::string Buf;
63       raw_string_ostream OS(Buf);
64       OS.write_escaped(llvm::StringRef(ArMemHdr->Terminator,
65                                        sizeof(ArMemHdr->Terminator)));
66       OS.flush();
67       std::string Msg("terminator characters in archive member \"" + Buf +
68                       "\" not the correct \"`\\n\" values for the archive "
69                       "member header ");
70       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
76         *Err = malformedError(Msg + "for " + NameOrErr.get());
77     }
78     return;
79   }
80 }
81 
82 // 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.
84 Expected<StringRef> ArchiveMemberHeader::getRawName() const {
85   char EndCond;
86   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] == '#')
97     EndCond = ' ';
98   else
99     EndCond = '/';
100   llvm::StringRef::size_type end =
101       llvm::StringRef(ArMemHdr->Name, sizeof(ArMemHdr->Name)).find(EndCond);
102   if (end == llvm::StringRef::npos)
103     end = sizeof(ArMemHdr->Name);
104   assert(end <= sizeof(ArMemHdr->Name) && end > 0);
105   // Don't include the EndCond if there is one.
106   return llvm::StringRef(ArMemHdr->Name, end);
107 }
108 
109 // 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.
112 Expected<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);
143       OS.write_escaped(Name.substr(1).rtrim(' '));
144       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     }
169     return addr;
170   }
171 
172   if (Name.startswith("#1/")) {
173     uint64_t NameLength;
174     if (Name.substr(3).rtrim(' ').getAsInteger(10, NameLength)) {
175       std::string Buf;
176       raw_string_ostream OS(Buf);
177       OS.write_escaped(Name.substr(3).rtrim(' '));
178       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');
196   }
197 
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 
202   // It's a simple name.
203   return Name.drop_back(1);
204 }
205 
206 Expected<uint32_t> ArchiveMemberHeader::getSize() const {
207   uint32_t Ret;
208   if (llvm::StringRef(ArMemHdr->Size,
209         sizeof(ArMemHdr->Size)).rtrim(" ").getAsInteger(10, Ret)) {
210     std::string Buf;
211     raw_string_ostream OS(Buf);
212     OS.write_escaped(llvm::StringRef(ArMemHdr->Size,
213                                      sizeof(ArMemHdr->Size)).rtrim(" "));
214     OS.flush();
215     uint64_t Offset = reinterpret_cast<const char *>(ArMemHdr) -
216                       Parent->getData().data();
217     return malformedError("characters in size field in archive header are not "
218                           "all decimal numbers: '" + Buf + "' for archive "
219                           "member header at offset " + Twine(Offset));
220   }
221   return Ret;
222 }
223 
224 Expected<sys::fs::perms> ArchiveMemberHeader::getAccessMode() const {
225   unsigned Ret;
226   if (StringRef(ArMemHdr->AccessMode,
227                 sizeof(ArMemHdr->AccessMode)).rtrim(' ').getAsInteger(8, Ret)) {
228     std::string Buf;
229     raw_string_ostream OS(Buf);
230     OS.write_escaped(llvm::StringRef(ArMemHdr->AccessMode,
231                                    sizeof(ArMemHdr->AccessMode)).rtrim(" "));
232     OS.flush();
233     uint64_t Offset = reinterpret_cast<const char *>(ArMemHdr) -
234                       Parent->getData().data();
235     return malformedError("characters in AccessMode field in archive header "
236                           "are not all decimal numbers: '" + Buf + "' for the "
237                           "archive member header at offset " + Twine(Offset));
238   }
239   return static_cast<sys::fs::perms>(Ret);
240 }
241 
242 Expected<sys::TimeValue> ArchiveMemberHeader::getLastModified() const {
243   unsigned Seconds;
244   if (StringRef(ArMemHdr->LastModified,
245                 sizeof(ArMemHdr->LastModified)).rtrim(' ')
246           .getAsInteger(10, Seconds)) {
247     std::string Buf;
248     raw_string_ostream OS(Buf);
249     OS.write_escaped(llvm::StringRef(ArMemHdr->LastModified,
250                                    sizeof(ArMemHdr->LastModified)).rtrim(" "));
251     OS.flush();
252     uint64_t Offset = reinterpret_cast<const char *>(ArMemHdr) -
253                       Parent->getData().data();
254     return malformedError("characters in LastModified field in archive header "
255                           "are not all decimal numbers: '" + Buf + "' for the "
256                           "archive member header at offset " + Twine(Offset));
257   }
258 
259   sys::TimeValue Ret;
260   Ret.fromEpochTime(Seconds);
261   return Ret;
262 }
263 
264 Expected<unsigned> ArchiveMemberHeader::getUID() const {
265   unsigned Ret;
266   StringRef User = StringRef(ArMemHdr->UID, sizeof(ArMemHdr->UID)).rtrim(' ');
267   if (User.empty())
268     return 0;
269   if (User.getAsInteger(10, Ret)) {
270     std::string Buf;
271     raw_string_ostream OS(Buf);
272     OS.write_escaped(User);
273     OS.flush();
274     uint64_t Offset = reinterpret_cast<const char *>(ArMemHdr) -
275                       Parent->getData().data();
276     return malformedError("characters in UID field in archive header "
277                           "are not all decimal numbers: '" + Buf + "' for the "
278                           "archive member header at offset " + Twine(Offset));
279   }
280   return Ret;
281 }
282 
283 Expected<unsigned> ArchiveMemberHeader::getGID() const {
284   unsigned Ret;
285   StringRef Group = StringRef(ArMemHdr->GID, sizeof(ArMemHdr->GID)).rtrim(' ');
286   if (Group.empty())
287     return 0;
288   if (Group.getAsInteger(10, Ret)) {
289     std::string Buf;
290     raw_string_ostream OS(Buf);
291     OS.write_escaped(Group);
292     OS.flush();
293     uint64_t Offset = reinterpret_cast<const char *>(ArMemHdr) -
294                       Parent->getData().data();
295     return malformedError("characters in GID 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 Archive::Child::Child(const Archive *Parent, StringRef Data,
303                       uint16_t StartOfFile)
304     : Parent(Parent), Header(Parent, Data.data(), Data.size(), nullptr),
305       Data(Data), StartOfFile(StartOfFile) {
306 }
307 
308 Archive::Child::Child(const Archive *Parent, const char *Start, Error *Err)
309     : Parent(Parent),
310       Header(Parent, Start,
311              Parent
312                ? Parent->getData().size() - (Start - Parent->getData().data())
313                : 0, Err) {
314   if (!Start)
315     return;
316 
317   // If we are pointed to real data, Start is not a nullptr, then there must be
318   // a non-null Err pointer available to report malformed data on.  Only in
319   // the case sentinel value is being constructed is Err is permitted to be a
320   // nullptr.
321   assert(Err && "Err can't be nullptr if Start is not a nullptr");
322 
323   ErrorAsOutParameter ErrAsOutParam(Err);
324 
325   // If there was an error in the construction of the Header
326   // then just return with the error now set.
327   if (*Err)
328     return;
329 
330   uint64_t Size = Header.getSizeOf();
331   Data = StringRef(Start, Size);
332   Expected<bool> isThinOrErr = isThinMember();
333   if (!isThinOrErr) {
334     *Err = isThinOrErr.takeError();
335     return;
336   }
337   bool isThin = isThinOrErr.get();
338   if (!isThin) {
339     Expected<uint64_t> MemberSize = getRawSize();
340     if (!MemberSize) {
341       *Err = MemberSize.takeError();
342       return;
343     }
344     Size += MemberSize.get();
345     Data = StringRef(Start, Size);
346   }
347 
348   // Setup StartOfFile and PaddingBytes.
349   StartOfFile = Header.getSizeOf();
350   // Don't include attached name.
351   Expected<StringRef> NameOrErr = getRawName();
352   if (!NameOrErr){
353     *Err = NameOrErr.takeError();
354     return;
355   }
356   StringRef Name = NameOrErr.get();
357   if (Name.startswith("#1/")) {
358     uint64_t NameSize;
359     if (Name.substr(3).rtrim(' ').getAsInteger(10, NameSize)) {
360       std::string Buf;
361       raw_string_ostream OS(Buf);
362       OS.write_escaped(Name.substr(3).rtrim(' '));
363       OS.flush();
364       uint64_t Offset = Start - Parent->getData().data();
365       *Err = malformedError("long name length characters after the #1/ are "
366                             "not all decimal numbers: '" + Buf + "' for "
367                             "archive member header at offset " +
368                             Twine(Offset));
369       return;
370     }
371     StartOfFile += NameSize;
372   }
373 }
374 
375 Expected<uint64_t> Archive::Child::getSize() const {
376   if (Parent->IsThin) {
377     Expected<uint32_t> Size = Header.getSize();
378     if (!Size)
379       return Size.takeError();
380     return Size.get();
381   }
382   return Data.size() - StartOfFile;
383 }
384 
385 Expected<uint64_t> Archive::Child::getRawSize() const {
386   return Header.getSize();
387 }
388 
389 Expected<bool> Archive::Child::isThinMember() const {
390   Expected<StringRef> NameOrErr = Header.getRawName();
391   if (!NameOrErr)
392     return NameOrErr.takeError();
393   StringRef Name = NameOrErr.get();
394   return Parent->IsThin && Name != "/" && Name != "//";
395 }
396 
397 Expected<std::string> Archive::Child::getFullName() const {
398   Expected<bool> isThin = isThinMember();
399   if (!isThin)
400     return isThin.takeError();
401   assert(isThin.get());
402   Expected<StringRef> NameOrErr = getName();
403   if (!NameOrErr)
404     return NameOrErr.takeError();
405   StringRef Name = *NameOrErr;
406   if (sys::path::is_absolute(Name))
407     return Name;
408 
409   SmallString<128> FullName = sys::path::parent_path(
410       Parent->getMemoryBufferRef().getBufferIdentifier());
411   sys::path::append(FullName, Name);
412   return StringRef(FullName);
413 }
414 
415 Expected<StringRef> Archive::Child::getBuffer() const {
416   Expected<bool> isThinOrErr = isThinMember();
417   if (!isThinOrErr)
418     return isThinOrErr.takeError();
419   bool isThin = isThinOrErr.get();
420   if (!isThin) {
421     Expected<uint32_t> Size = getSize();
422     if (!Size)
423       return Size.takeError();
424     return StringRef(Data.data() + StartOfFile, Size.get());
425   }
426   Expected<std::string> FullNameOrErr = getFullName();
427   if (!FullNameOrErr)
428     return FullNameOrErr.takeError();
429   const std::string &FullName = *FullNameOrErr;
430   ErrorOr<std::unique_ptr<MemoryBuffer>> Buf = MemoryBuffer::getFile(FullName);
431   if (std::error_code EC = Buf.getError())
432     return errorCodeToError(EC);
433   Parent->ThinBuffers.push_back(std::move(*Buf));
434   return Parent->ThinBuffers.back()->getBuffer();
435 }
436 
437 Expected<Archive::Child> Archive::Child::getNext() const {
438   size_t SpaceToSkip = Data.size();
439   // If it's odd, add 1 to make it even.
440   if (SpaceToSkip & 1)
441     ++SpaceToSkip;
442 
443   const char *NextLoc = Data.data() + SpaceToSkip;
444 
445   // Check to see if this is at the end of the archive.
446   if (NextLoc == Parent->Data.getBufferEnd())
447     return Child(nullptr, nullptr, nullptr);
448 
449   // Check to see if this is past the end of the archive.
450   if (NextLoc > Parent->Data.getBufferEnd()) {
451     std::string Msg("offset to next archive member past the end of the archive "
452                     "after member ");
453     Expected<StringRef> NameOrErr = getName();
454     if (!NameOrErr) {
455       consumeError(NameOrErr.takeError());
456       uint64_t Offset = Data.data() - Parent->getData().data();
457       return malformedError(Msg + "at offset " + Twine(Offset));
458     } else
459       return malformedError(Msg + NameOrErr.get());
460   }
461 
462   Error Err;
463   Child Ret(Parent, NextLoc, &Err);
464   if (Err)
465     return std::move(Err);
466   return Ret;
467 }
468 
469 uint64_t Archive::Child::getChildOffset() const {
470   const char *a = Parent->Data.getBuffer().data();
471   const char *c = Data.data();
472   uint64_t offset = c - a;
473   return offset;
474 }
475 
476 Expected<StringRef> Archive::Child::getName() const {
477   Expected<uint64_t> RawSizeOrErr = getRawSize();
478   if (!RawSizeOrErr)
479     return RawSizeOrErr.takeError();
480   uint64_t RawSize = RawSizeOrErr.get();
481   Expected<StringRef> NameOrErr = Header.getName(Header.getSizeOf() + RawSize);
482   if (!NameOrErr)
483     return NameOrErr.takeError();
484   StringRef Name = NameOrErr.get();
485   return Name;
486 }
487 
488 Expected<MemoryBufferRef> Archive::Child::getMemoryBufferRef() const {
489   Expected<StringRef> NameOrErr = getName();
490   if (!NameOrErr)
491     return NameOrErr.takeError();
492   StringRef Name = NameOrErr.get();
493   Expected<StringRef> Buf = getBuffer();
494   if (!Buf)
495     return Buf.takeError();
496   return MemoryBufferRef(*Buf, Name);
497 }
498 
499 Expected<std::unique_ptr<Binary>>
500 Archive::Child::getAsBinary(LLVMContext *Context) const {
501   Expected<MemoryBufferRef> BuffOrErr = getMemoryBufferRef();
502   if (!BuffOrErr)
503     return BuffOrErr.takeError();
504 
505   auto BinaryOrErr = createBinary(BuffOrErr.get(), Context);
506   if (BinaryOrErr)
507     return std::move(*BinaryOrErr);
508   return BinaryOrErr.takeError();
509 }
510 
511 Expected<std::unique_ptr<Archive>> Archive::create(MemoryBufferRef Source) {
512   Error Err;
513   std::unique_ptr<Archive> Ret(new Archive(Source, Err));
514   if (Err)
515     return std::move(Err);
516   return std::move(Ret);
517 }
518 
519 void Archive::setFirstRegular(const Child &C) {
520   FirstRegularData = C.Data;
521   FirstRegularStartOfFile = C.StartOfFile;
522 }
523 
524 Archive::Archive(MemoryBufferRef Source, Error &Err)
525     : Binary(Binary::ID_Archive, Source) {
526   ErrorAsOutParameter ErrAsOutParam(&Err);
527   StringRef Buffer = Data.getBuffer();
528   // Check for sufficient magic.
529   if (Buffer.startswith(ThinMagic)) {
530     IsThin = true;
531   } else if (Buffer.startswith(Magic)) {
532     IsThin = false;
533   } else {
534     Err = make_error<GenericBinaryError>("File too small to be an archive",
535                                          object_error::invalid_file_type);
536     return;
537   }
538 
539   // Make sure Format is initialized before any call to
540   // ArchiveMemberHeader::getName() is made.  This could be a valid empty
541   // archive which is the same in all formats.  So claiming it to be gnu to is
542   // fine if not totally correct before we look for a string table or table of
543   // contents.
544   Format = K_GNU;
545 
546   // Get the special members.
547   child_iterator I = child_begin(Err, false);
548   if (Err)
549     return;
550   child_iterator E = child_end();
551 
552   // See if this is a valid empty archive and if so return.
553   if (I == E) {
554     Err = Error::success();
555     return;
556   }
557   const Child *C = &*I;
558 
559   auto Increment = [&]() {
560     ++I;
561     if (Err)
562       return true;
563     C = &*I;
564     return false;
565   };
566 
567   Expected<StringRef> NameOrErr = C->getRawName();
568   if (!NameOrErr) {
569     Err = NameOrErr.takeError();
570     return;
571   }
572   StringRef Name = NameOrErr.get();
573 
574   // Below is the pattern that is used to figure out the archive format
575   // GNU archive format
576   //  First member : / (may exist, if it exists, points to the symbol table )
577   //  Second member : // (may exist, if it exists, points to the string table)
578   //  Note : The string table is used if the filename exceeds 15 characters
579   // BSD archive format
580   //  First member : __.SYMDEF or "__.SYMDEF SORTED" (the symbol table)
581   //  There is no string table, if the filename exceeds 15 characters or has a
582   //  embedded space, the filename has #1/<size>, The size represents the size
583   //  of the filename that needs to be read after the archive header
584   // COFF archive format
585   //  First member : /
586   //  Second member : / (provides a directory of symbols)
587   //  Third member : // (may exist, if it exists, contains the string table)
588   //  Note: Microsoft PE/COFF Spec 8.3 says that the third member is present
589   //  even if the string table is empty. However, lib.exe does not in fact
590   //  seem to create the third member if there's no member whose filename
591   //  exceeds 15 characters. So the third member is optional.
592 
593   if (Name == "__.SYMDEF" || Name == "__.SYMDEF_64") {
594     if (Name == "__.SYMDEF")
595       Format = K_BSD;
596     else // Name == "__.SYMDEF_64"
597       Format = K_DARWIN64;
598     // We know that the symbol table is not an external file, but we still must
599     // check any Expected<> return value.
600     Expected<StringRef> BufOrErr = C->getBuffer();
601     if (!BufOrErr) {
602       Err = BufOrErr.takeError();
603       return;
604     }
605     SymbolTable = BufOrErr.get();
606     if (Increment())
607       return;
608     setFirstRegular(*C);
609 
610     Err = Error::success();
611     return;
612   }
613 
614   if (Name.startswith("#1/")) {
615     Format = K_BSD;
616     // We know this is BSD, so getName will work since there is no string table.
617     Expected<StringRef> NameOrErr = C->getName();
618     if (!NameOrErr) {
619       Err = NameOrErr.takeError();
620       return;
621     }
622     Name = NameOrErr.get();
623     if (Name == "__.SYMDEF SORTED" || Name == "__.SYMDEF") {
624       // We know that the symbol table is not an external file, but we still
625       // must check any Expected<> return value.
626       Expected<StringRef> BufOrErr = C->getBuffer();
627       if (!BufOrErr) {
628         Err = BufOrErr.takeError();
629         return;
630       }
631       SymbolTable = BufOrErr.get();
632       if (Increment())
633         return;
634     }
635     else if (Name == "__.SYMDEF_64 SORTED" || Name == "__.SYMDEF_64") {
636       Format = K_DARWIN64;
637       // We know that the symbol table is not an external file, but we still
638       // must check any Expected<> return value.
639       Expected<StringRef> BufOrErr = C->getBuffer();
640       if (!BufOrErr) {
641         Err = BufOrErr.takeError();
642         return;
643       }
644       SymbolTable = BufOrErr.get();
645       if (Increment())
646         return;
647     }
648     setFirstRegular(*C);
649     return;
650   }
651 
652   // MIPS 64-bit ELF archives use a special format of a symbol table.
653   // This format is marked by `ar_name` field equals to "/SYM64/".
654   // For detailed description see page 96 in the following document:
655   // http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf
656 
657   bool has64SymTable = false;
658   if (Name == "/" || Name == "/SYM64/") {
659     // We know that the symbol table is not an external file, but we still
660     // must check any Expected<> return value.
661     Expected<StringRef> BufOrErr = C->getBuffer();
662     if (!BufOrErr) {
663       Err = BufOrErr.takeError();
664       return;
665     }
666     SymbolTable = BufOrErr.get();
667     if (Name == "/SYM64/")
668       has64SymTable = true;
669 
670     if (Increment())
671       return;
672     if (I == E) {
673       Err = Error::success();
674       return;
675     }
676     Expected<StringRef> NameOrErr = C->getRawName();
677     if (!NameOrErr) {
678       Err = NameOrErr.takeError();
679       return;
680     }
681     Name = NameOrErr.get();
682   }
683 
684   if (Name == "//") {
685     Format = has64SymTable ? K_MIPS64 : K_GNU;
686     // The string table is never an external member, but we still
687     // must check any Expected<> return value.
688     Expected<StringRef> BufOrErr = C->getBuffer();
689     if (!BufOrErr) {
690       Err = BufOrErr.takeError();
691       return;
692     }
693     StringTable = BufOrErr.get();
694     if (Increment())
695       return;
696     setFirstRegular(*C);
697     Err = Error::success();
698     return;
699   }
700 
701   if (Name[0] != '/') {
702     Format = has64SymTable ? K_MIPS64 : K_GNU;
703     setFirstRegular(*C);
704     Err = Error::success();
705     return;
706   }
707 
708   if (Name != "/") {
709     Err = errorCodeToError(object_error::parse_failed);
710     return;
711   }
712 
713   Format = K_COFF;
714   // We know that the symbol table is not an external file, but we still
715   // must check any Expected<> return value.
716   Expected<StringRef> BufOrErr = C->getBuffer();
717   if (!BufOrErr) {
718     Err = BufOrErr.takeError();
719     return;
720   }
721   SymbolTable = BufOrErr.get();
722 
723   if (Increment())
724     return;
725 
726   if (I == E) {
727     setFirstRegular(*C);
728     Err = Error::success();
729     return;
730   }
731 
732   NameOrErr = C->getRawName();
733   if (!NameOrErr) {
734     Err = NameOrErr.takeError();
735     return;
736   }
737   Name = NameOrErr.get();
738 
739   if (Name == "//") {
740     // The string table is never an external member, but we still
741     // must check any Expected<> return value.
742     Expected<StringRef> BufOrErr = C->getBuffer();
743     if (!BufOrErr) {
744       Err = BufOrErr.takeError();
745       return;
746     }
747     StringTable = BufOrErr.get();
748     if (Increment())
749       return;
750   }
751 
752   setFirstRegular(*C);
753   Err = Error::success();
754 }
755 
756 Archive::child_iterator Archive::child_begin(Error &Err,
757                                              bool SkipInternal) const {
758   if (isEmpty())
759     return child_end();
760 
761   if (SkipInternal)
762     return child_iterator(Child(this, FirstRegularData,
763                                 FirstRegularStartOfFile),
764                           &Err);
765 
766   const char *Loc = Data.getBufferStart() + strlen(Magic);
767   Child C(this, Loc, &Err);
768   if (Err)
769     return child_end();
770   return child_iterator(C, &Err);
771 }
772 
773 Archive::child_iterator Archive::child_end() const {
774   return child_iterator(Child(nullptr, nullptr, nullptr), nullptr);
775 }
776 
777 StringRef Archive::Symbol::getName() const {
778   return Parent->getSymbolTable().begin() + StringIndex;
779 }
780 
781 Expected<Archive::Child> Archive::Symbol::getMember() const {
782   const char *Buf = Parent->getSymbolTable().begin();
783   const char *Offsets = Buf;
784   if (Parent->kind() == K_MIPS64 || Parent->kind() == K_DARWIN64)
785     Offsets += sizeof(uint64_t);
786   else
787     Offsets += sizeof(uint32_t);
788   uint32_t Offset = 0;
789   if (Parent->kind() == K_GNU) {
790     Offset = read32be(Offsets + SymbolIndex * 4);
791   } else if (Parent->kind() == K_MIPS64) {
792     Offset = read64be(Offsets + SymbolIndex * 8);
793   } else if (Parent->kind() == K_BSD) {
794     // The SymbolIndex is an index into the ranlib structs that start at
795     // Offsets (the first uint32_t is the number of bytes of the ranlib
796     // structs).  The ranlib structs are a pair of uint32_t's the first
797     // being a string table offset and the second being the offset into
798     // the archive of the member that defines the symbol.  Which is what
799     // is needed here.
800     Offset = read32le(Offsets + SymbolIndex * 8 + 4);
801   } else if (Parent->kind() == K_DARWIN64) {
802     // The SymbolIndex is an index into the ranlib_64 structs that start at
803     // Offsets (the first uint64_t is the number of bytes of the ranlib_64
804     // structs).  The ranlib_64 structs are a pair of uint64_t's the first
805     // being a string table offset and the second being the offset into
806     // the archive of the member that defines the symbol.  Which is what
807     // is needed here.
808     Offset = read64le(Offsets + SymbolIndex * 16 + 8);
809   } else {
810     // Skip offsets.
811     uint32_t MemberCount = read32le(Buf);
812     Buf += MemberCount * 4 + 4;
813 
814     uint32_t SymbolCount = read32le(Buf);
815     if (SymbolIndex >= SymbolCount)
816       return errorCodeToError(object_error::parse_failed);
817 
818     // Skip SymbolCount to get to the indices table.
819     const char *Indices = Buf + 4;
820 
821     // Get the index of the offset in the file member offset table for this
822     // symbol.
823     uint16_t OffsetIndex = read16le(Indices + SymbolIndex * 2);
824     // Subtract 1 since OffsetIndex is 1 based.
825     --OffsetIndex;
826 
827     if (OffsetIndex >= MemberCount)
828       return errorCodeToError(object_error::parse_failed);
829 
830     Offset = read32le(Offsets + OffsetIndex * 4);
831   }
832 
833   const char *Loc = Parent->getData().begin() + Offset;
834   Error Err;
835   Child C(Parent, Loc, &Err);
836   if (Err)
837     return std::move(Err);
838   return C;
839 }
840 
841 Archive::Symbol Archive::Symbol::getNext() const {
842   Symbol t(*this);
843   if (Parent->kind() == K_BSD) {
844     // t.StringIndex is an offset from the start of the __.SYMDEF or
845     // "__.SYMDEF SORTED" member into the string table for the ranlib
846     // struct indexed by t.SymbolIndex .  To change t.StringIndex to the
847     // offset in the string table for t.SymbolIndex+1 we subtract the
848     // its offset from the start of the string table for t.SymbolIndex
849     // and add the offset of the string table for t.SymbolIndex+1.
850 
851     // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t
852     // which is the number of bytes of ranlib structs that follow.  The ranlib
853     // structs are a pair of uint32_t's the first being a string table offset
854     // and the second being the offset into the archive of the member that
855     // define the symbol. After that the next uint32_t is the byte count of
856     // the string table followed by the string table.
857     const char *Buf = Parent->getSymbolTable().begin();
858     uint32_t RanlibCount = 0;
859     RanlibCount = read32le(Buf) / 8;
860     // If t.SymbolIndex + 1 will be past the count of symbols (the RanlibCount)
861     // don't change the t.StringIndex as we don't want to reference a ranlib
862     // past RanlibCount.
863     if (t.SymbolIndex + 1 < RanlibCount) {
864       const char *Ranlibs = Buf + 4;
865       uint32_t CurRanStrx = 0;
866       uint32_t NextRanStrx = 0;
867       CurRanStrx = read32le(Ranlibs + t.SymbolIndex * 8);
868       NextRanStrx = read32le(Ranlibs + (t.SymbolIndex + 1) * 8);
869       t.StringIndex -= CurRanStrx;
870       t.StringIndex += NextRanStrx;
871     }
872   } else {
873     // Go to one past next null.
874     t.StringIndex = Parent->getSymbolTable().find('\0', t.StringIndex) + 1;
875   }
876   ++t.SymbolIndex;
877   return t;
878 }
879 
880 Archive::symbol_iterator Archive::symbol_begin() const {
881   if (!hasSymbolTable())
882     return symbol_iterator(Symbol(this, 0, 0));
883 
884   const char *buf = getSymbolTable().begin();
885   if (kind() == K_GNU) {
886     uint32_t symbol_count = 0;
887     symbol_count = read32be(buf);
888     buf += sizeof(uint32_t) + (symbol_count * (sizeof(uint32_t)));
889   } else if (kind() == K_MIPS64) {
890     uint64_t symbol_count = read64be(buf);
891     buf += sizeof(uint64_t) + (symbol_count * (sizeof(uint64_t)));
892   } else if (kind() == K_BSD) {
893     // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t
894     // which is the number of bytes of ranlib structs that follow.  The ranlib
895     // structs are a pair of uint32_t's the first being a string table offset
896     // and the second being the offset into the archive of the member that
897     // define the symbol. After that the next uint32_t is the byte count of
898     // the string table followed by the string table.
899     uint32_t ranlib_count = 0;
900     ranlib_count = read32le(buf) / 8;
901     const char *ranlibs = buf + 4;
902     uint32_t ran_strx = 0;
903     ran_strx = read32le(ranlibs);
904     buf += sizeof(uint32_t) + (ranlib_count * (2 * (sizeof(uint32_t))));
905     // Skip the byte count of the string table.
906     buf += sizeof(uint32_t);
907     buf += ran_strx;
908   } else if (kind() == K_DARWIN64) {
909     // The __.SYMDEF_64 or "__.SYMDEF_64 SORTED" member starts with a uint64_t
910     // which is the number of bytes of ranlib_64 structs that follow.  The
911     // ranlib_64 structs are a pair of uint64_t's the first being a string
912     // table offset and the second being the offset into the archive of the
913     // member that define the symbol. After that the next uint64_t is the byte
914     // count of the string table followed by the string table.
915     uint64_t ranlib_count = 0;
916     ranlib_count = read64le(buf) / 16;
917     const char *ranlibs = buf + 8;
918     uint64_t ran_strx = 0;
919     ran_strx = read64le(ranlibs);
920     buf += sizeof(uint64_t) + (ranlib_count * (2 * (sizeof(uint64_t))));
921     // Skip the byte count of the string table.
922     buf += sizeof(uint64_t);
923     buf += ran_strx;
924   } else {
925     uint32_t member_count = 0;
926     uint32_t symbol_count = 0;
927     member_count = read32le(buf);
928     buf += 4 + (member_count * 4); // Skip offsets.
929     symbol_count = read32le(buf);
930     buf += 4 + (symbol_count * 2); // Skip indices.
931   }
932   uint32_t string_start_offset = buf - getSymbolTable().begin();
933   return symbol_iterator(Symbol(this, 0, string_start_offset));
934 }
935 
936 Archive::symbol_iterator Archive::symbol_end() const {
937   return symbol_iterator(Symbol(this, getNumberOfSymbols(), 0));
938 }
939 
940 uint32_t Archive::getNumberOfSymbols() const {
941   if (!hasSymbolTable())
942     return 0;
943   const char *buf = getSymbolTable().begin();
944   if (kind() == K_GNU)
945     return read32be(buf);
946   if (kind() == K_MIPS64)
947     return read64be(buf);
948   if (kind() == K_BSD)
949     return read32le(buf) / 8;
950   if (kind() == K_DARWIN64)
951     return read64le(buf) / 16;
952   uint32_t member_count = 0;
953   member_count = read32le(buf);
954   buf += 4 + (member_count * 4); // Skip offsets.
955   return read32le(buf);
956 }
957 
958 Expected<Optional<Archive::Child>> Archive::findSym(StringRef name) const {
959   Archive::symbol_iterator bs = symbol_begin();
960   Archive::symbol_iterator es = symbol_end();
961 
962   for (; bs != es; ++bs) {
963     StringRef SymName = bs->getName();
964     if (SymName == name) {
965       if (auto MemberOrErr = bs->getMember())
966         return Child(*MemberOrErr);
967       else
968         return MemberOrErr.takeError();
969     }
970   }
971   return Optional<Child>();
972 }
973 
974 // Returns true if archive file contains no member file.
975 bool Archive::isEmpty() const { return Data.getBufferSize() == 8; }
976 
977 bool Archive::hasSymbolTable() const { return !SymbolTable.empty(); }
978