-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmdil_ctl_parser.cpp
More file actions
754 lines (671 loc) · 26 KB
/
mdil_ctl_parser.cpp
File metadata and controls
754 lines (671 loc) · 26 KB
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
#include "stdafx.h"
#include "mdil_ctl_parser.h"
using namespace std;
enum CompactLayoutToken
{
CLT_INVALID,
CLT_START_TYPE,
CLT_SMALL_START_TYPE,
CLT_SIMPLE_START_TYPE,
CLT_MODEST_START_TYPE,
CLT_END_TYPE,
CLT_IMPLEMENT_INTERFACE,
CLT_ADVANCE_ENCLOSING_TYPEDEF,
CLT_ADVANCE_METHODDEF,
CLT_ADVANCE_METHODDEF_SHORT_MINUS_8,
CLT_ADVANCE_METHODDEF_SHORT_0 = 0x11,
CLT_ADVANCE_METHODDEF_SHORT_PLUS_8 = 0x19,
CLT_ADVANCE_FIELDDEF,
CLT_ADVANCE_FIELDDEF_SHORT_MINUS_8,
CLT_ADVANCE_FIELDDEF_SHORT_0 = 0x23,
CLT_ADVANCE_FIELDDEF_SHORT_PLUS_8 = 0x2B,
CLT_FIELD_OFFSET,
CLT_IMPLEMENT_INTERFACE_METHOD,
CLT_METHOD,
CLT_NORMAL_METHOD,
CLT_SIMPLE_METHOD,
CLT_PINVOKE_METHOD = 0x50,
CLT_METHOD_IMPL,
CLT_FIELD_INSTANCE,
CLT_FIELD_STATIC,
CLT_FIELD_THREADLOCAL,
CLT_FIELD_CONTEXTLOCAL,
CLT_FIELD_RVA,
CLT_FIELD_SIMPLE,
CLT_FIELD_MAX = 0x67,
CLT_DLLEXPORT_METHOD = 0x67,
CLT_RUNTIME_IMPORT_METHOD,
CLT_RUNTIME_EXPORT_METHOD,
// below are new
CLT_GENERIC_TYPE_1 = 0x6A,
CLT_GENERIC_TYPE_2 = 0x6B,
CLT_GENERIC_TYPE_N = 0x6C,
CLT_PACK = 0x6D,
CLT_SIZE = 0x6E,
CLT_GENERIC_PARAM = 0x6F,
CLT_NATIVE_FIELD = 0x70,
CLT_GUIDINFO = 0x71,
CLT_STUB_METHOD = 0x72,
CLT_TYPE_FLAGS = 0x73,
CLT_SPECIAL_TYPE = 0x74,
};
uint8_t mdil_ctl_parser::peek_byte()
{
return m_buffer[m_pos];
}
uint8_t mdil_ctl_parser::read_byte()
{
return m_buffer[m_pos++];
}
uint32_t mdil_ctl_parser::read_uint32_le()
{
unsigned long ret = m_buffer[m_pos] + (m_buffer[m_pos+1] << 8) + (m_buffer[m_pos+2] << 16) + (m_buffer[m_pos+3] << 24);
m_pos += 4;
return ret;
}
int32_t mdil_ctl_parser::read_compressed_int32()
{
int32_t ret = 0;
int8_t b = int8_t(m_buffer[m_pos++]);
if ((b & 1) == 0) {
ret = b >> 1;
} else if ((b & 2) == 0) {
ret = (b >> 2) + (int8_t(m_buffer[m_pos++]) << 6);
} else if ((b & 4) == 0) {
ret = (b >> 3) + (int8_t(m_buffer[m_pos]) << 5) + (int8_t(m_buffer[m_pos+1]) << 13);
m_pos += 2;
} else if ((b & 8) == 0) {
ret = (b >> 4) + (int8_t(m_buffer[m_pos]) << 4) + (int8_t(m_buffer[m_pos+1]) << 12) + (int8_t(m_buffer[m_pos+2]) << 20);
m_pos += 3;
} else { // 0x0F
if (b != 0xf) log_type_def("invalid compressed %02X", b);
ret = read_uint32_le();
}
return ret;
}
uint32_t mdil_ctl_parser::read_compressed_uint32()
{
uint32_t ret = 0;
uint8_t b = m_buffer[m_pos++];
if ((b & 1) == 0) {
ret = b >> 1;
} else if ((b & 2) == 0) {
ret = (b >> 2) + (m_buffer[m_pos++] << 6);
} else if ((b & 4) == 0) {
ret = (b >> 3) + (m_buffer[m_pos] << 5) + (m_buffer[m_pos+1] << 13);
m_pos += 2;
} else if ((b & 8) == 0) {
ret = (b >> 4) + (m_buffer[m_pos] << 4) + (m_buffer[m_pos+1] << 12) + (m_buffer[m_pos+2] << 20);
m_pos += 3;
} else { // 0x0F
if (b != 0xf) log_type_def("invalid compressed %02X", b);
ret = read_uint32_le();
}
return ret;
}
void mdil_ctl_parser::log_type_def( const char* format, ... )
{
#if 0
va_list args;
va_start(args, format);
char msg[512];
vsprintf_s(msg, format, args);
printf_s("TPD@%04X: %s\n", m_pos, msg);
va_end(args);
#endif
}
mdToken mdil_ctl_parser::read_compressed_type_token()
{
mdToken ret = read_compressed_uint32();
uint8_t token_type_flag = ret & 3;
CorTokenType token_type = (token_type_flag == 0) ? mdtModule : (token_type_flag == 1) ? mdtTypeDef : (token_type_flag == 2) ? mdtTypeRef : mdtTypeSpec;
return (ret >> 2) | token_type;
}
mdToken mdil_ctl_parser::read_compressed_method_token()
{
mdToken ret = read_compressed_uint32();
uint8_t token_type_flag = ret & 3;
CorTokenType token_type = (token_type_flag == 0) ? mdtModule : (token_type_flag == 1) ? mdtMethodDef : (token_type_flag == 2) ? mdtMemberRef : mdtMethodSpec;
if (token_type == mdtModule) log_type_def("invalid method token");
return (ret >> 2) | token_type;
}
shared_ptr<mdil_type_def> mdil_ctl_parser::parse_type_def(const uint32_t index)
{
auto ret = make_shared<mdil_type_def>(mdtTypeDef | index);
bool fine = true;
uint8_t byte = read_byte();
shared_ptr<uint32_t> generic_parameter_count;
if (byte == CLT_GENERIC_TYPE_1) {
generic_parameter_count = make_shared<uint32_t>(1);
log_type_def("CLT_GENERIC_TYPE_1");
} else if (byte == CLT_GENERIC_TYPE_2) {
generic_parameter_count = make_shared<uint32_t>(2);
log_type_def("CLT_GENERIC_TYPE_2");
} else if (byte == CLT_GENERIC_TYPE_N) {
generic_parameter_count = make_shared<uint32_t>(read_compressed_uint32());
log_type_def("CLT_GENERIC_TYPE_N %d", *generic_parameter_count);
}
if (generic_parameter_count) {
ret->generic_params.resize(*generic_parameter_count);
for (uint32_t i = 0; i < *generic_parameter_count; ++i) {
byte = read_byte();
if (byte == CLT_GENERIC_PARAM) {
uint32_t rid = read_compressed_uint32(); // what ??
CorGenericParamAttr attributes = (CorGenericParamAttr) read_compressed_uint32(); // only variance ?
ret->generic_params->at(i) = make_shared<mdil_generic_param>(mdtGenericParam | rid, attributes);
log_type_def("CLT_GENERIC_PARAM %06X %08X", rid, attributes);
} else { log_type_def("expecting CLT_GENERIC_PARAM got %02X", byte); fine = false; }
}
byte = read_byte();
}
ret->enclosing_type_token = mdTypeDefNil;
if (byte == CLT_ADVANCE_ENCLOSING_TYPEDEF) {
ret->enclosing_type_token = mdtTypeDef + read_compressed_uint32();
log_type_def("CLT_ADVANCE_ENCLOSING_TYPEDEF=%08X", ret->enclosing_type_token);
byte = read_byte();
}
uint32_t field_count = 0, method_count = 0, interface_count = 0;
switch (byte)
{
case CLT_START_TYPE:
{
ret->attributes = (CorTypeAttr) read_compressed_uint32();
ret->base_type_token = read_compressed_type_token();
interface_count = read_compressed_uint32();
field_count = read_compressed_uint32();
method_count = read_compressed_uint32();
uint32_t new_count = read_compressed_uint32();
uint32_t virtual_count = read_compressed_uint32();
log_type_def("CLT_START_TYPE attr=%08X base=%08X fld=%d mtd=%d new=%d vir=%d int=%d", ret->attributes, ret->base_type_token, field_count, method_count, new_count, virtual_count, interface_count);
break;
}
case CLT_SMALL_START_TYPE:
{
ret->attributes = (CorTypeAttr) read_compressed_uint32();
ret->base_type_token = read_compressed_type_token();
uint32_t counts = read_compressed_uint32();
field_count = counts & 7;
method_count = counts >> 3;
log_type_def("CLT_SMALL_START_TYPE attr=%08X base=%08X fld=%d mtd=%d int=%d", ret->attributes, ret->base_type_token, field_count, method_count, interface_count);
break;
}
case CLT_SIMPLE_START_TYPE:
{
ret->attributes = (CorTypeAttr) read_compressed_uint32();
ret->base_type_token = read_compressed_type_token();
field_count = read_compressed_uint32();
method_count = read_compressed_uint32();
log_type_def("CLT_SIMPLE_START_TYPE attr=%08X base=%08X int=%d fld=%d mtd=%d", ret->attributes, ret->base_type_token, interface_count, field_count, method_count);
break;
}
case CLT_MODEST_START_TYPE:
{
ret->attributes = (CorTypeAttr) read_compressed_uint32();
ret->base_type_token = read_compressed_type_token();
field_count = read_compressed_uint32();
method_count = read_compressed_uint32();
uint32_t counts = read_compressed_uint32();
uint32_t new_count = (counts >> 2) & 3;
uint32_t virtual_count = counts >> 4;
interface_count = counts & 3;
log_type_def("CLT_MODEST_START_TYPE attr=%08X base=%08X int=%d fld=%d mtd=%d new=%d, vir=%d", ret->attributes, ret->base_type_token, interface_count, field_count, method_count, new_count, virtual_count);
break;
}
default: log_type_def("expecting *_START_TYPE start got %02X", byte); fine = false; break;
}
if (fine) {
byte = peek_byte();
if (byte == CLT_PACK) {
read_byte();
ret->layout_pack = make_shared<uint32_t>(read_compressed_uint32());
log_type_def("CLT_PACK=%08X", *ret->layout_pack);
byte = peek_byte();
}
if (byte == CLT_TYPE_FLAGS) {
read_byte();
ret->extended_flags = make_shared<uint32_t>(read_compressed_uint32());
log_type_def("CLT_TYPE_FLAGS=%08X", *ret->extended_flags);
byte = peek_byte();
}
if (byte == CLT_SPECIAL_TYPE) {
read_byte();
ret->winrt_redirected = make_shared<uint32_t>(read_compressed_uint32());
log_type_def("CLT_SPECIAL_TYPE=%08X", *ret->winrt_redirected);
byte = peek_byte();
}
if (byte == CLT_GUIDINFO) {
read_byte();
auto guid_info = make_shared<mdil_type_guid>();
for (int i = 0; i < 16; i++) guid_info->guid[i] = read_byte();
guid_info->unknown = read_compressed_uint32();
ret->guid_information = guid_info;
log_type_def("CLT_GUID_INFO %08X", guid_info->unknown);
}
}
if (fine) {
ret->fields.resize(field_count);
for (uint32_t i = 0; i < field_count; i++) {
log_type_def("\tField %d", i);
ret->fields[i] = parse_field_def();
fine = bool(ret->fields[i]);
if (!fine) break;
}
}
if (fine) {
while (true) {
uint32_t pos = m_pos;
if(parse_field_def(true) == nullptr) { // NativeField
m_pos = pos;
break;
}
}
}
if (fine) {
byte = peek_byte();
if (byte == CLT_SIZE) {
read_byte();
ret->layout_size = make_shared<uint32_t>(read_compressed_uint32());
log_type_def("CLT_SIZE=%08X", *ret->layout_size);
byte = peek_byte();
}
}
if (fine) {
ret->methods.resize(method_count);
for (uint32_t i = 0; i < method_count; i++) {
log_type_def("\tMethod %d", i);
auto method = parse_method_def();
if (method) {
method->type_token = ret->token;
ret->methods[i] = method;
} else ret->methods[i].reset();
fine = bool(ret->methods[i]);
if (!fine) break;
}
}
if (fine) {
ret->impl_interfaces.resize(interface_count, mdTokenNil);
for (uint32_t i = 0; i < interface_count; i++) {
log_type_def("\tInterface %d", i);
uint8_t byte = read_byte();
if (byte == CLT_IMPLEMENT_INTERFACE) {
ret->impl_interfaces[i].reset(new uint32_t(read_compressed_type_token()));
log_type_def("\t\tCLT_IMPLEMENT_INTERFACE=%08X", *ret->impl_interfaces[i]);
} else {
log_type_def("\t\texpecting CLT_IMPLEMENT_INTERFACE got %02X", byte);
fine = false;
}
if (!fine) break;
}
}
if (fine) {
bool go_on = true;
do {
go_on = true;
uint8_t byte = m_buffer[m_pos]; // peeking
unique_ptr<int32_t> diff;
if (byte == CLT_ADVANCE_METHODDEF) {
m_pos++;
diff.reset(new int32_t(read_compressed_int32()));
byte = m_buffer[m_pos]; // peeking
log_type_def("\tInterface Method %d", ret->impl_interface_methods.size());
log_type_def("\t\tCLT_ADVANCE_METHODDEF=%d", *diff);
} else if ((byte >= CLT_ADVANCE_METHODDEF_SHORT_MINUS_8) && (byte <= CLT_ADVANCE_METHODDEF_SHORT_PLUS_8)) {
m_pos++;
diff.reset(new int32_t(byte - CLT_ADVANCE_METHODDEF_SHORT_0));
byte = m_buffer[m_pos]; // peeking
log_type_def("\tInterface Method %d", ret->impl_interface_methods.size());
log_type_def("\t\tCLT_ADVANCE_METHODDEF_SHORT=%d", *diff);
}
if (byte == CLT_IMPLEMENT_INTERFACE_METHOD) {
m_pos++;
if (!diff) log_type_def("\tInterface Method %d", ret->impl_interface_methods.size());
auto method = make_shared<mdil_method_def>();
method->token = current_method_token + 1;
if (diff) method->token += *diff; // logic here is different from method def ??? bizarre !!!
current_method_token = method->token;
method->overridden_method_token = read_compressed_method_token();
log_type_def("\t\tCLT_IMPLEMENT_INTERFACE_METHOD=%08X", method->overridden_method_token);
ret->impl_interface_methods.push_back(method);
} else {
if (diff) {
log_type_def("\texpecting CLT_IMPLEMENT_INTERFACE_METHOD got %02X", byte);
fine = false;
}
go_on = false;
}
} while (go_on);
}
if (fine) {
byte = read_byte();
if (byte != CLT_END_TYPE) { log_type_def("expecting CLT_END_TYPE got %02X", byte); fine = false; }
else log_type_def("CLT_END_TYPE");
}
if (!fine) {
log_type_def("*ERROR*");
ret.reset();
}
return ret;
}
shared_ptr<mdil_field_def> mdil_ctl_parser::parse_field_def(bool peek)
{
static const uint32_t field_encodings[] = { 0x0112, 0x1112, 0x0608, 0x0108, 0x0102, 0x0312, 0x0612, 0x1108,
0x0308, 0x1612, 0x0111, 0x1312, 0x0618, 0x0309, 0x0609, 0x0311, };
auto field = make_shared<mdil_field_def>();
bool fine = true;
field->token = current_field_token;
uint8_t byte = m_buffer[m_pos++];
if (byte == CLT_ADVANCE_FIELDDEF) {
int32_t advance_diff = read_compressed_uint32();
log_type_def("\t\tCLT_ADVANCE_FIELDDEF=%d", advance_diff);
field->token = TokenFromRid(advance_diff, mdtFieldDef); // not advancing but reset !?!
byte = read_byte();
} else if ((byte >= CLT_ADVANCE_FIELDDEF_SHORT_MINUS_8) && (byte <= CLT_ADVANCE_FIELDDEF_SHORT_PLUS_8)) {
log_type_def("\t\tCLT_ADVANCE_FIELDDEF_SHORT=%d", byte - CLT_ADVANCE_FIELDDEF_SHORT_0);
field->token = TokenFromRid(byte - CLT_ADVANCE_FIELDDEF_SHORT_0, mdtFieldDef); // not advancing but reset !?!
byte = read_byte();
}
field->token++;
if (byte == CLT_FIELD_OFFSET) {
uint32_t offset = read_compressed_uint32();
log_type_def("\t\tCLT_FIELD_OFFSET=%04X", offset);
field->explicit_offset.reset(new uint32_t(offset));
byte = read_byte();
}
if ((byte >= CLT_FIELD_SIMPLE) && (byte < CLT_FIELD_MAX)) {
uint32_t encoding = field_encodings[byte - CLT_FIELD_SIMPLE];
field->storage = mdil_field_def::field_storage(encoding >> 12);
field->protection = mdil_field_def::field_protection((encoding >> 8) & 0xf);
field->element_type = CorElementType(encoding & 0xff);
log_type_def("\t\tCLT_FIELD_SIMPLE type=%02X", field->element_type);
} else if ((byte >= CLT_FIELD_INSTANCE) && (byte <= CLT_FIELD_RVA)) {
field->storage = mdil_field_def::field_storage(byte - CLT_FIELD_INSTANCE);
uint8_t b = m_buffer[m_pos++];
field->protection = mdil_field_def::field_protection(b >> 5);
field->element_type = CorElementType(b & 0x1F);
log_type_def("\t\tCLT_FIELD type=%02X stor=%08X prot=%08X", field->element_type, field->storage, field->protection);
} else if (byte == CLT_NATIVE_FIELD) {
uint32_t f = read_compressed_uint32();
if (f & 0x40) read_compressed_uint32();
if ((char) f < 0) read_compressed_uint32();
if (f & 0x100) read_compressed_uint32();
if (f & 0x200) read_compressed_uint32();
log_type_def("\t\tCLT_NATIVE_FIELD=%08X", f);
} else {
if (!peek) log_type_def("\t\texpecting CLT_FIELD_* got %02X", byte);
fine = false;
}
if (fine && (field->element_type == ELEMENT_TYPE_VALUETYPE)) {
field->boxing_type_token = make_shared<mdToken>(read_compressed_type_token());
log_type_def("\t\tBox Token=%08X", *field->boxing_type_token);
}
if (fine) {
current_field_token = field->token;
} else {
field.reset();
}
return field;
}
std::shared_ptr<mdil_method_def> mdil_ctl_parser::parse_method_def()
{
auto method = make_shared<mdil_method_def>();
bool fine = true;
method->token = current_method_token;
uint8_t byte = read_byte();
if (byte == CLT_ADVANCE_METHODDEF) {
int32_t advance_diff = read_compressed_uint32();
log_type_def("\t\tCLT_ADVANCE_METHODDEF=%x", advance_diff);
method->token = TokenFromRid(advance_diff, mdtMethodDef); // not advancing but reset !?!
byte = read_byte();
} else if ((byte >= CLT_ADVANCE_METHODDEF_SHORT_MINUS_8) && (byte <= CLT_ADVANCE_METHODDEF_SHORT_PLUS_8)) {
log_type_def("\t\tCLT_ADVANCE_METHODDEF_SHORT=%x", byte - CLT_ADVANCE_METHODDEF_SHORT_0);
method->token = TokenFromRid(byte - CLT_ADVANCE_METHODDEF_SHORT_0, mdtMethodDef); // not advancing but reset !?!
byte = read_byte();
}
method->token++;
current_method_token = method->token;
log_type_def("\t\tToken = %08x", method->token);
if (byte == CLT_METHOD) {
method->kind = mdil_method_def::mkNormal;
method->attributes = (CorMethodAttr) read_compressed_uint32();
method->impl_attributes = (CorMethodImpl) read_compressed_uint32();
method->impl_hints = (mdil_method_def::method_impl_hints) read_compressed_uint32();
if ((method->attributes & mdVirtual) && ((method->attributes & mdNewSlot) != mdNewSlot))
method->overridden_method_token = read_compressed_method_token();
log_type_def("\t\tCLT_METHOD%s%s", (method->attributes & mdVirtual) ? " virtual" : "", (method->attributes & mdNewSlot) ? " new" : "");
} else if (byte == CLT_NORMAL_METHOD) {
log_type_def("\t\tCLT_NORMAL_METHOD");
method->kind = mdil_method_def::mkNormal;
method->attributes = (CorMethodAttr) read_compressed_uint32();
if ((method->attributes & mdVirtual) && ((method->attributes & mdNewSlot) != mdNewSlot))
method->overridden_method_token = read_compressed_method_token();
} else if ((byte >= CLT_SIMPLE_METHOD) && (byte <= (CLT_SIMPLE_METHOD + 31))) {
method->kind = mdil_method_def::mkNormal;
static const uint32_t method_encodings[] = { 2182u, 129u, 100664774u, 134u, 198u, 131u, 2246u, 268441734u,
2179u, 150u, 196u, 2198u, 481u, 147u, 145u, 805312646u,
454u, 486u, 452u, 268441731u, 2502u, 1073748113u, 100666822u, 50528710u,
41951382u, 707u, 41951379u, 2177u, 2534u, 2529u, 132u, 109053382u };
uint32_t encoding = method_encodings[byte - CLT_SIMPLE_METHOD];
method->attributes = (CorMethodAttr) (encoding & 0xffff);
method->impl_attributes = (CorMethodImpl) ((encoding >> 16) & 0xff);
method->impl_hints = (mdil_method_def::method_impl_hints) (encoding >> 24);
if ((method->attributes & mdVirtual) && ((method->attributes & mdNewSlot) != mdNewSlot))
method->overridden_method_token = read_compressed_method_token();
log_type_def("\t\tCLT_SIMPLE_METHOD%s%s", (method->attributes & mdVirtual) ? " virtual" : "", (method->attributes & mdNewSlot) ? " new" : "");
} else if (byte == CLT_PINVOKE_METHOD) {
log_type_def("\t\tCLT_PINVOKE_METHOD");
method->kind = mdil_method_def::mkPInvoke;
method->attributes = (CorMethodAttr) read_compressed_uint32();
method->impl_attributes = (CorMethodImpl) read_compressed_uint32();
method->impl_hints = (mdil_method_def::method_impl_hints) read_compressed_uint32();
method->module_name = read_compressed_uint32();
method->entry_point_name = read_compressed_uint32();
read_compressed_uint32(); // ??????
} else if (byte == CLT_DLLEXPORT_METHOD) {
log_type_def("\t\tCLT_DLLEXPORT_METHOD");
method->kind = mdil_method_def::mkNativeCallable;
method->attributes = (CorMethodAttr) read_compressed_uint32();
method->impl_attributes = (CorMethodImpl) read_compressed_uint32();
method->impl_hints = (mdil_method_def::method_impl_hints) read_compressed_uint32();
method->entry_point_name = read_compressed_uint32();
method->calling_convention = (CorUnmanagedCallingConvention) read_compressed_uint32();
} else if (byte == CLT_RUNTIME_IMPORT_METHOD) {
log_type_def("\t\tCLT_RUNTIME_IMPORT_METHOD");
method->kind = mdil_method_def::mkRuntimeImport;
method->attributes = (CorMethodAttr) read_compressed_uint32();
method->impl_attributes = (CorMethodImpl) read_compressed_uint32();
method->impl_hints = (mdil_method_def::method_impl_hints) read_compressed_uint32();
method->entry_point_name = read_compressed_uint32();
method->calling_convention = (CorUnmanagedCallingConvention) read_compressed_uint32();
} else if (byte == CLT_RUNTIME_EXPORT_METHOD) {
log_type_def("\t\tCLT_RUNTIME_EXPORT_METHOD");
method->kind = mdil_method_def::mkRuntimeExport;
method->attributes = (CorMethodAttr) read_compressed_uint32();
method->impl_attributes = (CorMethodImpl) read_compressed_uint32();
method->impl_hints = (mdil_method_def::method_impl_hints) read_compressed_uint32();
method->entry_point_name = read_compressed_uint32();
} else if (byte == CLT_METHOD_IMPL) {
method->kind = mdil_method_def::mkImplement;
method->overridden_method_token = read_compressed_method_token();
log_type_def("\t\tCLT_METHOD_IMPL=%08X", method->overridden_method_token);
} else if (byte == CLT_STUB_METHOD) {
uint32_t v1 = read_compressed_uint32();
uint32_t v2 = 0;
if (v1 & 0x20) v2 = read_compressed_uint32();
log_type_def("\t\tCLT_STUB_METHOD=%08X %06X", v1, v2);
} else {
fine = false;
log_type_def("expecting *_METHOD got %02X", byte);
}
if (fine) {
while (peek_byte() == CLT_GENERIC_PARAM) {
read_byte();
uint32_t rid = read_compressed_uint32();
CorGenericParamAttr attributes = (CorGenericParamAttr) read_compressed_uint32();
if (!method->generic_params) method->generic_params.resize(0);
method->generic_params->push_back(make_shared<mdil_generic_param>(mdtGenericParam | rid, attributes));
log_type_def("\t\tCLT_GENERIC_PARAM %06X %08X", rid, attributes);
}
}
if (fine) {
auto rid = RidFromToken(method->token);
if (rid < m_data.method_map.method_def_mappings.size()) {
m_data.method_map.method_def_mappings->at(rid)->method_def = method;
}
} else method.reset();
return method;
}
shared_ptr<mdil_type_spec> mdil_ctl_parser::parse_type_spec(const uint32_t rid)
{
shared_ptr<mdil_type_spec> ret;
uint8_t byte = read_byte();
switch (byte) {
case ELEMENT_TYPE_VOID:
case ELEMENT_TYPE_BOOLEAN:
case ELEMENT_TYPE_CHAR:
case ELEMENT_TYPE_I1:
case ELEMENT_TYPE_U1:
case ELEMENT_TYPE_I2:
case ELEMENT_TYPE_U2:
case ELEMENT_TYPE_I4:
case ELEMENT_TYPE_U4:
case ELEMENT_TYPE_I8:
case ELEMENT_TYPE_U8:
case ELEMENT_TYPE_R4:
case ELEMENT_TYPE_R8:
case ELEMENT_TYPE_STRING:
case ELEMENT_TYPE_I:
case ELEMENT_TYPE_U:
case ELEMENT_TYPE_FNPTR: // TODO
case ELEMENT_TYPE_OBJECT:
ret = make_shared<mdil_type_spec>(mdtTypeSpec | rid, (CorElementType) byte);
break;
case ELEMENT_TYPE_PTR:
case ELEMENT_TYPE_BYREF:
case ELEMENT_TYPE_TYPEDBYREF:
ret.reset(new mdil_type_spec_with_child(mdtTypeSpec | rid, (CorElementType) byte, parse_type_spec()));
break;
case ELEMENT_TYPE_VALUETYPE:
case ELEMENT_TYPE_CLASS:
ret.reset(new mdil_type_spec_with_type(mdtTypeSpec | rid, (CorElementType) byte, read_compressed_type_token()));
break;
case ELEMENT_TYPE_VAR:
case ELEMENT_TYPE_MVAR:
ret.reset(new mdil_type_spec_with_number(mdtTypeSpec | rid, (CorElementType) byte, read_compressed_uint32()));
break;
case ELEMENT_TYPE_ARRAY: {
auto type = parse_type_spec(0);
if (type) {
uint32_t rank = read_compressed_uint32();
uint32_t bcount = read_compressed_uint32();
vector<uint32_t> bounds(bcount);
for (uint32_t i = 0; i < bcount; ++i) bounds[i] = read_compressed_uint32();
uint32_t lbcount = read_compressed_uint32();
vector<uint32_t> lbounds(lbcount);
for (uint32_t i = 0; i < lbcount; ++i) lbounds[i] = read_compressed_uint32();
ret.reset(new mdil_type_spec_array(mdtTypeSpec | rid, type, rank, bounds, lbounds));
}
break;
}
case ELEMENT_TYPE_SZARRAY:
ret.reset(new mdil_type_spec_array(mdtTypeSpec | rid, parse_type_spec(), 1));
break;
case ELEMENT_TYPE_GENERICINST: {
auto type = parse_type_spec();
if (type != nullptr) {
uint32_t count = read_compressed_uint32();
auto args = vector<shared_ptr<mdil_type_spec>>(count);
for (uint32_t i=0; i < count; ++i) {
auto arg = parse_type_spec();
if (arg == nullptr) break;
args[i] = arg;
}
ret.reset(new mdil_type_spec_generic(mdtTypeSpec | rid, type, args));
}
break;
}
default: break;
}
return ret;
}
std::shared_ptr<mdil_method_spec> mdil_ctl_parser::parse_method_spec()
{
auto ret = make_shared<mdil_method_spec>();
ret->method_token = read_compressed_method_token();
uint32_t count = read_compressed_uint32();
ret->parameters.resize(count);
for(uint32_t i = 0; i < count; i++) {
auto type_spec = parse_type_spec();
ret->parameters->at(i) = type_spec;
}
return ret;
}
void mdil_ctl_parser::parse()
{
// method map
m_data.method_map.method_def_mappings.resize(m_data.method_map.raw.size());
for (uint32_t i = 1; i < m_data.method_map.raw->size(); ++i) {
uint32_t mapping = m_data.method_map.raw->at(i);
auto method_def_mapping = make_shared<mdil_method_def_mapping>();
method_def_mapping->is_generic_inst = (mapping & (1 << 31)) != 0 ;
method_def_mapping->offset = mapping & ~(1 << 31);
m_data.method_map.method_def_mappings->at(i) = method_def_mapping;
}
// generic instances
if (m_data.generic_instances.raw.size() > 4) {
auto sig = *(DWORD*) m_data.generic_instances.raw->data();
if (sig == 'MDGI') {
size_t pos = 4;
while (pos < m_data.generic_instances.raw.size()) {
auto generic_method = make_shared<mdil_generic_method>();
uint32_t offset = pos;
generic_method->instance_count = *(uint16_t*) &m_data.generic_instances.raw->at(pos); pos += 2;
generic_method->flags = *(uint8_t*) &m_data.generic_instances.raw->at(pos); pos += 1;
generic_method->argument_count = *(uint8_t*) &m_data.generic_instances.raw->at(pos); pos += 1;
generic_method->instances.resize(generic_method->instance_count);
for (uint16_t i = 0; i < generic_method->instance_count; ++i) {
generic_method->instances.at(i) = make_shared<mdil_generic_instance>();
generic_method->instances.at(i)->code_offset = *(DWORD*) &m_data.generic_instances.raw->at(pos); pos += 4;
generic_method->instances.at(i)->debug_offset = *(DWORD*) &m_data.generic_instances.raw->at(pos); pos += 4;
}
for (uint16_t i = 0; i < generic_method->instance_count; ++i) {
generic_method->instances[i]->argument_types.resize(generic_method->argument_count);
for (uint32_t a = 0; a < generic_method->argument_count; ++a) {
generic_method->instances[i]->argument_types[a] = *(DWORD*) (m_data.generic_instances.raw->data() + pos);
pos += 4;
}
}
m_data.generic_instances.generic_methods[offset] = generic_method;
}
} // else { printf("Signature Incorrect, should be 'MDGI'"); }
}
// type map
current_field_token = mdFieldDefNil;
current_method_token = mdMethodDefNil;
m_data.type_map.type_defs.resize(m_data.type_map.raw.size());
for (unsigned long i = 1; i < m_data.type_map.raw.size(); i++) {
auto offset = m_data.type_map.raw->at(i);
if (offset != 0) {
m_pos = offset;
log_type_def("TYPEDEF_%08X", offset);
m_data.type_map.type_defs->at(i) = parse_type_def(i);
}
}
// type specs
m_data.type_specs.type_specs.resize(m_data.type_specs.raw.size());
for (unsigned long i = 1; i < m_data.type_specs.raw.size(); i++) {
auto offset = m_data.type_specs.raw->at(i);;
if (offset != 0) {
m_pos = offset;
m_data.type_specs.type_specs->at(i) = parse_type_spec(i);
}
}
// method specs
m_data.method_specs.method_specs.resize(m_data.method_specs.raw.size());
for (unsigned long i = 1; i < m_data.method_specs.raw.size(); i++) {
auto offset = m_data.method_specs.raw->at(i);;
if (offset != 0) {
m_pos = offset;
m_data.method_specs.method_specs->at(i) = parse_method_spec();
}
}
}