| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
#include "core/io/file_access.h" |
| 36 |
#include "core/object/ref_counted.h" |
| 37 |
|
| 38 |
class MachO : public RefCounted { |
| 39 |
GDSOFTCLASS(MachO, RefCounted); |
| 40 |
|
| 41 |
public: |
| 42 |
struct MachHeader { |
| 43 |
uint32_t cputype; |
| 44 |
uint32_t cpusubtype; |
| 45 |
uint32_t filetype; |
| 46 |
uint32_t ncmds; |
| 47 |
uint32_t sizeofcmds; |
| 48 |
uint32_t flags; |
| 49 |
}; |
| 50 |
|
| 51 |
enum LoadCommandID { |
| 52 |
LC_SEGMENT = 0x00000001, |
| 53 |
LC_SYMTAB = 0x00000002, |
| 54 |
LC_SYMSEG = 0x00000003, |
| 55 |
LC_THREAD = 0x00000004, |
| 56 |
LC_UNIXTHREAD = 0x00000005, |
| 57 |
LC_LOADFVMLIB = 0x00000006, |
| 58 |
LC_IDFVMLIB = 0x00000007, |
| 59 |
LC_IDENT = 0x00000008, |
| 60 |
LC_FVMFILE = 0x00000009, |
| 61 |
LC_PREPAGE = 0x0000000a, |
| 62 |
LC_DYSYMTAB = 0x0000000b, |
| 63 |
LC_LOAD_DYLIB = 0x0000000c, |
| 64 |
LC_ID_DYLIB = 0x0000000d, |
| 65 |
LC_LOAD_DYLINKER = 0x0000000e, |
| 66 |
LC_ID_DYLINKER = 0x0000000f, |
| 67 |
LC_PREBOUND_DYLIB = 0x00000010, |
| 68 |
LC_ROUTINES = 0x00000011, |
| 69 |
LC_SUB_FRAMEWORK = 0x00000012, |
| 70 |
LC_SUB_UMBRELLA = 0x00000013, |
| 71 |
LC_SUB_CLIENT = 0x00000014, |
| 72 |
LC_SUB_LIBRARY = 0x00000015, |
| 73 |
LC_TWOLEVEL_HINTS = 0x00000016, |
| 74 |
LC_PREBIND_CKSUM = 0x00000017, |
| 75 |
LC_LOAD_WEAK_DYLIB = 0x80000018, |
| 76 |
LC_SEGMENT_64 = 0x00000019, |
| 77 |
LC_ROUTINES_64 = 0x0000001a, |
| 78 |
LC_UUID = 0x0000001b, |
| 79 |
LC_RPATH = 0x8000001c, |
| 80 |
LC_CODE_SIGNATURE = 0x0000001d, |
| 81 |
LC_SEGMENT_SPLIT_INFO = 0x0000001e, |
| 82 |
LC_REEXPORT_DYLIB = 0x8000001f, |
| 83 |
LC_LAZY_LOAD_DYLIB = 0x00000020, |
| 84 |
LC_ENCRYPTION_INFO = 0x00000021, |
| 85 |
LC_DYLD_INFO = 0x00000022, |
| 86 |
LC_DYLD_INFO_ONLY = 0x80000022, |
| 87 |
LC_LOAD_UPWARD_DYLIB = 0x80000023, |
| 88 |
LC_VERSION_MIN_MACOSX = 0x00000024, |
| 89 |
LC_VERSION_MIN_IPHONEOS = 0x00000025, |
| 90 |
LC_FUNCTION_STARTS = 0x00000026, |
| 91 |
LC_DYLD_ENVIRONMENT = 0x00000027, |
| 92 |
LC_MAIN = 0x80000028, |
| 93 |
LC_DATA_IN_CODE = 0x00000029, |
| 94 |
LC_SOURCE_VERSION = 0x0000002a, |
| 95 |
LC_DYLIB_CODE_SIGN_DRS = 0x0000002b, |
| 96 |
LC_ENCRYPTION_INFO_64 = 0x0000002c, |
| 97 |
LC_LINKER_OPTION = 0x0000002d, |
| 98 |
LC_LINKER_OPTIMIZATION_HINT = 0x0000002e, |
| 99 |
LC_VERSION_MIN_TVOS = 0x0000002f, |
| 100 |
LC_VERSION_MIN_WATCHOS = 0x00000030, |
| 101 |
LC_BUILD_VERSION = 0x00000032, |
| 102 |
}; |
| 103 |
|
| 104 |
enum PlatformID { |
| 105 |
PLATFORM_UNKNOWN = 0, |
| 106 |
PLATFORM_MACOS = 1, |
| 107 |
PLATFORM_IOS = 2, |
| 108 |
PLATFORM_TVOS = 3, |
| 109 |
PLATFORM_WATCHOS = 4, |
| 110 |
PLATFORM_BRIDGEOS = 5, |
| 111 |
PLATFORM_MACCATALYST = 6, |
| 112 |
PLATFORM_IOSSIMULATOR = 7, |
| 113 |
PLATFORM_TVOSSIMULATOR = 8, |
| 114 |
PLATFORM_WATCHOSSIMULATOR = 9, |
| 115 |
PLATFORM_DRIVERKIT = 10, |
| 116 |
PLATFORM_VISIONOS = 11, |
| 117 |
PLATFORM_VISIONOSSIMULATOR = 12, |
| 118 |
}; |
| 119 |
|
| 120 |
struct LoadCommandHeader { |
| 121 |
uint32_t cmd; |
| 122 |
uint32_t cmdsize; |
| 123 |
}; |
| 124 |
|
| 125 |
struct LoadCommandSegment { |
| 126 |
char segname[16]; |
| 127 |
uint32_t vmaddr; |
| 128 |
uint32_t vmsize; |
| 129 |
uint32_t fileoff; |
| 130 |
uint32_t filesize; |
| 131 |
uint32_t maxprot; |
| 132 |
uint32_t initprot; |
| 133 |
uint32_t nsects; |
| 134 |
uint32_t flags; |
| 135 |
}; |
| 136 |
|
| 137 |
struct LoadCommandSegment64 { |
| 138 |
char segname[16]; |
| 139 |
uint64_t vmaddr; |
| 140 |
uint64_t vmsize; |
| 141 |
uint64_t fileoff; |
| 142 |
uint64_t filesize; |
| 143 |
uint32_t maxprot; |
| 144 |
uint32_t initprot; |
| 145 |
uint32_t nsects; |
| 146 |
uint32_t flags; |
| 147 |
}; |
| 148 |
|
| 149 |
struct Section { |
| 150 |
char sectname[16]; |
| 151 |
char segname[16]; |
| 152 |
uint32_t addr; |
| 153 |
uint32_t size; |
| 154 |
uint32_t offset; |
| 155 |
uint32_t align; |
| 156 |
uint32_t reloff; |
| 157 |
uint32_t nreloc; |
| 158 |
uint32_t flags; |
| 159 |
uint32_t reserved1; |
| 160 |
uint32_t reserved2; |
| 161 |
}; |
| 162 |
|
| 163 |
struct Section64 { |
| 164 |
char sectname[16]; |
| 165 |
char segname[16]; |
| 166 |
uint64_t addr; |
| 167 |
uint64_t size; |
| 168 |
uint32_t offset; |
| 169 |
uint32_t align; |
| 170 |
uint32_t reloff; |
| 171 |
uint32_t nreloc; |
| 172 |
uint32_t flags; |
| 173 |
uint32_t reserved1; |
| 174 |
uint32_t reserved2; |
| 175 |
uint32_t reserved3; |
| 176 |
}; |
| 177 |
|
| 178 |
private: |
| 179 |
Ref<FileAccess> fa; |
| 180 |
bool swap = false; |
| 181 |
|
| 182 |
uint64_t lc_limit = 0; |
| 183 |
|
| 184 |
uint64_t exe_limit = 0; |
| 185 |
uint64_t exe_base = std::numeric_limits<uint64_t>::max(); |
| 186 |
uint32_t align = 0; |
| 187 |
uint32_t cputype = 0; |
| 188 |
uint32_t cpusubtype = 0; |
| 189 |
|
| 190 |
uint64_t link_edit_offset = 0; |
| 191 |
uint64_t signature_offset = 0; |
| 192 |
|
| 193 |
uint32_t seg_align(uint64_t p_vmaddr, uint32_t p_min, uint32_t p_max); |
| 194 |
bool alloc_signature(uint64_t p_size); |
| 195 |
|
| 196 |
static inline size_t PAD(size_t s, size_t a) { |
| 197 |
return (a - s % a); |
| 198 |
} |
| 199 |
|
| 200 |
public: |
| 201 |
static bool is_macho(const String &p_path); |
| 202 |
static uint32_t get_filetype(const String &p_path); |
| 203 |
|
| 204 |
bool open_file(const String &p_path); |
| 205 |
|
| 206 |
uint64_t get_exe_base(); |
| 207 |
uint64_t get_exe_limit(); |
| 208 |
int32_t get_align(); |
| 209 |
uint32_t get_cputype(); |
| 210 |
uint32_t get_cpusubtype(); |
| 211 |
uint64_t get_size(); |
| 212 |
uint64_t get_code_limit(); |
| 213 |
|
| 214 |
uint64_t get_signature_offset(); |
| 215 |
bool is_signed(); |
| 216 |
|
| 217 |
PackedByteArray get_cdhash_sha1(); |
| 218 |
PackedByteArray get_cdhash_sha256(); |
| 219 |
|
| 220 |
PackedByteArray get_requirements(); |
| 221 |
|
| 222 |
const Ref<FileAccess> get_file() const; |
| 223 |
Ref<FileAccess> get_file(); |
| 224 |
|
| 225 |
uint64_t get_signature_size(); |
| 226 |
bool set_signature_size(uint64_t p_size); |
| 227 |
}; |