-
-
Notifications
You must be signed in to change notification settings - Fork 176
/
pe.hexpat
1303 lines (1173 loc) · 37 KB
/
pe.hexpat
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
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#pragma author WerWolv
#pragma description Microsoft PE Portable Executable (exe/dll)
#pragma MIME application/x-dosexec
#pragma MIME application/x-msdownload
#pragma MIME application/vnd.microsoft.portable-executable
import std.core;
import std.string;
import type.guid;
import type.time;
struct DOSHeader {
char signature[2] [[hex::spec_name("e_magic")]];
u16 extraPageSize [[hex::spec_name("e_cblp")]];
u16 numberOfPages [[hex::spec_name("e_cp")]];
u16 relocations [[name("stubRelocations"), hex::spec_name("e_crlc")]];
u16 headerSizeInParagraphs [[hex::spec_name("e_cparhdr")]];
u16 minimumAllocatedParagraphs [[hex::spec_name("e_minalloc")]];
u16 maximumAllocatedParagraphs [[hex::spec_name("e_maxalloc")]];
u16 initialSSValue [[hex::spec_name("e_ss")]];
u16 initialRelativeSPValue [[hex::spec_name("e_sp")]];
u16 checksum [[name("stubChecksum"), hex::spec_name("e_csum")]];
u16 initialRelativeIPValue [[hex::spec_name("e_ip")]];
u16 initialCSValue [[hex::spec_name("e_cs")]];
u16 relocationsTablePointer [[hex::spec_name("e_lfarlc")]];
u16 overlayNumber [[hex::spec_name("e_ovno")]];
u16 reservedWords[4] [[hex::spec_name("e_res")]];
u16 oemIdentifier [[hex::spec_name("e_oemid")]];
u16 oemInformation [[hex::spec_name("e_oeminfo")]];
u16 otherReservedWords[10] [[hex::spec_name("e_res2")]];
u32 coffHeaderPointer [[hex::spec_name("e_lfanew")]];
};
u16 dosMessageOffset;
fn isstubdata(char c) {
return c == 0x0D || c == 0x0A || c == '$';
};
struct DOSStub {
u8 code[while($ < addressof(this) + dosMessageOffset)];
char message[while(!isstubdata(std::mem::read_unsigned($, 1)))];
char data[while(std::mem::read_string($-1, 1) != "$")];
} [[hex::spec_name("e_program")]];
fn finddosmessage() {
for (u8 i = $, i < $+16, i += 1) {
if (std::mem::read_unsigned(i, 1) == 0xBA) { // Message offset instruction
dosMessageOffset = std::mem::read_unsigned(i+1, 2);
break;
}
}
};
struct PEHeader {
DOSHeader dosHeader;
finddosmessage();
if (!dosMessageOffset)
u8 dosStub[while(std::mem::read_unsigned($, 4))] [[hex::spec_name("e_program")]];
else DOSStub dosStub;
};
PEHeader peHeader @ 0x00;
enum ArchitectureType : u16 {
Unknown = 0x00,
ALPHAAXPOld = 0x183,
ALPHAAXP = 0x184,
ALPHAAXP64Bit = 0x284,
AM33 = 0x1D3,
AMD64 = 0x8664,
ARM = 0x1C0,
ARM64 = 0xAA64,
ARMNT = 0x1C4,
CLRPureMSIL = 0xC0EE,
EBC = 0xEBC,
I386 = 0x14C,
I860 = 0x14D,
IA64 = 0x200,
LOONGARCH32 = 0x6232,
LOONGARCH64 = 0x6264,
M32R = 0x9041,
MIPS16 = 0x266,
MIPSFPU = 0x366,
MIPSFPU16 = 0x466,
MOTOROLA68000 = 0x268,
POWERPC = 0x1F0,
POWERPCFP = 0x1F1,
POWERPC64 = 0x1F2,
R3000 = 0x162,
R4000 = 0x166,
R10000 = 0x168,
RISCV32 = 0x5032,
RISCV64 = 0x5064,
RISCV128 = 0x5128,
SH3 = 0x1A2,
SH3DSP = 0x1A3,
SH4 = 0x1A6,
SH5 = 0x1A8,
THUMB = 0x1C2,
WCEMIPSV2 = 0x169
} [[hex::spec_name("MachineType")]];
enum PEFormat : u16 {
ROM = 0x107,
PE32 = 0x10B,
PE32Plus = 0x20B
};
enum SubsystemType : u16 {
Unknown = 0x00,
Native = 0x01,
WindowsGUI = 0x02,
WindowsCUI = 0x03,
OS2CUI = 0x05,
POSIXCUI = 0x07,
Windows9xNative = 0x08,
WindowsCEGUI = 0x09,
EFIApplication = 0x0A,
EFIBootServiceDriver = 0x0B,
EFIRuntimeDriver = 0x0C,
EFIROM = 0x0D,
Xbox = 0x0E,
WindowsBootApplication = 0x10
};
bitfield Characteristics {
baseRelocationsStripped : 1 [[hex::spec_name("IMAGE_FILE_RELOCS_STRIPPED")]];
executableImage : 1 [[hex::spec_name("IMAGE_FILE_EXECUTABLE_IMAGE")]];
lineNumbersStripped : 1 [[hex::spec_name("IMAGE_FILE_LINE_NUMS_STRIPPED")]];
symbolsStripped : 1 [[hex::spec_name("IMAGE_FILE_LOCAL_SYMS_STRIPPED")]];
aggressivelyTrimWorkingSet : 1 [[hex::spec_name("IMAGE_FILE_AGGRESSIVE_WS_TRIM")]];
largeAddressAware : 1 [[hex::spec_name("IMAGE_FILE_LARGE_ADDRESS_AWARE")]];
padding : 1;
bytesReversedLo : 1 [[hex::spec_name("IMAGE_FILE_BYTES_REVERSED_LO")]];
machine32Bit : 1 [[hex::spec_name("IMAGE_FILE_32BIT_MACHINE")]];
debugInfoStripped : 1 [[hex::spec_name("IMAGE_FILE_DEBUG_STRIPPED")]];
removableRunFromSwap : 1 [[hex::spec_name("IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP")]];
netRunFromSwap : 1 [[hex::spec_name("IMAGE_FILE_NET_RUN_FROM_SWAP")]];
systemFile : 1 [[hex::spec_name("IMAGE_FILE_SYSTEM")]];
dll : 1 [[hex::spec_name("IMAGE_FILE_DLL")]];
uniprocessorMachineOnly : 1 [[hex::spec_name("IMAGE_FILE_UP_SYSTEM_ONLY")]];
bytesReversedHi : 1 [[hex::spec_name("IMAGE_FILE_BYTES_REVERSED_HI")]];
};
bitfield DLLCharacteristics {
callWhenLoaded : 1;
callWhenThreadTerminates : 1;
callWhenThreadStarts : 1;
callWhenExiting : 1;
padding : 1;
highEntropyVA : 1 [[hex::spec_name("IMAGE_DLLCHARACTERISTICS_HIGH_ENTROPY_VA")]];
dynamicBase : 1 [[hex::spec_name("IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE")]];
forceIntegrity : 1 [[hex::spec_name("IMAGE_DLLCHARACTERISTICS_FORCE_INTEGRITY")]];
nxCompatible : 1 [[hex::spec_name("IMAGE_DLLCHARACTERISTICS_NX_COMPAT")]];
noIsolation : 1 [[hex::spec_name("IMAGE_DLLCHARACTERISTICS_NO_ISOLATION")]];
noSEH : 1 [[hex::spec_name("IMAGE_DLLCHARACTERISTICS_NO_SEH")]];
doNotBind : 1 [[hex::spec_name("IMAGE_DLLCHARACTERISTICS_NO_BIND")]];
appContainer : 1 [[hex::spec_name("IMAGE_DLLCHARACTERISTICS_APPCONTAINER")]];
isWDMDriver : 1 [[hex::spec_name("IMAGE_DLLCHARACTERISTICS_WDM_DRIVER")]];
supportsControlFlowGuard : 1 [[hex::spec_name("IMAGE_DLLCHARACTERISTICS_GUARD_CF")]];
terminalServerAware : 1 [[hex::spec_name("IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE ")]];
};
bitfield LoaderFlags {
prestartBreakpoint : 1 [[comment("Invoke a breakpoint instruction before starting the process")]];
postloadingDebugger : 1 [[comment("Invoke a debugger on the process after it's been loaded")]];
padding : 30;
} [[comment("(Officially declared reserved by Microsoft)")]];
struct DataDirectory {
u32 rva;
u32 size;
};
struct OptionalHeader {
PEFormat magic;
u8 majorLinkerVersion;
u8 minorLinkerVersion;
u32 sizeOfCode;
u32 sizeOfInitializedData;
u32 sizeOfUninitializedData;
u32 addressOfEntryPoint;
u32 baseOfCode;
if (magic == PEFormat::PE32Plus) {
u64 imageBase;
}
else {
u32 baseOfData;
u32 imageBase;
}
u32 virtualSectionAlignment [[hex::spec_name("sectionAlignment")]];
u32 rawSectionAlignment [[hex::spec_name("fileAlignment")]];
u16 majorOperatingSystemVersion;
u16 minorOperatingSystemVersion;
u16 majorImageVersion;
u16 minorImageVersion;
u16 majorSubsystemVersion;
u16 minorSubsystemVersion;
u32 win32VersionValue;
u32 sizeOfImage;
u32 sizeOfHeaders;
u32 checksum;
SubsystemType subsystem;
DLLCharacteristics dllCharacteristics;
if (magic == PEFormat::PE32Plus) {
u64 sizeOfStackReserve;
u64 sizeOfStackCommit;
u64 sizeOfHeapReserve;
u64 sizeOfHeapCommit;
}
else {
u32 sizeOfStackReserve;
u32 sizeOfStackCommit;
u32 sizeOfHeapReserve;
u32 sizeOfHeapCommit;
}
LoaderFlags loaderFlags;
u32 numberOfRVAsAndSizes [[hex::spec_name("numberOfRvaAndSizes")]];
DataDirectory directories[numberOfRVAsAndSizes];
};
struct COFFHeader {
char signature[4];
ArchitectureType architecture [[hex::spec_name("machine")]];
u16 numberOfSections;
type::time32_t timeDateStamp;
u32 pointerToSymbolTable;
u32 numberOfSymbols;
u16 sizeOfOptionalHeader;
Characteristics characteristics;
OptionalHeader optionalHeader;
};
COFFHeader coffHeader @ peHeader.dosHeader.coffHeaderPointer;
enum AlignmentType : u8 {
BoundaryOf1Byte = 1,
BoundaryOf2Bytes,
BoundaryOf4Bytes,
BoundaryOf8Bytes,
BoundaryOf16Bytes,
BoundaryOf32Bytes,
BoundaryOf64Bytes,
BoundaryOf128Bytes,
BoundaryOf256Bytes,
BoundaryOf512Bytes,
BoundaryOf1024Bytes,
BoundaryOf2048Bytes,
BoundaryOf4096Bytes,
BoundaryOf8192Bytes,
};
fn formatAlignmentBits(u8 value) {
if (value > 0) {
AlignmentType enumValue = value;
return enumValue;
}
return value;
};
bitfield SectionFlags {
padding : 3;
doNotPad : 1 [[hex::spec_name("IMAGE_SCN_TYPE_NO_PAD")]];
padding : 1;
containsCode : 1 [[hex::spec_name("IMAGE_SCN_CNT_CODE")]];
containsInitializedData : 1 [[hex::spec_name("IMAGE_SCN_CNT_INITIALIZED_DATA")]];
containsUninitializedData : 1 [[hex::spec_name("IMAGE_SCN_CNT_UNINITIALIZED_DATA")]];
linkOther : 1 [[hex::spec_name("IMAGE_SCN_LNK_OTHER")]];
linkHasInformation : 1 [[hex::spec_name("IMAGE_SCN_LNK_INFO")]];
padding : 1;
linkRemove : 1 [[hex::spec_name("IMAGE_SCN_LNK_REMOVE")]];
linkHasCOMDAT : 1 [[hex::spec_name("IMAGE_SCN_LNK_COMDAT")]];
padding : 1;
resetSpeculativeExceptions : 1 [[hex::spec_name("")]];
globalPointerRelocations : 1 [[hex::spec_name("IMAGE_SCN_GPREL")]];
purgeable : 1 [[hex::spec_name("IMAGE_SCN_MEM_PURGEABLE")]];
is16Bit : 1 [[hex::spec_name("IMAGE_SCN_MEM_16BIT")]];
locked : 1 [[hex::spec_name("IMAGE_SCN_MEM_LOCKED")]];
preloaded : 1 [[hex::spec_name("IMAGE_SCN_MEM_PRELOAD")]];
dataAlignment : 4 [[format("formatAlignmentBits")]];
linkExtendedRelocations : 1 [[hex::spec_name("IMAGE_SCN_LNK_NRELOC_OVFL")]];
discardable : 1 [[hex::spec_name("IMAGE_SCN_MEM_DISCARDABLE")]];
notCached : 1 [[hex::spec_name("IMAGE_SCN_MEM_NOT_CACHED")]];
notPageable : 1 [[hex::spec_name("IMAGE_SCN_MEM_NOT_PAGED")]];
shared : 1 [[hex::spec_name("IMAGE_SCN_MEM_SHARED")]];
executed : 1 [[hex::spec_name("IMAGE_SCN_MEM_EXECUTE")]];
read : 1 [[hex::spec_name("IMAGE_SCN_MEM_READ")]];
writtenOn : 1 [[hex::spec_name("IMAGE_SCN_MEM_WRITE")]];
};
fn formatSectionName(str string) {
for (u8 i = 0, i < 8, i += 1) {
if (std::mem::read_unsigned($+i, 1) == 0) {
return "\"" + std::string::substr(string, 0, i) + "\"";
}
}
};
struct SectionHeader {
char name[8] [[name("sectionName"), hex::spec_name("name"), format("formatSectionName")]];
u32 virtualSize;
u32 rva [[hex::spec_name("virtualAddress")]];
u32 sizeOfRawData;
u32 ptrRawData;
u32 ptrRelocations;
u32 ptrLineNumbers;
u16 numberOfRelocations;
u16 numberOfLineNumbers;
SectionFlags characteristics;
};
SectionHeader sectionsTable[coffHeader.numberOfSections] @ addressof(coffHeader.optionalHeader) + coffHeader.sizeOfOptionalHeader;
// General Section things
u16 currentSectionIndex;
fn relativeVirtualDifference() {
return sectionsTable[currentSectionIndex].rva - sectionsTable[currentSectionIndex].ptrRawData;
};
fn wordsize() {
return std::mem::read_unsigned(addressof(coffHeader.optionalHeader.magic), 2) / 0x41;
};
fn formatNullTerminatedString(str string) {
return "\"" + std::string::substr(string, 0, std::string::length(string)-1) + "\"";
};
// Exception Table
bitfield FunctionBitfield {
prologLength : 8;
functionLength : 22;
instructions32Bit : 1;
exceptionHandler : 1;
};
struct FunctionTableEntry {
if (coffHeader.architecture == ArchitectureType::MIPSFPU || coffHeader.architecture == ArchitectureType::R3000) {
u32 beginVA;
u32 endVA;
u32 exceptionHandlerPointer;
u32 handlerDataPointer;
u32 prologEndVA;
} else if (coffHeader.architecture == ArchitectureType::ARM || coffHeader.architecture == ArchitectureType::ARM64 || coffHeader.architecture == ArchitectureType::ARMNT ||
coffHeader.architecture == ArchitectureType::POWERPC || coffHeader.architecture == ArchitectureType::POWERPCFP ||
coffHeader.architecture == ArchitectureType::SH3 || coffHeader.architecture == ArchitectureType::SH3DSP || coffHeader.architecture == ArchitectureType::SH4) {
u32 beginVA;
FunctionBitfield miscellaneousBits;
} else if (coffHeader.architecture == ArchitectureType::AMD64 || coffHeader.architecture == ArchitectureType::IA64) {
u32 beginRVA;
u32 endRVA;
u32 unwindInformationRVA;
}
};
struct ExceptionTable {
FunctionTableEntry functionTableEntries[while($ < (coffHeader.optionalHeader.directories[3].rva - relativeVirtualDifference()) + coffHeader.optionalHeader.directories[3].size)] [[inline]];
};
// Exports Table
struct ExportDirectoryTable {
u32 flags [[name("exportFlags")]];
type::time32_t timeDateStamp [[name("exportTimeDateStamp")]];
u16 majorVersion;
u16 minorVersion;
u32 imageNameRVA;
u32 ordinalBase [[name("exportOrdinalBase")]];
u32 addressesAmount [[name("exportAddressesAmount")]];
u32 namePointersAmount [[name("exportNamePointersAmount")]];
u32 addressTableRVA [[name("exportAddressTableRVA")]];
u32 namePointerTableRVA [[name("exportNamePointerTableRVA")]];
u32 ordinalTableRVA [[name("exportOrdinalTableRVA")]];
};
struct ExportAddress {
if (sectionsTable[currentSectionIndex].ptrRawData > std::mem::read_unsigned($, 4) > sectionsTable[currentSectionIndex].sizeOfRawData) {
u32 exportRVA;
}
else {
u32 forwarderRVA;
}
};
struct ExportNamePointer {
u32 exportNameRVA;
char exportName[] @ exportNameRVA - relativeVirtualDifference() [[format("formatNullTerminatedString")]];
};
struct ExportsTable {
ExportDirectoryTable directoryTable;
ExportAddress exportAddressTable[directoryTable.addressesAmount] @ directoryTable.addressTableRVA - relativeVirtualDifference();
ExportNamePointer exportNamePointerTable[directoryTable.namePointersAmount] @ directoryTable.namePointerTableRVA - relativeVirtualDifference();
u16 exportOrdinalTable[directoryTable.namePointersAmount] @ directoryTable.ordinalTableRVA - relativeVirtualDifference();
char imageName[] @ directoryTable.imageNameRVA - relativeVirtualDifference() [[format("formatNullTerminatedString")]];
$ = addressof(this)+coffHeader.optionalHeader.directories[0].size;
};
// Imports Table
bitfield OrdinalFlagByte {
padding : 7;
flag : 1 [[name("ordinalFlag")]];
};
struct ImportsName {
u16 hint;
char name[] [[format("formatNullTerminatedString")]];
if ($ % 2 == 1) { u8 pad; }
};
struct ImportsAddress {
OrdinalFlagByte ordinalFlagByte @ $+(wordsize()-1);
if (ordinalFlagByte.flag) {
u16 ordinalNumber;
padding[wordsize()-2];
} else {
u32 nameTableRVA;
if (coffHeader.optionalHeader.magic == PEFormat::PE32Plus) {
padding[4];
}
}
};
struct ImportsLookup : ImportsAddress {
if (!ordinalFlagByte.flag && std::mem::read_unsigned($-wordsize(), wordsize()) > 0) {
ImportsName name @ nameTableRVA - relativeVirtualDifference();
}
};
struct ImportsDirectory {
u32 lookupTableRVA;
u32 timeDateStamp;
u32 forwarderChain;
u32 dllNameRVA;
u32 addressTableRVA;
};
struct ImportsStructure {
if (parent.importsDirectoryTable[std::core::array_index()].lookupTableRVA > 0) {
ImportsLookup lookupTable[while(std::mem::read_unsigned($, wordsize()) != 0)] @ parent.importsDirectoryTable[std::core::array_index()].lookupTableRVA - relativeVirtualDifference();
}
if (parent.importsDirectoryTable[std::core::array_index()].addressTableRVA > 0) {
ImportsAddress addressTable[while(std::mem::read_unsigned($, wordsize()) != 0)] @ parent.importsDirectoryTable[std::core::array_index()].addressTableRVA - relativeVirtualDifference();
}
if (parent.importsDirectoryTable[std::core::array_index()].dllNameRVA > 0) {
char dllName[] @ parent.importsDirectoryTable[std::core::array_index()].dllNameRVA - relativeVirtualDifference() [[format("formatNullTerminatedString")]];
}
} [[inline]];
struct ImportsTable {
ImportsDirectory importsDirectoryTable[while(std::mem::read_unsigned($, 16) != 0)];
ImportsStructure importsStructures[sizeof(importsDirectoryTable)/sizeof(importsDirectoryTable[0])] [[inline]];
$ = addressof(this)+coffHeader.optionalHeader.directories[1].size;
};
struct DelayedImportsDirectory {
u32 attributes;
u32 name;
u32 moduleHandle;
u32 delayImportAddressTable;
u32 delayImportNameTable;
u32 boundDelayImportTable;
u32 unloadDelayImportTable;
u32 timeStamp;
};
struct DelayImportsStructure {
if (parent.delayImportsDirectoryTable[std::core::array_index()].delayImportNameTable > 0) {
ImportsLookup delayedLookupTable[while(std::mem::read_unsigned($, wordsize()) != 0)] @ parent.delayImportsDirectoryTable[std::core::array_index()].delayImportNameTable - relativeVirtualDifference();
}
if (parent.delayImportsDirectoryTable[std::core::array_index()].name > 0) {
char dllName[] @ parent.delayImportsDirectoryTable[std::core::array_index()].name - relativeVirtualDifference() [[format("formatNullTerminatedString")]];
}
} [[inline]];
// https://learn.microsoft.com/en-us/windows/win32/debug/pe-format#delay-load-import-tables-image-only
struct DelayedImportsTable {
DelayedImportsDirectory delayImportsDirectoryTable[while(std::mem::read_unsigned($, 16) != 0)];
DelayImportsStructure delayImportsStructures[sizeof(delayImportsDirectoryTable)/sizeof(delayImportsDirectoryTable[0])] [[inline]];
$ = addressof(this)+coffHeader.optionalHeader.directories[1].size;
};
// General Resource Table things
fn formatNullTerminatedString16(str string) {
return "\"" + std::string::substr(string, 0, std::string::length(string)) + "\"";
};
// * Bitmap Resource
struct BitmapHeader {
u32 size [[name("bitmapSize")]];
u32 width [[name("bitmapWidth")]];
u32 height [[name("bitmapHeight")]];
u16 planes [[name("bitmapPlanes")]];
u16 bitCount [[name("bitmapBitCount")]];
u32 compression [[name("bitmapCompression")]];
u32 imageSize [[name("bitmapImageSize")]];
u32 xPelsPerMeter [[name("bitmapXPelsPerMeter")]];
u32 yPelsPerMeter [[name("bitmapYPelsPerMeter")]];
u32 clrUsed [[name("bitmapClrUsed")]];
u32 clrImportant [[name("bitmapClrImportant")]];
};
struct Colors {
u8 blue [[color("1F77B4")]];
u8 green [[color("2CA02C")]];
u8 red [[color("D62728")]];
u8 reserved [[color("828282")]];
};
u32 imageDataSize;
struct Bitmap {
BitmapHeader bmh [[name("bitmapHeader")]];
if ((bmh.bitCount != 24) && (bmh.bitCount != 32)) {
Colors rgbq[bmh.clrUsed*((1 << bmh.bitCount)*!bmh.clrUsed)];
imageDataSize = imageDataSize - sizeof(rgbq);
}
u8 imageData[imageDataSize-sizeof(bmh)];
};
// * Cursor Resource
struct Cursor {
u16 reserved;
u16 type;
if (!type) {
imageDataSize = parent.size-4;
Bitmap bitmapData [[inline]];
}
};
// * Dialog Resource
enum AtomType : u16 {
Button = 0x80,
Edit,
Static,
ListBox,
ScrollBar,
ComboBox
};
struct VLSElement {
if (std::mem::read_unsigned($, 2) == 0xFFFF) {
u16 atomDefiner;
}
else {
AtomType atom;
}
};
struct VariableLengthStructure {
if (std::mem::read_unsigned($, 2) == 0xFFFF) {
VLSElement vlsElement[2];
}
else {
char16 string[] [[format("formatNullTerminatedString16")]];
}
};
struct DialogTemplate {
if (parent.parent.parent.parent.id == 0x411) {
u16 version;
u16 signature;
u32 helpContextIdentifier;
u32 extendedStyles;
u32 style;
}
else {
u32 style;
u32 extendedStyles;
}
u16 numberOfItems;
u16 x;
u16 y;
u16 width;
u16 height;
if (parent.parent.parent.parent.id == 0x409 || parent.parent.parent.parent.id == 0x411) {
VariableLengthStructure dialogMenu;
VariableLengthStructure dialogClass;
char16 dialogTitle[] [[format("formatNullTerminatedString16")]];
u16 dialogFontSize;
if (parent.parent.parent.parent.id == 0x411) {
u16 weight;
bool italic;
u8 charset;
}
char16 dialogFontTypeface[] [[format("formatNullTerminatedString16")]];
if (($+2)%16 == 0 || 4 || 8 || 12)
u16 alignment;
}
};
struct DialogItemTemplate {
if (parent.parent.parent.parent.parent.id == 0x411) {
u32 helpContextIdentifier;
u32 extendedStyles [[name("itemExtendedStyles")]];
u32 style [[name("itemStyle")]];
}
else {
u32 style [[name("itemStyle")]];
u32 extendedStyles [[name("itemExtendedStyles")]];
}
u16 x;
u16 y;
u16 width;
u16 height;
if (parent.parent.parent.parent.parent.id == 0x409 || parent.parent.parent.parent.parent.id == 0x411) {
u32 controlID;
VariableLengthStructure windowClass;
VariableLengthStructure dialogItemTitle;
u16 extraCount;
} else {
u16 controlID;
}
if (($+2)%16 == 0 || 4 || 8)
u16 alignment;
};
struct DialogItem {
DialogItemTemplate template [[inline]];
/*if (parent.parent.parent.parent.id == 0x409 || parent.parent.parent.parent.id == 0x411) {
u8 itemCreationData[template.extraCount];
}*/
};
struct Dialog {
DialogTemplate dialogTemplate;
DialogItem items[dialogTemplate.numberOfItems];
};
// * String Resource
struct StringTableResource {
std::string::SizedString16<u16> strings[while($ < (parent.dataRVA - relativeVirtualDifference()) + parent.size)];
};
// * GroupCursor Resource
struct GroupCursor {
u16 assorteddata;
u16 colors;
u16 otherassorteddata;
u16 pixels;
u16 assorteddataarray[5];
u16 ordinalName;
};
// * GroupIcon Resource
struct GroupIconHeader {
u16 reserved;
u16 type;
u16 count;
};
struct GroupIconEntry {
u8 width;
u8 height;
u8 colorCount;
u8 reserved;
u16 planes;
u16 bitCount;
u32 bytesInResource;
u16 id;
};
struct GroupIcon {
GroupIconHeader header;
GroupIconEntry entries[header.count];
};
// * Version Resource
struct VersionEntryHeader {
u16 length;
u16 valueLength;
u16 type;
char16 key[] [[format("formatNullTerminatedString16")]];
padding[while(std::mem::read_unsigned($, 1) == 0 && $ < addressof(key)+sizeof(key)+5)];
};
struct StringInfo {
VersionEntryHeader stringInfoHeader;
if (stringInfoHeader.valueLength > 0) {
char16 string[] [[format("formatNullTerminatedString16")]];
padding[while(std::mem::read_unsigned($, 1) == 0)];
}
};
struct VersionEntry {
VersionEntryHeader header [[inline]];
if (header.key == "StringFileInfo") {
VersionEntryHeader stringTableHeader;
StringInfo strings[while($ < addressof(stringTableHeader) + stringTableHeader.length)];
}
else if (header.key == "VarFileInfo") {
VersionEntryHeader varHeader;
u16 translation[varHeader.valueLength / 2];
}
else {
u8 value[header.valueLength];
padding[while(std::mem::read_unsigned($, 1) == 0)];
}
};
struct Version {
VersionEntryHeader header [[inline]];
u32 signature;
u16 structVersion[2];
u16 fileVersion[4];
u16 productVersion[4];
u32 fileFlagsMask[2];
u32 fileFlags;
u32 fileOS;
u32 fileType;
u32 fileSubType;
u32 fileTimestamp;
VersionEntry children[while($ < (parent.dataRVA - relativeVirtualDifference()) + parent.size)];
};
// * Resources Using TrueChar
fn displayTrueChar(u8 value) {
str notation = "0x";
if (value < 0x10)
notation += "0";
if (value == 0)
return "'␀' (" + std::format(notation + "{:X}", value) + ")";
else
return "'" + char(value) + "' (" + std::format(notation + "{:X}", value) + ")";
};
// Resource Table
enum ResourceID : u32 {
Cursor = 0x01,
Bitmap,
Icon,
Menu,
Dialog,
String,
Accelerator = 0x09,
StringData,
GroupCursor = 0x0C,
GroupIcon = 0x0E,
Version = 0x10,
Manifest = 0x18
};
ResourceID resourceIDType;
using TrueChar = u8 [[format("displayTrueChar")]];
struct DataEntry {
u32 dataRVA;
u32 size;
u32 codepage;
u32 reserved;
if (resourceIDType == ResourceID::Cursor) {
Cursor cursor @ dataRVA - relativeVirtualDifference();
}
else if (resourceIDType == ResourceID::Bitmap || (resourceIDType == ResourceID::Icon && std::mem::read_string((dataRVA - relativeVirtualDifference())+1, 3) != "PNG")) {
imageDataSize = size;
Bitmap bitmap @ dataRVA - relativeVirtualDifference();
}
else if (resourceIDType == ResourceID::Dialog) {
Dialog dialog @ dataRVA - relativeVirtualDifference();
}
else if (resourceIDType == ResourceID::String) {
StringTableResource stringTableResource @ dataRVA - relativeVirtualDifference();
}
else if (resourceIDType == ResourceID::StringData) {
TrueChar stringData[size] @ dataRVA - relativeVirtualDifference();
}
else if (resourceIDType == ResourceID::GroupCursor) {
GroupCursor groupCursor @ dataRVA - relativeVirtualDifference();
}
else if (resourceIDType == ResourceID::GroupIcon) {
GroupIcon groupIcon @ dataRVA - relativeVirtualDifference();
}
else if (resourceIDType == ResourceID::Version) {
Version version @ dataRVA - relativeVirtualDifference();
}
else if (resourceIDType == ResourceID::Manifest) {
TrueChar manifest[size] @ dataRVA - relativeVirtualDifference();
}
else {
u8 resource[size] @ dataRVA - relativeVirtualDifference();
}
};
using ResourceDirectory;
bitfield OffsetField {
offset : 31;
pointingToDirectory : 1;
};
struct DataField {
if (std::mem::read_unsigned($+3, 1) >= 0x80) {
OffsetField offsetToData;
ResourceDirectory directory @ coffHeader.optionalHeader.directories[2].rva - relativeVirtualDifference() + offsetToData.offset;
}
else {
u32 offsetToData;
DataEntry dataEntry @ coffHeader.optionalHeader.directories[2].rva - relativeVirtualDifference() + offsetToData;
}
} [[inline]];
struct IdDirectoryEntry {
if ($ > coffHeader.optionalHeader.directories[2].rva - relativeVirtualDifference() + 0x10 + 8*(parent.directoryTable.nameEntriesAmount + parent.directoryTable.idEntriesAmount)) {
u32 id;
}
else {
ResourceID id;
resourceIDType = std::mem::read_unsigned(addressof(id), 4);
}
DataField datafield;
};
struct NameDirectoryEntry {
OffsetField offsetToName;
std::string::SizedString16<u16> name @ coffHeader.optionalHeader.directories[2].rva - relativeVirtualDifference() + offsetToName.offset;
DataField datafield;
};
struct ResourceDirectoryTable {
u32 characteristics;
u32 timeDateStamp;
u16 majorVersion;
u16 minorVersion;
u16 nameEntriesAmount;
u16 idEntriesAmount;
};
struct ResourceDirectory {
ResourceDirectoryTable directoryTable [[hex::spec_name("resourceDirectoryTable")]];
NameDirectoryEntry nameEntries[directoryTable.nameEntriesAmount];
IdDirectoryEntry idEntries[directoryTable.idEntriesAmount];
};
struct ResourceTable {
ResourceDirectory rootDirectory;
$ = addressof(this)+coffHeader.optionalHeader.directories[2].size;
};
// Base Relocations Table
enum BaseRelocationType : u8 {
Absolute,
High,
Low,
HighLow,
HighAdjacent,
Reserved = 6,
DIR64 = 10
};
enum MIPSBaseRelocationType : u8 {
Absolute,
High,
Low,
HighLow,
HighAdjacent,
MIPSJMPAddress,
Reserved,
MIPSJMPAddress16 = 9,
DIR64
};
enum ARMBaseRelocationType : u8 {
Absolute,
High,
Low,
HighLow,
HighAdjacent,
ARMMOV32,
Reserved,
DIR64 = 10
};
enum RISCVBaseRelocationType : u8 {
Absolute,
High,
Low,
HighLow,
HighAdjacent,
RISCVHigh20,
Reserved,
RISCVLow12I,
RISCVLow12S,
DIR64 = 10
};
enum THUMBBaseRelocationType : u8 {
Absolute,
High,
Low,
HighLow,
HighAdjacent,
ARMMOV32,
Reserved,
ThumbMOV32,
DIR64 = 10
};
enum LoongarchBaseRelocationType : u8 {
Absolute,
High,
Low,
HighLow,
HighAdjacent,
Reserved = 6,
MarkLA = 8,
DIR64 = 10
};
fn formatBaseRelocationType(u8 value) {
if (coffHeader.architecture == ArchitectureType::MIPS16 || coffHeader.architecture == ArchitectureType::MIPSFPU || coffHeader.architecture == ArchitectureType::MIPSFPU16 ||
coffHeader.architecture == ArchitectureType::R3000 || coffHeader.architecture == ArchitectureType::R4000 || coffHeader.architecture == ArchitectureType::R10000) {
MIPSBaseRelocationType mipsTypeBits = value;
return mipsTypeBits;
}
else if (coffHeader.architecture == ArchitectureType::ARM || coffHeader.architecture == ArchitectureType::ARM64 || coffHeader.architecture == ArchitectureType::ARMNT) {
ARMBaseRelocationType armTypeBits = value;
return armTypeBits;
}
else if (coffHeader.architecture == ArchitectureType::RISCV32 || coffHeader.architecture == ArchitectureType::RISCV64 || coffHeader.architecture == ArchitectureType::RISCV128) {
RISCVBaseRelocationType riscvTypeBits = value;
return riscvTypeBits;
}
else if (coffHeader.architecture == ArchitectureType::THUMB) {
THUMBBaseRelocationType thumbTypeBits = value;
return thumbTypeBits;
}
else if (coffHeader.architecture == ArchitectureType::LOONGARCH32 || coffHeader.architecture == ArchitectureType::LOONGARCH64) {
LoongarchBaseRelocationType loongarchTypeBits = value;
return loongarchTypeBits;
}
else {
BaseRelocationType genericTypeBits = value;
return genericTypeBits;
}
};
bitfield BaseRelocationWord {
offset : 12;
type : 4 [[format("formatBaseRelocationType")]];
};
struct BaseRelocationBlock {
u32 pageRVA;
u32 blockSize;
BaseRelocationWord word[while($ < addressof(this) + this.blockSize)] [[inline]];
};
struct BaseRelocationTable {
BaseRelocationBlock baseRelocationBlocks[while($ < addressof(this) + coffHeader.optionalHeader.directories[5].size)] [[inline]];
};
// Debug Table
enum DebugType : u32 {
Unknown,
COFF,
Codeview,
FPO,
Misc,
Exception,
Fixup,
OmapToSRC,
OmapFromSRC,
Borland,
Reserved10,
CLSID,
REPRO = 16,
ExtendedDLLCharacteristics = 20
};
struct RSDS {
char signature[4];
type::GUID guid;
u32 age;
char path[] [[format("formatNullTerminatedString")]];
};
struct DebugDirectory {
u32 characteristics;
type::time32_t timeDateStamp;
u16 majorVersion;
u16 minorVersion;
DebugType type;
u32 sizeOfData;
u32 virtualAddressOfRawData;
u32 pointerOfRawData;
};
struct DebugData {
DebugDirectory directory;
if (std::mem::read_string(directory.pointerOfRawData, 4) == "RSDS") {
RSDS rsds @ directory.pointerOfRawData;
}
else {