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());
66 Child(const Archive *Parent, const char *Start);
68 bool operator ==(const Child &other) const {
69 assert(Parent == other.Parent);
70 return Data.begin() == other.Data.begin();
73 bool operator <(const Child &other) const {
74 return Data.begin() < other.Data.begin();
77 Child getNext() const;
79 ErrorOr<StringRef> getName() const;
80 StringRef getRawName() const { return getHeader()->getName(); }
81 sys::TimeValue getLastModified() const {
82 return getHeader()->getLastModified();
84 StringRef getRawLastModified() const {
85 return getHeader()->getRawLastModified();
87 unsigned getUID() const { return getHeader()->getUID(); }
88 unsigned getGID() const { return getHeader()->getGID(); }
89 sys::fs::perms getAccessMode() const {
90 return getHeader()->getAccessMode();
92 /// \return the size of the archive member without the header or padding.
93 uint64_t getSize() const;
94 /// \return the size of the archive member with the header and padding.
95 uint64_t getRawSize() const;
97 StringRef getBuffer() const {
98 return StringRef(Data.data() + StartOfFile, getSize());
100 uint64_t getChildOffset() const;
102 ErrorOr<MemoryBufferRef> getMemoryBufferRef() const;
104 ErrorOr<std::unique_ptr<Binary>>
105 getAsBinary(LLVMContext *Context = nullptr) const;
108 class child_iterator {
112 child_iterator() : child(Child(nullptr, nullptr)) {}
113 child_iterator(const Child &c) : child(c) {}
114 const Child *operator->() const { return &child; }
115 const Child &operator*() const { return child; }
117 bool operator==(const child_iterator &other) const {
118 return child == other.child;
121 bool operator!=(const child_iterator &other) const {
122 return !(*this == other);
125 bool operator<(const child_iterator &other) const {
126 return child < other.child;
129 child_iterator &operator++() { // Preincrement
130 child = child.getNext();
136 const Archive *Parent;
137 uint32_t SymbolIndex;
138 uint32_t StringIndex; // Extra index to the string.
141 bool operator ==(const Symbol &other) const {
142 return (Parent == other.Parent) && (SymbolIndex == other.SymbolIndex);
145 Symbol(const Archive *p, uint32_t symi, uint32_t stri)
148 , StringIndex(stri) {}
149 StringRef getName() const;
150 ErrorOr<child_iterator> getMember() const;
151 Symbol getNext() const;
154 class symbol_iterator {
157 symbol_iterator(const Symbol &s) : symbol(s) {}
158 const Symbol *operator->() const {
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);
185 Kind kind() const { return (Kind)Format; }
187 child_iterator child_begin(bool SkipInternal = true) const;
188 child_iterator child_end() const;
189 iterator_range<child_iterator> children(bool SkipInternal = true) const {
190 return iterator_range<child_iterator>(child_begin(SkipInternal),
194 symbol_iterator symbol_begin() const;
195 symbol_iterator symbol_end() const;
198 static inline bool classof(Binary const *v) {
199 return v->isArchive();
202 // check if a symbol is in the archive
203 child_iterator findSym(StringRef name) const;
205 bool hasSymbolTable() const;
206 child_iterator getSymbolTableChild() const { return SymbolTable; }
209 child_iterator SymbolTable;
210 child_iterator StringTable;
211 child_iterator FirstRegular;