Binary.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. TypeID == ID_XCOFF32 || TypeID == ID_XCOFF64);
  119. }
  120. bool isWinRes() const { return TypeID == ID_WinRes; }
  121. Triple::ObjectFormatType getTripleObjectFormat() const {
  122. if (isCOFF())
  123. return Triple::COFF;
  124. if (isMachO())
  125. return Triple::MachO;
  126. if (isELF())
  127. return Triple::ELF;
  128. return Triple::UnknownObjectFormat;
  129. }
  130. static Error checkOffset(MemoryBufferRef M, uintptr_t Addr,
  131. const uint64_t Size) {
  132. if (Addr + Size < Addr || Addr + Size < Size ||
  133. Addr + Size > reinterpret_cast<uintptr_t>(M.getBufferEnd()) ||
  134. Addr < reinterpret_cast<uintptr_t>(M.getBufferStart())) {
  135. return errorCodeToError(object_error::unexpected_eof);
  136. }
  137. return Error::success();
  138. }
  139. };
  140. // Create wrappers for C Binding types (see CBindingWrapping.h).
  141. DEFINE_ISA_CONVERSION_FUNCTIONS(Binary, LLVMBinaryRef)
  142. /// Create a Binary from Source, autodetecting the file type.
  143. ///
  144. /// @param Source The data to create the Binary from.
  145. Expected<std::unique_ptr<Binary>> createBinary(MemoryBufferRef Source,
  146. LLVMContext *Context = nullptr,
  147. bool InitContent = true);
  148. template <typename T> class OwningBinary {
  149. std::unique_ptr<T> Bin;
  150. std::unique_ptr<MemoryBuffer> Buf;
  151. public:
  152. OwningBinary();
  153. OwningBinary(std::unique_ptr<T> Bin, std::unique_ptr<MemoryBuffer> Buf);
  154. OwningBinary(OwningBinary<T>&& Other);
  155. OwningBinary<T> &operator=(OwningBinary<T> &&Other);
  156. std::pair<std::unique_ptr<T>, std::unique_ptr<MemoryBuffer>> takeBinary();
  157. T* getBinary();
  158. const T* getBinary() const;
  159. };
  160. template <typename T>
  161. OwningBinary<T>::OwningBinary(std::unique_ptr<T> Bin,
  162. std::unique_ptr<MemoryBuffer> Buf)
  163. : Bin(std::move(Bin)), Buf(std::move(Buf)) {}
  164. template <typename T> OwningBinary<T>::OwningBinary() = default;
  165. template <typename T>
  166. OwningBinary<T>::OwningBinary(OwningBinary &&Other)
  167. : Bin(std::move(Other.Bin)), Buf(std::move(Other.Buf)) {}
  168. template <typename T>
  169. OwningBinary<T> &OwningBinary<T>::operator=(OwningBinary &&Other) {
  170. Bin = std::move(Other.Bin);
  171. Buf = std::move(Other.Buf);
  172. return *this;
  173. }
  174. template <typename T>
  175. std::pair<std::unique_ptr<T>, std::unique_ptr<MemoryBuffer>>
  176. OwningBinary<T>::takeBinary() {
  177. return std::make_pair(std::move(Bin), std::move(Buf));
  178. }
  179. template <typename T> T* OwningBinary<T>::getBinary() {
  180. return Bin.get();
  181. }
  182. template <typename T> const T* OwningBinary<T>::getBinary() const {
  183. return Bin.get();
  184. }
  185. Expected<OwningBinary<Binary>> createBinary(StringRef Path,
  186. LLVMContext *Context = nullptr,
  187. bool InitContent = true);
  188. } // end namespace object
  189. } // end namespace llvm
  190. #endif // LLVM_OBJECT_BINARY_H
  191. #ifdef __GNUC__
  192. #pragma GCC diagnostic pop
  193. #endif