]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/Object/Archive.h
Merge ^/head r288197 through r288456.
[FreeBSD/FreeBSD.git] / contrib / llvm / include / llvm / Object / Archive.h
1 //===- Archive.h - ar archive file format -----------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file declares the ar archive file format class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_OBJECT_ARCHIVE_H
15 #define LLVM_OBJECT_ARCHIVE_H
16
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/ADT/iterator_range.h"
19 #include "llvm/Object/Binary.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/ErrorOr.h"
22 #include "llvm/Support/FileSystem.h"
23 #include "llvm/Support/MemoryBuffer.h"
24
25 namespace llvm {
26 namespace object {
27 struct ArchiveMemberHeader {
28   char Name[16];
29   char LastModified[12];
30   char UID[6];
31   char GID[6];
32   char AccessMode[8];
33   char Size[10]; ///< Size of data, not including header or padding.
34   char Terminator[2];
35
36   /// Get the name without looking up long names.
37   llvm::StringRef getName() const;
38
39   /// Members are not larger than 4GB.
40   uint32_t getSize() const;
41
42   sys::fs::perms getAccessMode() const;
43   sys::TimeValue getLastModified() const;
44   llvm::StringRef getRawLastModified() const {
45     return StringRef(LastModified, sizeof(LastModified)).rtrim(" ");
46   }
47   unsigned getUID() const;
48   unsigned getGID() const;
49 };
50
51 class Archive : public Binary {
52   virtual void anchor();
53 public:
54   class Child {
55     const Archive *Parent;
56     /// \brief Includes header but not padding byte.
57     StringRef Data;
58     /// \brief Offset from Data to the start of the file.
59     uint16_t StartOfFile;
60
61     const ArchiveMemberHeader *getHeader() const {
62       return reinterpret_cast<const ArchiveMemberHeader *>(Data.data());
63     }
64
65   public:
66     Child(const Archive *Parent, const char *Start);
67
68     bool operator ==(const Child &other) const {
69       assert(Parent == other.Parent);
70       return Data.begin() == other.Data.begin();
71     }
72
73     bool operator <(const Child &other) const {
74       return Data.begin() < other.Data.begin();
75     }
76
77     Child getNext() const;
78
79     ErrorOr<StringRef> getName() const;
80     StringRef getRawName() const { return getHeader()->getName(); }
81     sys::TimeValue getLastModified() const {
82       return getHeader()->getLastModified();
83     }
84     StringRef getRawLastModified() const {
85       return getHeader()->getRawLastModified();
86     }
87     unsigned getUID() const { return getHeader()->getUID(); }
88     unsigned getGID() const { return getHeader()->getGID(); }
89     sys::fs::perms getAccessMode() const {
90       return getHeader()->getAccessMode();
91     }
92     /// \return the size of the archive member without the header or padding.
93     uint64_t getSize() const;
94     /// \return the size in the archive header for this member.
95     uint64_t getRawSize() const;
96
97     ErrorOr<StringRef> getBuffer() const;
98     uint64_t getChildOffset() const;
99
100     ErrorOr<MemoryBufferRef> getMemoryBufferRef() const;
101
102     ErrorOr<std::unique_ptr<Binary>>
103     getAsBinary(LLVMContext *Context = nullptr) const;
104   };
105
106   class child_iterator {
107     Child child;
108
109   public:
110     child_iterator() : child(Child(nullptr, nullptr)) {}
111     child_iterator(const Child &c) : child(c) {}
112     const Child *operator->() const { return &child; }
113     const Child &operator*() const { return child; }
114
115     bool operator==(const child_iterator &other) const {
116       return child == other.child;
117     }
118
119     bool operator!=(const child_iterator &other) const {
120       return !(*this == other);
121     }
122
123     bool operator<(const child_iterator &other) const {
124       return child < other.child;
125     }
126
127     child_iterator &operator++() { // Preincrement
128       child = child.getNext();
129       return *this;
130     }
131   };
132
133   class Symbol {
134     const Archive *Parent;
135     uint32_t SymbolIndex;
136     uint32_t StringIndex; // Extra index to the string.
137
138   public:
139     bool operator ==(const Symbol &other) const {
140       return (Parent == other.Parent) && (SymbolIndex == other.SymbolIndex);
141     }
142
143     Symbol(const Archive *p, uint32_t symi, uint32_t stri)
144       : Parent(p)
145       , SymbolIndex(symi)
146       , StringIndex(stri) {}
147     StringRef getName() const;
148     ErrorOr<child_iterator> getMember() const;
149     Symbol getNext() const;
150   };
151
152   class symbol_iterator {
153     Symbol symbol;
154   public:
155     symbol_iterator(const Symbol &s) : symbol(s) {}
156     const Symbol *operator->() const { return &symbol; }
157     const Symbol &operator*() const { return symbol; }
158
159     bool operator==(const symbol_iterator &other) const {
160       return symbol == other.symbol;
161     }
162
163     bool operator!=(const symbol_iterator &other) const {
164       return !(*this == other);
165     }
166
167     symbol_iterator& operator++() {  // Preincrement
168       symbol = symbol.getNext();
169       return *this;
170     }
171   };
172
173   Archive(MemoryBufferRef Source, std::error_code &EC);
174   static ErrorOr<std::unique_ptr<Archive>> create(MemoryBufferRef Source);
175
176   enum Kind {
177     K_GNU,
178     K_MIPS64,
179     K_BSD,
180     K_COFF
181   };
182
183   Kind kind() const { return (Kind)Format; }
184   bool isThin() const { return IsThin; }
185
186   child_iterator child_begin(bool SkipInternal = true) const;
187   child_iterator child_end() const;
188   iterator_range<child_iterator> children(bool SkipInternal = true) const {
189     return iterator_range<child_iterator>(child_begin(SkipInternal),
190                                           child_end());
191   }
192
193   symbol_iterator symbol_begin() const;
194   symbol_iterator symbol_end() const;
195   iterator_range<symbol_iterator> symbols() const {
196     return iterator_range<symbol_iterator>(symbol_begin(), symbol_end());
197   }
198
199   // Cast methods.
200   static inline bool classof(Binary const *v) {
201     return v->isArchive();
202   }
203
204   // check if a symbol is in the archive
205   child_iterator findSym(StringRef name) const;
206
207   bool hasSymbolTable() const;
208   child_iterator getSymbolTableChild() const { return SymbolTable; }
209   StringRef getSymbolTable() const {
210     // We know that the symbol table is not an external file,
211     // so we just assert there is no error.
212     return *SymbolTable->getBuffer();
213   }
214   uint32_t getNumberOfSymbols() const;
215
216 private:
217   child_iterator SymbolTable;
218   child_iterator StringTable;
219   child_iterator FirstRegular;
220   unsigned Format : 2;
221   unsigned IsThin : 1;
222   mutable std::vector<std::unique_ptr<MemoryBuffer>> ThinBuffers;
223 };
224
225 }
226 }
227
228 #endif