1 //===- Archive.h - ar archive file format -----------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares the ar archive file format class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_OBJECT_ARCHIVE_H
15 #define LLVM_OBJECT_ARCHIVE_H
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"
32 class ArchiveMemberHeader {
35 ArchiveMemberHeader(Archive const *Parent, const char *RawHeaderPtr,
36 uint64_t Size, Error *Err);
37 // ArchiveMemberHeader() = default;
39 /// Get the name without looking up long names.
40 Expected<llvm::StringRef> getRawName() const;
42 /// Get the name looking up long names.
43 Expected<llvm::StringRef> getName(uint64_t Size) const;
45 /// Members are not larger than 4GB.
46 Expected<uint32_t> getSize() const;
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(' ');
54 Expected<unsigned> getUID() const;
55 Expected<unsigned> getGID() const;
57 // This returns the size of the private struct ArMemHdrType
58 uint64_t getSizeOf() const {
59 return sizeof(ArMemHdrType);
65 char LastModified[12];
69 char Size[10]; ///< Size of data, not including header or padding.
72 Archive const *Parent;
73 ArMemHdrType const *ArMemHdr;
76 class Archive : public Binary {
77 virtual void anchor();
81 const Archive *Parent;
82 friend ArchiveMemberHeader;
83 ArchiveMemberHeader Header;
84 /// \brief Includes header but not padding byte.
86 /// \brief Offset from Data to the start of the file.
89 Expected<bool> isThinMember() const;
92 Child(const Archive *Parent, const char *Start, Error *Err);
93 Child(const Archive *Parent, StringRef Data, uint16_t StartOfFile);
95 bool operator ==(const Child &other) const {
96 assert(!Parent || !other.Parent || Parent == other.Parent);
97 return Data.begin() == other.Data.begin();
100 const Archive *getParent() const { return Parent; }
101 Expected<Child> getNext() const;
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();
109 StringRef getRawLastModified() const {
110 return Header.getRawLastModified();
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();
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;
122 Expected<StringRef> getBuffer() const;
123 uint64_t getChildOffset() const;
125 Expected<MemoryBufferRef> getMemoryBufferRef() const;
127 Expected<std::unique_ptr<Binary>>
128 getAsBinary(LLVMContext *Context = nullptr) const;
131 class child_iterator {
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; }
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.
148 bool operator!=(const child_iterator &other) const {
149 return !(*this == other);
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())
160 C = C.getParent()->child_end().C;
161 *E = ChildOrErr.takeError();
169 const Archive *Parent;
170 uint32_t SymbolIndex;
171 uint32_t StringIndex; // Extra index to the string.
174 bool operator ==(const Symbol &other) const {
175 return (Parent == other.Parent) && (SymbolIndex == other.SymbolIndex);
178 Symbol(const Archive *p, uint32_t symi, uint32_t stri)
181 , StringIndex(stri) {}
182 StringRef getName() const;
183 Expected<Child> getMember() const;
184 Symbol getNext() const;
187 class symbol_iterator {
190 symbol_iterator(const Symbol &s) : symbol(s) {}
191 const Symbol *operator->() const { return &symbol; }
192 const Symbol &operator*() const { return symbol; }
194 bool operator==(const symbol_iterator &other) const {
195 return symbol == other.symbol;
198 bool operator!=(const symbol_iterator &other) const {
199 return !(*this == other);
202 symbol_iterator& operator++() { // Preincrement
203 symbol = symbol.getNext();
208 Archive(MemoryBufferRef Source, Error &Err);
209 static Expected<std::unique_ptr<Archive>> create(MemoryBufferRef Source);
219 Kind kind() const { return (Kind)Format; }
220 bool isThin() const { return IsThin; }
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());
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());
236 static inline bool classof(Binary const *v) {
237 return v->isArchive();
240 // check if a symbol is in the archive
241 Expected<Optional<Child>> findSym(StringRef name) const;
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;
249 std::vector<std::unique_ptr<MemoryBuffer>> takeThinBuffers() {
250 return std::move(ThinBuffers);
254 StringRef SymbolTable;
255 StringRef StringTable;
257 StringRef FirstRegularData;
258 uint16_t FirstRegularStartOfFile = -1;
259 void setFirstRegular(const Child &C);
263 mutable std::vector<std::unique_ptr<MemoryBuffer>> ThinBuffers;