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