• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/Support/MemoryBuffer.h"
17 
18 using namespace llvm;
19 using namespace object;
20 
21 namespace {
22 const StringRef Magic = "!<arch>\n";
23 
24 struct ArchiveMemberHeader {
25   char Name[16];
26   char LastModified[12];
27   char UID[6];
28   char GID[6];
29   char AccessMode[8];
30   char Size[10]; //< Size of data, not including header or padding.
31   char Terminator[2];
32 
33   ///! Get the name without looking up long names.
getName__anond85b87e50111::ArchiveMemberHeader34   StringRef getName() const {
35     char EndCond = Name[0] == '/' ? ' ' : '/';
36     StringRef::size_type end = StringRef(Name, sizeof(Name)).find(EndCond);
37     if (end == StringRef::npos)
38       end = sizeof(Name);
39     assert(end <= sizeof(Name) && end > 0);
40     // Don't include the EndCond if there is one.
41     return StringRef(Name, end);
42   }
43 
getSize__anond85b87e50111::ArchiveMemberHeader44   uint64_t getSize() const {
45     APInt ret;
46     StringRef(Size, sizeof(Size)).getAsInteger(10, ret);
47     return ret.getZExtValue();
48   }
49 };
50 
ToHeader(const char * base)51 const ArchiveMemberHeader *ToHeader(const char *base) {
52   return reinterpret_cast<const ArchiveMemberHeader *>(base);
53 }
54 }
55 
getNext() const56 Archive::Child Archive::Child::getNext() const {
57   size_t SpaceToSkip = sizeof(ArchiveMemberHeader) +
58     ToHeader(Data.data())->getSize();
59   // If it's odd, add 1 to make it even.
60   if (SpaceToSkip & 1)
61     ++SpaceToSkip;
62 
63   const char *NextLoc = Data.data() + SpaceToSkip;
64 
65   // Check to see if this is past the end of the archive.
66   if (NextLoc >= Parent->Data->getBufferEnd())
67     return Child(Parent, StringRef(0, 0));
68 
69   size_t NextSize = sizeof(ArchiveMemberHeader) +
70     ToHeader(NextLoc)->getSize();
71 
72   return Child(Parent, StringRef(NextLoc, NextSize));
73 }
74 
getName(StringRef & Result) const75 error_code Archive::Child::getName(StringRef &Result) const {
76   StringRef name = ToHeader(Data.data())->getName();
77   // Check if it's a special name.
78   if (name[0] == '/') {
79     if (name.size() == 1) { // Linker member.
80       Result = name;
81       return object_error::success;
82     }
83     if (name.size() == 2 && name[1] == '/') { // String table.
84       Result = name;
85       return object_error::success;
86     }
87     // It's a long name.
88     // Get the offset.
89     APInt offset;
90     name.substr(1).getAsInteger(10, offset);
91     const char *addr = Parent->StringTable->Data.begin()
92                        + sizeof(ArchiveMemberHeader)
93                        + offset.getZExtValue();
94     // Verify it.
95     if (Parent->StringTable == Parent->end_children()
96         || addr < (Parent->StringTable->Data.begin()
97                    + sizeof(ArchiveMemberHeader))
98         || addr > (Parent->StringTable->Data.begin()
99                    + sizeof(ArchiveMemberHeader)
100                    + Parent->StringTable->getSize()))
101       return object_error::parse_failed;
102     Result = addr;
103     return object_error::success;
104   }
105   // It's a simple name.
106   if (name[name.size() - 1] == '/')
107     Result = name.substr(0, name.size() - 1);
108   else
109     Result = name;
110   return object_error::success;
111 }
112 
getSize() const113 uint64_t Archive::Child::getSize() const {
114   return ToHeader(Data.data())->getSize();
115 }
116 
getBuffer() const117 MemoryBuffer *Archive::Child::getBuffer() const {
118   StringRef name;
119   if (getName(name)) return NULL;
120   return MemoryBuffer::getMemBuffer(Data.substr(sizeof(ArchiveMemberHeader),
121                                                 getSize()),
122                                     name,
123                                     false);
124 }
125 
getAsBinary(OwningPtr<Binary> & Result) const126 error_code Archive::Child::getAsBinary(OwningPtr<Binary> &Result) const {
127   OwningPtr<Binary> ret;
128   if (error_code ec =
129     createBinary(getBuffer(), ret))
130     return ec;
131   Result.swap(ret);
132   return object_error::success;
133 }
134 
Archive(MemoryBuffer * source,error_code & ec)135 Archive::Archive(MemoryBuffer *source, error_code &ec)
136   : Binary(Binary::isArchive, source)
137   , StringTable(Child(this, StringRef(0, 0))) {
138   // Check for sufficient magic.
139   if (!source || source->getBufferSize()
140                  < (8 + sizeof(ArchiveMemberHeader) + 2) // Smallest archive.
141               || StringRef(source->getBufferStart(), 8) != Magic) {
142     ec = object_error::invalid_file_type;
143     return;
144   }
145 
146   // Get the string table. It's the 3rd member.
147   child_iterator StrTable = begin_children();
148   child_iterator e = end_children();
149   for (int i = 0; StrTable != e && i < 2; ++StrTable, ++i) {}
150 
151   // Check to see if there were 3 members, or the 3rd member wasn't named "//".
152   StringRef name;
153   if (StrTable != e && !StrTable->getName(name) && name == "//")
154     StringTable = StrTable;
155 
156   ec = object_error::success;
157 }
158 
begin_children() const159 Archive::child_iterator Archive::begin_children() const {
160   const char *Loc = Data->getBufferStart() + Magic.size();
161   size_t Size = sizeof(ArchiveMemberHeader) +
162     ToHeader(Loc)->getSize();
163   return Child(this, StringRef(Loc, Size));
164 }
165 
end_children() const166 Archive::child_iterator Archive::end_children() const {
167   return Child(this, StringRef(0, 0));
168 }
169 
170 namespace llvm {
171 
172 } // end namespace llvm
173