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