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