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/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"
27 struct ArchiveMemberHeader {
29 char LastModified[12];
33 char Size[10]; ///< Size of data, not including header or padding.
36 /// Get the name without looking up long names.
37 llvm::StringRef getName() const;
39 /// Members are not larger than 4GB.
40 ErrorOr<uint32_t> getSize() const;
42 sys::fs::perms getAccessMode() const;
43 sys::TimeValue getLastModified() const;
44 llvm::StringRef getRawLastModified() const {
45 return StringRef(LastModified, sizeof(LastModified)).rtrim(" ");
47 unsigned getUID() const;
48 unsigned getGID() const;
51 class Archive : public Binary {
52 virtual void anchor();
55 const Archive *Parent;
56 /// \brief Includes header but not padding byte.
58 /// \brief Offset from Data to the start of the file.
61 const ArchiveMemberHeader *getHeader() const {
62 return reinterpret_cast<const ArchiveMemberHeader *>(Data.data());
65 bool isThinMember() const;
68 Child(const Archive *Parent, const char *Start,
70 static ErrorOr<std::unique_ptr<Child>> create(const Archive *Parent,
73 bool operator ==(const Child &other) const {
74 assert(Parent == other.Parent);
75 return Data.begin() == other.Data.begin();
78 bool operator <(const Child &other) const {
79 return Data.begin() < other.Data.begin();
82 const Archive *getParent() const { return Parent; }
83 ErrorOr<Child> getNext() const;
85 ErrorOr<StringRef> getName() const;
86 StringRef getRawName() const { return getHeader()->getName(); }
87 sys::TimeValue getLastModified() const {
88 return getHeader()->getLastModified();
90 StringRef getRawLastModified() const {
91 return getHeader()->getRawLastModified();
93 unsigned getUID() const { return getHeader()->getUID(); }
94 unsigned getGID() const { return getHeader()->getGID(); }
95 sys::fs::perms getAccessMode() const {
96 return getHeader()->getAccessMode();
98 /// \return the size of the archive member without the header or padding.
99 ErrorOr<uint64_t> getSize() const;
100 /// \return the size in the archive header for this member.
101 ErrorOr<uint64_t> getRawSize() const;
103 ErrorOr<StringRef> getBuffer() const;
104 uint64_t getChildOffset() const;
106 ErrorOr<MemoryBufferRef> getMemoryBufferRef() const;
108 ErrorOr<std::unique_ptr<Binary>>
109 getAsBinary(LLVMContext *Context = nullptr) const;
112 class child_iterator {
113 ErrorOr<Child> child;
116 child_iterator() : child(Child(nullptr, nullptr, nullptr)) {}
117 child_iterator(const Child &c) : child(c) {}
118 child_iterator(std::error_code EC) : child(EC) {}
119 const ErrorOr<Child> *operator->() const { return &child; }
120 const ErrorOr<Child> &operator*() const { return child; }
122 bool operator==(const child_iterator &other) const {
123 if ((*this)->getError())
125 if (other->getError())
127 return (*this)->get() == other->get();
130 bool operator!=(const child_iterator &other) const {
131 return !(*this == other);
134 // No operator< as we can't do less than compares with iterators that
137 // Code in loops with child_iterators must check for errors on each loop
138 // iteration. And if there is an error break out of the loop.
139 child_iterator &operator++() { // Preincrement
140 assert(child && "Can't increment iterator with error");
141 child = child->getNext();
147 const Archive *Parent;
148 uint32_t SymbolIndex;
149 uint32_t StringIndex; // Extra index to the string.
152 bool operator ==(const Symbol &other) const {
153 return (Parent == other.Parent) && (SymbolIndex == other.SymbolIndex);
156 Symbol(const Archive *p, uint32_t symi, uint32_t stri)
159 , StringIndex(stri) {}
160 StringRef getName() const;
161 ErrorOr<child_iterator> getMember() const;
162 Symbol getNext() const;
165 class symbol_iterator {
168 symbol_iterator(const Symbol &s) : symbol(s) {}
169 const Symbol *operator->() const { return &symbol; }
170 const Symbol &operator*() const { return symbol; }
172 bool operator==(const symbol_iterator &other) const {
173 return symbol == other.symbol;
176 bool operator!=(const symbol_iterator &other) const {
177 return !(*this == other);
180 symbol_iterator& operator++() { // Preincrement
181 symbol = symbol.getNext();
186 Archive(MemoryBufferRef Source, std::error_code &EC);
187 static ErrorOr<std::unique_ptr<Archive>> create(MemoryBufferRef Source);
196 Kind kind() const { return (Kind)Format; }
197 bool isThin() const { return IsThin; }
199 child_iterator child_begin(bool SkipInternal = true) const;
200 child_iterator child_end() const;
201 iterator_range<child_iterator> children(bool SkipInternal = true) const {
202 return iterator_range<child_iterator>(child_begin(SkipInternal),
206 symbol_iterator symbol_begin() const;
207 symbol_iterator symbol_end() const;
208 iterator_range<symbol_iterator> symbols() const {
209 return iterator_range<symbol_iterator>(symbol_begin(), symbol_end());
213 static inline bool classof(Binary const *v) {
214 return v->isArchive();
217 // check if a symbol is in the archive
218 child_iterator findSym(StringRef name) const;
220 bool hasSymbolTable() const;
221 child_iterator getSymbolTableChild() const { return SymbolTable; }
222 StringRef getSymbolTable() const {
223 // We know that the symbol table is not an external file,
224 // so we just assert there is no error.
225 return *(*SymbolTable)->getBuffer();
227 uint32_t getNumberOfSymbols() const;
230 child_iterator SymbolTable;
231 child_iterator StringTable;
232 child_iterator FirstRegular;
235 mutable std::vector<std::unique_ptr<MemoryBuffer>> ThinBuffers;