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