1 //===- Archive.h - ar archive file format -----------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file declares the ar archive file format class.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_OBJECT_ARCHIVE_H
14 #define LLVM_OBJECT_ARCHIVE_H
16 #include "llvm/ADT/Optional.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/ADT/fallible_iterator.h"
19 #include "llvm/ADT/iterator_range.h"
20 #include "llvm/Object/Binary.h"
21 #include "llvm/Support/Chrono.h"
22 #include "llvm/Support/Error.h"
23 #include "llvm/Support/FileSystem.h"
24 #include "llvm/Support/MemoryBuffer.h"
37 class ArchiveMemberHeader {
41 ArchiveMemberHeader(Archive const *Parent, const char *RawHeaderPtr,
42 uint64_t Size, Error *Err);
43 // ArchiveMemberHeader() = default;
45 /// Get the name without looking up long names.
46 Expected<StringRef> getRawName() const;
48 /// Get the name looking up long names.
49 Expected<StringRef> getName(uint64_t Size) const;
51 Expected<uint64_t> getSize() const;
53 Expected<sys::fs::perms> getAccessMode() const;
54 Expected<sys::TimePoint<std::chrono::seconds>> getLastModified() const;
56 StringRef getRawLastModified() const {
57 return StringRef(ArMemHdr->LastModified, sizeof(ArMemHdr->LastModified))
61 Expected<unsigned> getUID() const;
62 Expected<unsigned> getGID() const;
64 // This returns the size of the private struct ArMemHdrType
65 uint64_t getSizeOf() const { return sizeof(ArMemHdrType); }
70 char LastModified[12];
74 char Size[10]; ///< Size of data, not including header or padding.
77 Archive const *Parent;
78 ArMemHdrType const *ArMemHdr;
81 class Archive : public Binary {
82 virtual void anchor();
87 friend ArchiveMemberHeader;
89 const Archive *Parent;
90 ArchiveMemberHeader Header;
91 /// Includes header but not padding byte.
93 /// Offset from Data to the start of the file.
96 Expected<bool> isThinMember() const;
99 Child(const Archive *Parent, const char *Start, Error *Err);
100 Child(const Archive *Parent, StringRef Data, uint16_t StartOfFile);
102 bool operator==(const Child &other) const {
103 assert(!Parent || !other.Parent || Parent == other.Parent);
104 return Data.begin() == other.Data.begin();
107 const Archive *getParent() const { return Parent; }
108 Expected<Child> getNext() const;
110 Expected<StringRef> getName() const;
111 Expected<std::string> getFullName() const;
112 Expected<StringRef> getRawName() const { return Header.getRawName(); }
114 Expected<sys::TimePoint<std::chrono::seconds>> getLastModified() const {
115 return Header.getLastModified();
118 StringRef getRawLastModified() const { return Header.getRawLastModified(); }
120 Expected<unsigned> getUID() const { return Header.getUID(); }
121 Expected<unsigned> getGID() const { return Header.getGID(); }
123 Expected<sys::fs::perms> getAccessMode() const {
124 return Header.getAccessMode();
127 /// \return the size of the archive member without the header or padding.
128 Expected<uint64_t> getSize() const;
129 /// \return the size in the archive header for this member.
130 Expected<uint64_t> getRawSize() const;
132 Expected<StringRef> getBuffer() const;
133 uint64_t getChildOffset() const;
134 uint64_t getDataOffset() const { return getChildOffset() + StartOfFile; }
136 Expected<MemoryBufferRef> getMemoryBufferRef() const;
138 Expected<std::unique_ptr<Binary>>
139 getAsBinary(LLVMContext *Context = nullptr) const;
142 class ChildFallibleIterator {
146 ChildFallibleIterator() : C(Child(nullptr, nullptr, nullptr)) {}
147 ChildFallibleIterator(const Child &C) : C(C) {}
149 const Child *operator->() const { return &C; }
150 const Child &operator*() const { return C; }
152 bool operator==(const ChildFallibleIterator &other) const {
153 // Ignore errors here: If an error occurred during increment then getNext
154 // will have been set to child_end(), and the following comparison should
155 // do the right thing.
159 bool operator!=(const ChildFallibleIterator &other) const {
160 return !(*this == other);
164 auto NextChild = C.getNext();
166 return NextChild.takeError();
167 C = std::move(*NextChild);
168 return Error::success();
172 using child_iterator = fallible_iterator<ChildFallibleIterator>;
175 const Archive *Parent;
176 uint32_t SymbolIndex;
177 uint32_t StringIndex; // Extra index to the string.
180 Symbol(const Archive *p, uint32_t symi, uint32_t stri)
181 : Parent(p), SymbolIndex(symi), StringIndex(stri) {}
183 bool operator==(const Symbol &other) const {
184 return (Parent == other.Parent) && (SymbolIndex == other.SymbolIndex);
187 StringRef getName() const;
188 Expected<Child> getMember() const;
189 Symbol getNext() const;
192 class symbol_iterator {
196 symbol_iterator(const Symbol &s) : symbol(s) {}
198 const Symbol *operator->() const { return &symbol; }
199 const Symbol &operator*() const { return symbol; }
201 bool operator==(const symbol_iterator &other) const {
202 return symbol == other.symbol;
205 bool operator!=(const symbol_iterator &other) const {
206 return !(*this == other);
209 symbol_iterator &operator++() { // Preincrement
210 symbol = symbol.getNext();
215 Archive(MemoryBufferRef Source, Error &Err);
216 static Expected<std::unique_ptr<Archive>> create(MemoryBufferRef Source);
218 /// Size field is 10 decimal digits long
219 static const uint64_t MaxMemberSize = 9999999999;
221 enum Kind { K_GNU, K_GNU64, K_BSD, K_DARWIN, K_DARWIN64, K_COFF };
223 Kind kind() const { return (Kind)Format; }
224 bool isThin() const { return IsThin; }
226 child_iterator child_begin(Error &Err, bool SkipInternal = true) const;
227 child_iterator child_end() const;
228 iterator_range<child_iterator> children(Error &Err,
229 bool SkipInternal = true) const {
230 return make_range(child_begin(Err, SkipInternal), child_end());
233 symbol_iterator symbol_begin() const;
234 symbol_iterator symbol_end() const;
235 iterator_range<symbol_iterator> symbols() const {
236 return make_range(symbol_begin(), symbol_end());
240 static bool classof(Binary const *v) { return v->isArchive(); }
242 // check if a symbol is in the archive
243 Expected<Optional<Child>> findSym(StringRef name) const;
245 bool isEmpty() const;
246 bool hasSymbolTable() const;
247 StringRef getSymbolTable() const { return SymbolTable; }
248 StringRef getStringTable() const { return StringTable; }
249 uint32_t getNumberOfSymbols() const;
251 std::vector<std::unique_ptr<MemoryBuffer>> takeThinBuffers() {
252 return std::move(ThinBuffers);
256 StringRef SymbolTable;
257 StringRef StringTable;
259 StringRef FirstRegularData;
260 uint16_t FirstRegularStartOfFile = -1;
261 void setFirstRegular(const Child &C);
265 mutable std::vector<std::unique_ptr<MemoryBuffer>> ThinBuffers;
268 } // end namespace object
269 } // end namespace llvm
271 #endif // LLVM_OBJECT_ARCHIVE_H