1 //===- Archive.h - ar archive file format -----------------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file declares the ar archive file format class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_OBJECT_ARCHIVE_H
14 #define LLVM_OBJECT_ARCHIVE_H
15 
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/ADT/fallible_iterator.h"
18 #include "llvm/ADT/iterator_range.h"
19 #include "llvm/Object/Binary.h"
20 #include "llvm/Support/Chrono.h"
21 #include "llvm/Support/Error.h"
22 #include "llvm/Support/FileSystem.h"
23 #include "llvm/Support/MemoryBuffer.h"
24 #include <cassert>
25 #include <cstdint>
26 #include <memory>
27 #include <string>
28 #include <vector>
29 
30 namespace llvm {
31 
32 template <typename T> class Optional;
33 
34 namespace object {
35 
36 const char ArchiveMagic[] = "!<arch>\n";
37 const char ThinArchiveMagic[] = "!<thin>\n";
38 const char BigArchiveMagic[] = "<bigaf>\n";
39 
40 class Archive;
41 
42 class AbstractArchiveMemberHeader {
43 protected:
44   AbstractArchiveMemberHeader(const Archive *Parent) : Parent(Parent){};
45 
46 public:
47   friend class Archive;
48   virtual std::unique_ptr<AbstractArchiveMemberHeader> clone() const = 0;
49   virtual ~AbstractArchiveMemberHeader() = default;
50 
51   /// Get the name without looking up long names.
52   virtual Expected<StringRef> getRawName() const = 0;
53   virtual StringRef getRawAccessMode() const = 0;
54   virtual StringRef getRawLastModified() const = 0;
55   virtual StringRef getRawUID() const = 0;
56   virtual StringRef getRawGID() const = 0;
57 
58   /// Get the name looking up long names.
59   virtual Expected<StringRef> getName(uint64_t Size) const = 0;
60   virtual Expected<uint64_t> getSize() const = 0;
61   virtual uint64_t getOffset() const = 0;
62 
63   /// Get next file member location.
64   virtual Expected<const char *> getNextChildLoc() const = 0;
65   virtual Expected<bool> isThin() const = 0;
66 
67   Expected<sys::fs::perms> getAccessMode() const;
68   Expected<sys::TimePoint<std::chrono::seconds>> getLastModified() const;
69   Expected<unsigned> getUID() const;
70   Expected<unsigned> getGID() const;
71 
72   /// Returns the size in bytes of the format-defined member header of the
73   /// concrete archive type.
74   virtual uint64_t getSizeOf() const = 0;
75 
76   const Archive *Parent;
77 };
78 
79 template <typename T>
80 class CommonArchiveMemberHeader : public AbstractArchiveMemberHeader {
81 public:
82   CommonArchiveMemberHeader(const Archive *Parent, const T *RawHeaderPtr)
83       : AbstractArchiveMemberHeader(Parent), ArMemHdr(RawHeaderPtr){};
84   StringRef getRawAccessMode() const override;
85   StringRef getRawLastModified() const override;
86   StringRef getRawUID() const override;
87   StringRef getRawGID() const override;
88 
89   uint64_t getOffset() const override;
90   uint64_t getSizeOf() const override { return sizeof(T); }
91 
92   T const *ArMemHdr;
93 };
94 
95 struct UnixArMemHdrType {
96   char Name[16];
97   char LastModified[12];
98   char UID[6];
99   char GID[6];
100   char AccessMode[8];
101   char Size[10]; ///< Size of data, not including header or padding.
102   char Terminator[2];
103 };
104 
105 class ArchiveMemberHeader : public CommonArchiveMemberHeader<UnixArMemHdrType> {
106 public:
107   ArchiveMemberHeader(const Archive *Parent, const char *RawHeaderPtr,
108                       uint64_t Size, Error *Err);
109 
110   std::unique_ptr<AbstractArchiveMemberHeader> clone() const override {
111     return std::make_unique<ArchiveMemberHeader>(*this);
112   }
113 
114   Expected<StringRef> getRawName() const override;
115 
116   Expected<StringRef> getName(uint64_t Size) const override;
117   Expected<uint64_t> getSize() const override;
118   Expected<const char *> getNextChildLoc() const override;
119   Expected<bool> isThin() const override;
120 };
121 
122 // File Member Header
123 struct BigArMemHdrType {
124   char Size[20];       // File member size in decimal
125   char NextOffset[20]; // Next member offset in decimal
126   char PrevOffset[20]; // Previous member offset in decimal
127   char LastModified[12];
128   char UID[12];
129   char GID[12];
130   char AccessMode[12];
131   char NameLen[4]; // File member name length in decimal
132   union {
133     char Name[2]; // Start of member name
134     char Terminator[2];
135   };
136 };
137 
138 // Define file member header of AIX big archive.
139 class BigArchiveMemberHeader
140     : public CommonArchiveMemberHeader<BigArMemHdrType> {
141 
142 public:
143   BigArchiveMemberHeader(Archive const *Parent, const char *RawHeaderPtr,
144                          uint64_t Size, Error *Err);
145   std::unique_ptr<AbstractArchiveMemberHeader> clone() const override {
146     return std::make_unique<BigArchiveMemberHeader>(*this);
147   }
148 
149   Expected<StringRef> getRawName() const override;
150   Expected<uint64_t> getRawNameSize() const;
151 
152   Expected<StringRef> getName(uint64_t Size) const override;
153   Expected<uint64_t> getSize() const override;
154   Expected<const char *> getNextChildLoc() const override;
155   Expected<uint64_t> getNextOffset() const;
156   Expected<bool> isThin() const override { return false; }
157 };
158 
159 class Archive : public Binary {
160   virtual void anchor();
161 
162 public:
163   class Child {
164     friend Archive;
165     friend AbstractArchiveMemberHeader;
166 
167     const Archive *Parent;
168     std::unique_ptr<AbstractArchiveMemberHeader> Header;
169     /// Includes header but not padding byte.
170     StringRef Data;
171     /// Offset from Data to the start of the file.
172     uint16_t StartOfFile;
173 
174     Expected<bool> isThinMember() const;
175 
176   public:
177     Child(const Archive *Parent, const char *Start, Error *Err);
178     Child(const Archive *Parent, StringRef Data, uint16_t StartOfFile);
179 
180     Child(const Child &C)
181         : Parent(C.Parent), Data(C.Data), StartOfFile(C.StartOfFile) {
182       if (C.Header)
183         Header = C.Header->clone();
184     }
185 
186     Child(Child &&C) {
187       Parent = std::move(C.Parent);
188       Header = std::move(C.Header);
189       Data = C.Data;
190       StartOfFile = C.StartOfFile;
191     }
192 
193     Child &operator=(Child &&C) noexcept {
194       if (&C == this)
195         return *this;
196 
197       Parent = std::move(C.Parent);
198       Header = std::move(C.Header);
199       Data = C.Data;
200       StartOfFile = C.StartOfFile;
201 
202       return *this;
203     }
204 
205     Child &operator=(const Child &C) {
206       if (&C == this)
207         return *this;
208 
209       Parent = C.Parent;
210       if (C.Header)
211         Header = C.Header->clone();
212       Data = C.Data;
213       StartOfFile = C.StartOfFile;
214 
215       return *this;
216     }
217 
218     bool operator==(const Child &other) const {
219       assert(!Parent || !other.Parent || Parent == other.Parent);
220       return Data.begin() == other.Data.begin();
221     }
222 
223     const Archive *getParent() const { return Parent; }
224     Expected<Child> getNext() const;
225 
226     Expected<StringRef> getName() const;
227     Expected<std::string> getFullName() const;
228     Expected<StringRef> getRawName() const { return Header->getRawName(); }
229 
230     Expected<sys::TimePoint<std::chrono::seconds>> getLastModified() const {
231       return Header->getLastModified();
232     }
233 
234     StringRef getRawLastModified() const {
235       return Header->getRawLastModified();
236     }
237 
238     Expected<unsigned> getUID() const { return Header->getUID(); }
239     Expected<unsigned> getGID() const { return Header->getGID(); }
240 
241     Expected<sys::fs::perms> getAccessMode() const {
242       return Header->getAccessMode();
243     }
244 
245     /// \return the size of the archive member without the header or padding.
246     Expected<uint64_t> getSize() const;
247     /// \return the size in the archive header for this member.
248     Expected<uint64_t> getRawSize() const;
249 
250     Expected<StringRef> getBuffer() const;
251     uint64_t getChildOffset() const;
252     uint64_t getDataOffset() const { return getChildOffset() + StartOfFile; }
253 
254     Expected<MemoryBufferRef> getMemoryBufferRef() const;
255 
256     Expected<std::unique_ptr<Binary>>
257     getAsBinary(LLVMContext *Context = nullptr) const;
258   };
259 
260   class ChildFallibleIterator {
261     Child C;
262 
263   public:
264     ChildFallibleIterator() : C(Child(nullptr, nullptr, nullptr)) {}
265     ChildFallibleIterator(const Child &C) : C(C) {}
266 
267     const Child *operator->() const { return &C; }
268     const Child &operator*() const { return C; }
269 
270     bool operator==(const ChildFallibleIterator &other) const {
271       // Ignore errors here: If an error occurred during increment then getNext
272       // will have been set to child_end(), and the following comparison should
273       // do the right thing.
274       return C == other.C;
275     }
276 
277     bool operator!=(const ChildFallibleIterator &other) const {
278       return !(*this == other);
279     }
280 
281     Error inc() {
282       auto NextChild = C.getNext();
283       if (!NextChild)
284         return NextChild.takeError();
285       C = std::move(*NextChild);
286       return Error::success();
287     }
288   };
289 
290   using child_iterator = fallible_iterator<ChildFallibleIterator>;
291 
292   class Symbol {
293     const Archive *Parent;
294     uint32_t SymbolIndex;
295     uint32_t StringIndex; // Extra index to the string.
296 
297   public:
298     Symbol(const Archive *p, uint32_t symi, uint32_t stri)
299         : Parent(p), SymbolIndex(symi), StringIndex(stri) {}
300 
301     bool operator==(const Symbol &other) const {
302       return (Parent == other.Parent) && (SymbolIndex == other.SymbolIndex);
303     }
304 
305     StringRef getName() const;
306     Expected<Child> getMember() const;
307     Symbol getNext() const;
308   };
309 
310   class symbol_iterator {
311     Symbol symbol;
312 
313   public:
314     symbol_iterator(const Symbol &s) : symbol(s) {}
315 
316     const Symbol *operator->() const { return &symbol; }
317     const Symbol &operator*() const { return symbol; }
318 
319     bool operator==(const symbol_iterator &other) const {
320       return symbol == other.symbol;
321     }
322 
323     bool operator!=(const symbol_iterator &other) const {
324       return !(*this == other);
325     }
326 
327     symbol_iterator &operator++() { // Preincrement
328       symbol = symbol.getNext();
329       return *this;
330     }
331   };
332 
333   Archive(MemoryBufferRef Source, Error &Err);
334   static Expected<std::unique_ptr<Archive>> create(MemoryBufferRef Source);
335 
336   /// Size field is 10 decimal digits long
337   static const uint64_t MaxMemberSize = 9999999999;
338 
339   enum Kind { K_GNU, K_GNU64, K_BSD, K_DARWIN, K_DARWIN64, K_COFF, K_AIXBIG };
340 
341   Kind kind() const { return (Kind)Format; }
342   bool isThin() const { return IsThin; }
343   static object::Archive::Kind getDefaultKindForHost();
344 
345   child_iterator child_begin(Error &Err, bool SkipInternal = true) const;
346   child_iterator child_end() const;
347   iterator_range<child_iterator> children(Error &Err,
348                                           bool SkipInternal = true) const {
349     return make_range(child_begin(Err, SkipInternal), child_end());
350   }
351 
352   symbol_iterator symbol_begin() const;
353   symbol_iterator symbol_end() const;
354   iterator_range<symbol_iterator> symbols() const {
355     return make_range(symbol_begin(), symbol_end());
356   }
357 
358   static bool classof(Binary const *v) { return v->isArchive(); }
359 
360   // check if a symbol is in the archive
361   Expected<Optional<Child>> findSym(StringRef name) const;
362 
363   virtual bool isEmpty() const;
364   bool hasSymbolTable() const;
365   StringRef getSymbolTable() const { return SymbolTable; }
366   StringRef getStringTable() const { return StringTable; }
367   uint32_t getNumberOfSymbols() const;
368   virtual uint64_t getFirstChildOffset() const { return getArchiveMagicLen(); }
369 
370   std::vector<std::unique_ptr<MemoryBuffer>> takeThinBuffers() {
371     return std::move(ThinBuffers);
372   }
373 
374   std::unique_ptr<AbstractArchiveMemberHeader>
375   createArchiveMemberHeader(const char *RawHeaderPtr, uint64_t Size,
376                             Error *Err) const;
377 
378 protected:
379   uint64_t getArchiveMagicLen() const;
380   void setFirstRegular(const Child &C);
381 
382   StringRef SymbolTable;
383   StringRef StringTable;
384 
385 private:
386   StringRef FirstRegularData;
387   uint16_t FirstRegularStartOfFile = -1;
388 
389   unsigned Format : 3;
390   unsigned IsThin : 1;
391   mutable std::vector<std::unique_ptr<MemoryBuffer>> ThinBuffers;
392 };
393 
394 class BigArchive : public Archive {
395 public:
396   /// Fixed-Length Header.
397   struct FixLenHdr {
398     char Magic[sizeof(BigArchiveMagic) - 1]; ///< Big archive magic string.
399     char MemOffset[20];                      ///< Offset to member table.
400     char GlobSymOffset[20];                  ///< Offset to global symbol table.
401     char
402         GlobSym64Offset[20]; ///< Offset global symbol table for 64-bit objects.
403     char FirstChildOffset[20]; ///< Offset to first archive member.
404     char LastChildOffset[20];  ///< Offset to last archive member.
405     char FreeOffset[20];       ///< Offset to first mem on free list.
406   };
407 
408   const FixLenHdr *ArFixLenHdr;
409   uint64_t FirstChildOffset = 0;
410   uint64_t LastChildOffset = 0;
411 
412 public:
413   BigArchive(MemoryBufferRef Source, Error &Err);
414   uint64_t getFirstChildOffset() const override { return FirstChildOffset; }
415   uint64_t getLastChildOffset() const { return LastChildOffset; }
416   bool isEmpty() const override {
417     return Data.getBufferSize() == sizeof(FixLenHdr);
418   };
419 };
420 
421 } // end namespace object
422 } // end namespace llvm
423 
424 #endif // LLVM_OBJECT_ARCHIVE_H
425