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 /// Members are not larger than 4GB.
37 uint32_t getSize() const;
40 class Archive : public Binary {
41 virtual void anchor();
44 const Archive *Parent;
45 /// \brief Includes header but not padding byte.
47 /// \brief Offset from Data to the start of the file.
50 const ArchiveMemberHeader *getHeader() const {
51 return reinterpret_cast<const ArchiveMemberHeader *>(Data.data());
55 Child(const Archive *Parent, const char *Start);
57 bool operator ==(const Child &other) const {
58 assert(Parent == other.Parent);
59 return Data.begin() == other.Data.begin();
62 bool operator <(const Child &other) const {
63 return Data.begin() < other.Data.begin();
66 Child getNext() const;
68 error_code getName(StringRef &Result) const;
69 StringRef getRawName() const { return getHeader()->getName(); }
70 /// \return the size of the archive member without the header or padding.
71 uint64_t getSize() const { return Data.size() - StartOfFile; }
73 StringRef getBuffer() const {
74 return StringRef(Data.data() + StartOfFile, getSize());
77 error_code getMemoryBuffer(OwningPtr<MemoryBuffer> &Result,
78 bool FullPath = false) const;
80 error_code getAsBinary(OwningPtr<Binary> &Result) const;
83 class child_iterator {
86 child_iterator() : child(Child(0, 0)) {}
87 child_iterator(const Child &c) : child(c) {}
88 const Child* operator->() const {
92 bool operator==(const child_iterator &other) const {
93 return child == other.child;
96 bool operator!=(const child_iterator &other) const {
97 return !(*this == other);
100 bool operator <(const child_iterator &other) const {
101 return child < other.child;
104 child_iterator& operator++() { // Preincrement
105 child = child.getNext();
111 const Archive *Parent;
112 uint32_t SymbolIndex;
113 uint32_t StringIndex; // Extra index to the string.
116 bool operator ==(const Symbol &other) const {
117 return (Parent == other.Parent) && (SymbolIndex == other.SymbolIndex);
120 Symbol(const Archive *p, uint32_t symi, uint32_t stri)
123 , StringIndex(stri) {}
124 error_code getName(StringRef &Result) const;
125 error_code getMember(child_iterator &Result) const;
126 Symbol getNext() const;
129 class symbol_iterator {
132 symbol_iterator(const Symbol &s) : symbol(s) {}
133 const Symbol *operator->() const {
137 bool operator==(const symbol_iterator &other) const {
138 return symbol == other.symbol;
141 bool operator!=(const symbol_iterator &other) const {
142 return !(*this == other);
145 symbol_iterator& operator++() { // Preincrement
146 symbol = symbol.getNext();
151 Archive(MemoryBuffer *source, error_code &ec);
163 child_iterator begin_children(bool skip_internal = true) const;
164 child_iterator end_children() const;
166 symbol_iterator begin_symbols() const;
167 symbol_iterator end_symbols() const;
170 static inline bool classof(Binary const *v) {
171 return v->isArchive();
174 // check if a symbol is in the archive
175 child_iterator findSym(StringRef name) const;
178 child_iterator SymbolTable;
179 child_iterator StringTable;