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/MemoryBuffer.h"
24 struct ArchiveMemberHeader {
26 char LastModified[12];
30 char Size[10]; ///< Size of data, not including header or padding.
33 /// Get the name without looking up long names.
34 llvm::StringRef getName() const;
36 uint64_t getSize() const;
39 class Archive : public Binary {
40 virtual void anchor();
43 const Archive *Parent;
44 /// \brief Includes header but not padding byte.
46 /// \brief Offset from Data to the start of the file.
49 const ArchiveMemberHeader *getHeader() const {
50 return reinterpret_cast<const ArchiveMemberHeader *>(Data.data());
54 Child(const Archive *Parent, const char *Start);
56 bool operator ==(const Child &other) const {
57 return (Parent == other.Parent) && (Data.begin() == other.Data.begin());
60 bool operator <(const Child &other) const {
61 return Data.begin() < other.Data.begin();
64 Child getNext() const;
66 error_code getName(StringRef &Result) const;
67 StringRef getRawName() const { return getHeader()->getName(); }
68 /// \return the size of the archive member without the header or padding.
69 uint64_t getSize() const { return Data.size() - StartOfFile; }
71 StringRef getBuffer() const {
72 return StringRef(Data.data() + StartOfFile, getSize());
75 error_code getMemoryBuffer(OwningPtr<MemoryBuffer> &Result,
76 bool FullPath = false) const;
78 error_code getAsBinary(OwningPtr<Binary> &Result) const;
81 class child_iterator {
84 child_iterator() : child(Child(0, 0)) {}
85 child_iterator(const Child &c) : child(c) {}
86 const Child* operator->() const {
90 bool operator==(const child_iterator &other) const {
91 return child == other.child;
94 bool operator!=(const child_iterator &other) const {
95 return !(*this == other);
98 bool operator <(const child_iterator &other) const {
99 return child < other.child;
102 child_iterator& operator++() { // Preincrement
103 child = child.getNext();
109 const Archive *Parent;
110 uint32_t SymbolIndex;
111 uint32_t StringIndex; // Extra index to the string.
114 bool operator ==(const Symbol &other) const {
115 return (Parent == other.Parent) && (SymbolIndex == other.SymbolIndex);
118 Symbol(const Archive *p, uint32_t symi, uint32_t stri)
121 , StringIndex(stri) {}
122 error_code getName(StringRef &Result) const;
123 error_code getMember(child_iterator &Result) const;
124 Symbol getNext() const;
127 class symbol_iterator {
130 symbol_iterator(const Symbol &s) : symbol(s) {}
131 const Symbol *operator->() const {
135 bool operator==(const symbol_iterator &other) const {
136 return symbol == other.symbol;
139 bool operator!=(const symbol_iterator &other) const {
140 return !(*this == other);
143 symbol_iterator& operator++() { // Preincrement
144 symbol = symbol.getNext();
149 Archive(MemoryBuffer *source, error_code &ec);
161 child_iterator begin_children(bool skip_internal = true) const;
162 child_iterator end_children() const;
164 symbol_iterator begin_symbols() const;
165 symbol_iterator end_symbols() const;
168 static inline bool classof(Binary const *v) {
169 return v->isArchive();
172 // check if a symbol is in the archive
173 child_iterator findSym(StringRef name) const;
176 child_iterator SymbolTable;
177 child_iterator StringTable;