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;
95 error_code getAsBinary(OwningPtr<Binary> &Result) const;
98 class child_iterator {
101 child_iterator() : child(Child(0, 0)) {}
102 child_iterator(const Child &c) : child(c) {}
103 const Child* operator->() const {
107 bool operator==(const child_iterator &other) const {
108 return child == other.child;
111 bool operator!=(const child_iterator &other) const {
112 return !(*this == other);
115 bool operator <(const child_iterator &other) const {
116 return child < other.child;
119 child_iterator& operator++() { // Preincrement
120 child = child.getNext();
126 const Archive *Parent;
127 uint32_t SymbolIndex;
128 uint32_t StringIndex; // Extra index to the string.
131 bool operator ==(const Symbol &other) const {
132 return (Parent == other.Parent) && (SymbolIndex == other.SymbolIndex);
135 Symbol(const Archive *p, uint32_t symi, uint32_t stri)
138 , StringIndex(stri) {}
139 error_code getName(StringRef &Result) const;
140 error_code getMember(child_iterator &Result) const;
141 Symbol getNext() const;
144 class symbol_iterator {
147 symbol_iterator(const Symbol &s) : symbol(s) {}
148 const Symbol *operator->() const {
152 bool operator==(const symbol_iterator &other) const {
153 return symbol == other.symbol;
156 bool operator!=(const symbol_iterator &other) const {
157 return !(*this == other);
160 symbol_iterator& operator++() { // Preincrement
161 symbol = symbol.getNext();
166 Archive(MemoryBuffer *source, error_code &ec);
167 static ErrorOr<Archive *> create(MemoryBuffer *Source);
179 child_iterator child_begin(bool SkipInternal = true) const;
180 child_iterator child_end() const;
182 symbol_iterator symbol_begin() const;
183 symbol_iterator symbol_end() const;
186 static inline bool classof(Binary const *v) {
187 return v->isArchive();
190 // check if a symbol is in the archive
191 child_iterator findSym(StringRef name) const;
193 bool hasSymbolTable() const;
196 child_iterator SymbolTable;
197 child_iterator StringTable;
198 child_iterator FirstRegular;