xref: /freebsd/contrib/llvm-project/llvm/lib/Object/Archive.cpp (revision 9f23cbd6cae82fd77edfad7173432fa8dccd0a95)
1 //===- Archive.cpp - ar File Format implementation ------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the ArchiveObjectFile class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/Object/Archive.h"
14 #include "llvm/ADT/SmallString.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/ADT/Twine.h"
17 #include "llvm/Object/Binary.h"
18 #include "llvm/Object/Error.h"
19 #include "llvm/Support/Chrono.h"
20 #include "llvm/Support/Endian.h"
21 #include "llvm/Support/Error.h"
22 #include "llvm/Support/ErrorOr.h"
23 #include "llvm/Support/FileSystem.h"
24 #include "llvm/Support/Host.h"
25 #include "llvm/Support/MathExtras.h"
26 #include "llvm/Support/MemoryBuffer.h"
27 #include "llvm/Support/Path.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include <algorithm>
30 #include <cassert>
31 #include <cstddef>
32 #include <cstdint>
33 #include <memory>
34 #include <string>
35 #include <system_error>
36 
37 using namespace llvm;
38 using namespace object;
39 using namespace llvm::support::endian;
40 
41 void Archive::anchor() {}
42 
43 static Error malformedError(Twine Msg) {
44   std::string StringMsg = "truncated or malformed archive (" + Msg.str() + ")";
45   return make_error<GenericBinaryError>(std::move(StringMsg),
46                                         object_error::parse_failed);
47 }
48 
49 static Error
50 createMemberHeaderParseError(const AbstractArchiveMemberHeader *ArMemHeader,
51                              const char *RawHeaderPtr, uint64_t Size) {
52   StringRef Msg("remaining size of archive too small for next archive "
53                 "member header ");
54 
55   Expected<StringRef> NameOrErr = ArMemHeader->getName(Size);
56   if (NameOrErr)
57     return malformedError(Msg + "for " + *NameOrErr);
58 
59   consumeError(NameOrErr.takeError());
60   uint64_t Offset = RawHeaderPtr - ArMemHeader->Parent->getData().data();
61   return malformedError(Msg + "at offset " + Twine(Offset));
62 }
63 
64 template <class T, std::size_t N>
65 StringRef getFieldRawString(const T (&Field)[N]) {
66   return StringRef(Field, N).rtrim(" ");
67 }
68 
69 template <class T>
70 StringRef CommonArchiveMemberHeader<T>::getRawAccessMode() const {
71   return getFieldRawString(ArMemHdr->AccessMode);
72 }
73 
74 template <class T>
75 StringRef CommonArchiveMemberHeader<T>::getRawLastModified() const {
76   return getFieldRawString(ArMemHdr->LastModified);
77 }
78 
79 template <class T> StringRef CommonArchiveMemberHeader<T>::getRawUID() const {
80   return getFieldRawString(ArMemHdr->UID);
81 }
82 
83 template <class T> StringRef CommonArchiveMemberHeader<T>::getRawGID() const {
84   return getFieldRawString(ArMemHdr->GID);
85 }
86 
87 template <class T> uint64_t CommonArchiveMemberHeader<T>::getOffset() const {
88   return reinterpret_cast<const char *>(ArMemHdr) - Parent->getData().data();
89 }
90 
91 template class object::CommonArchiveMemberHeader<UnixArMemHdrType>;
92 template class object::CommonArchiveMemberHeader<BigArMemHdrType>;
93 
94 ArchiveMemberHeader::ArchiveMemberHeader(const Archive *Parent,
95                                          const char *RawHeaderPtr,
96                                          uint64_t Size, Error *Err)
97     : CommonArchiveMemberHeader<UnixArMemHdrType>(
98           Parent, reinterpret_cast<const UnixArMemHdrType *>(RawHeaderPtr)) {
99   if (RawHeaderPtr == nullptr)
100     return;
101   ErrorAsOutParameter ErrAsOutParam(Err);
102 
103   if (Size < getSizeOf()) {
104     *Err = createMemberHeaderParseError(this, RawHeaderPtr, Size);
105     return;
106   }
107   if (ArMemHdr->Terminator[0] != '`' || ArMemHdr->Terminator[1] != '\n') {
108     if (Err) {
109       std::string Buf;
110       raw_string_ostream OS(Buf);
111       OS.write_escaped(
112           StringRef(ArMemHdr->Terminator, sizeof(ArMemHdr->Terminator)));
113       OS.flush();
114       std::string Msg("terminator characters in archive member \"" + Buf +
115                       "\" not the correct \"`\\n\" values for the archive "
116                       "member header ");
117       Expected<StringRef> NameOrErr = getName(Size);
118       if (!NameOrErr) {
119         consumeError(NameOrErr.takeError());
120         uint64_t Offset = RawHeaderPtr - Parent->getData().data();
121         *Err = malformedError(Msg + "at offset " + Twine(Offset));
122       } else
123         *Err = malformedError(Msg + "for " + NameOrErr.get());
124     }
125     return;
126   }
127 }
128 
129 BigArchiveMemberHeader::BigArchiveMemberHeader(const Archive *Parent,
130                                                const char *RawHeaderPtr,
131                                                uint64_t Size, Error *Err)
132     : CommonArchiveMemberHeader<BigArMemHdrType>(
133           Parent, reinterpret_cast<const BigArMemHdrType *>(RawHeaderPtr)) {
134   if (RawHeaderPtr == nullptr)
135     return;
136   ErrorAsOutParameter ErrAsOutParam(Err);
137 
138   if (Size < getSizeOf()) {
139     Error SubErr = createMemberHeaderParseError(this, RawHeaderPtr, Size);
140     if (Err)
141       *Err = std::move(SubErr);
142   }
143 }
144 
145 // This gets the raw name from the ArMemHdr->Name field and checks that it is
146 // valid for the kind of archive.  If it is not valid it returns an Error.
147 Expected<StringRef> ArchiveMemberHeader::getRawName() const {
148   char EndCond;
149   auto Kind = Parent->kind();
150   if (Kind == Archive::K_BSD || Kind == Archive::K_DARWIN64) {
151     if (ArMemHdr->Name[0] == ' ') {
152       uint64_t Offset =
153           reinterpret_cast<const char *>(ArMemHdr) - Parent->getData().data();
154       return malformedError("name contains a leading space for archive member "
155                             "header at offset " +
156                             Twine(Offset));
157     }
158     EndCond = ' ';
159   } else if (ArMemHdr->Name[0] == '/' || ArMemHdr->Name[0] == '#')
160     EndCond = ' ';
161   else
162     EndCond = '/';
163   StringRef::size_type end =
164       StringRef(ArMemHdr->Name, sizeof(ArMemHdr->Name)).find(EndCond);
165   if (end == StringRef::npos)
166     end = sizeof(ArMemHdr->Name);
167   assert(end <= sizeof(ArMemHdr->Name) && end > 0);
168   // Don't include the EndCond if there is one.
169   return StringRef(ArMemHdr->Name, end);
170 }
171 
172 Expected<uint64_t>
173 getArchiveMemberDecField(Twine FieldName, const StringRef RawField,
174                          const Archive *Parent,
175                          const AbstractArchiveMemberHeader *MemHeader) {
176   uint64_t Value;
177   if (RawField.getAsInteger(10, Value)) {
178     uint64_t Offset = MemHeader->getOffset();
179     return malformedError("characters in " + FieldName +
180                           " field in archive member header are not "
181                           "all decimal numbers: '" +
182                           RawField +
183                           "' for the archive "
184                           "member header at offset " +
185                           Twine(Offset));
186   }
187   return Value;
188 }
189 
190 Expected<uint64_t>
191 getArchiveMemberOctField(Twine FieldName, const StringRef RawField,
192                          const Archive *Parent,
193                          const AbstractArchiveMemberHeader *MemHeader) {
194   uint64_t Value;
195   if (RawField.getAsInteger(8, Value)) {
196     uint64_t Offset = MemHeader->getOffset();
197     return malformedError("characters in " + FieldName +
198                           " field in archive member header are not "
199                           "all octal numbers: '" +
200                           RawField +
201                           "' for the archive "
202                           "member header at offset " +
203                           Twine(Offset));
204   }
205   return Value;
206 }
207 
208 Expected<StringRef> BigArchiveMemberHeader::getRawName() const {
209   Expected<uint64_t> NameLenOrErr = getArchiveMemberDecField(
210       "NameLen", getFieldRawString(ArMemHdr->NameLen), Parent, this);
211   if (!NameLenOrErr)
212     // TODO: Out-of-line.
213     return NameLenOrErr.takeError();
214   uint64_t NameLen = NameLenOrErr.get();
215 
216   // If the name length is odd, pad with '\0' to get an even length. After
217   // padding, there is the name terminator "`\n".
218   uint64_t NameLenWithPadding = alignTo(NameLen, 2);
219   StringRef NameTerminator = "`\n";
220   StringRef NameStringWithNameTerminator =
221       StringRef(ArMemHdr->Name, NameLenWithPadding + NameTerminator.size());
222   if (!NameStringWithNameTerminator.endswith(NameTerminator)) {
223     uint64_t Offset =
224         reinterpret_cast<const char *>(ArMemHdr->Name + NameLenWithPadding) -
225         Parent->getData().data();
226     // TODO: Out-of-line.
227     return malformedError(
228         "name does not have name terminator \"`\\n\" for archive member"
229         "header at offset " +
230         Twine(Offset));
231   }
232   return StringRef(ArMemHdr->Name, NameLen);
233 }
234 
235 // member including the header, so the size of any name following the header
236 // is checked to make sure it does not overflow.
237 Expected<StringRef> ArchiveMemberHeader::getName(uint64_t Size) const {
238 
239   // This can be called from the ArchiveMemberHeader constructor when the
240   // archive header is truncated to produce an error message with the name.
241   // Make sure the name field is not truncated.
242   if (Size < offsetof(UnixArMemHdrType, Name) + sizeof(ArMemHdr->Name)) {
243     uint64_t ArchiveOffset =
244         reinterpret_cast<const char *>(ArMemHdr) - Parent->getData().data();
245     return malformedError("archive header truncated before the name field "
246                           "for archive member header at offset " +
247                           Twine(ArchiveOffset));
248   }
249 
250   // The raw name itself can be invalid.
251   Expected<StringRef> NameOrErr = getRawName();
252   if (!NameOrErr)
253     return NameOrErr.takeError();
254   StringRef Name = NameOrErr.get();
255 
256   // Check if it's a special name.
257   if (Name[0] == '/') {
258     if (Name.size() == 1) // Linker member.
259       return Name;
260     if (Name.size() == 2 && Name[1] == '/') // String table.
261       return Name;
262     // System libraries from the Windows SDK for Windows 11 contain this symbol.
263     // It looks like a CFG guard: we just skip it for now.
264     if (Name.equals("/<XFGHASHMAP>/"))
265       return Name;
266     // Some libraries (e.g., arm64rt.lib) from the Windows WDK
267     // (version 10.0.22000.0) contain this undocumented special member.
268     if (Name.equals("/<ECSYMBOLS>/"))
269       return Name;
270     // It's a long name.
271     // Get the string table offset.
272     std::size_t StringOffset;
273     if (Name.substr(1).rtrim(' ').getAsInteger(10, StringOffset)) {
274       std::string Buf;
275       raw_string_ostream OS(Buf);
276       OS.write_escaped(Name.substr(1).rtrim(' '));
277       OS.flush();
278       uint64_t ArchiveOffset =
279           reinterpret_cast<const char *>(ArMemHdr) - Parent->getData().data();
280       return malformedError("long name offset characters after the '/' are "
281                             "not all decimal numbers: '" +
282                             Buf + "' for archive member header at offset " +
283                             Twine(ArchiveOffset));
284     }
285 
286     // Verify it.
287     if (StringOffset >= Parent->getStringTable().size()) {
288       uint64_t ArchiveOffset =
289           reinterpret_cast<const char *>(ArMemHdr) - Parent->getData().data();
290       return malformedError("long name offset " + Twine(StringOffset) +
291                             " past the end of the string table for archive "
292                             "member header at offset " +
293                             Twine(ArchiveOffset));
294     }
295 
296     // GNU long file names end with a "/\n".
297     if (Parent->kind() == Archive::K_GNU ||
298         Parent->kind() == Archive::K_GNU64) {
299       size_t End = Parent->getStringTable().find('\n', /*From=*/StringOffset);
300       if (End == StringRef::npos || End < 1 ||
301           Parent->getStringTable()[End - 1] != '/') {
302         return malformedError("string table at long name offset " +
303                               Twine(StringOffset) + "not terminated");
304       }
305       return Parent->getStringTable().slice(StringOffset, End - 1);
306     }
307     return Parent->getStringTable().begin() + StringOffset;
308   }
309 
310   if (Name.startswith("#1/")) {
311     uint64_t NameLength;
312     if (Name.substr(3).rtrim(' ').getAsInteger(10, NameLength)) {
313       std::string Buf;
314       raw_string_ostream OS(Buf);
315       OS.write_escaped(Name.substr(3).rtrim(' '));
316       OS.flush();
317       uint64_t ArchiveOffset =
318           reinterpret_cast<const char *>(ArMemHdr) - Parent->getData().data();
319       return malformedError("long name length characters after the #1/ are "
320                             "not all decimal numbers: '" +
321                             Buf + "' for archive member header at offset " +
322                             Twine(ArchiveOffset));
323     }
324     if (getSizeOf() + NameLength > Size) {
325       uint64_t ArchiveOffset =
326           reinterpret_cast<const char *>(ArMemHdr) - Parent->getData().data();
327       return malformedError("long name length: " + Twine(NameLength) +
328                             " extends past the end of the member or archive "
329                             "for archive member header at offset " +
330                             Twine(ArchiveOffset));
331     }
332     return StringRef(reinterpret_cast<const char *>(ArMemHdr) + getSizeOf(),
333                      NameLength)
334         .rtrim('\0');
335   }
336 
337   // It is not a long name so trim the blanks at the end of the name.
338   if (Name[Name.size() - 1] != '/')
339     return Name.rtrim(' ');
340 
341   // It's a simple name.
342   return Name.drop_back(1);
343 }
344 
345 Expected<StringRef> BigArchiveMemberHeader::getName(uint64_t Size) const {
346   return getRawName();
347 }
348 
349 Expected<uint64_t> ArchiveMemberHeader::getSize() const {
350   return getArchiveMemberDecField("size", getFieldRawString(ArMemHdr->Size),
351                                   Parent, this);
352 }
353 
354 Expected<uint64_t> BigArchiveMemberHeader::getSize() const {
355   Expected<uint64_t> SizeOrErr = getArchiveMemberDecField(
356       "size", getFieldRawString(ArMemHdr->Size), Parent, this);
357   if (!SizeOrErr)
358     return SizeOrErr.takeError();
359 
360   Expected<uint64_t> NameLenOrErr = getRawNameSize();
361   if (!NameLenOrErr)
362     return NameLenOrErr.takeError();
363 
364   return *SizeOrErr + alignTo(*NameLenOrErr, 2);
365 }
366 
367 Expected<uint64_t> BigArchiveMemberHeader::getRawNameSize() const {
368   return getArchiveMemberDecField(
369       "NameLen", getFieldRawString(ArMemHdr->NameLen), Parent, this);
370 }
371 
372 Expected<uint64_t> BigArchiveMemberHeader::getNextOffset() const {
373   return getArchiveMemberDecField(
374       "NextOffset", getFieldRawString(ArMemHdr->NextOffset), Parent, this);
375 }
376 
377 Expected<sys::fs::perms> AbstractArchiveMemberHeader::getAccessMode() const {
378   Expected<uint64_t> AccessModeOrErr =
379       getArchiveMemberOctField("AccessMode", getRawAccessMode(), Parent, this);
380   if (!AccessModeOrErr)
381     return AccessModeOrErr.takeError();
382   return static_cast<sys::fs::perms>(*AccessModeOrErr);
383 }
384 
385 Expected<sys::TimePoint<std::chrono::seconds>>
386 AbstractArchiveMemberHeader::getLastModified() const {
387   Expected<uint64_t> SecondsOrErr = getArchiveMemberDecField(
388       "LastModified", getRawLastModified(), Parent, this);
389 
390   if (!SecondsOrErr)
391     return SecondsOrErr.takeError();
392 
393   return sys::toTimePoint(*SecondsOrErr);
394 }
395 
396 Expected<unsigned> AbstractArchiveMemberHeader::getUID() const {
397   StringRef User = getRawUID();
398   if (User.empty())
399     return 0;
400   return getArchiveMemberDecField("UID", User, Parent, this);
401 }
402 
403 Expected<unsigned> AbstractArchiveMemberHeader::getGID() const {
404   StringRef Group = getRawGID();
405   if (Group.empty())
406     return 0;
407   return getArchiveMemberDecField("GID", Group, Parent, this);
408 }
409 
410 Expected<bool> ArchiveMemberHeader::isThin() const {
411   Expected<StringRef> NameOrErr = getRawName();
412   if (!NameOrErr)
413     return NameOrErr.takeError();
414   StringRef Name = NameOrErr.get();
415   return Parent->isThin() && Name != "/" && Name != "//" && Name != "/SYM64/";
416 }
417 
418 Expected<const char *> ArchiveMemberHeader::getNextChildLoc() const {
419   uint64_t Size = getSizeOf();
420   Expected<bool> isThinOrErr = isThin();
421   if (!isThinOrErr)
422     return isThinOrErr.takeError();
423 
424   bool isThin = isThinOrErr.get();
425   if (!isThin) {
426     Expected<uint64_t> MemberSize = getSize();
427     if (!MemberSize)
428       return MemberSize.takeError();
429 
430     Size += MemberSize.get();
431   }
432 
433   // If Size is odd, add 1 to make it even.
434   const char *NextLoc =
435       reinterpret_cast<const char *>(ArMemHdr) + alignTo(Size, 2);
436 
437   if (NextLoc == Parent->getMemoryBufferRef().getBufferEnd())
438     return nullptr;
439 
440   return NextLoc;
441 }
442 
443 Expected<const char *> BigArchiveMemberHeader::getNextChildLoc() const {
444   if (getOffset() ==
445       static_cast<const BigArchive *>(Parent)->getLastChildOffset())
446     return nullptr;
447 
448   Expected<uint64_t> NextOffsetOrErr = getNextOffset();
449   if (!NextOffsetOrErr)
450     return NextOffsetOrErr.takeError();
451   return Parent->getData().data() + NextOffsetOrErr.get();
452 }
453 
454 Archive::Child::Child(const Archive *Parent, StringRef Data,
455                       uint16_t StartOfFile)
456     : Parent(Parent), Data(Data), StartOfFile(StartOfFile) {
457   Header = Parent->createArchiveMemberHeader(Data.data(), Data.size(), nullptr);
458 }
459 
460 Archive::Child::Child(const Archive *Parent, const char *Start, Error *Err)
461     : Parent(Parent) {
462   if (!Start) {
463     Header = nullptr;
464     return;
465   }
466 
467   Header = Parent->createArchiveMemberHeader(
468       Start,
469       Parent ? Parent->getData().size() - (Start - Parent->getData().data())
470              : 0,
471       Err);
472 
473   // If we are pointed to real data, Start is not a nullptr, then there must be
474   // a non-null Err pointer available to report malformed data on.  Only in
475   // the case sentinel value is being constructed is Err is permitted to be a
476   // nullptr.
477   assert(Err && "Err can't be nullptr if Start is not a nullptr");
478 
479   ErrorAsOutParameter ErrAsOutParam(Err);
480 
481   // If there was an error in the construction of the Header
482   // then just return with the error now set.
483   if (*Err)
484     return;
485 
486   uint64_t Size = Header->getSizeOf();
487   Data = StringRef(Start, Size);
488   Expected<bool> isThinOrErr = isThinMember();
489   if (!isThinOrErr) {
490     *Err = isThinOrErr.takeError();
491     return;
492   }
493   bool isThin = isThinOrErr.get();
494   if (!isThin) {
495     Expected<uint64_t> MemberSize = getRawSize();
496     if (!MemberSize) {
497       *Err = MemberSize.takeError();
498       return;
499     }
500     Size += MemberSize.get();
501     Data = StringRef(Start, Size);
502   }
503 
504   // Setup StartOfFile and PaddingBytes.
505   StartOfFile = Header->getSizeOf();
506   // Don't include attached name.
507   Expected<StringRef> NameOrErr = getRawName();
508   if (!NameOrErr) {
509     *Err = NameOrErr.takeError();
510     return;
511   }
512   StringRef Name = NameOrErr.get();
513 
514   if (Parent->kind() == Archive::K_AIXBIG) {
515     // The actual start of the file is after the name and any necessary
516     // even-alignment padding.
517     StartOfFile += ((Name.size() + 1) >> 1) << 1;
518   } else if (Name.startswith("#1/")) {
519     uint64_t NameSize;
520     StringRef RawNameSize = Name.substr(3).rtrim(' ');
521     if (RawNameSize.getAsInteger(10, NameSize)) {
522       uint64_t Offset = Start - Parent->getData().data();
523       *Err = malformedError("long name length characters after the #1/ are "
524                             "not all decimal numbers: '" +
525                             RawNameSize +
526                             "' for archive member header at offset " +
527                             Twine(Offset));
528       return;
529     }
530     StartOfFile += NameSize;
531   }
532 }
533 
534 Expected<uint64_t> Archive::Child::getSize() const {
535   if (Parent->IsThin)
536     return Header->getSize();
537   return Data.size() - StartOfFile;
538 }
539 
540 Expected<uint64_t> Archive::Child::getRawSize() const {
541   return Header->getSize();
542 }
543 
544 Expected<bool> Archive::Child::isThinMember() const { return Header->isThin(); }
545 
546 Expected<std::string> Archive::Child::getFullName() const {
547   Expected<bool> isThin = isThinMember();
548   if (!isThin)
549     return isThin.takeError();
550   assert(isThin.get());
551   Expected<StringRef> NameOrErr = getName();
552   if (!NameOrErr)
553     return NameOrErr.takeError();
554   StringRef Name = *NameOrErr;
555   if (sys::path::is_absolute(Name))
556     return std::string(Name);
557 
558   SmallString<128> FullName = sys::path::parent_path(
559       Parent->getMemoryBufferRef().getBufferIdentifier());
560   sys::path::append(FullName, Name);
561   return std::string(FullName.str());
562 }
563 
564 Expected<StringRef> Archive::Child::getBuffer() const {
565   Expected<bool> isThinOrErr = isThinMember();
566   if (!isThinOrErr)
567     return isThinOrErr.takeError();
568   bool isThin = isThinOrErr.get();
569   if (!isThin) {
570     Expected<uint64_t> Size = getSize();
571     if (!Size)
572       return Size.takeError();
573     return StringRef(Data.data() + StartOfFile, Size.get());
574   }
575   Expected<std::string> FullNameOrErr = getFullName();
576   if (!FullNameOrErr)
577     return FullNameOrErr.takeError();
578   const std::string &FullName = *FullNameOrErr;
579   ErrorOr<std::unique_ptr<MemoryBuffer>> Buf = MemoryBuffer::getFile(FullName);
580   if (std::error_code EC = Buf.getError())
581     return errorCodeToError(EC);
582   Parent->ThinBuffers.push_back(std::move(*Buf));
583   return Parent->ThinBuffers.back()->getBuffer();
584 }
585 
586 Expected<Archive::Child> Archive::Child::getNext() const {
587   Expected<const char *> NextLocOrErr = Header->getNextChildLoc();
588   if (!NextLocOrErr)
589     return NextLocOrErr.takeError();
590 
591   const char *NextLoc = *NextLocOrErr;
592 
593   // Check to see if this is at the end of the archive.
594   if (NextLoc == nullptr)
595     return Child(nullptr, nullptr, nullptr);
596 
597   // Check to see if this is past the end of the archive.
598   if (NextLoc > Parent->Data.getBufferEnd()) {
599     std::string Msg("offset to next archive member past the end of the archive "
600                     "after member ");
601     Expected<StringRef> NameOrErr = getName();
602     if (!NameOrErr) {
603       consumeError(NameOrErr.takeError());
604       uint64_t Offset = Data.data() - Parent->getData().data();
605       return malformedError(Msg + "at offset " + Twine(Offset));
606     } else
607       return malformedError(Msg + NameOrErr.get());
608   }
609 
610   Error Err = Error::success();
611   Child Ret(Parent, NextLoc, &Err);
612   if (Err)
613     return std::move(Err);
614   return Ret;
615 }
616 
617 uint64_t Archive::Child::getChildOffset() const {
618   const char *a = Parent->Data.getBuffer().data();
619   const char *c = Data.data();
620   uint64_t offset = c - a;
621   return offset;
622 }
623 
624 Expected<StringRef> Archive::Child::getName() const {
625   Expected<uint64_t> RawSizeOrErr = getRawSize();
626   if (!RawSizeOrErr)
627     return RawSizeOrErr.takeError();
628   uint64_t RawSize = RawSizeOrErr.get();
629   Expected<StringRef> NameOrErr =
630       Header->getName(Header->getSizeOf() + RawSize);
631   if (!NameOrErr)
632     return NameOrErr.takeError();
633   StringRef Name = NameOrErr.get();
634   return Name;
635 }
636 
637 Expected<MemoryBufferRef> Archive::Child::getMemoryBufferRef() const {
638   Expected<StringRef> NameOrErr = getName();
639   if (!NameOrErr)
640     return NameOrErr.takeError();
641   StringRef Name = NameOrErr.get();
642   Expected<StringRef> Buf = getBuffer();
643   if (!Buf)
644     return createFileError(Name, Buf.takeError());
645   return MemoryBufferRef(*Buf, Name);
646 }
647 
648 Expected<std::unique_ptr<Binary>>
649 Archive::Child::getAsBinary(LLVMContext *Context) const {
650   Expected<MemoryBufferRef> BuffOrErr = getMemoryBufferRef();
651   if (!BuffOrErr)
652     return BuffOrErr.takeError();
653 
654   auto BinaryOrErr = createBinary(BuffOrErr.get(), Context);
655   if (BinaryOrErr)
656     return std::move(*BinaryOrErr);
657   return BinaryOrErr.takeError();
658 }
659 
660 Expected<std::unique_ptr<Archive>> Archive::create(MemoryBufferRef Source) {
661   Error Err = Error::success();
662   std::unique_ptr<Archive> Ret;
663   StringRef Buffer = Source.getBuffer();
664 
665   if (Buffer.startswith(BigArchiveMagic))
666     Ret = std::make_unique<BigArchive>(Source, Err);
667   else
668     Ret = std::make_unique<Archive>(Source, Err);
669 
670   if (Err)
671     return std::move(Err);
672   return std::move(Ret);
673 }
674 
675 std::unique_ptr<AbstractArchiveMemberHeader>
676 Archive::createArchiveMemberHeader(const char *RawHeaderPtr, uint64_t Size,
677                                    Error *Err) const {
678   ErrorAsOutParameter ErrAsOutParam(Err);
679   if (kind() != K_AIXBIG)
680     return std::make_unique<ArchiveMemberHeader>(this, RawHeaderPtr, Size, Err);
681   return std::make_unique<BigArchiveMemberHeader>(this, RawHeaderPtr, Size,
682                                                   Err);
683 }
684 
685 uint64_t Archive::getArchiveMagicLen() const {
686   if (isThin())
687     return sizeof(ThinArchiveMagic) - 1;
688 
689   if (Kind() == K_AIXBIG)
690     return sizeof(BigArchiveMagic) - 1;
691 
692   return sizeof(ArchiveMagic) - 1;
693 }
694 
695 void Archive::setFirstRegular(const Child &C) {
696   FirstRegularData = C.Data;
697   FirstRegularStartOfFile = C.StartOfFile;
698 }
699 
700 Archive::Archive(MemoryBufferRef Source, Error &Err)
701     : Binary(Binary::ID_Archive, Source) {
702   ErrorAsOutParameter ErrAsOutParam(&Err);
703   StringRef Buffer = Data.getBuffer();
704   // Check for sufficient magic.
705   if (Buffer.startswith(ThinArchiveMagic)) {
706     IsThin = true;
707   } else if (Buffer.startswith(ArchiveMagic)) {
708     IsThin = false;
709   } else if (Buffer.startswith(BigArchiveMagic)) {
710     Format = K_AIXBIG;
711     IsThin = false;
712     return;
713   } else {
714     Err = make_error<GenericBinaryError>("file too small to be an archive",
715                                          object_error::invalid_file_type);
716     return;
717   }
718 
719   // Make sure Format is initialized before any call to
720   // ArchiveMemberHeader::getName() is made.  This could be a valid empty
721   // archive which is the same in all formats.  So claiming it to be gnu to is
722   // fine if not totally correct before we look for a string table or table of
723   // contents.
724   Format = K_GNU;
725 
726   // Get the special members.
727   child_iterator I = child_begin(Err, false);
728   if (Err)
729     return;
730   child_iterator E = child_end();
731 
732   // See if this is a valid empty archive and if so return.
733   if (I == E) {
734     Err = Error::success();
735     return;
736   }
737   const Child *C = &*I;
738 
739   auto Increment = [&]() {
740     ++I;
741     if (Err)
742       return true;
743     C = &*I;
744     return false;
745   };
746 
747   Expected<StringRef> NameOrErr = C->getRawName();
748   if (!NameOrErr) {
749     Err = NameOrErr.takeError();
750     return;
751   }
752   StringRef Name = NameOrErr.get();
753 
754   // Below is the pattern that is used to figure out the archive format
755   // GNU archive format
756   //  First member : / (may exist, if it exists, points to the symbol table )
757   //  Second member : // (may exist, if it exists, points to the string table)
758   //  Note : The string table is used if the filename exceeds 15 characters
759   // BSD archive format
760   //  First member : __.SYMDEF or "__.SYMDEF SORTED" (the symbol table)
761   //  There is no string table, if the filename exceeds 15 characters or has a
762   //  embedded space, the filename has #1/<size>, The size represents the size
763   //  of the filename that needs to be read after the archive header
764   // COFF archive format
765   //  First member : /
766   //  Second member : / (provides a directory of symbols)
767   //  Third member : // (may exist, if it exists, contains the string table)
768   //  Note: Microsoft PE/COFF Spec 8.3 says that the third member is present
769   //  even if the string table is empty. However, lib.exe does not in fact
770   //  seem to create the third member if there's no member whose filename
771   //  exceeds 15 characters. So the third member is optional.
772 
773   if (Name == "__.SYMDEF" || Name == "__.SYMDEF_64") {
774     if (Name == "__.SYMDEF")
775       Format = K_BSD;
776     else // Name == "__.SYMDEF_64"
777       Format = K_DARWIN64;
778     // We know that the symbol table is not an external file, but we still must
779     // check any Expected<> return value.
780     Expected<StringRef> BufOrErr = C->getBuffer();
781     if (!BufOrErr) {
782       Err = BufOrErr.takeError();
783       return;
784     }
785     SymbolTable = BufOrErr.get();
786     if (Increment())
787       return;
788     setFirstRegular(*C);
789 
790     Err = Error::success();
791     return;
792   }
793 
794   if (Name.startswith("#1/")) {
795     Format = K_BSD;
796     // We know this is BSD, so getName will work since there is no string table.
797     Expected<StringRef> NameOrErr = C->getName();
798     if (!NameOrErr) {
799       Err = NameOrErr.takeError();
800       return;
801     }
802     Name = NameOrErr.get();
803     if (Name == "__.SYMDEF SORTED" || Name == "__.SYMDEF") {
804       // We know that the symbol table is not an external file, but we still
805       // must check any Expected<> return value.
806       Expected<StringRef> BufOrErr = C->getBuffer();
807       if (!BufOrErr) {
808         Err = BufOrErr.takeError();
809         return;
810       }
811       SymbolTable = BufOrErr.get();
812       if (Increment())
813         return;
814     } else if (Name == "__.SYMDEF_64 SORTED" || Name == "__.SYMDEF_64") {
815       Format = K_DARWIN64;
816       // We know that the symbol table is not an external file, but we still
817       // must check any Expected<> return value.
818       Expected<StringRef> BufOrErr = C->getBuffer();
819       if (!BufOrErr) {
820         Err = BufOrErr.takeError();
821         return;
822       }
823       SymbolTable = BufOrErr.get();
824       if (Increment())
825         return;
826     }
827     setFirstRegular(*C);
828     return;
829   }
830 
831   // MIPS 64-bit ELF archives use a special format of a symbol table.
832   // This format is marked by `ar_name` field equals to "/SYM64/".
833   // For detailed description see page 96 in the following document:
834   // http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf
835 
836   bool has64SymTable = false;
837   if (Name == "/" || Name == "/SYM64/") {
838     // We know that the symbol table is not an external file, but we still
839     // must check any Expected<> return value.
840     Expected<StringRef> BufOrErr = C->getBuffer();
841     if (!BufOrErr) {
842       Err = BufOrErr.takeError();
843       return;
844     }
845     SymbolTable = BufOrErr.get();
846     if (Name == "/SYM64/")
847       has64SymTable = true;
848 
849     if (Increment())
850       return;
851     if (I == E) {
852       Err = Error::success();
853       return;
854     }
855     Expected<StringRef> NameOrErr = C->getRawName();
856     if (!NameOrErr) {
857       Err = NameOrErr.takeError();
858       return;
859     }
860     Name = NameOrErr.get();
861   }
862 
863   if (Name == "//") {
864     Format = has64SymTable ? K_GNU64 : K_GNU;
865     // The string table is never an external member, but we still
866     // must check any Expected<> return value.
867     Expected<StringRef> BufOrErr = C->getBuffer();
868     if (!BufOrErr) {
869       Err = BufOrErr.takeError();
870       return;
871     }
872     StringTable = BufOrErr.get();
873     if (Increment())
874       return;
875     setFirstRegular(*C);
876     Err = Error::success();
877     return;
878   }
879 
880   if (Name[0] != '/') {
881     Format = has64SymTable ? K_GNU64 : K_GNU;
882     setFirstRegular(*C);
883     Err = Error::success();
884     return;
885   }
886 
887   if (Name != "/") {
888     Err = errorCodeToError(object_error::parse_failed);
889     return;
890   }
891 
892   Format = K_COFF;
893   // We know that the symbol table is not an external file, but we still
894   // must check any Expected<> return value.
895   Expected<StringRef> BufOrErr = C->getBuffer();
896   if (!BufOrErr) {
897     Err = BufOrErr.takeError();
898     return;
899   }
900   SymbolTable = BufOrErr.get();
901 
902   if (Increment())
903     return;
904 
905   if (I == E) {
906     setFirstRegular(*C);
907     Err = Error::success();
908     return;
909   }
910 
911   NameOrErr = C->getRawName();
912   if (!NameOrErr) {
913     Err = NameOrErr.takeError();
914     return;
915   }
916   Name = NameOrErr.get();
917 
918   if (Name == "//") {
919     // The string table is never an external member, but we still
920     // must check any Expected<> return value.
921     Expected<StringRef> BufOrErr = C->getBuffer();
922     if (!BufOrErr) {
923       Err = BufOrErr.takeError();
924       return;
925     }
926     StringTable = BufOrErr.get();
927     if (Increment())
928       return;
929   }
930 
931   setFirstRegular(*C);
932   Err = Error::success();
933 }
934 
935 object::Archive::Kind Archive::getDefaultKindForHost() {
936   Triple HostTriple(sys::getProcessTriple());
937   return HostTriple.isOSDarwin()
938              ? object::Archive::K_DARWIN
939              : (HostTriple.isOSAIX() ? object::Archive::K_AIXBIG
940                                      : object::Archive::K_GNU);
941 }
942 
943 Archive::child_iterator Archive::child_begin(Error &Err,
944                                              bool SkipInternal) const {
945   if (isEmpty())
946     return child_end();
947 
948   if (SkipInternal)
949     return child_iterator::itr(
950         Child(this, FirstRegularData, FirstRegularStartOfFile), Err);
951 
952   const char *Loc = Data.getBufferStart() + getFirstChildOffset();
953   Child C(this, Loc, &Err);
954   if (Err)
955     return child_end();
956   return child_iterator::itr(C, Err);
957 }
958 
959 Archive::child_iterator Archive::child_end() const {
960   return child_iterator::end(Child(nullptr, nullptr, nullptr));
961 }
962 
963 StringRef Archive::Symbol::getName() const {
964   return Parent->getSymbolTable().begin() + StringIndex;
965 }
966 
967 Expected<Archive::Child> Archive::Symbol::getMember() const {
968   const char *Buf = Parent->getSymbolTable().begin();
969   const char *Offsets = Buf;
970   if (Parent->kind() == K_GNU64 || Parent->kind() == K_DARWIN64 ||
971       Parent->kind() == K_AIXBIG)
972     Offsets += sizeof(uint64_t);
973   else
974     Offsets += sizeof(uint32_t);
975   uint64_t Offset = 0;
976   if (Parent->kind() == K_GNU) {
977     Offset = read32be(Offsets + SymbolIndex * 4);
978   } else if (Parent->kind() == K_GNU64 || Parent->kind() == K_AIXBIG) {
979     Offset = read64be(Offsets + SymbolIndex * 8);
980   } else if (Parent->kind() == K_BSD) {
981     // The SymbolIndex is an index into the ranlib structs that start at
982     // Offsets (the first uint32_t is the number of bytes of the ranlib
983     // structs).  The ranlib structs are a pair of uint32_t's the first
984     // being a string table offset and the second being the offset into
985     // the archive of the member that defines the symbol.  Which is what
986     // is needed here.
987     Offset = read32le(Offsets + SymbolIndex * 8 + 4);
988   } else if (Parent->kind() == K_DARWIN64) {
989     // The SymbolIndex is an index into the ranlib_64 structs that start at
990     // Offsets (the first uint64_t is the number of bytes of the ranlib_64
991     // structs).  The ranlib_64 structs are a pair of uint64_t's the first
992     // being a string table offset and the second being the offset into
993     // the archive of the member that defines the symbol.  Which is what
994     // is needed here.
995     Offset = read64le(Offsets + SymbolIndex * 16 + 8);
996   } else {
997     // Skip offsets.
998     uint32_t MemberCount = read32le(Buf);
999     Buf += MemberCount * 4 + 4;
1000 
1001     uint32_t SymbolCount = read32le(Buf);
1002     if (SymbolIndex >= SymbolCount)
1003       return errorCodeToError(object_error::parse_failed);
1004 
1005     // Skip SymbolCount to get to the indices table.
1006     const char *Indices = Buf + 4;
1007 
1008     // Get the index of the offset in the file member offset table for this
1009     // symbol.
1010     uint16_t OffsetIndex = read16le(Indices + SymbolIndex * 2);
1011     // Subtract 1 since OffsetIndex is 1 based.
1012     --OffsetIndex;
1013 
1014     if (OffsetIndex >= MemberCount)
1015       return errorCodeToError(object_error::parse_failed);
1016 
1017     Offset = read32le(Offsets + OffsetIndex * 4);
1018   }
1019 
1020   const char *Loc = Parent->getData().begin() + Offset;
1021   Error Err = Error::success();
1022   Child C(Parent, Loc, &Err);
1023   if (Err)
1024     return std::move(Err);
1025   return C;
1026 }
1027 
1028 Archive::Symbol Archive::Symbol::getNext() const {
1029   Symbol t(*this);
1030   if (Parent->kind() == K_BSD) {
1031     // t.StringIndex is an offset from the start of the __.SYMDEF or
1032     // "__.SYMDEF SORTED" member into the string table for the ranlib
1033     // struct indexed by t.SymbolIndex .  To change t.StringIndex to the
1034     // offset in the string table for t.SymbolIndex+1 we subtract the
1035     // its offset from the start of the string table for t.SymbolIndex
1036     // and add the offset of the string table for t.SymbolIndex+1.
1037 
1038     // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t
1039     // which is the number of bytes of ranlib structs that follow.  The ranlib
1040     // structs are a pair of uint32_t's the first being a string table offset
1041     // and the second being the offset into the archive of the member that
1042     // define the symbol. After that the next uint32_t is the byte count of
1043     // the string table followed by the string table.
1044     const char *Buf = Parent->getSymbolTable().begin();
1045     uint32_t RanlibCount = 0;
1046     RanlibCount = read32le(Buf) / 8;
1047     // If t.SymbolIndex + 1 will be past the count of symbols (the RanlibCount)
1048     // don't change the t.StringIndex as we don't want to reference a ranlib
1049     // past RanlibCount.
1050     if (t.SymbolIndex + 1 < RanlibCount) {
1051       const char *Ranlibs = Buf + 4;
1052       uint32_t CurRanStrx = 0;
1053       uint32_t NextRanStrx = 0;
1054       CurRanStrx = read32le(Ranlibs + t.SymbolIndex * 8);
1055       NextRanStrx = read32le(Ranlibs + (t.SymbolIndex + 1) * 8);
1056       t.StringIndex -= CurRanStrx;
1057       t.StringIndex += NextRanStrx;
1058     }
1059   } else {
1060     // Go to one past next null.
1061     t.StringIndex = Parent->getSymbolTable().find('\0', t.StringIndex) + 1;
1062   }
1063   ++t.SymbolIndex;
1064   return t;
1065 }
1066 
1067 Archive::symbol_iterator Archive::symbol_begin() const {
1068   if (!hasSymbolTable())
1069     return symbol_iterator(Symbol(this, 0, 0));
1070 
1071   const char *buf = getSymbolTable().begin();
1072   if (kind() == K_GNU) {
1073     uint32_t symbol_count = 0;
1074     symbol_count = read32be(buf);
1075     buf += sizeof(uint32_t) + (symbol_count * (sizeof(uint32_t)));
1076   } else if (kind() == K_GNU64) {
1077     uint64_t symbol_count = read64be(buf);
1078     buf += sizeof(uint64_t) + (symbol_count * (sizeof(uint64_t)));
1079   } else if (kind() == K_BSD) {
1080     // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t
1081     // which is the number of bytes of ranlib structs that follow.  The ranlib
1082     // structs are a pair of uint32_t's the first being a string table offset
1083     // and the second being the offset into the archive of the member that
1084     // define the symbol. After that the next uint32_t is the byte count of
1085     // the string table followed by the string table.
1086     uint32_t ranlib_count = 0;
1087     ranlib_count = read32le(buf) / 8;
1088     const char *ranlibs = buf + 4;
1089     uint32_t ran_strx = 0;
1090     ran_strx = read32le(ranlibs);
1091     buf += sizeof(uint32_t) + (ranlib_count * (2 * (sizeof(uint32_t))));
1092     // Skip the byte count of the string table.
1093     buf += sizeof(uint32_t);
1094     buf += ran_strx;
1095   } else if (kind() == K_DARWIN64) {
1096     // The __.SYMDEF_64 or "__.SYMDEF_64 SORTED" member starts with a uint64_t
1097     // which is the number of bytes of ranlib_64 structs that follow.  The
1098     // ranlib_64 structs are a pair of uint64_t's the first being a string
1099     // table offset and the second being the offset into the archive of the
1100     // member that define the symbol. After that the next uint64_t is the byte
1101     // count of the string table followed by the string table.
1102     uint64_t ranlib_count = 0;
1103     ranlib_count = read64le(buf) / 16;
1104     const char *ranlibs = buf + 8;
1105     uint64_t ran_strx = 0;
1106     ran_strx = read64le(ranlibs);
1107     buf += sizeof(uint64_t) + (ranlib_count * (2 * (sizeof(uint64_t))));
1108     // Skip the byte count of the string table.
1109     buf += sizeof(uint64_t);
1110     buf += ran_strx;
1111   } else if (kind() == K_AIXBIG) {
1112     buf = getStringTable().begin();
1113   } else {
1114     uint32_t member_count = 0;
1115     uint32_t symbol_count = 0;
1116     member_count = read32le(buf);
1117     buf += 4 + (member_count * 4); // Skip offsets.
1118     symbol_count = read32le(buf);
1119     buf += 4 + (symbol_count * 2); // Skip indices.
1120   }
1121   uint32_t string_start_offset = buf - getSymbolTable().begin();
1122   return symbol_iterator(Symbol(this, 0, string_start_offset));
1123 }
1124 
1125 Archive::symbol_iterator Archive::symbol_end() const {
1126   return symbol_iterator(Symbol(this, getNumberOfSymbols(), 0));
1127 }
1128 
1129 uint32_t Archive::getNumberOfSymbols() const {
1130   if (!hasSymbolTable())
1131     return 0;
1132   const char *buf = getSymbolTable().begin();
1133   if (kind() == K_GNU)
1134     return read32be(buf);
1135   if (kind() == K_GNU64 || kind() == K_AIXBIG)
1136     return read64be(buf);
1137   if (kind() == K_BSD)
1138     return read32le(buf) / 8;
1139   if (kind() == K_DARWIN64)
1140     return read64le(buf) / 16;
1141   uint32_t member_count = 0;
1142   member_count = read32le(buf);
1143   buf += 4 + (member_count * 4); // Skip offsets.
1144   return read32le(buf);
1145 }
1146 
1147 Expected<std::optional<Archive::Child>> Archive::findSym(StringRef name) const {
1148   Archive::symbol_iterator bs = symbol_begin();
1149   Archive::symbol_iterator es = symbol_end();
1150 
1151   for (; bs != es; ++bs) {
1152     StringRef SymName = bs->getName();
1153     if (SymName == name) {
1154       if (auto MemberOrErr = bs->getMember())
1155         return Child(*MemberOrErr);
1156       else
1157         return MemberOrErr.takeError();
1158     }
1159   }
1160   return std::nullopt;
1161 }
1162 
1163 // Returns true if archive file contains no member file.
1164 bool Archive::isEmpty() const {
1165   return Data.getBufferSize() == getArchiveMagicLen();
1166 }
1167 
1168 bool Archive::hasSymbolTable() const { return !SymbolTable.empty(); }
1169 
1170 BigArchive::BigArchive(MemoryBufferRef Source, Error &Err)
1171     : Archive(Source, Err) {
1172   ErrorAsOutParameter ErrAsOutParam(&Err);
1173   StringRef Buffer = Data.getBuffer();
1174   ArFixLenHdr = reinterpret_cast<const FixLenHdr *>(Buffer.data());
1175 
1176   StringRef RawOffset = getFieldRawString(ArFixLenHdr->FirstChildOffset);
1177   if (RawOffset.getAsInteger(10, FirstChildOffset))
1178     // TODO: Out-of-line.
1179     Err = malformedError("malformed AIX big archive: first member offset \"" +
1180                          RawOffset + "\" is not a number");
1181 
1182   RawOffset = getFieldRawString(ArFixLenHdr->LastChildOffset);
1183   if (RawOffset.getAsInteger(10, LastChildOffset))
1184     // TODO: Out-of-line.
1185     Err = malformedError("malformed AIX big archive: last member offset \"" +
1186                          RawOffset + "\" is not a number");
1187 
1188   // Calculate the global symbol table.
1189   uint64_t GlobSymOffset = 0;
1190   RawOffset = getFieldRawString(ArFixLenHdr->GlobSymOffset);
1191   if (RawOffset.getAsInteger(10, GlobSymOffset))
1192     // TODO: add test case.
1193     Err = malformedError(
1194         "malformed AIX big archive: global symbol table offset \"" + RawOffset +
1195         "\" is not a number");
1196 
1197   if (Err)
1198     return;
1199 
1200   if (GlobSymOffset > 0) {
1201     uint64_t BufferSize = Data.getBufferSize();
1202     uint64_t GlobalSymTblContentOffset =
1203         GlobSymOffset + sizeof(BigArMemHdrType);
1204     if (GlobalSymTblContentOffset > BufferSize) {
1205       Err = malformedError("global symbol table header at offset 0x" +
1206                            Twine::utohexstr(GlobSymOffset) + " and size 0x" +
1207                            Twine::utohexstr(sizeof(BigArMemHdrType)) +
1208                            " goes past the end of file");
1209       return;
1210     }
1211 
1212     const char *GlobSymTblLoc = Data.getBufferStart() + GlobSymOffset;
1213     const BigArMemHdrType *GlobalSymHdr =
1214         reinterpret_cast<const BigArMemHdrType *>(GlobSymTblLoc);
1215     RawOffset = getFieldRawString(GlobalSymHdr->Size);
1216     uint64_t Size;
1217     if (RawOffset.getAsInteger(10, Size)) {
1218       // TODO: add test case.
1219       Err = malformedError(
1220           "malformed AIX big archive: global symbol table size \"" + RawOffset +
1221           "\" is not a number");
1222       return;
1223     }
1224     if (GlobalSymTblContentOffset + Size > BufferSize) {
1225       Err = malformedError("global symbol table content at offset 0x" +
1226                            Twine::utohexstr(GlobalSymTblContentOffset) +
1227                            " and size 0x" + Twine::utohexstr(Size) +
1228                            " goes past the end of file");
1229       return;
1230     }
1231     SymbolTable = StringRef(GlobSymTblLoc + sizeof(BigArMemHdrType), Size);
1232     unsigned SymNum = getNumberOfSymbols();
1233     unsigned SymOffsetsSize = 8 * (SymNum + 1);
1234     uint64_t SymbolTableStringSize = Size - SymOffsetsSize;
1235     StringTable =
1236         StringRef(GlobSymTblLoc + sizeof(BigArMemHdrType) + SymOffsetsSize,
1237                   SymbolTableStringSize);
1238   }
1239 
1240   child_iterator I = child_begin(Err, false);
1241   if (Err)
1242     return;
1243   child_iterator E = child_end();
1244   if (I == E) {
1245     Err = Error::success();
1246     return;
1247   }
1248   setFirstRegular(*I);
1249   Err = Error::success();
1250 }
1251