-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathswift.h
428 lines (330 loc) · 8.23 KB
/
swift.h
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/*
* Copyright (c) YungRaj
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <vector>
#include <types.h>
#include "objc.h"
#include "dictionary.h"
class MachO;
class Segment;
class Section;
namespace swift {
const unsigned MetadataKindIsNonType = 0x400;
const unsigned MetadataKindIsNonHeap = 0x200;
const unsigned MetadataKindIsRuntimePrivate = 0x100;
enum class ContextDescriptorKind : UInt8 {
Module = 0,
Extension = 1,
Anonymous = 2,
Protocol = 3,
OpaqueType = 4,
Type_First = 16,
Class = Type_First,
Struct = Type_First + 1,
Enum = Type_First + 2,
Type_Last = 31,
};
enum MetadataKind : UInt16 {
MK_Class = 0x0,
MK_Struct = 0x200,
MK_Enum = 0x201,
MK_Optional = 0x202,
MK_ForeignClass = 0x203,
MK_ForeignReferenceType = 0x204,
MK_Opaque = 0x300,
MK_Tuple = 0x301,
MK_Function = 0x302,
MK_Existential = 0x303,
MK_Metatype = 0x304,
MK_ObjCClassWrapper = 0x305,
MK_ExistentialMetatype = 0x306,
MK_ExtendedExistential = 0x307,
MK_HeapLocalVariable = 0x400,
MK_HeapGenericLocalVariable = 0x500,
MK_ErrorObject = 0x501,
MK_Task = 0x502,
MK_Job = 0x503,
MK_LastEnumerated = 0x7FF,
};
enum FieldDescriptorKind : UInt16 {
FDK_Struct = 0,
FDK_Class = 1,
FDK_Enum = 2,
FDK_MultiPayloadEnum = 3,
FDK_Protocol = 4,
FDK_ClassProtocol = 5,
FDK_ObjCProtocol = 6,
FDK_ObjCClass = 7,
};
#pragma pack(1)
struct ValueMetadata {
UInt64 kind;
UInt64 description;
};
struct ValueWitnessTable {
UInt64 initializeBufferWithCopyOfBuffer;
UInt64 destroy;
UInt64 initializeWithCopy;
UInt64 assignWithCopy;
UInt64 initializeWithTake;
UInt64 assignWithTake;
UInt64 GetEnumTagSinglePayload;
UInt64 storeEnumTagSinglePayload;
Size size;
Size stride;
UInt64 flags;
UInt64 extra_inhabitant_count;
};
struct Type {
struct ModuleDescriptor* module;
enum MetadataKind kind;
char* name;
struct Field* field;
};
struct TypeDescriptor {
UInt32 flags;
Int32 parent;
Int32 name;
Int32 access_function;
UInt32 field_descriptor;
};
struct AnonymousContextDescriptor {
UInt32 flags;
UInt32 parent;
};
struct ModuleDescriptor {
UInt32 flags;
Int32 parent;
Int32 name;
};
struct ProtocolDescriptor {
UInt32 flags;
Int32 name;
UInt32 num_requirements_in_signature;
UInt32 num_requirements;
Int32 associated_type_names;
};
struct ProtocolConformanceDescriptor {
Int32 protocol_descriptor;
Int32 nominal_type_descriptor;
Int32 protocol_witness_table;
UInt32 conformance_flags;
};
struct Protocol : Type {
struct ProtocolDescriptor descriptor;
char* name;
};
struct ClassDescriptor {
struct TypeDescriptor type;
UInt32 super_class_type;
UInt32 metadata_negative_size_in_words;
UInt32 metadata_positive_size_in_words;
UInt32 num_immediate_members;
UInt32 num_fields;
UInt32 field_offset_vector_offset;
};
struct MethodDescriptor {
UInt32 flags;
Int32 impl;
};
struct Method {
struct MethodDescriptor* descriptor;
UInt64 impl;
char* name;
};
struct ClassMetadata {
UInt64 destructor;
UInt64 value_witness_table;
struct objc::_objc_2_class objc;
UInt32 flags;
UInt32 instance_address_point;
UInt32 instance_size;
UInt16 instance_alignment_mask;
UInt16 reserved;
UInt32 class_object_size;
UInt32 class_address_point;
UInt64 type_descriptor;
};
struct Class : Type {
struct ClassDescriptor descriptor;
objc::ObjCClass* isa;
std::vector<Method*> methods;
objc::ObjCClass* GetObjCClass() {
return isa;
}
};
struct StructDescriptor {
struct TypeDescriptor type;
UInt32 num_fields;
UInt32 field_offset_vector_offset;
};
struct Struct : Type {
struct StructDescriptor descriptor;
};
struct EnumDescriptor {
struct TypeDescriptor type;
UInt32 num_payload_cases_and_payload_size_offset;
UInt32 num_empty_cases;
};
struct Enum : Type {
struct EnumDescriptor descriptor;
};
struct FieldDescriptor {
UInt32 type_ref;
UInt32 mangled_type_name;
UInt16 kind;
UInt16 field_record_size;
UInt32 num_fields;
};
struct FieldRecord {
UInt32 flags;
UInt32 mangled_type_name;
UInt32 field_name;
};
struct Field {
struct FieldRecord record;
char* name;
char* mangled_name;
char* demangled_name;
};
struct Fields {
struct FieldDescriptor* descriptor;
std::vector<struct Field*> records;
};
struct AssociatedTypeRecord {
Int32 name;
Int32 substituted_type_name;
};
struct AssociatedTypeDescriptor {
Int32 conforming_type_name;
Int32 protocol_type_name;
UInt32 num_associated_types;
UInt32 associated_type_record_size;
};
struct BuiltinTypeDescriptor {
Int32 type_name;
UInt32 size;
UInt32 alignment_and_flags;
UInt32 stride;
UInt32 num_extra_inhabitants;
};
struct CaptureTypeRecord {
Int32 mangled_type_name;
};
struct MetadataSourceRecord {
Int32 mangled_type_name;
Int32 mangled_metadata_source;
};
struct CaptureDescriptor {
UInt32 num_capture_types;
UInt32 num_metadata_sources;
UInt32 num_bindings;
};
#pragma options align = reset
class SwiftABI {
public:
explicit SwiftABI(MachO* macho, objc::ObjCData* objc) : macho(macho), objc(objc) {
PopulateSections();
ParseSwift();
}
explicit SwiftABI(MachO* macho, objc::ObjCData* objc, Segment* text)
: macho(macho), objc(objc), text(text) {
PopulateSections();
ParseSwift();
}
std::vector<Type*>* GetAllTypes() {
return &swift_types;
}
objc::ObjCData* GetObjCMetaData() {
return objc;
}
struct Class* GetClass(char* cl);
struct Struct* GetStruct(char* st);
struct Enum* GetEnum(char* en);
struct Protocol* GetProtocol(char* p);
Segment* GetTextSegment() {
return text;
}
Section* GetTypeRef() {
return typeref;
}
Section* GetEntry() {
return entry;
}
Section* GetBuiltIn() {
return builtin;
}
Section* GetReflStr() {
return reflstr;
}
Section* GetFieldMd() {
return fieldmd;
}
Section* GetAssocty() {
return assocty;
}
Section* GetProto() {
return proto;
}
Section* GetTypes() {
return types;
}
Section* GetProtos() {
return protos;
}
Section* GetCapture() {
return capture;
}
Section* GetMpenum() {
return mpenum;
}
void SetText(Segment* segment) {
text = segment;
PopulateSections();
ParseSwift();
}
void PopulateSections();
void ParseSwift();
void ParseSwiftObjectiveC();
void EnumerateTypes();
UInt64 GetTypeMetadata(struct TypeDescriptor* typeDescriptor);
struct Type* ParseTypeDescriptor(struct TypeDescriptor* typeDescriptor);
struct Protocol* ParseProtocolDescriptor(struct ProtocolDescriptor* protocolDescriptor);
void ParseFieldDescriptor(struct Type* type, struct FieldDescriptor* fieldDescriptor);
void ParseClassMetadata(Class* cls);
private:
MachO* macho;
objc::ObjCData* objc;
Segment* text;
std::vector<struct Type*> swift_types;
std::vector<struct Class*> classes;
std::vector<struct Struct*> structs;
std::vector<struct Enum*> enums;
std::vector<struct Protocol*> protocols;
Section* typeref;
Section* entry;
Section* builtin;
Section* reflstr;
Section* fieldmd;
Section* assocty;
Section* proto;
Section* types;
Section* protos;
Section* capture;
Section* mpenum;
};
SwiftABI* ParseSwift(darwin::MachOUserspace* macho);
}; // namespace swift