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 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 bool operator ==(const Child &other) const {
71 assert(Parent == other.Parent);
72 return Data.begin() == other.Data.begin();
75 bool operator <(const Child &other) const {
76 return Data.begin() < other.Data.begin();
79 const Archive *getParent() const { return Parent; }
80 Child getNext() const;
82 ErrorOr<StringRef> getName() const;
83 StringRef getRawName() const { return getHeader()->getName(); }
84 sys::TimeValue getLastModified() const {
85 return getHeader()->getLastModified();
87 StringRef getRawLastModified() const {
88 return getHeader()->getRawLastModified();
90 unsigned getUID() const { return getHeader()->getUID(); }
91 unsigned getGID() const { return getHeader()->getGID(); }
92 sys::fs::perms getAccessMode() const {
93 return getHeader()->getAccessMode();
95 /// \return the size of the archive member without the header or padding.
96 uint64_t getSize() const;
97 /// \return the size in the archive header for this member.
98 uint64_t getRawSize() const;
100 ErrorOr<StringRef> getBuffer() const;
101 uint64_t getChildOffset() const;
103 ErrorOr<MemoryBufferRef> getMemoryBufferRef() const;
105 ErrorOr<std::unique_ptr<Binary>>
106 getAsBinary(LLVMContext *Context = nullptr) const;
109 class child_iterator {
113 child_iterator() : child(Child(nullptr, nullptr)) {}
114 child_iterator(const Child &c) : child(c) {}
115 const Child *operator->() const { return &child; }
116 const Child &operator*() const { return child; }
118 bool operator==(const child_iterator &other) const {
119 return child == other.child;
122 bool operator!=(const child_iterator &other) const {
123 return !(*this == other);
126 bool operator<(const child_iterator &other) const {
127 return child < other.child;
130 child_iterator &operator++() { // Preincrement
131 child = child.getNext();
137 const Archive *Parent;
138 uint32_t SymbolIndex;
139 uint32_t StringIndex; // Extra index to the string.
142 bool operator ==(const Symbol &other) const {
143 return (Parent == other.Parent) && (SymbolIndex == other.SymbolIndex);
146 Symbol(const Archive *p, uint32_t symi, uint32_t stri)
149 , StringIndex(stri) {}
150 StringRef getName() const;
151 ErrorOr<child_iterator> getMember() const;
152 Symbol getNext() const;
155 class symbol_iterator {
158 symbol_iterator(const Symbol &s) : symbol(s) {}
159 const Symbol *operator->() const { return &symbol; }
160 const Symbol &operator*() const { return symbol; }
162 bool operator==(const symbol_iterator &other) const {
163 return symbol == other.symbol;
166 bool operator!=(const symbol_iterator &other) const {
167 return !(*this == other);
170 symbol_iterator& operator++() { // Preincrement
171 symbol = symbol.getNext();
176 Archive(MemoryBufferRef Source, std::error_code &EC);
177 static ErrorOr<std::unique_ptr<Archive>> create(MemoryBufferRef Source);
186 Kind kind() const { return (Kind)Format; }
187 bool isThin() const { return IsThin; }
189 child_iterator child_begin(bool SkipInternal = true) const;
190 child_iterator child_end() const;
191 iterator_range<child_iterator> children(bool SkipInternal = true) const {
192 return iterator_range<child_iterator>(child_begin(SkipInternal),
196 symbol_iterator symbol_begin() const;
197 symbol_iterator symbol_end() const;
198 iterator_range<symbol_iterator> symbols() const {
199 return iterator_range<symbol_iterator>(symbol_begin(), symbol_end());
203 static inline bool classof(Binary const *v) {
204 return v->isArchive();
207 // check if a symbol is in the archive
208 child_iterator findSym(StringRef name) const;
210 bool hasSymbolTable() const;
211 child_iterator getSymbolTableChild() const { return SymbolTable; }
212 StringRef getSymbolTable() const {
213 // We know that the symbol table is not an external file,
214 // so we just assert there is no error.
215 return *SymbolTable->getBuffer();
217 uint32_t getNumberOfSymbols() const;
220 child_iterator SymbolTable;
221 child_iterator StringTable;
222 child_iterator FirstRegular;
225 mutable std::vector<std::unique_ptr<MemoryBuffer>> ThinBuffers;