Simplify the handling of the archive string table.
[oota-llvm.git] / include / llvm / Object / Archive.h
1 //===- Archive.h - ar archive file format -----------------------*- 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 declares the ar archive file format class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_OBJECT_ARCHIVE_H
15 #define LLVM_OBJECT_ARCHIVE_H
16
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/ADT/iterator_range.h"
19 #include "llvm/Object/Binary.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/ErrorOr.h"
22 #include "llvm/Support/FileSystem.h"
23 #include "llvm/Support/MemoryBuffer.h"
24
25 namespace llvm {
26 namespace object {
27 struct ArchiveMemberHeader {
28   char Name[16];
29   char LastModified[12];
30   char UID[6];
31   char GID[6];
32   char AccessMode[8];
33   char Size[10]; ///< Size of data, not including header or padding.
34   char Terminator[2];
35
36   /// Get the name without looking up long names.
37   llvm::StringRef getName() const;
38
39   /// Members are not larger than 4GB.
40   ErrorOr<uint32_t> getSize() const;
41
42   sys::fs::perms getAccessMode() const;
43   sys::TimeValue getLastModified() const;
44   llvm::StringRef getRawLastModified() const {
45     return StringRef(LastModified, sizeof(LastModified)).rtrim(" ");
46   }
47   unsigned getUID() const;
48   unsigned getGID() const;
49 };
50
51 class Archive : public Binary {
52   virtual void anchor();
53 public:
54   class Child {
55     const Archive *Parent;
56     /// \brief Includes header but not padding byte.
57     StringRef Data;
58     /// \brief Offset from Data to the start of the file.
59     uint16_t StartOfFile;
60
61     const ArchiveMemberHeader *getHeader() const {
62       return reinterpret_cast<const ArchiveMemberHeader *>(Data.data());
63     }
64
65     bool isThinMember() const;
66
67   public:
68     Child(const Archive *Parent, const char *Start);
69
70     bool operator ==(const Child &other) const {
71       assert(Parent == other.Parent);
72       return Data.begin() == other.Data.begin();
73     }
74
75     bool operator <(const Child &other) const {
76       return Data.begin() < other.Data.begin();
77     }
78
79     const Archive *getParent() const { return Parent; }
80     Child getNext() const;
81
82     ErrorOr<StringRef> getName() const;
83     StringRef getRawName() const { return getHeader()->getName(); }
84     sys::TimeValue getLastModified() const {
85       return getHeader()->getLastModified();
86     }
87     StringRef getRawLastModified() const {
88       return getHeader()->getRawLastModified();
89     }
90     unsigned getUID() const { return getHeader()->getUID(); }
91     unsigned getGID() const { return getHeader()->getGID(); }
92     sys::fs::perms getAccessMode() const {
93       return getHeader()->getAccessMode();
94     }
95     /// \return the size of the archive member without the header or padding.
96     uint64_t getSize() const;
97     /// \return the size in the archive header for this member.
98     uint64_t getRawSize() const;
99
100     ErrorOr<StringRef> getBuffer() const;
101     uint64_t getChildOffset() const;
102
103     ErrorOr<MemoryBufferRef> getMemoryBufferRef() const;
104
105     ErrorOr<std::unique_ptr<Binary>>
106     getAsBinary(LLVMContext *Context = nullptr) const;
107   };
108
109   class child_iterator {
110     Child child;
111
112   public:
113     child_iterator() : child(Child(nullptr, nullptr)) {}
114     child_iterator(const Child &c) : child(c) {}
115     const Child *operator->() const { return &child; }
116     const Child &operator*() const { return child; }
117
118     bool operator==(const child_iterator &other) const {
119       return child == other.child;
120     }
121
122     bool operator!=(const child_iterator &other) const {
123       return !(*this == other);
124     }
125
126     bool operator<(const child_iterator &other) const {
127       return child < other.child;
128     }
129
130     child_iterator &operator++() { // Preincrement
131       child = child.getNext();
132       return *this;
133     }
134   };
135
136   class Symbol {
137     const Archive *Parent;
138     uint32_t SymbolIndex;
139     uint32_t StringIndex; // Extra index to the string.
140
141   public:
142     bool operator ==(const Symbol &other) const {
143       return (Parent == other.Parent) && (SymbolIndex == other.SymbolIndex);
144     }
145
146     Symbol(const Archive *p, uint32_t symi, uint32_t stri)
147       : Parent(p)
148       , SymbolIndex(symi)
149       , StringIndex(stri) {}
150     StringRef getName() const;
151     ErrorOr<child_iterator> getMember() const;
152     Symbol getNext() const;
153   };
154
155   class symbol_iterator {
156     Symbol symbol;
157   public:
158     symbol_iterator(const Symbol &s) : symbol(s) {}
159     const Symbol *operator->() const { return &symbol; }
160     const Symbol &operator*() const { return symbol; }
161
162     bool operator==(const symbol_iterator &other) const {
163       return symbol == other.symbol;
164     }
165
166     bool operator!=(const symbol_iterator &other) const {
167       return !(*this == other);
168     }
169
170     symbol_iterator& operator++() {  // Preincrement
171       symbol = symbol.getNext();
172       return *this;
173     }
174   };
175
176   Archive(MemoryBufferRef Source, std::error_code &EC);
177   static ErrorOr<std::unique_ptr<Archive>> create(MemoryBufferRef Source);
178
179   enum Kind {
180     K_GNU,
181     K_MIPS64,
182     K_BSD,
183     K_COFF
184   };
185
186   Kind kind() const { return (Kind)Format; }
187   bool isThin() const { return IsThin; }
188
189   child_iterator child_begin(bool SkipInternal = true) const;
190   child_iterator child_end() const;
191   iterator_range<child_iterator> children(bool SkipInternal = true) const {
192     return iterator_range<child_iterator>(child_begin(SkipInternal),
193                                           child_end());
194   }
195
196   symbol_iterator symbol_begin() const;
197   symbol_iterator symbol_end() const;
198   iterator_range<symbol_iterator> symbols() const {
199     return iterator_range<symbol_iterator>(symbol_begin(), symbol_end());
200   }
201
202   // Cast methods.
203   static inline bool classof(Binary const *v) {
204     return v->isArchive();
205   }
206
207   // check if a symbol is in the archive
208   child_iterator findSym(StringRef name) const;
209
210   bool hasSymbolTable() const;
211   child_iterator getSymbolTableChild() const { return SymbolTable; }
212   StringRef getSymbolTable() const {
213     // We know that the symbol table is not an external file,
214     // so we just assert there is no error.
215     return *SymbolTable->getBuffer();
216   }
217   uint32_t getNumberOfSymbols() const;
218
219 private:
220   child_iterator SymbolTable;
221   StringRef StringTable;
222   child_iterator FirstRegular;
223   unsigned Format : 2;
224   unsigned IsThin : 1;
225   mutable std::vector<std::unique_ptr<MemoryBuffer>> ThinBuffers;
226 };
227
228 }
229 }
230
231 #endif