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 StringRef ArchiveMemberHeader::getName() const {
31   char EndCond;
32   if (Name[0] == '/' || Name[0] == '#')
33     EndCond = ' ';
34   else
35     EndCond = '/';
36   llvm::StringRef::size_type end =
37       llvm::StringRef(Name, sizeof(Name)).find(EndCond);
38   if (end == llvm::StringRef::npos)
39     end = sizeof(Name);
40   assert(end <= sizeof(Name) && end > 0);
41   // Don't include the EndCond if there is one.
42   return llvm::StringRef(Name, end);
43 }
44 
45 ErrorOr<uint32_t> ArchiveMemberHeader::getSize() const {
46   uint32_t Ret;
47   if (llvm::StringRef(Size, sizeof(Size)).rtrim(" ").getAsInteger(10, Ret))
48     return object_error::parse_failed; // Size is not a decimal number.
49   return Ret;
50 }
51 
52 sys::fs::perms ArchiveMemberHeader::getAccessMode() const {
53   unsigned Ret;
54   if (StringRef(AccessMode, sizeof(AccessMode)).rtrim(' ').getAsInteger(8, Ret))
55     llvm_unreachable("Access mode is not an octal number.");
56   return static_cast<sys::fs::perms>(Ret);
57 }
58 
59 sys::TimeValue ArchiveMemberHeader::getLastModified() const {
60   unsigned Seconds;
61   if (StringRef(LastModified, sizeof(LastModified)).rtrim(' ')
62           .getAsInteger(10, Seconds))
63     llvm_unreachable("Last modified time not a decimal number.");
64 
65   sys::TimeValue Ret;
66   Ret.fromEpochTime(Seconds);
67   return Ret;
68 }
69 
70 unsigned ArchiveMemberHeader::getUID() const {
71   unsigned Ret;
72   if (StringRef(UID, sizeof(UID)).rtrim(' ').getAsInteger(10, Ret))
73     llvm_unreachable("UID time not a decimal number.");
74   return Ret;
75 }
76 
77 unsigned ArchiveMemberHeader::getGID() const {
78   unsigned Ret;
79   if (StringRef(GID, sizeof(GID)).rtrim(' ').getAsInteger(10, Ret))
80     llvm_unreachable("GID time not a decimal number.");
81   return Ret;
82 }
83 
84 Archive::Child::Child(const Archive *Parent, StringRef Data,
85                       uint16_t StartOfFile)
86     : Parent(Parent), Data(Data), StartOfFile(StartOfFile) {}
87 
88 Archive::Child::Child(const Archive *Parent, const char *Start,
89                       std::error_code *EC)
90     : Parent(Parent) {
91   if (!Start)
92     return;
93 
94   uint64_t Size = sizeof(ArchiveMemberHeader);
95   Data = StringRef(Start, Size);
96   if (!isThinMember()) {
97     ErrorOr<uint64_t> MemberSize = getRawSize();
98     if ((*EC = MemberSize.getError()))
99       return;
100     Size += MemberSize.get();
101     Data = StringRef(Start, Size);
102   }
103 
104   // Setup StartOfFile and PaddingBytes.
105   StartOfFile = sizeof(ArchiveMemberHeader);
106   // Don't include attached name.
107   StringRef Name = getRawName();
108   if (Name.startswith("#1/")) {
109     uint64_t NameSize;
110     if (Name.substr(3).rtrim(' ').getAsInteger(10, NameSize))
111       llvm_unreachable("Long name length is not an integer");
112     StartOfFile += NameSize;
113   }
114 }
115 
116 ErrorOr<uint64_t> Archive::Child::getSize() const {
117   if (Parent->IsThin) {
118     ErrorOr<uint32_t> Size = getHeader()->getSize();
119     if (std::error_code EC = Size.getError())
120       return EC;
121     return Size.get();
122   }
123   return Data.size() - StartOfFile;
124 }
125 
126 ErrorOr<uint64_t> Archive::Child::getRawSize() const {
127   ErrorOr<uint32_t> Size = getHeader()->getSize();
128   if (std::error_code EC = Size.getError())
129     return EC;
130   return Size.get();
131 }
132 
133 bool Archive::Child::isThinMember() const {
134   StringRef Name = getHeader()->getName();
135   return Parent->IsThin && Name != "/" && Name != "//";
136 }
137 
138 ErrorOr<std::string> Archive::Child::getFullName() const {
139   assert(isThinMember());
140   ErrorOr<StringRef> NameOrErr = getName();
141   if (std::error_code EC = NameOrErr.getError())
142     return EC;
143   StringRef Name = *NameOrErr;
144   if (sys::path::is_absolute(Name))
145     return Name;
146 
147   SmallString<128> FullName = sys::path::parent_path(
148       Parent->getMemoryBufferRef().getBufferIdentifier());
149   sys::path::append(FullName, Name);
150   return StringRef(FullName);
151 }
152 
153 ErrorOr<StringRef> Archive::Child::getBuffer() const {
154   if (!isThinMember()) {
155     ErrorOr<uint32_t> Size = getSize();
156     if (std::error_code EC = Size.getError())
157       return EC;
158     return StringRef(Data.data() + StartOfFile, Size.get());
159   }
160   ErrorOr<std::string> FullNameOrEr = getFullName();
161   if (std::error_code EC = FullNameOrEr.getError())
162     return EC;
163   const std::string &FullName = *FullNameOrEr;
164   ErrorOr<std::unique_ptr<MemoryBuffer>> Buf = MemoryBuffer::getFile(FullName);
165   if (std::error_code EC = Buf.getError())
166     return EC;
167   Parent->ThinBuffers.push_back(std::move(*Buf));
168   return Parent->ThinBuffers.back()->getBuffer();
169 }
170 
171 ErrorOr<Archive::Child> Archive::Child::getNext() const {
172   size_t SpaceToSkip = Data.size();
173   // If it's odd, add 1 to make it even.
174   if (SpaceToSkip & 1)
175     ++SpaceToSkip;
176 
177   const char *NextLoc = Data.data() + SpaceToSkip;
178 
179   // Check to see if this is at the end of the archive.
180   if (NextLoc == Parent->Data.getBufferEnd())
181     return Child(Parent, nullptr, nullptr);
182 
183   // Check to see if this is past the end of the archive.
184   if (NextLoc > Parent->Data.getBufferEnd())
185     return object_error::parse_failed;
186 
187   std::error_code EC;
188   Child Ret(Parent, NextLoc, &EC);
189   if (EC)
190     return EC;
191   return Ret;
192 }
193 
194 uint64_t Archive::Child::getChildOffset() const {
195   const char *a = Parent->Data.getBuffer().data();
196   const char *c = Data.data();
197   uint64_t offset = c - a;
198   return offset;
199 }
200 
201 ErrorOr<StringRef> Archive::Child::getName() const {
202   StringRef name = getRawName();
203   // Check if it's a special name.
204   if (name[0] == '/') {
205     if (name.size() == 1) // Linker member.
206       return name;
207     if (name.size() == 2 && name[1] == '/') // String table.
208       return name;
209     // It's a long name.
210     // Get the offset.
211     std::size_t offset;
212     if (name.substr(1).rtrim(' ').getAsInteger(10, offset))
213       llvm_unreachable("Long name offset is not an integer");
214 
215     // Verify it.
216     if (offset >= Parent->StringTable.size())
217       return object_error::parse_failed;
218     const char *addr = Parent->StringTable.begin() + offset;
219 
220     // GNU long file names end with a "/\n".
221     if (Parent->kind() == K_GNU || Parent->kind() == K_MIPS64) {
222       StringRef::size_type End = StringRef(addr).find('\n');
223       return StringRef(addr, End - 1);
224     }
225     return StringRef(addr);
226   } else if (name.startswith("#1/")) {
227     uint64_t name_size;
228     if (name.substr(3).rtrim(' ').getAsInteger(10, name_size))
229       llvm_unreachable("Long name length is not an ingeter");
230     return Data.substr(sizeof(ArchiveMemberHeader), name_size).rtrim('\0');
231   } else {
232     // It is not a long name so trim the blanks at the end of the name.
233     if (name[name.size() - 1] != '/') {
234       return name.rtrim(' ');
235     }
236   }
237   // It's a simple name.
238   if (name[name.size() - 1] == '/')
239     return name.substr(0, name.size() - 1);
240   return name;
241 }
242 
243 ErrorOr<MemoryBufferRef> Archive::Child::getMemoryBufferRef() const {
244   ErrorOr<StringRef> NameOrErr = getName();
245   if (std::error_code EC = NameOrErr.getError())
246     return EC;
247   StringRef Name = NameOrErr.get();
248   ErrorOr<StringRef> Buf = getBuffer();
249   if (std::error_code EC = Buf.getError())
250     return EC;
251   return MemoryBufferRef(*Buf, Name);
252 }
253 
254 Expected<std::unique_ptr<Binary>>
255 Archive::Child::getAsBinary(LLVMContext *Context) const {
256   ErrorOr<MemoryBufferRef> BuffOrErr = getMemoryBufferRef();
257   if (std::error_code EC = BuffOrErr.getError())
258     return errorCodeToError(EC);
259 
260   auto BinaryOrErr = createBinary(BuffOrErr.get(), Context);
261   if (BinaryOrErr)
262     return std::move(*BinaryOrErr);
263   return BinaryOrErr.takeError();
264 }
265 
266 ErrorOr<std::unique_ptr<Archive>> Archive::create(MemoryBufferRef Source) {
267   std::error_code EC;
268   std::unique_ptr<Archive> Ret(new Archive(Source, EC));
269   if (EC)
270     return EC;
271   return std::move(Ret);
272 }
273 
274 void Archive::setFirstRegular(const Child &C) {
275   FirstRegularData = C.Data;
276   FirstRegularStartOfFile = C.StartOfFile;
277 }
278 
279 Archive::Archive(MemoryBufferRef Source, std::error_code &ec)
280     : Binary(Binary::ID_Archive, Source) {
281   StringRef Buffer = Data.getBuffer();
282   // Check for sufficient magic.
283   if (Buffer.startswith(ThinMagic)) {
284     IsThin = true;
285   } else if (Buffer.startswith(Magic)) {
286     IsThin = false;
287   } else {
288     ec = object_error::invalid_file_type;
289     return;
290   }
291 
292   // Get the special members.
293   child_iterator I = child_begin(false);
294   if ((ec = I->getError()))
295     return;
296   child_iterator E = child_end();
297 
298   if (I == E) {
299     ec = std::error_code();
300     return;
301   }
302   const Child *C = &**I;
303 
304   auto Increment = [&]() {
305     ++I;
306     if ((ec = I->getError()))
307       return true;
308     C = &**I;
309     return false;
310   };
311 
312   StringRef Name = C->getRawName();
313 
314   // Below is the pattern that is used to figure out the archive format
315   // GNU archive format
316   //  First member : / (may exist, if it exists, points to the symbol table )
317   //  Second member : // (may exist, if it exists, points to the string table)
318   //  Note : The string table is used if the filename exceeds 15 characters
319   // BSD archive format
320   //  First member : __.SYMDEF or "__.SYMDEF SORTED" (the symbol table)
321   //  There is no string table, if the filename exceeds 15 characters or has a
322   //  embedded space, the filename has #1/<size>, The size represents the size
323   //  of the filename that needs to be read after the archive header
324   // COFF archive format
325   //  First member : /
326   //  Second member : / (provides a directory of symbols)
327   //  Third member : // (may exist, if it exists, contains the string table)
328   //  Note: Microsoft PE/COFF Spec 8.3 says that the third member is present
329   //  even if the string table is empty. However, lib.exe does not in fact
330   //  seem to create the third member if there's no member whose filename
331   //  exceeds 15 characters. So the third member is optional.
332 
333   if (Name == "__.SYMDEF" || Name == "__.SYMDEF_64") {
334     if (Name == "__.SYMDEF")
335       Format = K_BSD;
336     else // Name == "__.SYMDEF_64"
337       Format = K_DARWIN64;
338     // We know that the symbol table is not an external file, so we just assert
339     // there is no error.
340     SymbolTable = *C->getBuffer();
341     if (Increment())
342       return;
343     setFirstRegular(*C);
344 
345     ec = std::error_code();
346     return;
347   }
348 
349   if (Name.startswith("#1/")) {
350     Format = K_BSD;
351     // We know this is BSD, so getName will work since there is no string table.
352     ErrorOr<StringRef> NameOrErr = C->getName();
353     ec = NameOrErr.getError();
354     if (ec)
355       return;
356     Name = NameOrErr.get();
357     if (Name == "__.SYMDEF SORTED" || Name == "__.SYMDEF") {
358       // We know that the symbol table is not an external file, so we just
359       // assert there is no error.
360       SymbolTable = *C->getBuffer();
361       if (Increment())
362         return;
363     }
364     else if (Name == "__.SYMDEF_64 SORTED" || Name == "__.SYMDEF_64") {
365       Format = K_DARWIN64;
366       // We know that the symbol table is not an external file, so we just
367       // assert there is no error.
368       SymbolTable = *C->getBuffer();
369       if (Increment())
370         return;
371     }
372     setFirstRegular(*C);
373     return;
374   }
375 
376   // MIPS 64-bit ELF archives use a special format of a symbol table.
377   // This format is marked by `ar_name` field equals to "/SYM64/".
378   // For detailed description see page 96 in the following document:
379   // http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf
380 
381   bool has64SymTable = false;
382   if (Name == "/" || Name == "/SYM64/") {
383     // We know that the symbol table is not an external file, so we just assert
384     // there is no error.
385     SymbolTable = *C->getBuffer();
386     if (Name == "/SYM64/")
387       has64SymTable = true;
388 
389     if (Increment())
390       return;
391     if (I == E) {
392       ec = std::error_code();
393       return;
394     }
395     Name = C->getRawName();
396   }
397 
398   if (Name == "//") {
399     Format = has64SymTable ? K_MIPS64 : K_GNU;
400     // The string table is never an external member, so we just assert on the
401     // ErrorOr.
402     StringTable = *C->getBuffer();
403     if (Increment())
404       return;
405     setFirstRegular(*C);
406     ec = std::error_code();
407     return;
408   }
409 
410   if (Name[0] != '/') {
411     Format = has64SymTable ? K_MIPS64 : K_GNU;
412     setFirstRegular(*C);
413     ec = std::error_code();
414     return;
415   }
416 
417   if (Name != "/") {
418     ec = object_error::parse_failed;
419     return;
420   }
421 
422   Format = K_COFF;
423   // We know that the symbol table is not an external file, so we just assert
424   // there is no error.
425   SymbolTable = *C->getBuffer();
426 
427   if (Increment())
428     return;
429 
430   if (I == E) {
431     setFirstRegular(*C);
432     ec = std::error_code();
433     return;
434   }
435 
436   Name = C->getRawName();
437 
438   if (Name == "//") {
439     // The string table is never an external member, so we just assert on the
440     // ErrorOr.
441     StringTable = *C->getBuffer();
442     if (Increment())
443       return;
444   }
445 
446   setFirstRegular(*C);
447   ec = std::error_code();
448 }
449 
450 Archive::child_iterator Archive::child_begin(bool SkipInternal) const {
451   if (Data.getBufferSize() == 8) // empty archive.
452     return child_end();
453 
454   if (SkipInternal)
455     return Child(this, FirstRegularData, FirstRegularStartOfFile);
456 
457   const char *Loc = Data.getBufferStart() + strlen(Magic);
458   std::error_code EC;
459   Child c(this, Loc, &EC);
460   if (EC)
461     return child_iterator(EC);
462   return child_iterator(c);
463 }
464 
465 Archive::child_iterator Archive::child_end() const {
466   return Child(this, nullptr, nullptr);
467 }
468 
469 StringRef Archive::Symbol::getName() const {
470   return Parent->getSymbolTable().begin() + StringIndex;
471 }
472 
473 ErrorOr<Archive::Child> Archive::Symbol::getMember() const {
474   const char *Buf = Parent->getSymbolTable().begin();
475   const char *Offsets = Buf;
476   if (Parent->kind() == K_MIPS64 || Parent->kind() == K_DARWIN64)
477     Offsets += sizeof(uint64_t);
478   else
479     Offsets += sizeof(uint32_t);
480   uint32_t Offset = 0;
481   if (Parent->kind() == K_GNU) {
482     Offset = read32be(Offsets + SymbolIndex * 4);
483   } else if (Parent->kind() == K_MIPS64) {
484     Offset = read64be(Offsets + SymbolIndex * 8);
485   } else if (Parent->kind() == K_BSD) {
486     // The SymbolIndex is an index into the ranlib structs that start at
487     // Offsets (the first uint32_t is the number of bytes of the ranlib
488     // structs).  The ranlib structs are a pair of uint32_t's the first
489     // being a string table offset and the second being the offset into
490     // the archive of the member that defines the symbol.  Which is what
491     // is needed here.
492     Offset = read32le(Offsets + SymbolIndex * 8 + 4);
493   } else if (Parent->kind() == K_DARWIN64) {
494     // The SymbolIndex is an index into the ranlib_64 structs that start at
495     // Offsets (the first uint64_t is the number of bytes of the ranlib_64
496     // structs).  The ranlib_64 structs are a pair of uint64_t's the first
497     // being a string table offset and the second being the offset into
498     // the archive of the member that defines the symbol.  Which is what
499     // is needed here.
500     Offset = read64le(Offsets + SymbolIndex * 16 + 8);
501   } else {
502     // Skip offsets.
503     uint32_t MemberCount = read32le(Buf);
504     Buf += MemberCount * 4 + 4;
505 
506     uint32_t SymbolCount = read32le(Buf);
507     if (SymbolIndex >= SymbolCount)
508       return object_error::parse_failed;
509 
510     // Skip SymbolCount to get to the indices table.
511     const char *Indices = Buf + 4;
512 
513     // Get the index of the offset in the file member offset table for this
514     // symbol.
515     uint16_t OffsetIndex = read16le(Indices + SymbolIndex * 2);
516     // Subtract 1 since OffsetIndex is 1 based.
517     --OffsetIndex;
518 
519     if (OffsetIndex >= MemberCount)
520       return object_error::parse_failed;
521 
522     Offset = read32le(Offsets + OffsetIndex * 4);
523   }
524 
525   const char *Loc = Parent->getData().begin() + Offset;
526   std::error_code EC;
527   Child C(Parent, Loc, &EC);
528   if (EC)
529     return EC;
530   return C;
531 }
532 
533 Archive::Symbol Archive::Symbol::getNext() const {
534   Symbol t(*this);
535   if (Parent->kind() == K_BSD) {
536     // t.StringIndex is an offset from the start of the __.SYMDEF or
537     // "__.SYMDEF SORTED" member into the string table for the ranlib
538     // struct indexed by t.SymbolIndex .  To change t.StringIndex to the
539     // offset in the string table for t.SymbolIndex+1 we subtract the
540     // its offset from the start of the string table for t.SymbolIndex
541     // and add the offset of the string table for t.SymbolIndex+1.
542 
543     // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t
544     // which is the number of bytes of ranlib structs that follow.  The ranlib
545     // structs are a pair of uint32_t's the first being a string table offset
546     // and the second being the offset into the archive of the member that
547     // define the symbol. After that the next uint32_t is the byte count of
548     // the string table followed by the string table.
549     const char *Buf = Parent->getSymbolTable().begin();
550     uint32_t RanlibCount = 0;
551     RanlibCount = read32le(Buf) / 8;
552     // If t.SymbolIndex + 1 will be past the count of symbols (the RanlibCount)
553     // don't change the t.StringIndex as we don't want to reference a ranlib
554     // past RanlibCount.
555     if (t.SymbolIndex + 1 < RanlibCount) {
556       const char *Ranlibs = Buf + 4;
557       uint32_t CurRanStrx = 0;
558       uint32_t NextRanStrx = 0;
559       CurRanStrx = read32le(Ranlibs + t.SymbolIndex * 8);
560       NextRanStrx = read32le(Ranlibs + (t.SymbolIndex + 1) * 8);
561       t.StringIndex -= CurRanStrx;
562       t.StringIndex += NextRanStrx;
563     }
564   } else {
565     // Go to one past next null.
566     t.StringIndex = Parent->getSymbolTable().find('\0', t.StringIndex) + 1;
567   }
568   ++t.SymbolIndex;
569   return t;
570 }
571 
572 Archive::symbol_iterator Archive::symbol_begin() const {
573   if (!hasSymbolTable())
574     return symbol_iterator(Symbol(this, 0, 0));
575 
576   const char *buf = getSymbolTable().begin();
577   if (kind() == K_GNU) {
578     uint32_t symbol_count = 0;
579     symbol_count = read32be(buf);
580     buf += sizeof(uint32_t) + (symbol_count * (sizeof(uint32_t)));
581   } else if (kind() == K_MIPS64) {
582     uint64_t symbol_count = read64be(buf);
583     buf += sizeof(uint64_t) + (symbol_count * (sizeof(uint64_t)));
584   } else if (kind() == K_BSD) {
585     // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t
586     // which is the number of bytes of ranlib structs that follow.  The ranlib
587     // structs are a pair of uint32_t's the first being a string table offset
588     // and the second being the offset into the archive of the member that
589     // define the symbol. After that the next uint32_t is the byte count of
590     // the string table followed by the string table.
591     uint32_t ranlib_count = 0;
592     ranlib_count = read32le(buf) / 8;
593     const char *ranlibs = buf + 4;
594     uint32_t ran_strx = 0;
595     ran_strx = read32le(ranlibs);
596     buf += sizeof(uint32_t) + (ranlib_count * (2 * (sizeof(uint32_t))));
597     // Skip the byte count of the string table.
598     buf += sizeof(uint32_t);
599     buf += ran_strx;
600   } else if (kind() == K_DARWIN64) {
601     // The __.SYMDEF_64 or "__.SYMDEF_64 SORTED" member starts with a uint64_t
602     // which is the number of bytes of ranlib_64 structs that follow.  The
603     // ranlib_64 structs are a pair of uint64_t's the first being a string
604     // table offset and the second being the offset into the archive of the
605     // member that define the symbol. After that the next uint64_t is the byte
606     // count of the string table followed by the string table.
607     uint64_t ranlib_count = 0;
608     ranlib_count = read64le(buf) / 16;
609     const char *ranlibs = buf + 8;
610     uint64_t ran_strx = 0;
611     ran_strx = read64le(ranlibs);
612     buf += sizeof(uint64_t) + (ranlib_count * (2 * (sizeof(uint64_t))));
613     // Skip the byte count of the string table.
614     buf += sizeof(uint64_t);
615     buf += ran_strx;
616   } else {
617     uint32_t member_count = 0;
618     uint32_t symbol_count = 0;
619     member_count = read32le(buf);
620     buf += 4 + (member_count * 4); // Skip offsets.
621     symbol_count = read32le(buf);
622     buf += 4 + (symbol_count * 2); // Skip indices.
623   }
624   uint32_t string_start_offset = buf - getSymbolTable().begin();
625   return symbol_iterator(Symbol(this, 0, string_start_offset));
626 }
627 
628 Archive::symbol_iterator Archive::symbol_end() const {
629   return symbol_iterator(Symbol(this, getNumberOfSymbols(), 0));
630 }
631 
632 uint32_t Archive::getNumberOfSymbols() const {
633   if (!hasSymbolTable())
634     return 0;
635   const char *buf = getSymbolTable().begin();
636   if (kind() == K_GNU)
637     return read32be(buf);
638   if (kind() == K_MIPS64)
639     return read64be(buf);
640   if (kind() == K_BSD)
641     return read32le(buf) / 8;
642   if (kind() == K_DARWIN64)
643     return read64le(buf) / 16;
644   uint32_t member_count = 0;
645   member_count = read32le(buf);
646   buf += 4 + (member_count * 4); // Skip offsets.
647   return read32le(buf);
648 }
649 
650 Archive::child_iterator Archive::findSym(StringRef name) const {
651   Archive::symbol_iterator bs = symbol_begin();
652   Archive::symbol_iterator es = symbol_end();
653 
654   for (; bs != es; ++bs) {
655     StringRef SymName = bs->getName();
656     if (SymName == name) {
657       ErrorOr<Archive::child_iterator> ResultOrErr = bs->getMember();
658       // FIXME: Should we really eat the error?
659       if (ResultOrErr.getError())
660         return child_end();
661       return ResultOrErr.get();
662     }
663   }
664   return child_end();
665 }
666 
667 bool Archive::hasSymbolTable() const { return !SymbolTable.empty(); }
668