Binary.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- Binary.h - A generic binary file -------------------------*- 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 Binary class.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_OBJECT_BINARY_H
  18. #define LLVM_OBJECT_BINARY_H
  19. #include "llvm-c/Types.h"
  20. #include "llvm/ADT/Triple.h"
  21. #include "llvm/Object/Error.h"
  22. #include "llvm/Support/Error.h"
  23. #include "llvm/Support/MemoryBuffer.h"
  24. #include <algorithm>
  25. #include <memory>
  26. #include <utility>
  27. namespace llvm {
  28. class LLVMContext;
  29. class StringRef;
  30. namespace object {
  31. class Binary {
  32. private:
  33. unsigned int TypeID;
  34. protected:
  35. MemoryBufferRef Data;
  36. Binary(unsigned int Type, MemoryBufferRef Source);
  37. enum {
  38. ID_Archive,
  39. ID_MachOUniversalBinary,
  40. ID_COFFImportFile,
  41. ID_IR, // LLVM IR
  42. ID_TapiUniversal, // Text-based Dynamic Library Stub file.
  43. ID_TapiFile, // Text-based Dynamic Library Stub file.
  44. ID_Minidump,
  45. ID_WinRes, // Windows resource (.res) file.
  46. // Object and children.
  47. ID_StartObjects,
  48. ID_COFF,
  49. ID_XCOFF32, // AIX XCOFF 32-bit
  50. ID_XCOFF64, // AIX XCOFF 64-bit
  51. ID_ELF32L, // ELF 32-bit, little endian
  52. ID_ELF32B, // ELF 32-bit, big endian
  53. ID_ELF64L, // ELF 64-bit, little endian
  54. ID_ELF64B, // ELF 64-bit, big endian
  55. ID_MachO32L, // MachO 32-bit, little endian
  56. ID_MachO32B, // MachO 32-bit, big endian
  57. ID_MachO64L, // MachO 64-bit, little endian
  58. ID_MachO64B, // MachO 64-bit, big endian
  59. ID_Wasm,
  60. ID_EndObjects
  61. };
  62. static inline unsigned int getELFType(bool isLE, bool is64Bits) {
  63. if (isLE)
  64. return is64Bits ? ID_ELF64L : ID_ELF32L;
  65. else
  66. return is64Bits ? ID_ELF64B : ID_ELF32B;
  67. }
  68. static unsigned int getMachOType(bool isLE, bool is64Bits) {
  69. if (isLE)
  70. return is64Bits ? ID_MachO64L : ID_MachO32L;
  71. else
  72. return is64Bits ? ID_MachO64B : ID_MachO32B;
  73. }
  74. public:
  75. Binary() = delete;
  76. Binary(const Binary &other) = delete;
  77. virtual ~Binary();
  78. virtual Error initContent() { return Error::success(); };
  79. StringRef getData() const;
  80. StringRef getFileName() const;
  81. MemoryBufferRef getMemoryBufferRef() const;
  82. // Cast methods.
  83. unsigned int getType() const { return TypeID; }
  84. // Convenience methods
  85. bool isObject() const {
  86. return TypeID > ID_StartObjects && TypeID < ID_EndObjects;
  87. }
  88. bool isSymbolic() const {
  89. return isIR() || isObject() || isCOFFImportFile() || isTapiFile();
  90. }
  91. bool isArchive() const { return TypeID == ID_Archive; }
  92. bool isMachOUniversalBinary() const {
  93. return TypeID == ID_MachOUniversalBinary;
  94. }
  95. bool isTapiUniversal() const { return TypeID == ID_TapiUniversal; }
  96. bool isELF() const {
  97. return TypeID >= ID_ELF32L && TypeID <= ID_ELF64B;
  98. }
  99. bool isMachO() const {
  100. return TypeID >= ID_MachO32L && TypeID <= ID_MachO64B;
  101. }
  102. bool isCOFF() const {
  103. return TypeID == ID_COFF;
  104. }
  105. bool isXCOFF() const { return TypeID == ID_XCOFF32 || TypeID == ID_XCOFF64; }
  106. bool isWasm() const { return TypeID == ID_Wasm; }
  107. bool isCOFFImportFile() const {
  108. return TypeID == ID_COFFImportFile;
  109. }
  110. bool isIR() const {
  111. return TypeID == ID_IR;
  112. }
  113. bool isMinidump() const { return TypeID == ID_Minidump; }
  114. bool isTapiFile() const { return TypeID == ID_TapiFile; }
  115. bool isLittleEndian() const {
  116. return !(TypeID == ID_ELF32B || TypeID == ID_ELF64B ||
  117. TypeID == ID_MachO32B || TypeID == ID_MachO64B);
  118. }
  119. bool isWinRes() const { return TypeID == ID_WinRes; }
  120. Triple::ObjectFormatType getTripleObjectFormat() const {
  121. if (isCOFF())
  122. return Triple::COFF;
  123. if (isMachO())
  124. return Triple::MachO;
  125. if (isELF())
  126. return Triple::ELF;
  127. return Triple::UnknownObjectFormat;
  128. }
  129. static Error checkOffset(MemoryBufferRef M, uintptr_t Addr,
  130. const uint64_t Size) {
  131. if (Addr + Size < Addr || Addr + Size < Size ||
  132. Addr + Size > reinterpret_cast<uintptr_t>(M.getBufferEnd()) ||
  133. Addr < reinterpret_cast<uintptr_t>(M.getBufferStart())) {
  134. return errorCodeToError(object_error::unexpected_eof);
  135. }
  136. return Error::success();
  137. }
  138. };
  139. // Create wrappers for C Binding types (see CBindingWrapping.h).
  140. DEFINE_ISA_CONVERSION_FUNCTIONS(Binary, LLVMBinaryRef)
  141. /// Create a Binary from Source, autodetecting the file type.
  142. ///
  143. /// @param Source The data to create the Binary from.
  144. Expected<std::unique_ptr<Binary>> createBinary(MemoryBufferRef Source,
  145. LLVMContext *Context = nullptr,
  146. bool InitContent = true);
  147. template <typename T> class OwningBinary {
  148. std::unique_ptr<T> Bin;
  149. std::unique_ptr<MemoryBuffer> Buf;
  150. public:
  151. OwningBinary();
  152. OwningBinary(std::unique_ptr<T> Bin, std::unique_ptr<MemoryBuffer> Buf);
  153. OwningBinary(OwningBinary<T>&& Other);
  154. OwningBinary<T> &operator=(OwningBinary<T> &&Other);
  155. std::pair<std::unique_ptr<T>, std::unique_ptr<MemoryBuffer>> takeBinary();
  156. T* getBinary();
  157. const T* getBinary() const;
  158. };
  159. template <typename T>
  160. OwningBinary<T>::OwningBinary(std::unique_ptr<T> Bin,
  161. std::unique_ptr<MemoryBuffer> Buf)
  162. : Bin(std::move(Bin)), Buf(std::move(Buf)) {}
  163. template <typename T> OwningBinary<T>::OwningBinary() = default;
  164. template <typename T>
  165. OwningBinary<T>::OwningBinary(OwningBinary &&Other)
  166. : Bin(std::move(Other.Bin)), Buf(std::move(Other.Buf)) {}
  167. template <typename T>
  168. OwningBinary<T> &OwningBinary<T>::operator=(OwningBinary &&Other) {
  169. Bin = std::move(Other.Bin);
  170. Buf = std::move(Other.Buf);
  171. return *this;
  172. }
  173. template <typename T>
  174. std::pair<std::unique_ptr<T>, std::unique_ptr<MemoryBuffer>>
  175. OwningBinary<T>::takeBinary() {
  176. return std::make_pair(std::move(Bin), std::move(Buf));
  177. }
  178. template <typename T> T* OwningBinary<T>::getBinary() {
  179. return Bin.get();
  180. }
  181. template <typename T> const T* OwningBinary<T>::getBinary() const {
  182. return Bin.get();
  183. }
  184. Expected<OwningBinary<Binary>> createBinary(StringRef Path,
  185. LLVMContext *Context = nullptr,
  186. bool InitContent = true);
  187. } // end namespace object
  188. } // end namespace llvm
  189. #endif // LLVM_OBJECT_BINARY_H
  190. #ifdef __GNUC__
  191. #pragma GCC diagnostic pop
  192. #endif