OSDN Git Service

Merge "Don't try to use Clang on Windows yet."
[android-x86/external-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/ErrorOr.h"
21 #include "llvm/Support/FileSystem.h"
22 #include "llvm/Support/MemoryBuffer.h"
23
24 namespace llvm {
25 namespace object {
26 struct ArchiveMemberHeader {
27   char Name[16];
28   char LastModified[12];
29   char UID[6];
30   char GID[6];
31   char AccessMode[8];
32   char Size[10]; ///< Size of data, not including header or padding.
33   char Terminator[2];
34
35   /// Get the name without looking up long names.
36   llvm::StringRef getName() const;
37
38   /// Members are not larger than 4GB.
39   uint32_t getSize() const;
40
41   sys::fs::perms getAccessMode() const;
42   sys::TimeValue getLastModified() const;
43   unsigned getUID() const;
44   unsigned getGID() const;
45 };
46
47 class Archive : public Binary {
48   virtual void anchor();
49 public:
50   class Child {
51     const Archive *Parent;
52     /// \brief Includes header but not padding byte.
53     StringRef Data;
54     /// \brief Offset from Data to the start of the file.
55     uint16_t StartOfFile;
56
57     const ArchiveMemberHeader *getHeader() const {
58       return reinterpret_cast<const ArchiveMemberHeader *>(Data.data());
59     }
60
61   public:
62     Child(const Archive *Parent, const char *Start);
63
64     bool operator ==(const Child &other) const {
65       assert(Parent == other.Parent);
66       return Data.begin() == other.Data.begin();
67     }
68
69     bool operator <(const Child &other) const {
70       return Data.begin() < other.Data.begin();
71     }
72
73     Child getNext() const;
74
75     error_code getName(StringRef &Result) const;
76     StringRef getRawName() const { return getHeader()->getName(); }
77     sys::TimeValue getLastModified() const {
78       return getHeader()->getLastModified();
79     }
80     unsigned getUID() const { return getHeader()->getUID(); }
81     unsigned getGID() const { return getHeader()->getGID(); }
82     sys::fs::perms getAccessMode() const {
83       return getHeader()->getAccessMode();
84     }
85     /// \return the size of the archive member without the header or padding.
86     uint64_t getSize() const { return Data.size() - StartOfFile; }
87
88     StringRef getBuffer() const {
89       return StringRef(Data.data() + StartOfFile, getSize());
90     }
91
92     error_code getMemoryBuffer(OwningPtr<MemoryBuffer> &Result,
93                                bool FullPath = false) const;
94     error_code getMemoryBuffer(std::unique_ptr<MemoryBuffer> &Result,
95                                bool FullPath = false) const;
96
97     error_code getAsBinary(OwningPtr<Binary> &Result,
98                            LLVMContext *Context = 0) const;
99     error_code getAsBinary(std::unique_ptr<Binary> &Result,
100                            LLVMContext *Context = 0) const;
101   };
102
103   class child_iterator {
104     Child child;
105   public:
106     child_iterator() : child(Child(0, 0)) {}
107     child_iterator(const Child &c) : child(c) {}
108     const Child* operator->() const {
109       return &child;
110     }
111
112     bool operator==(const child_iterator &other) const {
113       return child == other.child;
114     }
115
116     bool operator!=(const child_iterator &other) const {
117       return !(*this == other);
118     }
119
120     bool operator <(const child_iterator &other) const {
121       return child < other.child;
122     }
123
124     child_iterator& operator++() {  // Preincrement
125       child = child.getNext();
126       return *this;
127     }
128   };
129
130   class Symbol {
131     const Archive *Parent;
132     uint32_t SymbolIndex;
133     uint32_t StringIndex; // Extra index to the string.
134
135   public:
136     bool operator ==(const Symbol &other) const {
137       return (Parent == other.Parent) && (SymbolIndex == other.SymbolIndex);
138     }
139
140     Symbol(const Archive *p, uint32_t symi, uint32_t stri)
141       : Parent(p)
142       , SymbolIndex(symi)
143       , StringIndex(stri) {}
144     error_code getName(StringRef &Result) const;
145     error_code getMember(child_iterator &Result) const;
146     Symbol getNext() const;
147   };
148
149   class symbol_iterator {
150     Symbol symbol;
151   public:
152     symbol_iterator(const Symbol &s) : symbol(s) {}
153     const Symbol *operator->() const {
154       return &symbol;
155     }
156
157     bool operator==(const symbol_iterator &other) const {
158       return symbol == other.symbol;
159     }
160
161     bool operator!=(const symbol_iterator &other) const {
162       return !(*this == other);
163     }
164
165     symbol_iterator& operator++() {  // Preincrement
166       symbol = symbol.getNext();
167       return *this;
168     }
169   };
170
171   Archive(MemoryBuffer *source, error_code &ec);
172   static ErrorOr<Archive *> create(MemoryBuffer *Source);
173
174   enum Kind {
175     K_GNU,
176     K_BSD,
177     K_COFF
178   };
179
180   Kind kind() const { 
181     return Format;
182   }
183
184   child_iterator child_begin(bool SkipInternal = true) const;
185   child_iterator child_end() const;
186
187   symbol_iterator symbol_begin() const;
188   symbol_iterator symbol_end() const;
189
190   // Cast methods.
191   static inline bool classof(Binary const *v) {
192     return v->isArchive();
193   }
194
195   // check if a symbol is in the archive
196   child_iterator findSym(StringRef name) const;
197
198   bool hasSymbolTable() const;
199
200 private:
201   child_iterator SymbolTable;
202   child_iterator StringTable;
203   child_iterator FirstRegular;
204   Kind Format;
205 };
206
207 }
208 }
209
210 #endif