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/Object/Binary.h"
19 #include "llvm/Support/ErrorHandling.h"
20 #include "llvm/Support/ErrorOr.h"
21 #include "llvm/Support/FileSystem.h"
22 #include "llvm/Support/MemoryBuffer.h"
26 struct ArchiveMemberHeader {
28 char LastModified[12];
32 char Size[10]; ///< Size of data, not including header or padding.
35 /// Get the name without looking up long names.
36 llvm::StringRef getName() const;
38 /// Members are not larger than 4GB.
39 uint32_t getSize() const;
41 sys::fs::perms getAccessMode() const;
42 sys::TimeValue getLastModified() const;
43 unsigned getUID() const;
44 unsigned getGID() const;
47 class Archive : public Binary {
48 virtual void anchor();
51 const Archive *Parent;
52 /// \brief Includes header but not padding byte.
54 /// \brief Offset from Data to the start of the file.
57 const ArchiveMemberHeader *getHeader() const {
58 return reinterpret_cast<const ArchiveMemberHeader *>(Data.data());
62 Child(const Archive *Parent, const char *Start);
64 bool operator ==(const Child &other) const {
65 assert(Parent == other.Parent);
66 return Data.begin() == other.Data.begin();
69 bool operator <(const Child &other) const {
70 return Data.begin() < other.Data.begin();
73 Child getNext() const;
75 error_code getName(StringRef &Result) const;
76 StringRef getRawName() const { return getHeader()->getName(); }
77 sys::TimeValue getLastModified() const {
78 return getHeader()->getLastModified();
80 unsigned getUID() const { return getHeader()->getUID(); }
81 unsigned getGID() const { return getHeader()->getGID(); }
82 sys::fs::perms getAccessMode() const {
83 return getHeader()->getAccessMode();
85 /// \return the size of the archive member without the header or padding.
86 uint64_t getSize() const { return Data.size() - StartOfFile; }
88 StringRef getBuffer() const {
89 return StringRef(Data.data() + StartOfFile, getSize());
92 error_code getMemoryBuffer(OwningPtr<MemoryBuffer> &Result,
93 bool FullPath = false) const;
94 error_code getMemoryBuffer(std::unique_ptr<MemoryBuffer> &Result,
95 bool FullPath = false) const;
97 error_code getAsBinary(OwningPtr<Binary> &Result,
98 LLVMContext *Context = 0) const;
99 error_code getAsBinary(std::unique_ptr<Binary> &Result,
100 LLVMContext *Context = 0) const;
103 class child_iterator {
106 child_iterator() : child(Child(0, 0)) {}
107 child_iterator(const Child &c) : child(c) {}
108 const Child* operator->() const {
112 bool operator==(const child_iterator &other) const {
113 return child == other.child;
116 bool operator!=(const child_iterator &other) const {
117 return !(*this == other);
120 bool operator <(const child_iterator &other) const {
121 return child < other.child;
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 error_code getName(StringRef &Result) const;
145 error_code getMember(child_iterator &Result) const;
146 Symbol getNext() const;
149 class symbol_iterator {
152 symbol_iterator(const Symbol &s) : symbol(s) {}
153 const Symbol *operator->() const {
157 bool operator==(const symbol_iterator &other) const {
158 return symbol == other.symbol;
161 bool operator!=(const symbol_iterator &other) const {
162 return !(*this == other);
165 symbol_iterator& operator++() { // Preincrement
166 symbol = symbol.getNext();
171 Archive(MemoryBuffer *source, error_code &ec);
172 static ErrorOr<Archive *> create(MemoryBuffer *Source);
184 child_iterator child_begin(bool SkipInternal = true) const;
185 child_iterator child_end() const;
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;