Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
2.9 KiB

  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/ADT/OwningPtr.h"
  16. #include "llvm/Object/Error.h"
  17. namespace llvm {
  18. class MemoryBuffer;
  19. class StringRef;
  20. namespace object {
  21. class Binary {
  22. private:
  23. Binary() LLVM_DELETED_FUNCTION;
  24. Binary(const Binary &other) LLVM_DELETED_FUNCTION;
  25. unsigned int TypeID;
  26. protected:
  27. MemoryBuffer *Data;
  28. Binary(unsigned int Type, MemoryBuffer *Source);
  29. enum {
  30. ID_Archive,
  31. // Object and children.
  32. ID_StartObjects,
  33. ID_COFF,
  34. ID_ELF32L, // ELF 32-bit, little endian
  35. ID_ELF32B, // ELF 32-bit, big endian
  36. ID_ELF64L, // ELF 64-bit, little endian
  37. ID_ELF64B, // ELF 64-bit, big endian
  38. ID_MachO32L, // MachO 32-bit, little endian
  39. ID_MachO32B, // MachO 32-bit, big endian
  40. ID_MachO64L, // MachO 64-bit, little endian
  41. ID_MachO64B, // MachO 64-bit, big endian
  42. ID_EndObjects
  43. };
  44. static inline unsigned int getELFType(bool isLE, bool is64Bits) {
  45. if (isLE)
  46. return is64Bits ? ID_ELF64L : ID_ELF32L;
  47. else
  48. return is64Bits ? ID_ELF64B : ID_ELF32B;
  49. }
  50. static unsigned int getMachOType(bool isLE, bool is64Bits) {
  51. if (isLE)
  52. return is64Bits ? ID_MachO64L : ID_MachO32L;
  53. else
  54. return is64Bits ? ID_MachO64B : ID_MachO32B;
  55. }
  56. public:
  57. virtual ~Binary();
  58. StringRef getData() const;
  59. StringRef getFileName() const;
  60. // Cast methods.
  61. unsigned int getType() const { return TypeID; }
  62. // Convenience methods
  63. bool isObject() const {
  64. return TypeID > ID_StartObjects && TypeID < ID_EndObjects;
  65. }
  66. bool isArchive() const {
  67. return TypeID == ID_Archive;
  68. }
  69. bool isELF() const {
  70. return TypeID >= ID_ELF32L && TypeID <= ID_ELF64B;
  71. }
  72. bool isMachO() const {
  73. return TypeID >= ID_MachO32L && TypeID <= ID_MachO64B;
  74. }
  75. bool isCOFF() const {
  76. return TypeID == ID_COFF;
  77. }
  78. bool isLittleEndian() const {
  79. return !(TypeID == ID_ELF32B || TypeID == ID_ELF64B ||
  80. TypeID == ID_MachO32B || TypeID == ID_MachO64B);
  81. }
  82. };
  83. /// @brief Create a Binary from Source, autodetecting the file type.
  84. ///
  85. /// @param Source The data to create the Binary from. Ownership is transferred
  86. /// to Result if successful. If an error is returned, Source is destroyed
  87. /// by createBinary before returning.
  88. /// @param Result A pointer to the resulting Binary if no error occured.
  89. error_code createBinary(MemoryBuffer *Source, OwningPtr<Binary> &Result);
  90. error_code createBinary(StringRef Path, OwningPtr<Binary> &Result);
  91. }
  92. }
  93. #endif