Binary.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. //===- Binary.h - A generic binary file -------------------------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file declares the Binary class.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_OBJECT_BINARY_H
  14. #define LLVM_OBJECT_BINARY_H
  15. #include "llvm/Object/Error.h"
  16. #include "llvm/Support/ErrorOr.h"
  17. #include "llvm/Support/FileSystem.h"
  18. #include "llvm/Support/MemoryBuffer.h"
  19. namespace llvm {
  20. class LLVMContext;
  21. class StringRef;
  22. namespace object {
  23. class Binary {
  24. private:
  25. Binary() = delete;
  26. Binary(const Binary &other) = delete;
  27. unsigned int TypeID;
  28. protected:
  29. MemoryBufferRef Data;
  30. Binary(unsigned int Type, MemoryBufferRef Source);
  31. enum {
  32. ID_Archive,
  33. ID_MachOUniversalBinary,
  34. ID_IR, // LLVM IR
  35. // Object and children.
  36. ID_StartObjects,
  37. ID_COFF,
  38. ID_ELF32L, // ELF 32-bit, little endian
  39. ID_ELF32B, // ELF 32-bit, big endian
  40. ID_ELF64L, // ELF 64-bit, little endian
  41. ID_ELF64B, // ELF 64-bit, big endian
  42. ID_MachO32L, // MachO 32-bit, little endian
  43. ID_MachO32B, // MachO 32-bit, big endian
  44. ID_MachO64L, // MachO 64-bit, little endian
  45. ID_MachO64B, // MachO 64-bit, big endian
  46. ID_EndObjects
  47. };
  48. static inline unsigned int getELFType(bool isLE, bool is64Bits) {
  49. if (isLE)
  50. return is64Bits ? ID_ELF64L : ID_ELF32L;
  51. else
  52. return is64Bits ? ID_ELF64B : ID_ELF32B;
  53. }
  54. static unsigned int getMachOType(bool isLE, bool is64Bits) {
  55. if (isLE)
  56. return is64Bits ? ID_MachO64L : ID_MachO32L;
  57. else
  58. return is64Bits ? ID_MachO64B : ID_MachO32B;
  59. }
  60. public:
  61. virtual ~Binary();
  62. StringRef getData() const;
  63. StringRef getFileName() const;
  64. MemoryBufferRef getMemoryBufferRef() const;
  65. // Cast methods.
  66. unsigned int getType() const { return TypeID; }
  67. // Convenience methods
  68. bool isObject() const {
  69. return TypeID > ID_StartObjects && TypeID < ID_EndObjects;
  70. }
  71. bool isSymbolic() const {
  72. return isIR() || isObject();
  73. }
  74. bool isArchive() const {
  75. return TypeID == ID_Archive;
  76. }
  77. bool isMachOUniversalBinary() const {
  78. return TypeID == ID_MachOUniversalBinary;
  79. }
  80. bool isELF() const {
  81. return TypeID >= ID_ELF32L && TypeID <= ID_ELF64B;
  82. }
  83. bool isMachO() const {
  84. return TypeID >= ID_MachO32L && TypeID <= ID_MachO64B;
  85. }
  86. bool isCOFF() const {
  87. return TypeID == ID_COFF;
  88. }
  89. bool isIR() const {
  90. return TypeID == ID_IR;
  91. }
  92. bool isLittleEndian() const {
  93. return !(TypeID == ID_ELF32B || TypeID == ID_ELF64B ||
  94. TypeID == ID_MachO32B || TypeID == ID_MachO64B);
  95. }
  96. };
  97. /// @brief Create a Binary from Source, autodetecting the file type.
  98. ///
  99. /// @param Source The data to create the Binary from.
  100. ErrorOr<std::unique_ptr<Binary>> createBinary(MemoryBufferRef Source,
  101. LLVMContext *Context = nullptr);
  102. template <typename T> class OwningBinary {
  103. std::unique_ptr<T> Bin;
  104. std::unique_ptr<MemoryBuffer> Buf;
  105. public:
  106. OwningBinary();
  107. OwningBinary(std::unique_ptr<T> Bin, std::unique_ptr<MemoryBuffer> Buf);
  108. OwningBinary(OwningBinary<T>&& Other);
  109. OwningBinary<T> &operator=(OwningBinary<T> &&Other);
  110. std::pair<std::unique_ptr<T>, std::unique_ptr<MemoryBuffer>> takeBinary();
  111. T* getBinary();
  112. const T* getBinary() const;
  113. };
  114. template <typename T>
  115. OwningBinary<T>::OwningBinary(std::unique_ptr<T> Bin,
  116. std::unique_ptr<MemoryBuffer> Buf)
  117. : Bin(std::move(Bin)), Buf(std::move(Buf)) {}
  118. template <typename T> OwningBinary<T>::OwningBinary() {}
  119. template <typename T>
  120. OwningBinary<T>::OwningBinary(OwningBinary &&Other)
  121. : Bin(std::move(Other.Bin)), Buf(std::move(Other.Buf)) {}
  122. template <typename T>
  123. OwningBinary<T> &OwningBinary<T>::operator=(OwningBinary &&Other) {
  124. Bin = std::move(Other.Bin);
  125. Buf = std::move(Other.Buf);
  126. return *this;
  127. }
  128. template <typename T>
  129. std::pair<std::unique_ptr<T>, std::unique_ptr<MemoryBuffer>>
  130. OwningBinary<T>::takeBinary() {
  131. return std::make_pair(std::move(Bin), std::move(Buf));
  132. }
  133. template <typename T> T* OwningBinary<T>::getBinary() {
  134. return Bin.get();
  135. }
  136. template <typename T> const T* OwningBinary<T>::getBinary() const {
  137. return Bin.get();
  138. }
  139. ErrorOr<OwningBinary<Binary>> createBinary(StringRef Path);
  140. }
  141. }
  142. #endif