2 * Generates assembly definitions from the target headers.
10 #define IMAGE_FILE_MACHINE_I386 0x014c
11 #define IMAGE_FILE_MACHINE_AMD64 0x8664
12 #define IMAGE_FILE_MACHINE_ARMNT 0x01c4
18 #define _stricmp strcasecmp
30 #define TYPE_CONSTANT 2
33 int main(int argc
, char* argv
[])
40 uint32_t e_lfanew
, signature
;
41 uint16_t Machine
, NumberOfSections
, SizeOfOptionalHeader
;
46 uint32_t VirtualAddress
;
49 uint32_t RelocAddress
;
51 uint16_t RelocationsNumber
;
52 uint16_t LineNumbersNumber
;
53 uint32_t Characteristics
;
57 if (argc
>= 4 && _stricmp(argv
[3], "-ms") == 0) ms_format
= 1;
59 /* Open the input file */
60 input
= fopen(argv
[1], "rb");
63 fprintf(stderr
, "Could not open input file '%s'\n", argv
[1]);
67 /* Open the output file */
68 output
= fopen(argv
[2], "w");
72 fprintf(stderr
, "Could not open output file '%s'\n", argv
[2]);
76 /* Read the DOS header */
77 if (fread(&header
, 1, 2, input
) != 2)
79 fprintf(stderr
, "Error reading header.\n");
83 if (header
[0] != 0x4d || header
[1] != 0x5a)
85 fprintf(stderr
, "Not a PE file.\n");
89 fseek(input
, 0x3C, SEEK_SET
);
90 if (fread(&e_lfanew
, 1, 4, input
) != 4)
92 fprintf(stderr
, "Could not read e_lfanew.\n");
96 fseek(input
, e_lfanew
, SEEK_SET
);
97 if (fread(&signature
, 1, 4, input
) != 4)
99 fprintf(stderr
, "Could not read signature.\n");
103 /* Verify the PE signature */
104 if (signature
!= 0x4550)
106 fprintf(stderr
, "Invalid signature: 0x%x.\n", signature
);
111 fseek(input
, e_lfanew
+ 4, SEEK_SET
);
112 if (fread(&Machine
, 1, 2, input
) != 2)
114 fprintf(stderr
, "Could not read ExportDirectoryRVA.\n");
118 if ((Machine
!= IMAGE_FILE_MACHINE_I386
) &&
119 (Machine
!= IMAGE_FILE_MACHINE_AMD64
) &&
120 (Machine
!= IMAGE_FILE_MACHINE_ARMNT
))
122 fprintf(stderr
, "Invalid Machine: 0x%x.\n", Machine
);
126 /* Read NumberOfSections */
127 if (fread(&NumberOfSections
, 1, 2, input
) != 2)
129 fprintf(stderr
, "Could not read NumberOfSections.\n");
133 fseek(input
, e_lfanew
+ 0x14, SEEK_SET
);
134 if (fread(&SizeOfOptionalHeader
, 1, 2, input
) != 2)
136 fprintf(stderr
, "Could not read SizeOfOptionalHeader.\n");
140 /* Read the section table */
141 fseek(input
, e_lfanew
+ 0x18 + SizeOfOptionalHeader
, SEEK_SET
);
143 /* Search for the .asmdef section */
144 for (i
= 0; i
< NumberOfSections
; i
++)
146 if (fread(§ion
, 1, sizeof(SECTION
), input
) != sizeof(SECTION
))
148 fprintf(stderr
, "Could not read section.\n");
152 if (strcmp(section
.Name
, ".asmdef") == 0)
158 if (i
== NumberOfSections
)
160 fprintf(stderr
, "Could not find section.\n");
164 /* Read the section table */
165 fseek(input
, section
.RawAddress
, SEEK_SET
);
170 if (fread(&data
, 1, sizeof(data
), input
) != sizeof(data
))
172 fprintf(stderr
, "Error reading input file.\n");
182 fprintf(output
, "%s\n", data
.Name
);
188 if (Machine
== IMAGE_FILE_MACHINE_ARMNT
)
190 fprintf(output
, "%s equ 0x%"PRIx64
"\n", data
.Name
, data
.Value
);
194 fprintf(output
, "%s equ 0%"PRIx64
"h\n", data
.Name
, data
.Value
);
199 fprintf(output
, "%s = 0x%"PRIx64
"\n", data
.Name
, data
.Value
);
206 fprintf(output
, "\n; %s\n", data
.Name
);
210 fprintf(output
, "\n/* %s */\n", data
.Name
);