Archive.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. class Archive;
  37. class ArchiveMemberHeader {
  38. public:
  39. friend class Archive;
  40. ArchiveMemberHeader(Archive const *Parent, const char *RawHeaderPtr,
  41. uint64_t Size, Error *Err);
  42. // ArchiveMemberHeader() = default;
  43. /// Get the name without looking up long names.
  44. Expected<StringRef> getRawName() const;
  45. /// Get the name looking up long names.
  46. Expected<StringRef> getName(uint64_t Size) const;
  47. Expected<uint64_t> getSize() const;
  48. Expected<sys::fs::perms> getAccessMode() const;
  49. Expected<sys::TimePoint<std::chrono::seconds>> getLastModified() const;
  50. StringRef getRawLastModified() const {
  51. return StringRef(ArMemHdr->LastModified,
  52. sizeof(ArMemHdr->LastModified)).rtrim(' ');
  53. }
  54. Expected<unsigned> getUID() const;
  55. Expected<unsigned> getGID() const;
  56. // This returns the size of the private struct ArMemHdrType
  57. uint64_t getSizeOf() const {
  58. return sizeof(ArMemHdrType);
  59. }
  60. private:
  61. struct ArMemHdrType {
  62. char Name[16];
  63. char LastModified[12];
  64. char UID[6];
  65. char GID[6];
  66. char AccessMode[8];
  67. char Size[10]; ///< Size of data, not including header or padding.
  68. char Terminator[2];
  69. };
  70. Archive const *Parent;
  71. ArMemHdrType const *ArMemHdr;
  72. };
  73. class Archive : public Binary {
  74. virtual void anchor();
  75. public:
  76. class Child {
  77. friend Archive;
  78. friend ArchiveMemberHeader;
  79. const Archive *Parent;
  80. ArchiveMemberHeader Header;
  81. /// Includes header but not padding byte.
  82. StringRef Data;
  83. /// Offset from Data to the start of the file.
  84. uint16_t StartOfFile;
  85. Expected<bool> isThinMember() const;
  86. public:
  87. Child(const Archive *Parent, const char *Start, Error *Err);
  88. Child(const Archive *Parent, StringRef Data, uint16_t StartOfFile);
  89. bool operator ==(const Child &other) const {
  90. assert(!Parent || !other.Parent || Parent == other.Parent);
  91. return Data.begin() == other.Data.begin();
  92. }
  93. const Archive *getParent() const { return Parent; }
  94. Expected<Child> getNext() const;
  95. Expected<StringRef> getName() const;
  96. Expected<std::string> getFullName() const;
  97. Expected<StringRef> getRawName() const { return Header.getRawName(); }
  98. Expected<sys::TimePoint<std::chrono::seconds>> getLastModified() const {
  99. return Header.getLastModified();
  100. }
  101. StringRef getRawLastModified() const {
  102. return Header.getRawLastModified();
  103. }
  104. Expected<unsigned> getUID() const { return Header.getUID(); }
  105. Expected<unsigned> getGID() const { return Header.getGID(); }
  106. Expected<sys::fs::perms> getAccessMode() const {
  107. return Header.getAccessMode();
  108. }
  109. /// \return the size of the archive member without the header or padding.
  110. Expected<uint64_t> getSize() const;
  111. /// \return the size in the archive header for this member.
  112. Expected<uint64_t> getRawSize() const;
  113. Expected<StringRef> getBuffer() const;
  114. uint64_t getChildOffset() const;
  115. uint64_t getDataOffset() const { return getChildOffset() + StartOfFile; }
  116. Expected<MemoryBufferRef> getMemoryBufferRef() const;
  117. Expected<std::unique_ptr<Binary>>
  118. getAsBinary(LLVMContext *Context = nullptr) const;
  119. };
  120. class ChildFallibleIterator {
  121. Child C;
  122. public:
  123. ChildFallibleIterator() : C(Child(nullptr, nullptr, nullptr)) {}
  124. ChildFallibleIterator(const Child &C) : C(C) {}
  125. const Child *operator->() const { return &C; }
  126. const Child &operator*() const { return C; }
  127. bool operator==(const ChildFallibleIterator &other) const {
  128. // Ignore errors here: If an error occurred during increment then getNext
  129. // will have been set to child_end(), and the following comparison should
  130. // do the right thing.
  131. return C == other.C;
  132. }
  133. bool operator!=(const ChildFallibleIterator &other) const {
  134. return !(*this == other);
  135. }
  136. Error inc() {
  137. auto NextChild = C.getNext();
  138. if (!NextChild)
  139. return NextChild.takeError();
  140. C = std::move(*NextChild);
  141. return Error::success();
  142. }
  143. };
  144. using child_iterator = fallible_iterator<ChildFallibleIterator>;
  145. class Symbol {
  146. const Archive *Parent;
  147. uint32_t SymbolIndex;
  148. uint32_t StringIndex; // Extra index to the string.
  149. public:
  150. Symbol(const Archive *p, uint32_t symi, uint32_t stri)
  151. : Parent(p)
  152. , SymbolIndex(symi)
  153. , StringIndex(stri) {}
  154. bool operator ==(const Symbol &other) const {
  155. return (Parent == other.Parent) && (SymbolIndex == other.SymbolIndex);
  156. }
  157. StringRef getName() const;
  158. Expected<Child> getMember() const;
  159. Symbol getNext() const;
  160. };
  161. class symbol_iterator {
  162. Symbol symbol;
  163. public:
  164. symbol_iterator(const Symbol &s) : symbol(s) {}
  165. const Symbol *operator->() const { return &symbol; }
  166. const Symbol &operator*() const { return symbol; }
  167. bool operator==(const symbol_iterator &other) const {
  168. return symbol == other.symbol;
  169. }
  170. bool operator!=(const symbol_iterator &other) const {
  171. return !(*this == other);
  172. }
  173. symbol_iterator& operator++() { // Preincrement
  174. symbol = symbol.getNext();
  175. return *this;
  176. }
  177. };
  178. Archive(MemoryBufferRef Source, Error &Err);
  179. static Expected<std::unique_ptr<Archive>> create(MemoryBufferRef Source);
  180. /// Size field is 10 decimal digits long
  181. static const uint64_t MaxMemberSize = 9999999999;
  182. enum Kind {
  183. K_GNU,
  184. K_GNU64,
  185. K_BSD,
  186. K_DARWIN,
  187. K_DARWIN64,
  188. K_COFF
  189. };
  190. Kind kind() const { return (Kind)Format; }
  191. bool isThin() const { return IsThin; }
  192. child_iterator child_begin(Error &Err, bool SkipInternal = true) const;
  193. child_iterator child_end() const;
  194. iterator_range<child_iterator> children(Error &Err,
  195. bool SkipInternal = true) const {
  196. return make_range(child_begin(Err, SkipInternal), child_end());
  197. }
  198. symbol_iterator symbol_begin() const;
  199. symbol_iterator symbol_end() const;
  200. iterator_range<symbol_iterator> symbols() const {
  201. return make_range(symbol_begin(), symbol_end());
  202. }
  203. // Cast methods.
  204. static bool classof(Binary const *v) {
  205. return v->isArchive();
  206. }
  207. // check if a symbol is in the archive
  208. Expected<Optional<Child>> findSym(StringRef name) const;
  209. bool isEmpty() const;
  210. bool hasSymbolTable() const;
  211. StringRef getSymbolTable() const { return SymbolTable; }
  212. StringRef getStringTable() const { return StringTable; }
  213. uint32_t getNumberOfSymbols() const;
  214. std::vector<std::unique_ptr<MemoryBuffer>> takeThinBuffers() {
  215. return std::move(ThinBuffers);
  216. }
  217. private:
  218. StringRef SymbolTable;
  219. StringRef StringTable;
  220. StringRef FirstRegularData;
  221. uint16_t FirstRegularStartOfFile = -1;
  222. void setFirstRegular(const Child &C);
  223. unsigned Format : 3;
  224. unsigned IsThin : 1;
  225. mutable std::vector<std::unique_ptr<MemoryBuffer>> ThinBuffers;
  226. };
  227. } // end namespace object
  228. } // end namespace llvm
  229. #endif // LLVM_OBJECT_ARCHIVE_H
  230. #ifdef __GNUC__
  231. #pragma GCC diagnostic pop
  232. #endif