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();
56 const Archive *Parent;
57 /// \brief Includes header but not padding byte.
59 /// \brief Offset from Data to the start of the file.
62 const ArchiveMemberHeader *getHeader() const {
63 return reinterpret_cast<const ArchiveMemberHeader *>(Data.data());
66 bool isThinMember() const;
69 Child(const Archive *Parent, const char *Start);
70 Child(const Archive *Parent, StringRef Data, uint16_t StartOfFile);
72 bool operator ==(const Child &other) const {
73 assert(Parent == other.Parent);
74 return Data.begin() == other.Data.begin();
77 const Archive *getParent() const { return Parent; }
78 Child getNext() const;
80 ErrorOr<StringRef> getName() const;
81 StringRef getRawName() const { return getHeader()->getName(); }
82 sys::TimeValue getLastModified() const {
83 return getHeader()->getLastModified();
85 StringRef getRawLastModified() const {
86 return getHeader()->getRawLastModified();
88 unsigned getUID() const { return getHeader()->getUID(); }
89 unsigned getGID() const { return getHeader()->getGID(); }
90 sys::fs::perms getAccessMode() const {
91 return getHeader()->getAccessMode();
93 /// \return the size of the archive member without the header or padding.
94 uint64_t getSize() const;
95 /// \return the size in the archive header for this member.
96 uint64_t getRawSize() const;
98 ErrorOr<StringRef> getBuffer() const;
99 uint64_t getChildOffset() const;
101 ErrorOr<MemoryBufferRef> getMemoryBufferRef() const;
103 ErrorOr<std::unique_ptr<Binary>>
104 getAsBinary(LLVMContext *Context = nullptr) const;
107 class child_iterator {
111 child_iterator() : child(Child(nullptr, nullptr)) {}
112 child_iterator(const Child &c) : child(c) {}
113 const Child *operator->() const { return &child; }
114 const Child &operator*() const { return child; }
116 bool operator==(const child_iterator &other) const {
117 return child == other.child;
120 bool operator!=(const child_iterator &other) const {
121 return !(*this == other);
124 child_iterator &operator++() { // Preincrement
125 child = child.getNext();
131 const Archive *Parent;
132 uint32_t SymbolIndex;
133 uint32_t StringIndex; // Extra index to the string.
136 bool operator ==(const Symbol &other) const {
137 return (Parent == other.Parent) && (SymbolIndex == other.SymbolIndex);
140 Symbol(const Archive *p, uint32_t symi, uint32_t stri)
143 , StringIndex(stri) {}
144 StringRef getName() const;
145 ErrorOr<Child> getMember() const;
146 Symbol getNext() const;
149 class symbol_iterator {
152 symbol_iterator(const Symbol &s) : symbol(s) {}
153 const Symbol *operator->() const { return &symbol; }
154 const Symbol &operator*() const { return symbol; }
156 bool operator==(const symbol_iterator &other) const {
157 return symbol == other.symbol;
160 bool operator!=(const symbol_iterator &other) const {
161 return !(*this == other);
164 symbol_iterator& operator++() { // Preincrement
165 symbol = symbol.getNext();
170 Archive(MemoryBufferRef Source, std::error_code &EC);
171 static ErrorOr<std::unique_ptr<Archive>> create(MemoryBufferRef Source);
180 Kind kind() const { return (Kind)Format; }
181 bool isThin() const { return IsThin; }
183 child_iterator child_begin(bool SkipInternal = true) const;
184 child_iterator child_end() const;
185 iterator_range<child_iterator> children(bool SkipInternal = true) const {
186 return iterator_range<child_iterator>(child_begin(SkipInternal),
190 symbol_iterator symbol_begin() const;
191 symbol_iterator symbol_end() const;
192 iterator_range<symbol_iterator> symbols() const {
193 return iterator_range<symbol_iterator>(symbol_begin(), symbol_end());
197 static inline bool classof(Binary const *v) {
198 return v->isArchive();
201 // check if a symbol is in the archive
202 child_iterator findSym(StringRef name) const;
204 bool hasSymbolTable() const;
205 StringRef getSymbolTable() const { return SymbolTable; }
206 uint32_t getNumberOfSymbols() const;
209 StringRef SymbolTable;
210 StringRef StringTable;
212 StringRef FirstRegularData;
213 uint16_t FirstRegularStartOfFile = -1;
214 void setFirstRegular(const Child &C);
218 mutable std::vector<std::unique_ptr<MemoryBuffer>> ThinBuffers;