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"
35 const char ArchiveMagic[] = "!<arch>\n";
36 const char ThinArchiveMagic[] = "!<thin>\n";
37 const char BigArchiveMagic[] = "<bigaf>\n";
41 class AbstractArchiveMemberHeader {
43 AbstractArchiveMemberHeader(const Archive *Parent) : Parent(Parent){};
47 virtual std::unique_ptr<AbstractArchiveMemberHeader> clone() const = 0;
48 virtual ~AbstractArchiveMemberHeader(){};
50 /// Get the name without looking up long names.
51 virtual Expected<StringRef> getRawName() const = 0;
52 virtual StringRef getRawAccessMode() const = 0;
53 virtual StringRef getRawLastModified() const = 0;
54 virtual StringRef getRawUID() const = 0;
55 virtual StringRef getRawGID() const = 0;
57 /// Get the name looking up long names.
58 virtual Expected<StringRef> getName(uint64_t Size) const = 0;
59 virtual Expected<uint64_t> getSize() const = 0;
60 virtual uint64_t getOffset() const = 0;
62 /// Get next file member location.
63 virtual Expected<const char *> getNextChildLoc() const = 0;
64 virtual Expected<bool> isThin() const = 0;
66 Expected<sys::fs::perms> getAccessMode() const;
67 Expected<sys::TimePoint<std::chrono::seconds>> getLastModified() const;
68 Expected<unsigned> getUID() const;
69 Expected<unsigned> getGID() const;
71 /// Returns the size in bytes of the format-defined member header of the
72 /// concrete archive type.
73 virtual uint64_t getSizeOf() const = 0;
75 const Archive *Parent;
79 class CommonArchiveMemberHeader : public AbstractArchiveMemberHeader {
81 CommonArchiveMemberHeader(const Archive *Parent, const T *RawHeaderPtr)
82 : AbstractArchiveMemberHeader(Parent), ArMemHdr(RawHeaderPtr){};
83 StringRef getRawAccessMode() const override;
84 StringRef getRawLastModified() const override;
85 StringRef getRawUID() const override;
86 StringRef getRawGID() const override;
88 uint64_t getOffset() const override;
89 uint64_t getSizeOf() const override { return sizeof(T); }
94 struct UnixArMemHdrType {
96 char LastModified[12];
100 char Size[10]; ///< Size of data, not including header or padding.
104 class ArchiveMemberHeader : public CommonArchiveMemberHeader<UnixArMemHdrType> {
106 ArchiveMemberHeader(const Archive *Parent, const char *RawHeaderPtr,
107 uint64_t Size, Error *Err);
109 std::unique_ptr<AbstractArchiveMemberHeader> clone() const override {
110 return std::make_unique<ArchiveMemberHeader>(*this);
113 Expected<StringRef> getRawName() const override;
115 Expected<StringRef> getName(uint64_t Size) const override;
116 Expected<uint64_t> getSize() const override;
117 Expected<const char *> getNextChildLoc() const override;
118 Expected<bool> isThin() const override;
121 // File Member Header
122 struct BigArMemHdrType {
123 char Size[20]; // File member size in decimal
124 char NextOffset[20]; // Next member offset in decimal
125 char PrevOffset[20]; // Previous member offset in decimal
126 char LastModified[12];
130 char NameLen[4]; // File member name length in decimal
132 char Name[2]; // Start of member name
137 // Define file member header of AIX big archive.
138 class BigArchiveMemberHeader
139 : public CommonArchiveMemberHeader<BigArMemHdrType> {
142 BigArchiveMemberHeader(Archive const *Parent, const char *RawHeaderPtr,
143 uint64_t Size, Error *Err);
144 std::unique_ptr<AbstractArchiveMemberHeader> clone() const override {
145 return std::make_unique<BigArchiveMemberHeader>(*this);
148 Expected<StringRef> getRawName() const override;
149 Expected<uint64_t> getRawNameSize() const;
151 Expected<StringRef> getName(uint64_t Size) const override;
152 Expected<uint64_t> getSize() const override;
153 Expected<const char *> getNextChildLoc() const override;
154 Expected<uint64_t> getNextOffset() const;
155 Expected<bool> isThin() const override { return false; }
158 class Archive : public Binary {
159 virtual void anchor();
164 friend AbstractArchiveMemberHeader;
166 const Archive *Parent;
167 std::unique_ptr<AbstractArchiveMemberHeader> Header;
168 /// Includes header but not padding byte.
170 /// Offset from Data to the start of the file.
171 uint16_t StartOfFile;
173 Expected<bool> isThinMember() const;
176 Child(const Archive *Parent, const char *Start, Error *Err);
177 Child(const Archive *Parent, StringRef Data, uint16_t StartOfFile);
179 Child(const Child &C)
180 : Parent(C.Parent), Data(C.Data), StartOfFile(C.StartOfFile) {
182 Header = C.Header->clone();
186 Parent = std::move(C.Parent);
187 Header = std::move(C.Header);
189 StartOfFile = C.StartOfFile;
192 Child &operator=(Child &&C) noexcept {
196 Parent = std::move(C.Parent);
197 Header = std::move(C.Header);
199 StartOfFile = C.StartOfFile;
204 Child &operator=(const Child &C) {
210 Header = C.Header->clone();
212 StartOfFile = C.StartOfFile;
217 bool operator==(const Child &other) const {
218 assert(!Parent || !other.Parent || Parent == other.Parent);
219 return Data.begin() == other.Data.begin();
222 const Archive *getParent() const { return Parent; }
223 Expected<Child> getNext() const;
225 Expected<StringRef> getName() const;
226 Expected<std::string> getFullName() const;
227 Expected<StringRef> getRawName() const { return Header->getRawName(); }
229 Expected<sys::TimePoint<std::chrono::seconds>> getLastModified() const {
230 return Header->getLastModified();
233 StringRef getRawLastModified() const {
234 return Header->getRawLastModified();
237 Expected<unsigned> getUID() const { return Header->getUID(); }
238 Expected<unsigned> getGID() const { return Header->getGID(); }
240 Expected<sys::fs::perms> getAccessMode() const {
241 return Header->getAccessMode();
244 /// \return the size of the archive member without the header or padding.
245 Expected<uint64_t> getSize() const;
246 /// \return the size in the archive header for this member.
247 Expected<uint64_t> getRawSize() const;
249 Expected<StringRef> getBuffer() const;
250 uint64_t getChildOffset() const;
251 uint64_t getDataOffset() const { return getChildOffset() + StartOfFile; }
253 Expected<MemoryBufferRef> getMemoryBufferRef() const;
255 Expected<std::unique_ptr<Binary>>
256 getAsBinary(LLVMContext *Context = nullptr) const;
259 class ChildFallibleIterator {
263 ChildFallibleIterator() : C(Child(nullptr, nullptr, nullptr)) {}
264 ChildFallibleIterator(const Child &C) : C(C) {}
266 const Child *operator->() const { return &C; }
267 const Child &operator*() const { return C; }
269 bool operator==(const ChildFallibleIterator &other) const {
270 // Ignore errors here: If an error occurred during increment then getNext
271 // will have been set to child_end(), and the following comparison should
272 // do the right thing.
276 bool operator!=(const ChildFallibleIterator &other) const {
277 return !(*this == other);
281 auto NextChild = C.getNext();
283 return NextChild.takeError();
284 C = std::move(*NextChild);
285 return Error::success();
289 using child_iterator = fallible_iterator<ChildFallibleIterator>;
292 const Archive *Parent;
293 uint32_t SymbolIndex;
294 uint32_t StringIndex; // Extra index to the string.
297 Symbol(const Archive *p, uint32_t symi, uint32_t stri)
298 : Parent(p), SymbolIndex(symi), StringIndex(stri) {}
300 bool operator==(const Symbol &other) const {
301 return (Parent == other.Parent) && (SymbolIndex == other.SymbolIndex);
304 StringRef getName() const;
305 Expected<Child> getMember() const;
306 Symbol getNext() const;
309 class symbol_iterator {
313 symbol_iterator(const Symbol &s) : symbol(s) {}
315 const Symbol *operator->() const { return &symbol; }
316 const Symbol &operator*() const { return symbol; }
318 bool operator==(const symbol_iterator &other) const {
319 return symbol == other.symbol;
322 bool operator!=(const symbol_iterator &other) const {
323 return !(*this == other);
326 symbol_iterator &operator++() { // Preincrement
327 symbol = symbol.getNext();
332 Archive(MemoryBufferRef Source, Error &Err);
333 static Expected<std::unique_ptr<Archive>> create(MemoryBufferRef Source);
335 /// Size field is 10 decimal digits long
336 static const uint64_t MaxMemberSize = 9999999999;
338 enum Kind { K_GNU, K_GNU64, K_BSD, K_DARWIN, K_DARWIN64, K_COFF, K_AIXBIG };
340 Kind kind() const { return (Kind)Format; }
341 bool isThin() const { return IsThin; }
343 child_iterator child_begin(Error &Err, bool SkipInternal = true) const;
344 child_iterator child_end() const;
345 iterator_range<child_iterator> children(Error &Err,
346 bool SkipInternal = true) const {
347 return make_range(child_begin(Err, SkipInternal), child_end());
350 symbol_iterator symbol_begin() const;
351 symbol_iterator symbol_end() const;
352 iterator_range<symbol_iterator> symbols() const {
353 return make_range(symbol_begin(), symbol_end());
356 static bool classof(Binary const *v) { return v->isArchive(); }
358 // check if a symbol is in the archive
359 Expected<Optional<Child>> findSym(StringRef name) const;
361 bool isEmpty() const;
362 bool hasSymbolTable() const;
363 StringRef getSymbolTable() const { return SymbolTable; }
364 StringRef getStringTable() const { return StringTable; }
365 uint32_t getNumberOfSymbols() const;
366 virtual uint64_t getFirstChildOffset() const { return getArchiveMagicLen(); }
368 std::vector<std::unique_ptr<MemoryBuffer>> takeThinBuffers() {
369 return std::move(ThinBuffers);
372 std::unique_ptr<AbstractArchiveMemberHeader>
373 createArchiveMemberHeader(const char *RawHeaderPtr, uint64_t Size,
377 uint64_t getArchiveMagicLen() const;
378 void setFirstRegular(const Child &C);
381 StringRef SymbolTable;
382 StringRef StringTable;
384 StringRef FirstRegularData;
385 uint16_t FirstRegularStartOfFile = -1;
389 mutable std::vector<std::unique_ptr<MemoryBuffer>> ThinBuffers;
392 class BigArchive : public Archive {
393 /// Fixed-Length Header.
395 char Magic[sizeof(BigArchiveMagic) - 1]; ///< Big archive magic string.
396 char MemOffset[20]; ///< Offset to member table.
397 char GlobSymOffset[20]; ///< Offset to global symbol table.
399 GlobSym64Offset[20]; ///< Offset global symbol table for 64-bit objects.
400 char FirstChildOffset[20]; ///< Offset to first archive member.
401 char LastChildOffset[20]; ///< Offset to last archive member.
402 char FreeOffset[20]; ///< Offset to first mem on free list.
405 const FixLenHdr *ArFixLenHdr;
406 uint64_t FirstChildOffset = 0;
407 uint64_t LastChildOffset = 0;
410 BigArchive(MemoryBufferRef Source, Error &Err);
411 uint64_t getFirstChildOffset() const override { return FirstChildOffset; }
412 uint64_t getLastChildOffset() const { return LastChildOffset; }
415 } // end namespace object
416 } // end namespace llvm
418 #endif // LLVM_OBJECT_ARCHIVE_H