fccc7a58fb0f273a8f8df9392888592e71e38413
2 * Generates assembly definitions from the target headers.
9 #define IMAGE_FILE_MACHINE_I386 0x014c
10 #define IMAGE_FILE_MACHINE_AMD64 0x8664
11 #define IMAGE_FILE_MACHINE_ARMNT 0x01c4
15 typedef unsigned __int16
uint16_t;
16 typedef unsigned __int32
uint32_t;
17 typedef unsigned __int64
uint64_t;
21 #define _stricmp strcasecmp
33 #define TYPE_CONSTANT 2
36 int main(int argc
, char* argv
[])
43 uint32_t e_lfanew
, signature
;
44 uint16_t Machine
, NumberOfSections
, SizeOfOptionalHeader
;
49 uint32_t VirtualAddress
;
52 uint32_t RelocAddress
;
54 uint16_t RelocationsNumber
;
55 uint16_t LineNumbersNumber
;
56 uint32_t Characteristics
;
60 if (argc
>= 4 && _stricmp(argv
[3], "-ms") == 0) ms_format
= 1;
62 /* Open the input file */
63 input
= fopen(argv
[1], "rb");
66 fprintf(stderr
, "Could not open input file '%s'\n", argv
[1]);
70 /* Open the output file */
71 output
= fopen(argv
[2], "w");
75 fprintf(stderr
, "Could not open output file '%s'\n", argv
[2]);
79 /* Read the DOS header */
80 if (fread(&header
, 1, 2, input
) != 2)
82 fprintf(stderr
, "Error reading header.\n");
86 if (header
[0] != 0x4d || header
[1] != 0x5a)
88 fprintf(stderr
, "Not a PE file.\n");
92 fseek(input
, 0x3C, SEEK_SET
);
93 if (fread(&e_lfanew
, 1, 4, input
) != 4)
95 fprintf(stderr
, "Could not read e_lfanew.\n");
99 fseek(input
, e_lfanew
, SEEK_SET
);
100 if (fread(&signature
, 1, 4, input
) != 4)
102 fprintf(stderr
, "Could not read signature.\n");
106 /* Verify the PE signature */
107 if (signature
!= 0x4550)
109 fprintf(stderr
, "Invalid signature: 0x%x.\n", signature
);
114 fseek(input
, e_lfanew
+ 4, SEEK_SET
);
115 if (fread(&Machine
, 1, 2, input
) != 2)
117 fprintf(stderr
, "Could not read ExportDirectoryRVA.\n");
121 if ((Machine
!= IMAGE_FILE_MACHINE_I386
) &&
122 (Machine
!= IMAGE_FILE_MACHINE_AMD64
) &&
123 (Machine
!= IMAGE_FILE_MACHINE_ARMNT
))
125 fprintf(stderr
, "Invalid Machine: 0x%x.\n", Machine
);
129 /* Read NumberOfSections */
130 if (fread(&NumberOfSections
, 1, 2, input
) != 2)
132 fprintf(stderr
, "Could not read NumberOfSections.\n");
136 fseek(input
, e_lfanew
+ 0x14, SEEK_SET
);
137 if (fread(&SizeOfOptionalHeader
, 1, 2, input
) != 2)
139 fprintf(stderr
, "Could not read SizeOfOptionalHeader.\n");
143 /* Read the section table */
144 fseek(input
, e_lfanew
+ 0x18 + SizeOfOptionalHeader
, SEEK_SET
);
146 /* Search for the .asmdef section */
147 for (i
= 0; i
< NumberOfSections
; i
++)
149 if (fread(§ion
, 1, sizeof(SECTION
), input
) != sizeof(SECTION
))
151 fprintf(stderr
, "Could not read section.\n");
155 if (strcmp(section
.Name
, ".asmdef") == 0)
161 if (i
== NumberOfSections
)
163 fprintf(stderr
, "Could not find section.\n");
167 /* Read the section table */
168 fseek(input
, section
.RawAddress
, SEEK_SET
);
173 if (fread(&data
, 1, sizeof(data
), input
) != sizeof(data
))
175 fprintf(stderr
, "Error reading input file.\n");
185 fprintf(output
, "%s\n", data
.Name
);
191 fprintf(output
, "%s equ 0%"PRIx64
"h\n", data
.Name
, data
.Value
);
195 fprintf(output
, "%s = 0x%"PRIx64
"\n", data
.Name
, data
.Value
);
202 fprintf(output
, "\n; %s\n", data
.Name
);
206 fprintf(output
, "\n/* %s */\n", data
.Name
);