8f9c7265754e6b6923d9b39e11718576cf59408d
[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/Object/Binary.h"
19 #include "llvm/Support/ErrorHandling.h"
20 #include "llvm/Support/MemoryBuffer.h"
21
22 namespace llvm {
23 namespace object {
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.
34   llvm::StringRef getName() const;
35
36   uint64_t getSize() const;
37 };
38
39 static const ArchiveMemberHeader *ToHeader(const char *base) {
40   return reinterpret_cast<const ArchiveMemberHeader *>(base);
41 }
42
43 class Archive : public Binary {
44   virtual void anchor();
45 public:
46   class Child {
47     const Archive *Parent;
48     /// \brief Includes header but not padding byte.
49     StringRef Data;
50     /// \brief Offset from Data to the start of the file.
51     uint16_t StartOfFile;
52
53   public:
54     Child(const Archive *p, StringRef d);
55
56     bool operator ==(const Child &other) const {
57       return (Parent == other.Parent) && (Data.begin() == other.Data.begin());
58     }
59
60     bool operator <(const Child &other) const {
61       return Data.begin() < other.Data.begin();
62     }
63
64     Child getNext() const;
65
66     error_code getName(StringRef &Result) const;
67     StringRef getRawName() const { return ToHeader(Data.data())->getName(); }
68     int getLastModified() const;
69     int getUID() const;
70     int getGID() const;
71     int getAccessMode() const;
72     /// \return the size of the archive member without the header or padding.
73     uint64_t getSize() const { return Data.size() - StartOfFile; }
74
75     StringRef getBuffer() const {
76       return StringRef(Data.data() + StartOfFile, getSize());
77     }
78
79     error_code getMemoryBuffer(OwningPtr<MemoryBuffer> &Result,
80                                bool FullPath = false) const;
81
82     error_code getAsBinary(OwningPtr<Binary> &Result) const;
83   };
84
85   class child_iterator {
86     Child child;
87   public:
88     child_iterator() : child(Child(0, StringRef())) {}
89     child_iterator(const Child &c) : child(c) {}
90     const Child* operator->() const {
91       return &child;
92     }
93
94     bool operator==(const child_iterator &other) const {
95       return child == other.child;
96     }
97
98     bool operator!=(const child_iterator &other) const {
99       return !(*this == other);
100     }
101
102     bool operator <(const child_iterator &other) const {
103       return child < other.child;
104     }
105
106     child_iterator& operator++() {  // Preincrement
107       child = child.getNext();
108       return *this;
109     }
110   };
111
112   class Symbol {
113     const Archive *Parent;
114     uint32_t SymbolIndex;
115     uint32_t StringIndex; // Extra index to the string.
116
117   public:
118     bool operator ==(const Symbol &other) const {
119       return (Parent == other.Parent) && (SymbolIndex == other.SymbolIndex);
120     }
121
122     Symbol(const Archive *p, uint32_t symi, uint32_t stri)
123       : Parent(p)
124       , SymbolIndex(symi)
125       , StringIndex(stri) {}
126     error_code getName(StringRef &Result) const;
127     error_code getMember(child_iterator &Result) const;
128     Symbol getNext() const;
129   };
130
131   class symbol_iterator {
132     Symbol symbol;
133   public:
134     symbol_iterator(const Symbol &s) : symbol(s) {}
135     const Symbol *operator->() const {
136       return &symbol;
137     }
138
139     bool operator==(const symbol_iterator &other) const {
140       return symbol == other.symbol;
141     }
142
143     bool operator!=(const symbol_iterator &other) const {
144       return !(*this == other);
145     }
146
147     symbol_iterator& operator++() {  // Preincrement
148       symbol = symbol.getNext();
149       return *this;
150     }
151   };
152
153   Archive(MemoryBuffer *source, error_code &ec);
154
155   enum Kind {
156     K_GNU,
157     K_BSD,
158     K_COFF
159   };
160
161   Kind kind() const { 
162     return Format;
163   }
164
165   child_iterator begin_children(bool skip_internal = true) const;
166   child_iterator end_children() const;
167
168   symbol_iterator begin_symbols() const;
169   symbol_iterator end_symbols() const;
170
171   // Cast methods.
172   static inline bool classof(Binary const *v) {
173     return v->isArchive();
174   }
175
176   // check if a symbol is in the archive
177   child_iterator findSym(StringRef name) const;
178
179 private:
180   child_iterator SymbolTable;
181   child_iterator StringTable;
182   Kind Format;
183 };
184
185 }
186 }
187
188 #endif