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 static const ArchiveMemberHeader *ToHeader(const char *base) {
40 return reinterpret_cast<const ArchiveMemberHeader *>(base);
43 class Archive : public Binary {
44 virtual void anchor();
47 const Archive *Parent;
48 /// \brief Includes header but not padding byte.
50 /// \brief Offset from Data to the start of the file.
54 Child(const Archive *p, StringRef d);
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 ToHeader(Data.data())->getName(); }
68 int getLastModified() const;
71 int getAccessMode() const;
72 /// \return the size of the archive member without the header or padding.
73 uint64_t getSize() const { return Data.size() - StartOfFile; }
75 StringRef getBuffer() const {
76 return StringRef(Data.data() + StartOfFile, getSize());
79 error_code getMemoryBuffer(OwningPtr<MemoryBuffer> &Result,
80 bool FullPath = false) const;
82 error_code getAsBinary(OwningPtr<Binary> &Result) const;
85 class child_iterator {
88 child_iterator() : child(Child(0, StringRef())) {}
89 child_iterator(const Child &c) : child(c) {}
90 const Child* operator->() const {
94 bool operator==(const child_iterator &other) const {
95 return child == other.child;
98 bool operator!=(const child_iterator &other) const {
99 return !(*this == other);
102 bool operator <(const child_iterator &other) const {
103 return child < other.child;
106 child_iterator& operator++() { // Preincrement
107 child = child.getNext();
113 const Archive *Parent;
114 uint32_t SymbolIndex;
115 uint32_t StringIndex; // Extra index to the string.
118 bool operator ==(const Symbol &other) const {
119 return (Parent == other.Parent) && (SymbolIndex == other.SymbolIndex);
122 Symbol(const Archive *p, uint32_t symi, uint32_t stri)
125 , StringIndex(stri) {}
126 error_code getName(StringRef &Result) const;
127 error_code getMember(child_iterator &Result) const;
128 Symbol getNext() const;
131 class symbol_iterator {
134 symbol_iterator(const Symbol &s) : symbol(s) {}
135 const Symbol *operator->() const {
139 bool operator==(const symbol_iterator &other) const {
140 return symbol == other.symbol;
143 bool operator!=(const symbol_iterator &other) const {
144 return !(*this == other);
147 symbol_iterator& operator++() { // Preincrement
148 symbol = symbol.getNext();
153 Archive(MemoryBuffer *source, error_code &ec);
165 child_iterator begin_children(bool skip_internal = true) const;
166 child_iterator end_children() const;
168 symbol_iterator begin_symbols() const;
169 symbol_iterator end_symbols() const;
172 static inline bool classof(Binary const *v) {
173 return v->isArchive();
176 // check if a symbol is in the archive
177 child_iterator findSym(StringRef name) const;
180 child_iterator SymbolTable;
181 child_iterator StringTable;