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   }
232   // It's a simple name.
233   if (name[name.size() - 1] == '/')
234     return name.substr(0, name.size() - 1);
235   return name;
236 }
237 
238 ErrorOr<MemoryBufferRef> Archive::Child::getMemoryBufferRef() const {
239   ErrorOr<StringRef> NameOrErr = getName();
240   if (std::error_code EC = NameOrErr.getError())
241     return EC;
242   StringRef Name = NameOrErr.get();
243   ErrorOr<StringRef> Buf = getBuffer();
244   if (std::error_code EC = Buf.getError())
245     return EC;
246   return MemoryBufferRef(*Buf, Name);
247 }
248 
249 ErrorOr<std::unique_ptr<Binary>>
250 Archive::Child::getAsBinary(LLVMContext *Context) const {
251   ErrorOr<MemoryBufferRef> BuffOrErr = getMemoryBufferRef();
252   if (std::error_code EC = BuffOrErr.getError())
253     return EC;
254 
255   auto BinaryOrErr = createBinary(BuffOrErr.get(), Context);
256   if (BinaryOrErr)
257     return std::move(*BinaryOrErr);
258   return errorToErrorCode(BinaryOrErr.takeError());
259 }
260 
261 ErrorOr<std::unique_ptr<Archive>> Archive::create(MemoryBufferRef Source) {
262   std::error_code EC;
263   std::unique_ptr<Archive> Ret(new Archive(Source, EC));
264   if (EC)
265     return EC;
266   return std::move(Ret);
267 }
268 
269 void Archive::setFirstRegular(const Child &C) {
270   FirstRegularData = C.Data;
271   FirstRegularStartOfFile = C.StartOfFile;
272 }
273 
274 Archive::Archive(MemoryBufferRef Source, std::error_code &ec)
275     : Binary(Binary::ID_Archive, Source) {
276   StringRef Buffer = Data.getBuffer();
277   // Check for sufficient magic.
278   if (Buffer.startswith(ThinMagic)) {
279     IsThin = true;
280   } else if (Buffer.startswith(Magic)) {
281     IsThin = false;
282   } else {
283     ec = object_error::invalid_file_type;
284     return;
285   }
286 
287   // Get the special members.
288   child_iterator I = child_begin(false);
289   if ((ec = I->getError()))
290     return;
291   child_iterator E = child_end();
292 
293   if (I == E) {
294     ec = std::error_code();
295     return;
296   }
297   const Child *C = &**I;
298 
299   auto Increment = [&]() {
300     ++I;
301     if ((ec = I->getError()))
302       return true;
303     C = &**I;
304     return false;
305   };
306 
307   StringRef Name = C->getRawName();
308 
309   // Below is the pattern that is used to figure out the archive format
310   // GNU archive format
311   //  First member : / (may exist, if it exists, points to the symbol table )
312   //  Second member : // (may exist, if it exists, points to the string table)
313   //  Note : The string table is used if the filename exceeds 15 characters
314   // BSD archive format
315   //  First member : __.SYMDEF or "__.SYMDEF SORTED" (the symbol table)
316   //  There is no string table, if the filename exceeds 15 characters or has a
317   //  embedded space, the filename has #1/<size>, The size represents the size
318   //  of the filename that needs to be read after the archive header
319   // COFF archive format
320   //  First member : /
321   //  Second member : / (provides a directory of symbols)
322   //  Third member : // (may exist, if it exists, contains the string table)
323   //  Note: Microsoft PE/COFF Spec 8.3 says that the third member is present
324   //  even if the string table is empty. However, lib.exe does not in fact
325   //  seem to create the third member if there's no member whose filename
326   //  exceeds 15 characters. So the third member is optional.
327 
328   if (Name == "__.SYMDEF") {
329     Format = K_BSD;
330     // We know that the symbol table is not an external file, so we just assert
331     // there is no error.
332     SymbolTable = *C->getBuffer();
333     if (Increment())
334       return;
335     setFirstRegular(*C);
336 
337     ec = std::error_code();
338     return;
339   }
340 
341   if (Name.startswith("#1/")) {
342     Format = K_BSD;
343     // We know this is BSD, so getName will work since there is no string table.
344     ErrorOr<StringRef> NameOrErr = C->getName();
345     ec = NameOrErr.getError();
346     if (ec)
347       return;
348     Name = NameOrErr.get();
349     if (Name == "__.SYMDEF SORTED" || Name == "__.SYMDEF") {
350       // We know that the symbol table is not an external file, so we just
351       // assert there is no error.
352       SymbolTable = *C->getBuffer();
353       if (Increment())
354         return;
355     }
356     setFirstRegular(*C);
357     return;
358   }
359 
360   // MIPS 64-bit ELF archives use a special format of a symbol table.
361   // This format is marked by `ar_name` field equals to "/SYM64/".
362   // For detailed description see page 96 in the following document:
363   // http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf
364 
365   bool has64SymTable = false;
366   if (Name == "/" || Name == "/SYM64/") {
367     // We know that the symbol table is not an external file, so we just assert
368     // there is no error.
369     SymbolTable = *C->getBuffer();
370     if (Name == "/SYM64/")
371       has64SymTable = true;
372 
373     if (Increment())
374       return;
375     if (I == E) {
376       ec = std::error_code();
377       return;
378     }
379     Name = C->getRawName();
380   }
381 
382   if (Name == "//") {
383     Format = has64SymTable ? K_MIPS64 : K_GNU;
384     // The string table is never an external member, so we just assert on the
385     // ErrorOr.
386     StringTable = *C->getBuffer();
387     if (Increment())
388       return;
389     setFirstRegular(*C);
390     ec = std::error_code();
391     return;
392   }
393 
394   if (Name[0] != '/') {
395     Format = has64SymTable ? K_MIPS64 : K_GNU;
396     setFirstRegular(*C);
397     ec = std::error_code();
398     return;
399   }
400 
401   if (Name != "/") {
402     ec = object_error::parse_failed;
403     return;
404   }
405 
406   Format = K_COFF;
407   // We know that the symbol table is not an external file, so we just assert
408   // there is no error.
409   SymbolTable = *C->getBuffer();
410 
411   if (Increment())
412     return;
413 
414   if (I == E) {
415     setFirstRegular(*C);
416     ec = std::error_code();
417     return;
418   }
419 
420   Name = C->getRawName();
421 
422   if (Name == "//") {
423     // The string table is never an external member, so we just assert on the
424     // ErrorOr.
425     StringTable = *C->getBuffer();
426     if (Increment())
427       return;
428   }
429 
430   setFirstRegular(*C);
431   ec = std::error_code();
432 }
433 
434 Archive::child_iterator Archive::child_begin(bool SkipInternal) const {
435   if (Data.getBufferSize() == 8) // empty archive.
436     return child_end();
437 
438   if (SkipInternal)
439     return Child(this, FirstRegularData, FirstRegularStartOfFile);
440 
441   const char *Loc = Data.getBufferStart() + strlen(Magic);
442   std::error_code EC;
443   Child c(this, Loc, &EC);
444   if (EC)
445     return child_iterator(EC);
446   return child_iterator(c);
447 }
448 
449 Archive::child_iterator Archive::child_end() const {
450   return Child(this, nullptr, nullptr);
451 }
452 
453 StringRef Archive::Symbol::getName() const {
454   return Parent->getSymbolTable().begin() + StringIndex;
455 }
456 
457 ErrorOr<Archive::Child> Archive::Symbol::getMember() const {
458   const char *Buf = Parent->getSymbolTable().begin();
459   const char *Offsets = Buf;
460   if (Parent->kind() == K_MIPS64)
461     Offsets += sizeof(uint64_t);
462   else
463     Offsets += sizeof(uint32_t);
464   uint32_t Offset = 0;
465   if (Parent->kind() == K_GNU) {
466     Offset = read32be(Offsets + SymbolIndex * 4);
467   } else if (Parent->kind() == K_MIPS64) {
468     Offset = read64be(Offsets + SymbolIndex * 8);
469   } else if (Parent->kind() == K_BSD) {
470     // The SymbolIndex is an index into the ranlib structs that start at
471     // Offsets (the first uint32_t is the number of bytes of the ranlib
472     // structs).  The ranlib structs are a pair of uint32_t's the first
473     // being a string table offset and the second being the offset into
474     // the archive of the member that defines the symbol.  Which is what
475     // is needed here.
476     Offset = read32le(Offsets + SymbolIndex * 8 + 4);
477   } else {
478     // Skip offsets.
479     uint32_t MemberCount = read32le(Buf);
480     Buf += MemberCount * 4 + 4;
481 
482     uint32_t SymbolCount = read32le(Buf);
483     if (SymbolIndex >= SymbolCount)
484       return object_error::parse_failed;
485 
486     // Skip SymbolCount to get to the indices table.
487     const char *Indices = Buf + 4;
488 
489     // Get the index of the offset in the file member offset table for this
490     // symbol.
491     uint16_t OffsetIndex = read16le(Indices + SymbolIndex * 2);
492     // Subtract 1 since OffsetIndex is 1 based.
493     --OffsetIndex;
494 
495     if (OffsetIndex >= MemberCount)
496       return object_error::parse_failed;
497 
498     Offset = read32le(Offsets + OffsetIndex * 4);
499   }
500 
501   const char *Loc = Parent->getData().begin() + Offset;
502   std::error_code EC;
503   Child C(Parent, Loc, &EC);
504   if (EC)
505     return EC;
506   return C;
507 }
508 
509 Archive::Symbol Archive::Symbol::getNext() const {
510   Symbol t(*this);
511   if (Parent->kind() == K_BSD) {
512     // t.StringIndex is an offset from the start of the __.SYMDEF or
513     // "__.SYMDEF SORTED" member into the string table for the ranlib
514     // struct indexed by t.SymbolIndex .  To change t.StringIndex to the
515     // offset in the string table for t.SymbolIndex+1 we subtract the
516     // its offset from the start of the string table for t.SymbolIndex
517     // and add the offset of the string table for t.SymbolIndex+1.
518 
519     // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t
520     // which is the number of bytes of ranlib structs that follow.  The ranlib
521     // structs are a pair of uint32_t's the first being a string table offset
522     // and the second being the offset into the archive of the member that
523     // define the symbol. After that the next uint32_t is the byte count of
524     // the string table followed by the string table.
525     const char *Buf = Parent->getSymbolTable().begin();
526     uint32_t RanlibCount = 0;
527     RanlibCount = read32le(Buf) / 8;
528     // If t.SymbolIndex + 1 will be past the count of symbols (the RanlibCount)
529     // don't change the t.StringIndex as we don't want to reference a ranlib
530     // past RanlibCount.
531     if (t.SymbolIndex + 1 < RanlibCount) {
532       const char *Ranlibs = Buf + 4;
533       uint32_t CurRanStrx = 0;
534       uint32_t NextRanStrx = 0;
535       CurRanStrx = read32le(Ranlibs + t.SymbolIndex * 8);
536       NextRanStrx = read32le(Ranlibs + (t.SymbolIndex + 1) * 8);
537       t.StringIndex -= CurRanStrx;
538       t.StringIndex += NextRanStrx;
539     }
540   } else {
541     // Go to one past next null.
542     t.StringIndex = Parent->getSymbolTable().find('\0', t.StringIndex) + 1;
543   }
544   ++t.SymbolIndex;
545   return t;
546 }
547 
548 Archive::symbol_iterator Archive::symbol_begin() const {
549   if (!hasSymbolTable())
550     return symbol_iterator(Symbol(this, 0, 0));
551 
552   const char *buf = getSymbolTable().begin();
553   if (kind() == K_GNU) {
554     uint32_t symbol_count = 0;
555     symbol_count = read32be(buf);
556     buf += sizeof(uint32_t) + (symbol_count * (sizeof(uint32_t)));
557   } else if (kind() == K_MIPS64) {
558     uint64_t symbol_count = read64be(buf);
559     buf += sizeof(uint64_t) + (symbol_count * (sizeof(uint64_t)));
560   } else if (kind() == K_BSD) {
561     // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t
562     // which is the number of bytes of ranlib structs that follow.  The ranlib
563     // structs are a pair of uint32_t's the first being a string table offset
564     // and the second being the offset into the archive of the member that
565     // define the symbol. After that the next uint32_t is the byte count of
566     // the string table followed by the string table.
567     uint32_t ranlib_count = 0;
568     ranlib_count = read32le(buf) / 8;
569     const char *ranlibs = buf + 4;
570     uint32_t ran_strx = 0;
571     ran_strx = read32le(ranlibs);
572     buf += sizeof(uint32_t) + (ranlib_count * (2 * (sizeof(uint32_t))));
573     // Skip the byte count of the string table.
574     buf += sizeof(uint32_t);
575     buf += ran_strx;
576   } else {
577     uint32_t member_count = 0;
578     uint32_t symbol_count = 0;
579     member_count = read32le(buf);
580     buf += 4 + (member_count * 4); // Skip offsets.
581     symbol_count = read32le(buf);
582     buf += 4 + (symbol_count * 2); // Skip indices.
583   }
584   uint32_t string_start_offset = buf - getSymbolTable().begin();
585   return symbol_iterator(Symbol(this, 0, string_start_offset));
586 }
587 
588 Archive::symbol_iterator Archive::symbol_end() const {
589   return symbol_iterator(Symbol(this, getNumberOfSymbols(), 0));
590 }
591 
592 uint32_t Archive::getNumberOfSymbols() const {
593   if (!hasSymbolTable())
594     return 0;
595   const char *buf = getSymbolTable().begin();
596   if (kind() == K_GNU)
597     return read32be(buf);
598   if (kind() == K_MIPS64)
599     return read64be(buf);
600   if (kind() == K_BSD)
601     return read32le(buf) / 8;
602   uint32_t member_count = 0;
603   member_count = read32le(buf);
604   buf += 4 + (member_count * 4); // Skip offsets.
605   return read32le(buf);
606 }
607 
608 Archive::child_iterator Archive::findSym(StringRef name) const {
609   Archive::symbol_iterator bs = symbol_begin();
610   Archive::symbol_iterator es = symbol_end();
611 
612   for (; bs != es; ++bs) {
613     StringRef SymName = bs->getName();
614     if (SymName == name) {
615       ErrorOr<Archive::child_iterator> ResultOrErr = bs->getMember();
616       // FIXME: Should we really eat the error?
617       if (ResultOrErr.getError())
618         return child_end();
619       return ResultOrErr.get();
620     }
621   }
622   return child_end();
623 }
624 
625 bool Archive::hasSymbolTable() const { return !SymbolTable.empty(); }
626