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/iterator_range.h"
18 #include "llvm/ADT/StringRef.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 unsigned getUID() const;
45 unsigned getGID() const;
48 class Archive : public Binary {
49 virtual void anchor();
52 const Archive *Parent;
53 /// \brief Includes header but not padding byte.
55 /// \brief Offset from Data to the start of the file.
58 const ArchiveMemberHeader *getHeader() const {
59 return reinterpret_cast<const ArchiveMemberHeader *>(Data.data());
63 Child(const Archive *Parent, const char *Start);
65 bool operator ==(const Child &other) const {
66 assert(Parent == other.Parent);
67 return Data.begin() == other.Data.begin();
70 bool operator <(const Child &other) const {
71 return Data.begin() < other.Data.begin();
74 Child getNext() const;
76 ErrorOr<StringRef> getName() const;
77 StringRef getRawName() const { return getHeader()->getName(); }
78 sys::TimeValue getLastModified() const {
79 return getHeader()->getLastModified();
81 unsigned getUID() const { return getHeader()->getUID(); }
82 unsigned getGID() const { return getHeader()->getGID(); }
83 sys::fs::perms getAccessMode() const {
84 return getHeader()->getAccessMode();
86 /// \return the size of the archive member without the header or padding.
87 uint64_t getSize() const { return Data.size() - StartOfFile; }
89 StringRef getBuffer() const {
90 return StringRef(Data.data() + StartOfFile, getSize());
93 ErrorOr<MemoryBufferRef> getMemoryBufferRef() const;
95 ErrorOr<std::unique_ptr<Binary>>
96 getAsBinary(LLVMContext *Context = nullptr) const;
99 class child_iterator {
103 child_iterator() : child(Child(nullptr, nullptr)) {}
104 child_iterator(const Child &c) : child(c) {}
105 const Child *operator->() const { return &child; }
106 const Child &operator*() const { return child; }
108 bool operator==(const child_iterator &other) const {
109 return child == other.child;
112 bool operator!=(const child_iterator &other) const {
113 return !(*this == other);
116 bool operator<(const child_iterator &other) const {
117 return child < other.child;
120 child_iterator &operator++() { // Preincrement
121 child = child.getNext();
127 const Archive *Parent;
128 uint32_t SymbolIndex;
129 uint32_t StringIndex; // Extra index to the string.
132 bool operator ==(const Symbol &other) const {
133 return (Parent == other.Parent) && (SymbolIndex == other.SymbolIndex);
136 Symbol(const Archive *p, uint32_t symi, uint32_t stri)
139 , StringIndex(stri) {}
140 StringRef getName() const;
141 ErrorOr<child_iterator> getMember() const;
142 Symbol getNext() const;
145 class symbol_iterator {
148 symbol_iterator(const Symbol &s) : symbol(s) {}
149 const Symbol *operator->() const {
153 bool operator==(const symbol_iterator &other) const {
154 return symbol == other.symbol;
157 bool operator!=(const symbol_iterator &other) const {
158 return !(*this == other);
161 symbol_iterator& operator++() { // Preincrement
162 symbol = symbol.getNext();
167 Archive(MemoryBufferRef Source, std::error_code &EC);
168 static ErrorOr<std::unique_ptr<Archive>> create(MemoryBufferRef Source);
180 child_iterator child_begin(bool SkipInternal = true) const;
181 child_iterator child_end() const;
182 iterator_range<child_iterator> children(bool SkipInternal = true) const {
183 return iterator_range<child_iterator>(child_begin(SkipInternal),
187 symbol_iterator symbol_begin() const;
188 symbol_iterator symbol_end() const;
191 static inline bool classof(Binary const *v) {
192 return v->isArchive();
195 // check if a symbol is in the archive
196 child_iterator findSym(StringRef name) const;
198 bool hasSymbolTable() const;
201 child_iterator SymbolTable;
202 child_iterator StringTable;
203 child_iterator FirstRegular;