Archive.h 13 KB

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