This removes the eating of the error in Archive::Child::getSize() when the characters
[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           std::error_code *EC);
70     static ErrorOr<std::unique_ptr<Child>> create(const Archive *Parent,
71                                                   const char *Start);
72
73     bool operator ==(const Child &other) const {
74       assert(Parent == other.Parent);
75       return Data.begin() == other.Data.begin();
76     }
77
78     bool operator <(const Child &other) const {
79       return Data.begin() < other.Data.begin();
80     }
81
82     const Archive *getParent() const { return Parent; }
83     ErrorOr<Child> getNext() const;
84
85     ErrorOr<StringRef> getName() const;
86     StringRef getRawName() const { return getHeader()->getName(); }
87     sys::TimeValue getLastModified() const {
88       return getHeader()->getLastModified();
89     }
90     StringRef getRawLastModified() const {
91       return getHeader()->getRawLastModified();
92     }
93     unsigned getUID() const { return getHeader()->getUID(); }
94     unsigned getGID() const { return getHeader()->getGID(); }
95     sys::fs::perms getAccessMode() const {
96       return getHeader()->getAccessMode();
97     }
98     /// \return the size of the archive member without the header or padding.
99     ErrorOr<uint64_t> getSize() const;
100     /// \return the size in the archive header for this member.
101     ErrorOr<uint64_t> getRawSize() const;
102
103     ErrorOr<StringRef> getBuffer() const;
104     uint64_t getChildOffset() const;
105
106     ErrorOr<MemoryBufferRef> getMemoryBufferRef() const;
107
108     ErrorOr<std::unique_ptr<Binary>>
109     getAsBinary(LLVMContext *Context = nullptr) const;
110   };
111
112   class child_iterator {
113     ErrorOr<Child> child;
114
115   public:
116     child_iterator() : child(Child(nullptr, nullptr, nullptr)) {}
117     child_iterator(const Child &c) : child(c) {}
118     child_iterator(std::error_code EC) : child(EC) {}
119     const ErrorOr<Child> *operator->() const { return &child; }
120     const ErrorOr<Child> &operator*() const { return child; }
121
122     bool operator==(const child_iterator &other) const {
123       if ((*this)->getError())
124         return false;
125       if (other->getError())
126         return false;
127       return (*this)->get() == other->get();
128     }
129
130     bool operator!=(const child_iterator &other) const {
131       return !(*this == other);
132     }
133
134     // No operator< as we can't do less than compares with iterators that
135     // contain errors.
136
137     // Code in loops with child_iterators must check for errors on each loop
138     // iteration.  And if there is an error break out of the loop.
139     child_iterator &operator++() { // Preincrement
140       assert(child && "Can't increment iterator with error");
141       child = child->getNext();
142       return *this;
143     }
144   };
145
146   class Symbol {
147     const Archive *Parent;
148     uint32_t SymbolIndex;
149     uint32_t StringIndex; // Extra index to the string.
150
151   public:
152     bool operator ==(const Symbol &other) const {
153       return (Parent == other.Parent) && (SymbolIndex == other.SymbolIndex);
154     }
155
156     Symbol(const Archive *p, uint32_t symi, uint32_t stri)
157       : Parent(p)
158       , SymbolIndex(symi)
159       , StringIndex(stri) {}
160     StringRef getName() const;
161     ErrorOr<child_iterator> getMember() const;
162     Symbol getNext() const;
163   };
164
165   class symbol_iterator {
166     Symbol symbol;
167   public:
168     symbol_iterator(const Symbol &s) : symbol(s) {}
169     const Symbol *operator->() const { return &symbol; }
170     const Symbol &operator*() const { return symbol; }
171
172     bool operator==(const symbol_iterator &other) const {
173       return symbol == other.symbol;
174     }
175
176     bool operator!=(const symbol_iterator &other) const {
177       return !(*this == other);
178     }
179
180     symbol_iterator& operator++() {  // Preincrement
181       symbol = symbol.getNext();
182       return *this;
183     }
184   };
185
186   Archive(MemoryBufferRef Source, std::error_code &EC);
187   static ErrorOr<std::unique_ptr<Archive>> create(MemoryBufferRef Source);
188
189   enum Kind {
190     K_GNU,
191     K_MIPS64,
192     K_BSD,
193     K_COFF
194   };
195
196   Kind kind() const { return (Kind)Format; }
197   bool isThin() const { return IsThin; }
198
199   child_iterator child_begin(bool SkipInternal = true) const;
200   child_iterator child_end() const;
201   iterator_range<child_iterator> children(bool SkipInternal = true) const {
202     return iterator_range<child_iterator>(child_begin(SkipInternal),
203                                           child_end());
204   }
205
206   symbol_iterator symbol_begin() const;
207   symbol_iterator symbol_end() const;
208   iterator_range<symbol_iterator> symbols() const {
209     return iterator_range<symbol_iterator>(symbol_begin(), symbol_end());
210   }
211
212   // Cast methods.
213   static inline bool classof(Binary const *v) {
214     return v->isArchive();
215   }
216
217   // check if a symbol is in the archive
218   child_iterator findSym(StringRef name) const;
219
220   bool hasSymbolTable() const;
221   child_iterator getSymbolTableChild() const { return SymbolTable; }
222   StringRef getSymbolTable() const {
223     // We know that the symbol table is not an external file,
224     // so we just assert there is no error.
225     return *(*SymbolTable)->getBuffer();
226   }
227   uint32_t getNumberOfSymbols() const;
228
229 private:
230   child_iterator SymbolTable;
231   child_iterator StringTable;
232   child_iterator FirstRegular;
233   unsigned Format : 2;
234   unsigned IsThin : 1;
235   mutable std::vector<std::unique_ptr<MemoryBuffer>> ThinBuffers;
236 };
237
238 }
239 }
240
241 #endif