macho.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /**************************************************************************/
  2. /* macho.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef MACHO_H
  31. #define MACHO_H
  32. // Mach-O binary object file format parser and editor.
  33. #include "core/crypto/crypto.h"
  34. #include "core/crypto/crypto_core.h"
  35. #include "core/io/file_access.h"
  36. #include "core/object/ref_counted.h"
  37. class MachO : public RefCounted {
  38. public:
  39. struct MachHeader {
  40. uint32_t cputype;
  41. uint32_t cpusubtype;
  42. uint32_t filetype;
  43. uint32_t ncmds;
  44. uint32_t sizeofcmds;
  45. uint32_t flags;
  46. };
  47. enum LoadCommandID {
  48. LC_SEGMENT = 0x00000001,
  49. LC_SYMTAB = 0x00000002,
  50. LC_SYMSEG = 0x00000003,
  51. LC_THREAD = 0x00000004,
  52. LC_UNIXTHREAD = 0x00000005,
  53. LC_LOADFVMLIB = 0x00000006,
  54. LC_IDFVMLIB = 0x00000007,
  55. LC_IDENT = 0x00000008,
  56. LC_FVMFILE = 0x00000009,
  57. LC_PREPAGE = 0x0000000a,
  58. LC_DYSYMTAB = 0x0000000b,
  59. LC_LOAD_DYLIB = 0x0000000c,
  60. LC_ID_DYLIB = 0x0000000d,
  61. LC_LOAD_DYLINKER = 0x0000000e,
  62. LC_ID_DYLINKER = 0x0000000f,
  63. LC_PREBOUND_DYLIB = 0x00000010,
  64. LC_ROUTINES = 0x00000011,
  65. LC_SUB_FRAMEWORK = 0x00000012,
  66. LC_SUB_UMBRELLA = 0x00000013,
  67. LC_SUB_CLIENT = 0x00000014,
  68. LC_SUB_LIBRARY = 0x00000015,
  69. LC_TWOLEVEL_HINTS = 0x00000016,
  70. LC_PREBIND_CKSUM = 0x00000017,
  71. LC_LOAD_WEAK_DYLIB = 0x80000018,
  72. LC_SEGMENT_64 = 0x00000019,
  73. LC_ROUTINES_64 = 0x0000001a,
  74. LC_UUID = 0x0000001b,
  75. LC_RPATH = 0x8000001c,
  76. LC_CODE_SIGNATURE = 0x0000001d,
  77. LC_SEGMENT_SPLIT_INFO = 0x0000001e,
  78. LC_REEXPORT_DYLIB = 0x8000001f,
  79. LC_LAZY_LOAD_DYLIB = 0x00000020,
  80. LC_ENCRYPTION_INFO = 0x00000021,
  81. LC_DYLD_INFO = 0x00000022,
  82. LC_DYLD_INFO_ONLY = 0x80000022,
  83. LC_LOAD_UPWARD_DYLIB = 0x80000023,
  84. LC_VERSION_MIN_MACOSX = 0x00000024,
  85. LC_VERSION_MIN_IPHONEOS = 0x00000025,
  86. LC_FUNCTION_STARTS = 0x00000026,
  87. LC_DYLD_ENVIRONMENT = 0x00000027,
  88. LC_MAIN = 0x80000028,
  89. LC_DATA_IN_CODE = 0x00000029,
  90. LC_SOURCE_VERSION = 0x0000002a,
  91. LC_DYLIB_CODE_SIGN_DRS = 0x0000002b,
  92. LC_ENCRYPTION_INFO_64 = 0x0000002c,
  93. LC_LINKER_OPTION = 0x0000002d,
  94. LC_LINKER_OPTIMIZATION_HINT = 0x0000002e,
  95. LC_VERSION_MIN_TVOS = 0x0000002f,
  96. LC_VERSION_MIN_WATCHOS = 0x00000030,
  97. LC_BUILD_VERSION = 0x00000032,
  98. };
  99. enum PlatformID {
  100. PLATFORM_UNKNOWN = 0,
  101. PLATFORM_MACOS = 1,
  102. PLATFORM_IOS = 2,
  103. PLATFORM_TVOS = 3,
  104. PLATFORM_WATCHOS = 4,
  105. PLATFORM_BRIDGEOS = 5,
  106. PLATFORM_MACCATALYST = 6,
  107. PLATFORM_IOSSIMULATOR = 7,
  108. PLATFORM_TVOSSIMULATOR = 8,
  109. PLATFORM_WATCHOSSIMULATOR = 9,
  110. PLATFORM_DRIVERKIT = 10,
  111. };
  112. struct LoadCommandHeader {
  113. uint32_t cmd;
  114. uint32_t cmdsize;
  115. };
  116. struct LoadCommandSegment {
  117. char segname[16];
  118. uint32_t vmaddr;
  119. uint32_t vmsize;
  120. uint32_t fileoff;
  121. uint32_t filesize;
  122. uint32_t maxprot;
  123. uint32_t initprot;
  124. uint32_t nsects;
  125. uint32_t flags;
  126. };
  127. struct LoadCommandSegment64 {
  128. char segname[16];
  129. uint64_t vmaddr;
  130. uint64_t vmsize;
  131. uint64_t fileoff;
  132. uint64_t filesize;
  133. uint32_t maxprot;
  134. uint32_t initprot;
  135. uint32_t nsects;
  136. uint32_t flags;
  137. };
  138. struct Section {
  139. char sectname[16];
  140. char segname[16];
  141. uint32_t addr;
  142. uint32_t size;
  143. uint32_t offset;
  144. uint32_t align;
  145. uint32_t reloff;
  146. uint32_t nreloc;
  147. uint32_t flags;
  148. uint32_t reserved1;
  149. uint32_t reserved2;
  150. };
  151. struct Section64 {
  152. char sectname[16];
  153. char segname[16];
  154. uint64_t addr;
  155. uint64_t size;
  156. uint32_t offset;
  157. uint32_t align;
  158. uint32_t reloff;
  159. uint32_t nreloc;
  160. uint32_t flags;
  161. uint32_t reserved1;
  162. uint32_t reserved2;
  163. uint32_t reserved3;
  164. };
  165. private:
  166. Ref<FileAccess> fa;
  167. bool swap = false;
  168. uint64_t lc_limit = 0;
  169. uint64_t exe_limit = 0;
  170. uint64_t exe_base = std::numeric_limits<uint64_t>::max(); // Start of first __text section.
  171. uint32_t align = 0;
  172. uint32_t cputype = 0;
  173. uint32_t cpusubtype = 0;
  174. uint64_t link_edit_offset = 0; // __LINKEDIT segment offset.
  175. uint64_t signature_offset = 0; // Load command offset.
  176. uint32_t seg_align(uint64_t p_vmaddr, uint32_t p_min, uint32_t p_max);
  177. bool alloc_signature(uint64_t p_size);
  178. static inline size_t PAD(size_t s, size_t a) {
  179. return (a - s % a);
  180. }
  181. public:
  182. static bool is_macho(const String &p_path);
  183. static uint32_t get_filetype(const String &p_path);
  184. bool open_file(const String &p_path);
  185. uint64_t get_exe_base();
  186. uint64_t get_exe_limit();
  187. int32_t get_align();
  188. uint32_t get_cputype();
  189. uint32_t get_cpusubtype();
  190. uint64_t get_size();
  191. uint64_t get_code_limit();
  192. uint64_t get_signature_offset();
  193. bool is_signed();
  194. PackedByteArray get_cdhash_sha1();
  195. PackedByteArray get_cdhash_sha256();
  196. PackedByteArray get_requirements();
  197. const Ref<FileAccess> get_file() const;
  198. Ref<FileAccess> get_file();
  199. uint64_t get_signature_size();
  200. bool set_signature_size(uint64_t p_size);
  201. };
  202. #endif // MACHO_H