Binary.h 6.8 KB

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