]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/Object/Archive.h
Update to ELF Tool Chain snapshot at r3561
[FreeBSD/FreeBSD.git] / contrib / llvm / 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/Optional.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/ADT/iterator_range.h"
20 #include "llvm/Object/Binary.h"
21 #include "llvm/Support/Chrono.h"
22 #include "llvm/Support/Error.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/FileSystem.h"
25 #include "llvm/Support/MemoryBuffer.h"
26
27 namespace llvm {
28 namespace object {
29
30 class Archive;
31
32 class ArchiveMemberHeader {
33 public:
34   friend class Archive;
35   ArchiveMemberHeader(Archive const *Parent, const char *RawHeaderPtr,
36                       uint64_t Size, Error *Err);
37   // ArchiveMemberHeader() = default;
38
39   /// Get the name without looking up long names.
40   Expected<llvm::StringRef> getRawName() const;
41
42   /// Get the name looking up long names.
43   Expected<llvm::StringRef> getName(uint64_t Size) const;
44
45   /// Members are not larger than 4GB.
46   Expected<uint32_t> getSize() const;
47
48   Expected<sys::fs::perms> getAccessMode() const;
49   Expected<sys::TimePoint<std::chrono::seconds>> getLastModified() const;
50   llvm::StringRef getRawLastModified() const {
51     return StringRef(ArMemHdr->LastModified,
52                      sizeof(ArMemHdr->LastModified)).rtrim(' ');
53   }
54   Expected<unsigned> getUID() const;
55   Expected<unsigned> getGID() const;
56
57   // This returns the size of the private struct ArMemHdrType
58   uint64_t getSizeOf() const {
59     return sizeof(ArMemHdrType);
60   }
61
62 private:
63   struct ArMemHdrType {
64     char Name[16];
65     char LastModified[12];
66     char UID[6];
67     char GID[6];
68     char AccessMode[8];
69     char Size[10]; ///< Size of data, not including header or padding.
70     char Terminator[2];
71   };
72   Archive const *Parent;
73   ArMemHdrType const *ArMemHdr;
74 };
75
76 class Archive : public Binary {
77   virtual void anchor();
78 public:
79   class Child {
80     friend Archive;
81     const Archive *Parent;
82     friend ArchiveMemberHeader;
83     ArchiveMemberHeader Header;
84     /// \brief Includes header but not padding byte.
85     StringRef Data;
86     /// \brief Offset from Data to the start of the file.
87     uint16_t StartOfFile;
88
89     Expected<bool> isThinMember() const;
90
91   public:
92     Child(const Archive *Parent, const char *Start, Error *Err);
93     Child(const Archive *Parent, StringRef Data, uint16_t StartOfFile);
94
95     bool operator ==(const Child &other) const {
96       assert(!Parent || !other.Parent || Parent == other.Parent);
97       return Data.begin() == other.Data.begin();
98     }
99
100     const Archive *getParent() const { return Parent; }
101     Expected<Child> getNext() const;
102
103     Expected<StringRef> getName() const;
104     Expected<std::string> getFullName() const;
105     Expected<StringRef> getRawName() const { return Header.getRawName(); }
106     Expected<sys::TimePoint<std::chrono::seconds>> getLastModified() const {
107       return Header.getLastModified();
108     }
109     StringRef getRawLastModified() const {
110       return Header.getRawLastModified();
111     }
112     Expected<unsigned> getUID() const { return Header.getUID(); }
113     Expected<unsigned> getGID() const { return Header.getGID(); }
114     Expected<sys::fs::perms> getAccessMode() const {
115       return Header.getAccessMode();
116     }
117     /// \return the size of the archive member without the header or padding.
118     Expected<uint64_t> getSize() const;
119     /// \return the size in the archive header for this member.
120     Expected<uint64_t> getRawSize() const;
121
122     Expected<StringRef> getBuffer() const;
123     uint64_t getChildOffset() const;
124
125     Expected<MemoryBufferRef> getMemoryBufferRef() const;
126
127     Expected<std::unique_ptr<Binary>>
128     getAsBinary(LLVMContext *Context = nullptr) const;
129   };
130
131   class child_iterator {
132     Child C;
133     Error *E;
134
135   public:
136     child_iterator() : C(Child(nullptr, nullptr, nullptr)), E(nullptr) {}
137     child_iterator(const Child &C, Error *E) : C(C), E(E) {}
138     const Child *operator->() const { return &C; }
139     const Child &operator*() const { return C; }
140
141     bool operator==(const child_iterator &other) const {
142       // Ignore errors here: If an error occurred during increment then getNext
143       // will have been set to child_end(), and the following comparison should
144       // do the right thing.
145       return C == other.C;
146     }
147
148     bool operator!=(const child_iterator &other) const {
149       return !(*this == other);
150     }
151
152     // Code in loops with child_iterators must check for errors on each loop
153     // iteration.  And if there is an error break out of the loop.
154     child_iterator &operator++() { // Preincrement
155       assert(E && "Can't increment iterator with no Error attached");
156       ErrorAsOutParameter ErrAsOutParam(E);
157       if (auto ChildOrErr = C.getNext())
158         C = *ChildOrErr;
159       else {
160         C = C.getParent()->child_end().C;
161         *E = ChildOrErr.takeError();
162         E = nullptr;
163       }
164       return *this;
165     }
166   };
167
168   class Symbol {
169     const Archive *Parent;
170     uint32_t SymbolIndex;
171     uint32_t StringIndex; // Extra index to the string.
172
173   public:
174     bool operator ==(const Symbol &other) const {
175       return (Parent == other.Parent) && (SymbolIndex == other.SymbolIndex);
176     }
177
178     Symbol(const Archive *p, uint32_t symi, uint32_t stri)
179       : Parent(p)
180       , SymbolIndex(symi)
181       , StringIndex(stri) {}
182     StringRef getName() const;
183     Expected<Child> getMember() const;
184     Symbol getNext() const;
185   };
186
187   class symbol_iterator {
188     Symbol symbol;
189   public:
190     symbol_iterator(const Symbol &s) : symbol(s) {}
191     const Symbol *operator->() const { return &symbol; }
192     const Symbol &operator*() const { return symbol; }
193
194     bool operator==(const symbol_iterator &other) const {
195       return symbol == other.symbol;
196     }
197
198     bool operator!=(const symbol_iterator &other) const {
199       return !(*this == other);
200     }
201
202     symbol_iterator& operator++() {  // Preincrement
203       symbol = symbol.getNext();
204       return *this;
205     }
206   };
207
208   Archive(MemoryBufferRef Source, Error &Err);
209   static Expected<std::unique_ptr<Archive>> create(MemoryBufferRef Source);
210
211   enum Kind {
212     K_GNU,
213     K_MIPS64,
214     K_BSD,
215     K_DARWIN64,
216     K_COFF
217   };
218
219   Kind kind() const { return (Kind)Format; }
220   bool isThin() const { return IsThin; }
221
222   child_iterator child_begin(Error &Err, bool SkipInternal = true) const;
223   child_iterator child_end() const;
224   iterator_range<child_iterator> children(Error &Err,
225                                           bool SkipInternal = true) const {
226     return make_range(child_begin(Err, SkipInternal), child_end());
227   }
228
229   symbol_iterator symbol_begin() const;
230   symbol_iterator symbol_end() const;
231   iterator_range<symbol_iterator> symbols() const {
232     return make_range(symbol_begin(), symbol_end());
233   }
234
235   // Cast methods.
236   static inline bool classof(Binary const *v) {
237     return v->isArchive();
238   }
239
240   // check if a symbol is in the archive
241   Expected<Optional<Child>> findSym(StringRef name) const;
242
243   bool isEmpty() const;
244   bool hasSymbolTable() const;
245   StringRef getSymbolTable() const { return SymbolTable; }
246   StringRef getStringTable() const { return StringTable; }
247   uint32_t getNumberOfSymbols() const;
248
249   std::vector<std::unique_ptr<MemoryBuffer>> takeThinBuffers() {
250     return std::move(ThinBuffers);
251   }
252
253 private:
254   StringRef SymbolTable;
255   StringRef StringTable;
256
257   StringRef FirstRegularData;
258   uint16_t FirstRegularStartOfFile = -1;
259   void setFirstRegular(const Child &C);
260
261   unsigned Format : 3;
262   unsigned IsThin : 1;
263   mutable std::vector<std::unique_ptr<MemoryBuffer>> ThinBuffers;
264 };
265
266 }
267 }
268
269 #endif