-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathpe.rs
3050 lines (2789 loc) · 98.8 KB
/
pe.rs
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
//! PE/COFF definitions.
//!
//! These definitions are independent of read/write support, although we do implement
//! some traits useful for those.
//!
//! This module is based heavily on "winnt.h" (10.0.17763.0).
#![allow(missing_docs)]
use core::convert::TryInto;
use crate::endian::{I32Bytes, LittleEndian as LE, U16Bytes, U32Bytes, I32, U16, U32, U64};
use crate::pod::Pod;
/// MZ
pub const IMAGE_DOS_SIGNATURE: u16 = 0x5A4D;
/// NE
pub const IMAGE_OS2_SIGNATURE: u16 = 0x454E;
/// LE
pub const IMAGE_OS2_SIGNATURE_LE: u16 = 0x454C;
/// LE
pub const IMAGE_VXD_SIGNATURE: u16 = 0x454C;
/// PE00
pub const IMAGE_NT_SIGNATURE: u32 = 0x0000_4550;
/// DOS .EXE header
#[derive(Debug, Clone, Copy)]
#[repr(C)]
pub struct ImageDosHeader {
/// Magic number
pub e_magic: U16<LE>,
/// Bytes on last page of file
pub e_cblp: U16<LE>,
/// Pages in file
pub e_cp: U16<LE>,
/// Relocations
pub e_crlc: U16<LE>,
/// Size of header in paragraphs
pub e_cparhdr: U16<LE>,
/// Minimum extra paragraphs needed
pub e_minalloc: U16<LE>,
/// Maximum extra paragraphs needed
pub e_maxalloc: U16<LE>,
/// Initial (relative) SS value
pub e_ss: U16<LE>,
/// Initial SP value
pub e_sp: U16<LE>,
/// Checksum
pub e_csum: U16<LE>,
/// Initial IP value
pub e_ip: U16<LE>,
/// Initial (relative) CS value
pub e_cs: U16<LE>,
/// File address of relocation table
pub e_lfarlc: U16<LE>,
/// Overlay number
pub e_ovno: U16<LE>,
/// Reserved words
pub e_res: [U16<LE>; 4],
/// OEM identifier (for e_oeminfo)
pub e_oemid: U16<LE>,
/// OEM information; e_oemid specific
pub e_oeminfo: U16<LE>,
/// Reserved words
pub e_res2: [U16<LE>; 10],
/// File address of new exe header
pub e_lfanew: U32<LE>,
}
/// OS/2 .EXE header
#[derive(Debug, Clone, Copy)]
#[repr(C)]
pub struct ImageOs2Header {
/// Magic number
pub ne_magic: U16<LE>,
/// Version number
pub ne_ver: i8,
/// Revision number
pub ne_rev: i8,
/// Offset of Entry Table
pub ne_enttab: U16<LE>,
/// Number of bytes in Entry Table
pub ne_cbenttab: U16<LE>,
/// Checksum of whole file
pub ne_crc: I32<LE>,
/// Flag word
pub ne_flags: U16<LE>,
/// Automatic data segment number
pub ne_autodata: U16<LE>,
/// Initial heap allocation
pub ne_heap: U16<LE>,
/// Initial stack allocation
pub ne_stack: U16<LE>,
/// Initial CS:IP setting
pub ne_csip: I32<LE>,
/// Initial SS:SP setting
pub ne_sssp: I32<LE>,
/// Count of file segments
pub ne_cseg: U16<LE>,
/// Entries in Module Reference Table
pub ne_cmod: U16<LE>,
/// Size of non-resident name table
pub ne_cbnrestab: U16<LE>,
/// Offset of Segment Table
pub ne_segtab: U16<LE>,
/// Offset of Resource Table
pub ne_rsrctab: U16<LE>,
/// Offset of resident name table
pub ne_restab: U16<LE>,
/// Offset of Module Reference Table
pub ne_modtab: U16<LE>,
/// Offset of Imported Names Table
pub ne_imptab: U16<LE>,
/// Offset of Non-resident Names Table
pub ne_nrestab: I32<LE>,
/// Count of movable entries
pub ne_cmovent: U16<LE>,
/// Segment alignment shift count
pub ne_align: U16<LE>,
/// Count of resource segments
pub ne_cres: U16<LE>,
/// Target Operating system
pub ne_exetyp: u8,
/// Other .EXE flags
pub ne_flagsothers: u8,
/// offset to return thunks
pub ne_pretthunks: U16<LE>,
/// offset to segment ref. bytes
pub ne_psegrefbytes: U16<LE>,
/// Minimum code swap area size
pub ne_swaparea: U16<LE>,
/// Expected Windows version number
pub ne_expver: U16<LE>,
}
/// Windows VXD header
#[derive(Debug, Clone, Copy)]
#[repr(C)]
pub struct ImageVxdHeader {
/// Magic number
pub e32_magic: U16<LE>,
/// The byte ordering for the VXD
pub e32_border: u8,
/// The word ordering for the VXD
pub e32_worder: u8,
/// The EXE format level for now = 0
pub e32_level: U32<LE>,
/// The CPU type
pub e32_cpu: U16<LE>,
/// The OS type
pub e32_os: U16<LE>,
/// Module version
pub e32_ver: U32<LE>,
/// Module flags
pub e32_mflags: U32<LE>,
/// Module # pages
pub e32_mpages: U32<LE>,
/// Object # for instruction pointer
pub e32_startobj: U32<LE>,
/// Extended instruction pointer
pub e32_eip: U32<LE>,
/// Object # for stack pointer
pub e32_stackobj: U32<LE>,
/// Extended stack pointer
pub e32_esp: U32<LE>,
/// VXD page size
pub e32_pagesize: U32<LE>,
/// Last page size in VXD
pub e32_lastpagesize: U32<LE>,
/// Fixup section size
pub e32_fixupsize: U32<LE>,
/// Fixup section checksum
pub e32_fixupsum: U32<LE>,
/// Loader section size
pub e32_ldrsize: U32<LE>,
/// Loader section checksum
pub e32_ldrsum: U32<LE>,
/// Object table offset
pub e32_objtab: U32<LE>,
/// Number of objects in module
pub e32_objcnt: U32<LE>,
/// Object page map offset
pub e32_objmap: U32<LE>,
/// Object iterated data map offset
pub e32_itermap: U32<LE>,
/// Offset of Resource Table
pub e32_rsrctab: U32<LE>,
/// Number of resource entries
pub e32_rsrccnt: U32<LE>,
/// Offset of resident name table
pub e32_restab: U32<LE>,
/// Offset of Entry Table
pub e32_enttab: U32<LE>,
/// Offset of Module Directive Table
pub e32_dirtab: U32<LE>,
/// Number of module directives
pub e32_dircnt: U32<LE>,
/// Offset of Fixup Page Table
pub e32_fpagetab: U32<LE>,
/// Offset of Fixup Record Table
pub e32_frectab: U32<LE>,
/// Offset of Import Module Name Table
pub e32_impmod: U32<LE>,
/// Number of entries in Import Module Name Table
pub e32_impmodcnt: U32<LE>,
/// Offset of Import Procedure Name Table
pub e32_impproc: U32<LE>,
/// Offset of Per-Page Checksum Table
pub e32_pagesum: U32<LE>,
/// Offset of Enumerated Data Pages
pub e32_datapage: U32<LE>,
/// Number of preload pages
pub e32_preload: U32<LE>,
/// Offset of Non-resident Names Table
pub e32_nrestab: U32<LE>,
/// Size of Non-resident Name Table
pub e32_cbnrestab: U32<LE>,
/// Non-resident Name Table Checksum
pub e32_nressum: U32<LE>,
/// Object # for automatic data object
pub e32_autodata: U32<LE>,
/// Offset of the debugging information
pub e32_debuginfo: U32<LE>,
/// The length of the debugging info. in bytes
pub e32_debuglen: U32<LE>,
/// Number of instance pages in preload section of VXD file
pub e32_instpreload: U32<LE>,
/// Number of instance pages in demand load section of VXD file
pub e32_instdemand: U32<LE>,
/// Size of heap - for 16-bit apps
pub e32_heapsize: U32<LE>,
/// Reserved words
pub e32_res3: [u8; 12],
pub e32_winresoff: U32<LE>,
pub e32_winreslen: U32<LE>,
/// Device ID for VxD
pub e32_devid: U16<LE>,
/// DDK version for VxD
pub e32_ddkver: U16<LE>,
}
/// A PE rich header entry.
///
/// Rich headers have no official documentation, but have been heavily
/// reversed-engineered and documented in the wild, e.g.:
/// * `http://www.ntcore.com/files/richsign.htm`
/// * `https://www.researchgate.net/figure/Structure-of-the-Rich-Header_fig1_318145388`
///
/// This data is "masked", i.e. XORed with a checksum derived from the file data.
#[derive(Debug, Clone, Copy)]
#[repr(C)]
pub struct MaskedRichHeaderEntry {
pub masked_comp_id: U32<LE>,
pub masked_count: U32<LE>,
}
//
// File header format.
//
#[derive(Debug, Clone, Copy)]
#[repr(C)]
pub struct ImageFileHeader {
pub machine: U16<LE>,
pub number_of_sections: U16<LE>,
pub time_date_stamp: U32<LE>,
pub pointer_to_symbol_table: U32<LE>,
pub number_of_symbols: U32<LE>,
pub size_of_optional_header: U16<LE>,
pub characteristics: U16<LE>,
}
pub const IMAGE_SIZEOF_FILE_HEADER: usize = 20;
/// Relocation info stripped from file.
pub const IMAGE_FILE_RELOCS_STRIPPED: u16 = 0x0001;
/// File is executable (i.e. no unresolved external references).
pub const IMAGE_FILE_EXECUTABLE_IMAGE: u16 = 0x0002;
/// Line numbers stripped from file.
pub const IMAGE_FILE_LINE_NUMS_STRIPPED: u16 = 0x0004;
/// Local symbols stripped from file.
pub const IMAGE_FILE_LOCAL_SYMS_STRIPPED: u16 = 0x0008;
/// Aggressively trim working set
pub const IMAGE_FILE_AGGRESIVE_WS_TRIM: u16 = 0x0010;
/// App can handle >2gb addresses
pub const IMAGE_FILE_LARGE_ADDRESS_AWARE: u16 = 0x0020;
/// Bytes of machine word are reversed.
pub const IMAGE_FILE_BYTES_REVERSED_LO: u16 = 0x0080;
/// 32 bit word machine.
pub const IMAGE_FILE_32BIT_MACHINE: u16 = 0x0100;
/// Debugging info stripped from file in .DBG file
pub const IMAGE_FILE_DEBUG_STRIPPED: u16 = 0x0200;
/// If Image is on removable media, copy and run from the swap file.
pub const IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP: u16 = 0x0400;
/// If Image is on Net, copy and run from the swap file.
pub const IMAGE_FILE_NET_RUN_FROM_SWAP: u16 = 0x0800;
/// System File.
pub const IMAGE_FILE_SYSTEM: u16 = 0x1000;
/// File is a DLL.
pub const IMAGE_FILE_DLL: u16 = 0x2000;
/// File should only be run on a UP machine
pub const IMAGE_FILE_UP_SYSTEM_ONLY: u16 = 0x4000;
/// Bytes of machine word are reversed.
pub const IMAGE_FILE_BYTES_REVERSED_HI: u16 = 0x8000;
pub const IMAGE_FILE_MACHINE_UNKNOWN: u16 = 0;
/// Useful for indicating we want to interact with the host and not a WoW guest.
pub const IMAGE_FILE_MACHINE_TARGET_HOST: u16 = 0x0001;
/// Intel 386.
pub const IMAGE_FILE_MACHINE_I386: u16 = 0x014c;
/// MIPS little-endian, 0x160 big-endian
pub const IMAGE_FILE_MACHINE_R3000: u16 = 0x0162;
/// MIPS little-endian
pub const IMAGE_FILE_MACHINE_R4000: u16 = 0x0166;
/// MIPS little-endian
pub const IMAGE_FILE_MACHINE_R10000: u16 = 0x0168;
/// MIPS little-endian WCE v2
pub const IMAGE_FILE_MACHINE_WCEMIPSV2: u16 = 0x0169;
/// Alpha_AXP
pub const IMAGE_FILE_MACHINE_ALPHA: u16 = 0x0184;
/// SH3 little-endian
pub const IMAGE_FILE_MACHINE_SH3: u16 = 0x01a2;
pub const IMAGE_FILE_MACHINE_SH3DSP: u16 = 0x01a3;
/// SH3E little-endian
pub const IMAGE_FILE_MACHINE_SH3E: u16 = 0x01a4;
/// SH4 little-endian
pub const IMAGE_FILE_MACHINE_SH4: u16 = 0x01a6;
/// SH5
pub const IMAGE_FILE_MACHINE_SH5: u16 = 0x01a8;
/// ARM Little-Endian
pub const IMAGE_FILE_MACHINE_ARM: u16 = 0x01c0;
/// ARM Thumb/Thumb-2 Little-Endian
pub const IMAGE_FILE_MACHINE_THUMB: u16 = 0x01c2;
/// ARM Thumb-2 Little-Endian
pub const IMAGE_FILE_MACHINE_ARMNT: u16 = 0x01c4;
pub const IMAGE_FILE_MACHINE_AM33: u16 = 0x01d3;
/// IBM PowerPC Little-Endian
pub const IMAGE_FILE_MACHINE_POWERPC: u16 = 0x01F0;
pub const IMAGE_FILE_MACHINE_POWERPCFP: u16 = 0x01f1;
/// Intel 64
pub const IMAGE_FILE_MACHINE_IA64: u16 = 0x0200;
/// MIPS
pub const IMAGE_FILE_MACHINE_MIPS16: u16 = 0x0266;
/// ALPHA64
pub const IMAGE_FILE_MACHINE_ALPHA64: u16 = 0x0284;
/// MIPS
pub const IMAGE_FILE_MACHINE_MIPSFPU: u16 = 0x0366;
/// MIPS
pub const IMAGE_FILE_MACHINE_MIPSFPU16: u16 = 0x0466;
pub const IMAGE_FILE_MACHINE_AXP64: u16 = IMAGE_FILE_MACHINE_ALPHA64;
/// Infineon
pub const IMAGE_FILE_MACHINE_TRICORE: u16 = 0x0520;
pub const IMAGE_FILE_MACHINE_CEF: u16 = 0x0CEF;
/// EFI Byte Code
pub const IMAGE_FILE_MACHINE_EBC: u16 = 0x0EBC;
/// AMD64 (K8)
pub const IMAGE_FILE_MACHINE_AMD64: u16 = 0x8664;
/// M32R little-endian
pub const IMAGE_FILE_MACHINE_M32R: u16 = 0x9041;
/// ARM64 Little-Endian
pub const IMAGE_FILE_MACHINE_ARM64: u16 = 0xAA64;
pub const IMAGE_FILE_MACHINE_CEE: u16 = 0xC0EE;
/// RISCV32
pub const IMAGE_FILE_MACHINE_RISCV32: u16 = 0x5032;
/// RISCV64
pub const IMAGE_FILE_MACHINE_RISCV64: u16 = 0x5064;
/// RISCV128
pub const IMAGE_FILE_MACHINE_RISCV128: u16 = 0x5128;
//
// Directory format.
//
#[derive(Debug, Clone, Copy)]
#[repr(C)]
pub struct ImageDataDirectory {
pub virtual_address: U32<LE>,
pub size: U32<LE>,
}
pub const IMAGE_NUMBEROF_DIRECTORY_ENTRIES: usize = 16;
//
// Optional header format.
//
#[derive(Debug, Clone, Copy)]
#[repr(C)]
pub struct ImageOptionalHeader32 {
// Standard fields.
pub magic: U16<LE>,
pub major_linker_version: u8,
pub minor_linker_version: u8,
pub size_of_code: U32<LE>,
pub size_of_initialized_data: U32<LE>,
pub size_of_uninitialized_data: U32<LE>,
pub address_of_entry_point: U32<LE>,
pub base_of_code: U32<LE>,
pub base_of_data: U32<LE>,
// NT additional fields.
pub image_base: U32<LE>,
pub section_alignment: U32<LE>,
pub file_alignment: U32<LE>,
pub major_operating_system_version: U16<LE>,
pub minor_operating_system_version: U16<LE>,
pub major_image_version: U16<LE>,
pub minor_image_version: U16<LE>,
pub major_subsystem_version: U16<LE>,
pub minor_subsystem_version: U16<LE>,
pub win32_version_value: U32<LE>,
pub size_of_image: U32<LE>,
pub size_of_headers: U32<LE>,
pub check_sum: U32<LE>,
pub subsystem: U16<LE>,
pub dll_characteristics: U16<LE>,
pub size_of_stack_reserve: U32<LE>,
pub size_of_stack_commit: U32<LE>,
pub size_of_heap_reserve: U32<LE>,
pub size_of_heap_commit: U32<LE>,
pub loader_flags: U32<LE>,
pub number_of_rva_and_sizes: U32<LE>,
//pub data_directory: [ImageDataDirectory; IMAGE_NUMBEROF_DIRECTORY_ENTRIES],
}
#[derive(Debug, Clone, Copy)]
#[repr(C)]
pub struct ImageRomOptionalHeader {
pub magic: U16<LE>,
pub major_linker_version: u8,
pub minor_linker_version: u8,
pub size_of_code: U32<LE>,
pub size_of_initialized_data: U32<LE>,
pub size_of_uninitialized_data: U32<LE>,
pub address_of_entry_point: U32<LE>,
pub base_of_code: U32<LE>,
pub base_of_data: U32<LE>,
pub base_of_bss: U32<LE>,
pub gpr_mask: U32<LE>,
pub cpr_mask: [U32<LE>; 4],
pub gp_value: U32<LE>,
}
#[derive(Debug, Clone, Copy)]
#[repr(C)]
pub struct ImageOptionalHeader64 {
pub magic: U16<LE>,
pub major_linker_version: u8,
pub minor_linker_version: u8,
pub size_of_code: U32<LE>,
pub size_of_initialized_data: U32<LE>,
pub size_of_uninitialized_data: U32<LE>,
pub address_of_entry_point: U32<LE>,
pub base_of_code: U32<LE>,
pub image_base: U64<LE>,
pub section_alignment: U32<LE>,
pub file_alignment: U32<LE>,
pub major_operating_system_version: U16<LE>,
pub minor_operating_system_version: U16<LE>,
pub major_image_version: U16<LE>,
pub minor_image_version: U16<LE>,
pub major_subsystem_version: U16<LE>,
pub minor_subsystem_version: U16<LE>,
pub win32_version_value: U32<LE>,
pub size_of_image: U32<LE>,
pub size_of_headers: U32<LE>,
pub check_sum: U32<LE>,
pub subsystem: U16<LE>,
pub dll_characteristics: U16<LE>,
pub size_of_stack_reserve: U64<LE>,
pub size_of_stack_commit: U64<LE>,
pub size_of_heap_reserve: U64<LE>,
pub size_of_heap_commit: U64<LE>,
pub loader_flags: U32<LE>,
pub number_of_rva_and_sizes: U32<LE>,
//pub data_directory: [ImageDataDirectory; IMAGE_NUMBEROF_DIRECTORY_ENTRIES],
}
pub const IMAGE_NT_OPTIONAL_HDR32_MAGIC: u16 = 0x10b;
pub const IMAGE_NT_OPTIONAL_HDR64_MAGIC: u16 = 0x20b;
pub const IMAGE_ROM_OPTIONAL_HDR_MAGIC: u16 = 0x107;
#[derive(Debug, Clone, Copy)]
#[repr(C)]
pub struct ImageNtHeaders64 {
pub signature: U32<LE>,
pub file_header: ImageFileHeader,
pub optional_header: ImageOptionalHeader64,
}
#[derive(Debug, Clone, Copy)]
#[repr(C)]
pub struct ImageNtHeaders32 {
pub signature: U32<LE>,
pub file_header: ImageFileHeader,
pub optional_header: ImageOptionalHeader32,
}
#[derive(Debug, Clone, Copy)]
#[repr(C)]
pub struct ImageRomHeaders {
pub file_header: ImageFileHeader,
pub optional_header: ImageRomOptionalHeader,
}
// Values for `ImageOptionalHeader*::subsystem`.
/// Unknown subsystem.
pub const IMAGE_SUBSYSTEM_UNKNOWN: u16 = 0;
/// Image doesn't require a subsystem.
pub const IMAGE_SUBSYSTEM_NATIVE: u16 = 1;
/// Image runs in the Windows GUI subsystem.
pub const IMAGE_SUBSYSTEM_WINDOWS_GUI: u16 = 2;
/// Image runs in the Windows character subsystem.
pub const IMAGE_SUBSYSTEM_WINDOWS_CUI: u16 = 3;
/// image runs in the OS/2 character subsystem.
pub const IMAGE_SUBSYSTEM_OS2_CUI: u16 = 5;
/// image runs in the Posix character subsystem.
pub const IMAGE_SUBSYSTEM_POSIX_CUI: u16 = 7;
/// image is a native Win9x driver.
pub const IMAGE_SUBSYSTEM_NATIVE_WINDOWS: u16 = 8;
/// Image runs in the Windows CE subsystem.
pub const IMAGE_SUBSYSTEM_WINDOWS_CE_GUI: u16 = 9;
pub const IMAGE_SUBSYSTEM_EFI_APPLICATION: u16 = 10;
pub const IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER: u16 = 11;
pub const IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER: u16 = 12;
pub const IMAGE_SUBSYSTEM_EFI_ROM: u16 = 13;
pub const IMAGE_SUBSYSTEM_XBOX: u16 = 14;
pub const IMAGE_SUBSYSTEM_WINDOWS_BOOT_APPLICATION: u16 = 16;
pub const IMAGE_SUBSYSTEM_XBOX_CODE_CATALOG: u16 = 17;
// Values for `ImageOptionalHeader*::dll_characteristics`.
// IMAGE_LIBRARY_PROCESS_INIT 0x0001 // Reserved.
// IMAGE_LIBRARY_PROCESS_TERM 0x0002 // Reserved.
// IMAGE_LIBRARY_THREAD_INIT 0x0004 // Reserved.
// IMAGE_LIBRARY_THREAD_TERM 0x0008 // Reserved.
/// Image can handle a high entropy 64-bit virtual address space.
pub const IMAGE_DLLCHARACTERISTICS_HIGH_ENTROPY_VA: u16 = 0x0020;
/// DLL can move.
pub const IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE: u16 = 0x0040;
/// Code Integrity Image
pub const IMAGE_DLLCHARACTERISTICS_FORCE_INTEGRITY: u16 = 0x0080;
/// Image is NX compatible
pub const IMAGE_DLLCHARACTERISTICS_NX_COMPAT: u16 = 0x0100;
/// Image understands isolation and doesn't want it
pub const IMAGE_DLLCHARACTERISTICS_NO_ISOLATION: u16 = 0x0200;
/// Image does not use SEH. No SE handler may reside in this image
pub const IMAGE_DLLCHARACTERISTICS_NO_SEH: u16 = 0x0400;
/// Do not bind this image.
pub const IMAGE_DLLCHARACTERISTICS_NO_BIND: u16 = 0x0800;
/// Image should execute in an AppContainer
pub const IMAGE_DLLCHARACTERISTICS_APPCONTAINER: u16 = 0x1000;
/// Driver uses WDM model
pub const IMAGE_DLLCHARACTERISTICS_WDM_DRIVER: u16 = 0x2000;
/// Image supports Control Flow Guard.
pub const IMAGE_DLLCHARACTERISTICS_GUARD_CF: u16 = 0x4000;
pub const IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE: u16 = 0x8000;
// Indices for `ImageOptionalHeader*::data_directory`.
/// Export Directory
pub const IMAGE_DIRECTORY_ENTRY_EXPORT: usize = 0;
/// Import Directory
pub const IMAGE_DIRECTORY_ENTRY_IMPORT: usize = 1;
/// Resource Directory
pub const IMAGE_DIRECTORY_ENTRY_RESOURCE: usize = 2;
/// Exception Directory
pub const IMAGE_DIRECTORY_ENTRY_EXCEPTION: usize = 3;
/// Security Directory
pub const IMAGE_DIRECTORY_ENTRY_SECURITY: usize = 4;
/// Base Relocation Table
pub const IMAGE_DIRECTORY_ENTRY_BASERELOC: usize = 5;
/// Debug Directory
pub const IMAGE_DIRECTORY_ENTRY_DEBUG: usize = 6;
// IMAGE_DIRECTORY_ENTRY_COPYRIGHT 7 // (X86 usage)
/// Architecture Specific Data
pub const IMAGE_DIRECTORY_ENTRY_ARCHITECTURE: usize = 7;
/// RVA of GP
pub const IMAGE_DIRECTORY_ENTRY_GLOBALPTR: usize = 8;
/// TLS Directory
pub const IMAGE_DIRECTORY_ENTRY_TLS: usize = 9;
/// Load Configuration Directory
pub const IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG: usize = 10;
/// Bound Import Directory in headers
pub const IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT: usize = 11;
/// Import Address Table
pub const IMAGE_DIRECTORY_ENTRY_IAT: usize = 12;
/// Delay Load Import Descriptors
pub const IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT: usize = 13;
/// COM Runtime descriptor
pub const IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR: usize = 14;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(C)]
pub struct Guid(pub [u8; 16]);
impl Guid {
#[inline]
pub fn data1(self) -> U32<LE> {
U32::from_bytes(self.0[0..4].try_into().unwrap())
}
#[inline]
pub fn data2(self) -> U16<LE> {
U16::from_bytes(self.0[4..6].try_into().unwrap())
}
#[inline]
pub fn data3(self) -> U16<LE> {
U16::from_bytes(self.0[6..8].try_into().unwrap())
}
#[inline]
pub fn data4(self) -> [u8; 8] {
self.0[8..16].try_into().unwrap()
}
}
pub use Guid as ClsId;
/// Non-COFF Object file header
#[derive(Debug, Clone, Copy)]
#[repr(C)]
pub struct AnonObjectHeader {
/// Must be IMAGE_FILE_MACHINE_UNKNOWN
pub sig1: U16<LE>,
/// Must be 0xffff
pub sig2: U16<LE>,
/// >= 1 (implies the ClsId field is present)
pub version: U16<LE>,
pub machine: U16<LE>,
pub time_date_stamp: U32<LE>,
/// Used to invoke CoCreateInstance
pub class_id: ClsId,
/// Size of data that follows the header
pub size_of_data: U32<LE>,
}
#[derive(Debug, Clone, Copy)]
#[repr(C)]
pub struct AnonObjectHeaderV2 {
/// Must be IMAGE_FILE_MACHINE_UNKNOWN
pub sig1: U16<LE>,
/// Must be 0xffff
pub sig2: U16<LE>,
/// >= 2 (implies the Flags field is present - otherwise V1)
pub version: U16<LE>,
pub machine: U16<LE>,
pub time_date_stamp: U32<LE>,
/// Used to invoke CoCreateInstance
pub class_id: ClsId,
/// Size of data that follows the header
pub size_of_data: U32<LE>,
/// 0x1 -> contains metadata
pub flags: U32<LE>,
/// Size of CLR metadata
pub meta_data_size: U32<LE>,
/// Offset of CLR metadata
pub meta_data_offset: U32<LE>,
}
/// The required value of `AnonObjectHeaderBigobj::class_id`.
pub const ANON_OBJECT_HEADER_BIGOBJ_CLASS_ID: ClsId = ClsId([
0xC7, 0xA1, 0xBA, 0xD1, 0xEE, 0xBA, 0xA9, 0x4B, 0xAF, 0x20, 0xFA, 0xF6, 0x6A, 0xA4, 0xDC, 0xB8,
]);
#[derive(Debug, Clone, Copy)]
#[repr(C)]
pub struct AnonObjectHeaderBigobj {
/* same as ANON_OBJECT_HEADER_V2 */
/// Must be IMAGE_FILE_MACHINE_UNKNOWN
pub sig1: U16<LE>,
/// Must be 0xffff
pub sig2: U16<LE>,
/// >= 2 (implies the Flags field is present)
pub version: U16<LE>,
/// Actual machine - IMAGE_FILE_MACHINE_xxx
pub machine: U16<LE>,
pub time_date_stamp: U32<LE>,
/// Must be `ANON_OBJECT_HEADER_BIGOBJ_CLASS_ID`.
pub class_id: ClsId,
/// Size of data that follows the header
pub size_of_data: U32<LE>,
/// 0x1 -> contains metadata
pub flags: U32<LE>,
/// Size of CLR metadata
pub meta_data_size: U32<LE>,
/// Offset of CLR metadata
pub meta_data_offset: U32<LE>,
/* bigobj specifics */
/// extended from WORD
pub number_of_sections: U32<LE>,
pub pointer_to_symbol_table: U32<LE>,
pub number_of_symbols: U32<LE>,
}
pub const IMAGE_SIZEOF_SHORT_NAME: usize = 8;
//
// Section header format.
//
#[derive(Debug, Default, Clone, Copy)]
#[repr(C)]
pub struct ImageSectionHeader {
pub name: [u8; IMAGE_SIZEOF_SHORT_NAME],
pub virtual_size: U32<LE>,
pub virtual_address: U32<LE>,
pub size_of_raw_data: U32<LE>,
pub pointer_to_raw_data: U32<LE>,
pub pointer_to_relocations: U32<LE>,
pub pointer_to_linenumbers: U32<LE>,
pub number_of_relocations: U16<LE>,
pub number_of_linenumbers: U16<LE>,
pub characteristics: U32<LE>,
}
pub const IMAGE_SIZEOF_SECTION_HEADER: usize = 40;
// Values for `ImageSectionHeader::characteristics`.
// IMAGE_SCN_TYPE_REG 0x00000000 // Reserved.
// IMAGE_SCN_TYPE_DSECT 0x00000001 // Reserved.
// IMAGE_SCN_TYPE_NOLOAD 0x00000002 // Reserved.
// IMAGE_SCN_TYPE_GROUP 0x00000004 // Reserved.
/// Reserved.
pub const IMAGE_SCN_TYPE_NO_PAD: u32 = 0x0000_0008;
// IMAGE_SCN_TYPE_COPY 0x00000010 // Reserved.
/// Section contains code.
pub const IMAGE_SCN_CNT_CODE: u32 = 0x0000_0020;
/// Section contains initialized data.
pub const IMAGE_SCN_CNT_INITIALIZED_DATA: u32 = 0x0000_0040;
/// Section contains uninitialized data.
pub const IMAGE_SCN_CNT_UNINITIALIZED_DATA: u32 = 0x0000_0080;
/// Reserved.
pub const IMAGE_SCN_LNK_OTHER: u32 = 0x0000_0100;
/// Section contains comments or some other type of information.
pub const IMAGE_SCN_LNK_INFO: u32 = 0x0000_0200;
// IMAGE_SCN_TYPE_OVER 0x00000400 // Reserved.
/// Section contents will not become part of image.
pub const IMAGE_SCN_LNK_REMOVE: u32 = 0x0000_0800;
/// Section contents comdat.
pub const IMAGE_SCN_LNK_COMDAT: u32 = 0x0000_1000;
// 0x00002000 // Reserved.
// IMAGE_SCN_MEM_PROTECTED - Obsolete 0x00004000
/// Reset speculative exceptions handling bits in the TLB entries for this section.
pub const IMAGE_SCN_NO_DEFER_SPEC_EXC: u32 = 0x0000_4000;
/// Section content can be accessed relative to GP
pub const IMAGE_SCN_GPREL: u32 = 0x0000_8000;
pub const IMAGE_SCN_MEM_FARDATA: u32 = 0x0000_8000;
// IMAGE_SCN_MEM_SYSHEAP - Obsolete 0x00010000
pub const IMAGE_SCN_MEM_PURGEABLE: u32 = 0x0002_0000;
pub const IMAGE_SCN_MEM_16BIT: u32 = 0x0002_0000;
pub const IMAGE_SCN_MEM_LOCKED: u32 = 0x0004_0000;
pub const IMAGE_SCN_MEM_PRELOAD: u32 = 0x0008_0000;
pub const IMAGE_SCN_ALIGN_1BYTES: u32 = 0x0010_0000;
pub const IMAGE_SCN_ALIGN_2BYTES: u32 = 0x0020_0000;
pub const IMAGE_SCN_ALIGN_4BYTES: u32 = 0x0030_0000;
pub const IMAGE_SCN_ALIGN_8BYTES: u32 = 0x0040_0000;
/// Default alignment if no others are specified.
pub const IMAGE_SCN_ALIGN_16BYTES: u32 = 0x0050_0000;
pub const IMAGE_SCN_ALIGN_32BYTES: u32 = 0x0060_0000;
pub const IMAGE_SCN_ALIGN_64BYTES: u32 = 0x0070_0000;
pub const IMAGE_SCN_ALIGN_128BYTES: u32 = 0x0080_0000;
pub const IMAGE_SCN_ALIGN_256BYTES: u32 = 0x0090_0000;
pub const IMAGE_SCN_ALIGN_512BYTES: u32 = 0x00A0_0000;
pub const IMAGE_SCN_ALIGN_1024BYTES: u32 = 0x00B0_0000;
pub const IMAGE_SCN_ALIGN_2048BYTES: u32 = 0x00C0_0000;
pub const IMAGE_SCN_ALIGN_4096BYTES: u32 = 0x00D0_0000;
pub const IMAGE_SCN_ALIGN_8192BYTES: u32 = 0x00E0_0000;
// Unused 0x00F0_0000
pub const IMAGE_SCN_ALIGN_MASK: u32 = 0x00F0_0000;
/// Section contains extended relocations.
pub const IMAGE_SCN_LNK_NRELOC_OVFL: u32 = 0x0100_0000;
/// Section can be discarded.
pub const IMAGE_SCN_MEM_DISCARDABLE: u32 = 0x0200_0000;
/// Section is not cacheable.
pub const IMAGE_SCN_MEM_NOT_CACHED: u32 = 0x0400_0000;
/// Section is not pageable.
pub const IMAGE_SCN_MEM_NOT_PAGED: u32 = 0x0800_0000;
/// Section is shareable.
pub const IMAGE_SCN_MEM_SHARED: u32 = 0x1000_0000;
/// Section is executable.
pub const IMAGE_SCN_MEM_EXECUTE: u32 = 0x2000_0000;
/// Section is readable.
pub const IMAGE_SCN_MEM_READ: u32 = 0x4000_0000;
/// Section is writeable.
pub const IMAGE_SCN_MEM_WRITE: u32 = 0x8000_0000;
//
// TLS Characteristic Flags
//
/// Tls index is scaled
pub const IMAGE_SCN_SCALE_INDEX: u32 = 0x0000_0001;
//
// Symbol format.
//
// This struct has alignment 1.
#[derive(Debug, Clone, Copy)]
#[repr(C)]
pub struct ImageSymbol {
/// If first 4 bytes are 0, then second 4 bytes are offset into string table.
pub name: [u8; 8],
pub value: U32Bytes<LE>,
pub section_number: U16Bytes<LE>,
pub typ: U16Bytes<LE>,
pub storage_class: u8,
pub number_of_aux_symbols: u8,
}
pub const IMAGE_SIZEOF_SYMBOL: usize = 18;
#[derive(Debug, Clone, Copy)]
#[repr(C)]
pub struct ImageSymbolBytes(pub [u8; IMAGE_SIZEOF_SYMBOL]);
// This struct has alignment 1.
#[derive(Debug, Clone, Copy)]
#[repr(C)]
pub struct ImageSymbolEx {
/// If first 4 bytes are 0, then second 4 bytes are offset into string table.
pub name: [u8; 8],
pub value: U32Bytes<LE>,
pub section_number: I32Bytes<LE>,
pub typ: U16Bytes<LE>,
pub storage_class: u8,
pub number_of_aux_symbols: u8,
}
pub const IMAGE_SIZEOF_SYMBOL_EX: usize = 20;
#[derive(Debug, Clone, Copy)]
#[repr(C)]
pub struct ImageSymbolExBytes(pub [u8; IMAGE_SIZEOF_SYMBOL_EX]);
// Values for `ImageSymbol::section_number`.
//
// Symbols have a section number of the section in which they are
// defined. Otherwise, section numbers have the following meanings:
/// Symbol is undefined or is common.
pub const IMAGE_SYM_UNDEFINED: i32 = 0;
/// Symbol is an absolute value.
pub const IMAGE_SYM_ABSOLUTE: i32 = -1;
/// Symbol is a special debug item.
pub const IMAGE_SYM_DEBUG: i32 = -2;
/// Values 0xFF00-0xFFFF are special
pub const IMAGE_SYM_SECTION_MAX: u16 = 0xFEFF;
pub const IMAGE_SYM_SECTION_MAX_EX: u32 = 0x7fff_ffff;
// Values for `ImageSymbol::typ` (basic component).
/// no type.
pub const IMAGE_SYM_TYPE_NULL: u16 = 0x0000;
pub const IMAGE_SYM_TYPE_VOID: u16 = 0x0001;
/// type character.
pub const IMAGE_SYM_TYPE_CHAR: u16 = 0x0002;
/// type short integer.
pub const IMAGE_SYM_TYPE_SHORT: u16 = 0x0003;
pub const IMAGE_SYM_TYPE_INT: u16 = 0x0004;
pub const IMAGE_SYM_TYPE_LONG: u16 = 0x0005;
pub const IMAGE_SYM_TYPE_FLOAT: u16 = 0x0006;
pub const IMAGE_SYM_TYPE_DOUBLE: u16 = 0x0007;
pub const IMAGE_SYM_TYPE_STRUCT: u16 = 0x0008;
pub const IMAGE_SYM_TYPE_UNION: u16 = 0x0009;
/// enumeration.
pub const IMAGE_SYM_TYPE_ENUM: u16 = 0x000A;
/// member of enumeration.
pub const IMAGE_SYM_TYPE_MOE: u16 = 0x000B;
pub const IMAGE_SYM_TYPE_BYTE: u16 = 0x000C;
pub const IMAGE_SYM_TYPE_WORD: u16 = 0x000D;
pub const IMAGE_SYM_TYPE_UINT: u16 = 0x000E;
pub const IMAGE_SYM_TYPE_DWORD: u16 = 0x000F;
pub const IMAGE_SYM_TYPE_PCODE: u16 = 0x8000;
// Values for `ImageSymbol::typ` (derived component).
/// no derived type.
pub const IMAGE_SYM_DTYPE_NULL: u16 = 0;
/// pointer.
pub const IMAGE_SYM_DTYPE_POINTER: u16 = 1;
/// function.
pub const IMAGE_SYM_DTYPE_FUNCTION: u16 = 2;
/// array.
pub const IMAGE_SYM_DTYPE_ARRAY: u16 = 3;
// Values for `ImageSymbol::storage_class`.
pub const IMAGE_SYM_CLASS_END_OF_FUNCTION: u8 = 0xff;
pub const IMAGE_SYM_CLASS_NULL: u8 = 0x00;
pub const IMAGE_SYM_CLASS_AUTOMATIC: u8 = 0x01;
pub const IMAGE_SYM_CLASS_EXTERNAL: u8 = 0x02;
pub const IMAGE_SYM_CLASS_STATIC: u8 = 0x03;
pub const IMAGE_SYM_CLASS_REGISTER: u8 = 0x04;
pub const IMAGE_SYM_CLASS_EXTERNAL_DEF: u8 = 0x05;
pub const IMAGE_SYM_CLASS_LABEL: u8 = 0x06;
pub const IMAGE_SYM_CLASS_UNDEFINED_LABEL: u8 = 0x07;
pub const IMAGE_SYM_CLASS_MEMBER_OF_STRUCT: u8 = 0x08;
pub const IMAGE_SYM_CLASS_ARGUMENT: u8 = 0x09;
pub const IMAGE_SYM_CLASS_STRUCT_TAG: u8 = 0x0A;
pub const IMAGE_SYM_CLASS_MEMBER_OF_UNION: u8 = 0x0B;
pub const IMAGE_SYM_CLASS_UNION_TAG: u8 = 0x0C;
pub const IMAGE_SYM_CLASS_TYPE_DEFINITION: u8 = 0x0D;
pub const IMAGE_SYM_CLASS_UNDEFINED_STATIC: u8 = 0x0E;
pub const IMAGE_SYM_CLASS_ENUM_TAG: u8 = 0x0F;
pub const IMAGE_SYM_CLASS_MEMBER_OF_ENUM: u8 = 0x10;
pub const IMAGE_SYM_CLASS_REGISTER_PARAM: u8 = 0x11;
pub const IMAGE_SYM_CLASS_BIT_FIELD: u8 = 0x12;
pub const IMAGE_SYM_CLASS_FAR_EXTERNAL: u8 = 0x44;
pub const IMAGE_SYM_CLASS_BLOCK: u8 = 0x64;
pub const IMAGE_SYM_CLASS_FUNCTION: u8 = 0x65;
pub const IMAGE_SYM_CLASS_END_OF_STRUCT: u8 = 0x66;
pub const IMAGE_SYM_CLASS_FILE: u8 = 0x67;
// new
pub const IMAGE_SYM_CLASS_SECTION: u8 = 0x68;
pub const IMAGE_SYM_CLASS_WEAK_EXTERNAL: u8 = 0x69;
pub const IMAGE_SYM_CLASS_CLR_TOKEN: u8 = 0x6B;
// type packing constants
pub const N_BTMASK: u16 = 0x000F;
pub const N_TMASK: u16 = 0x0030;
pub const N_TMASK1: u16 = 0x00C0;
pub const N_TMASK2: u16 = 0x00F0;
pub const N_BTSHFT: usize = 4;
pub const N_TSHIFT: usize = 2;
pub const IMAGE_SYM_DTYPE_SHIFT: usize = N_BTSHFT;
//
// Auxiliary entry format.
//
// Used for both ImageSymbol and ImageSymbolEx (with padding).
// This struct has alignment 1.
#[derive(Debug, Clone, Copy)]
#[repr(C)]
pub struct ImageAuxSymbolTokenDef {
/// IMAGE_AUX_SYMBOL_TYPE
pub aux_type: u8,
/// Must be 0
pub reserved1: u8,
pub symbol_table_index: U32Bytes<LE>,
/// Must be 0
pub reserved2: [u8; 12],
}
pub const IMAGE_AUX_SYMBOL_TYPE_TOKEN_DEF: u16 = 1;
/// Auxiliary symbol format 1: function definitions.
// This struct has alignment 1.
#[derive(Debug, Clone, Copy)]
#[repr(C)]
pub struct ImageAuxSymbolFunction {
pub tag_index: U32Bytes<LE>,
pub total_size: U32Bytes<LE>,
pub pointer_to_linenumber: U32Bytes<LE>,
pub pointer_to_next_function: U32Bytes<LE>,
pub unused: [u8; 2],
}
/// Auxiliary symbol format 2: .bf and .ef symbols.
// This struct has alignment 1.
#[derive(Debug, Clone, Copy)]
#[repr(C)]
pub struct ImageAuxSymbolFunctionBeginEnd {
pub unused1: [u8; 4],
/// declaration line number
pub linenumber: U16Bytes<LE>,
pub unused2: [u8; 6],
pub pointer_to_next_function: U32Bytes<LE>,
pub unused3: [u8; 2],
}
/// Auxiliary symbol format 3: weak externals.
///
/// Used for both `ImageSymbol` and `ImageSymbolEx` (both with padding).
// This struct has alignment 1.
#[derive(Debug, Clone, Copy)]
#[repr(C)]
pub struct ImageAuxSymbolWeak {
/// the weak extern default symbol index
pub weak_default_sym_index: U32Bytes<LE>,
pub weak_search_type: U32Bytes<LE>,
}
/// Auxiliary symbol format 5: sections.
///
/// Used for both `ImageSymbol` and `ImageSymbolEx` (with padding).
// This struct has alignment 1.