Archive.h 13 KB

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