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