Author: tkreuzer
Date: Thu Nov 11 13:35:57 2010
New Revision: 49560
URL:
http://svn.reactos.org/svn/reactos?rev=49560&view=rev
Log:
[GENINC]
Add geninc tool, that creates assembly definitions.
Added:
branches/cmake-bringup/tools/geninc/CMakeLists.txt (with props)
Modified:
branches/cmake-bringup/tools/CMakeLists.txt
branches/cmake-bringup/tools/geninc/geninc.c
Modified: branches/cmake-bringup/tools/CMakeLists.txt
URL:
http://svn.reactos.org/svn/reactos/branches/cmake-bringup/tools/CMakeLists.…
==============================================================================
--- branches/cmake-bringup/tools/CMakeLists.txt [iso-8859-1] (original)
+++ branches/cmake-bringup/tools/CMakeLists.txt [iso-8859-1] Thu Nov 11 13:35:57 2010
@@ -7,6 +7,7 @@
add_subdirectory(cabman)
add_subdirectory(cdmake)
add_subdirectory(gendib)
+add_subdirectory(geninc)
add_subdirectory(mkhive)
add_subdirectory(nci)
add_subdirectory(spec2pdef)
Added: branches/cmake-bringup/tools/geninc/CMakeLists.txt
URL:
http://svn.reactos.org/svn/reactos/branches/cmake-bringup/tools/geninc/CMak…
==============================================================================
--- branches/cmake-bringup/tools/geninc/CMakeLists.txt (added)
+++ branches/cmake-bringup/tools/geninc/CMakeLists.txt [iso-8859-1] Thu Nov 11 13:35:57
2010
@@ -1,0 +1,2 @@
+
+add_executable(geninc geninc.c)
Propchange: branches/cmake-bringup/tools/geninc/CMakeLists.txt
------------------------------------------------------------------------------
svn:eol-style = native
Modified: branches/cmake-bringup/tools/geninc/geninc.c
URL:
http://svn.reactos.org/svn/reactos/branches/cmake-bringup/tools/geninc/geni…
==============================================================================
--- branches/cmake-bringup/tools/geninc/geninc.c [iso-8859-1] (original)
+++ branches/cmake-bringup/tools/geninc/geninc.c [iso-8859-1] Thu Nov 11 13:35:57 2010
@@ -4,8 +4,205 @@
#include <stdlib.h>
#include <string.h>
-
+#ifdef _WIN32
+#define PRIx64 "I64x"
+#else
+#define PRIx64 "llx"
+#endif
+
+typedef struct
+{
+ char Type;
+ char Name[55];
+ unsigned __int64 Value;
+} ASMGENDATA;
+
+#define TYPE_END 0
+#define TYPE_RAW 1
+#define TYPE_CONSTANT 2
+#define TYPE_HEADER 3
int main(int argc, char* argv[])
{
+ FILE *input, *output;
+ ASMGENDATA data;
+ int i, result = -1;
+ int ms_format = 0;
+ char header[20];
+ unsigned __int32 e_lfanew, signature;
+ unsigned __int16 Machine, NumberOfSections, SizeOfOptionalHeader;
+ typedef struct
+ {
+ char Name[8];
+ unsigned __int32 VirtualSize;
+ unsigned __int32 VirtualAddress;
+ unsigned __int32 RawSize;
+ unsigned __int32 RawAddress;
+ unsigned __int32 RelocAddress;
+ unsigned __int32 LineNumbers;
+ unsigned __int16 RelocationsNumber;
+ unsigned __int16 LineNumbersNumber;
+ unsigned __int32 Characteristics;
+ } SECTION;
+ SECTION section;
+
+ if (argc >= 4 && stricmp(argv[3], "-ms") == 0) ms_format = 1;
+
+ /* Open the input file */
+ input = fopen(argv[1], "rb");
+ if (!input)
+ {
+ fprintf(stderr, "Could not open input file '%s'\n", argv[1]);
+ return -1;
+ }
+
+ /* Open the output file */
+ output = fopen(argv[2], "w");
+ if (!output)
+ {
+ fprintf(stderr, "Could not open output file '%s'\n", argv[2]);
+ return -1;
+ }
+
+ /* Read the DOS header */
+ if (fread(&header, 1, 2, input) != 2)
+ {
+ fprintf(stderr, "Error reading header.\n");
+ goto quit;
+ }
+
+ if (header[0] != 0x4d || header[1] != 0x5a)
+ {
+ fprintf(stderr, "Not a PE file.\n");
+ goto quit;
+ }
+
+ fseek(input, 0x3C, SEEK_SET);
+ if (fread(&e_lfanew, 1, 4, input) != 4)
+ {
+ fprintf(stderr, "Could not read e_lfanew.\n");
+ goto quit;
+ }
+
+ fseek(input, e_lfanew, SEEK_SET);
+ if (fread(&signature, 1, 4, input) != 4)
+ {
+ fprintf(stderr, "Could not read signature.\n");
+ goto quit;
+ }
+
+ /* Verify the PE signature */
+ if (signature != 0x4550)
+ {
+ fprintf(stderr, "Invalid signature: 0x%lx.\n", signature);
+ goto quit;
+ }
+
+ /* Read Machine */
+ fseek(input, e_lfanew + 4, SEEK_SET);
+ if (fread(&Machine, 1, 2, input) != 2)
+ {
+ fprintf(stderr, "Could not read ExportDirectoryRVA.\n");
+ goto quit;
+ }
+
+ if (Machine != 0x14c)
+ {
+ fprintf(stderr, "Invalid Machine: 0x%x.\n", Machine);
+ goto quit;
+ }
+
+ /* Read NumberOfSections */
+ if (fread(&NumberOfSections, 1, 2, input) != 2)
+ {
+ fprintf(stderr, "Could not read NumberOfSections.\n");
+ goto quit;
+ }
+
+ fseek(input, e_lfanew + 0x14, SEEK_SET);
+ if (fread(&SizeOfOptionalHeader, 1, 2, input) != 2)
+ {
+ fprintf(stderr, "Could not read SizeOfOptionalHeader.\n");
+ goto quit;
+ }
+
+ /* Read the section table */
+ fseek(input, e_lfanew + 0x18 + SizeOfOptionalHeader, SEEK_SET);
+
+ /* Search for the .asmdef section */
+ for (i = 0; i < NumberOfSections; i++)
+ {
+ if (fread(§ion, 1, sizeof(SECTION), input) != sizeof(SECTION))
+ {
+ fprintf(stderr, "Could not read section.\n");
+ goto quit;
+ }
+
+ if (strcmp(section.Name, ".asmdef") == 0)
+ {
+ break;
+ }
+ }
+
+ if (i == NumberOfSections)
+ {
+ fprintf(stderr, "Could not find section.\n");
+ goto quit;
+ }
+
+ /* Read the section table */
+ fseek(input, section.RawAddress, SEEK_SET);
+
+ while (1)
+ {
+ /* Read one entry */
+ if (fread(&data, 1, sizeof(data), input) != sizeof(data))
+ {
+ fprintf(stderr, "Error reading input file.\n");
+ goto quit;
+ }
+
+ switch(data.Type)
+ {
+ case TYPE_END:
+ break;
+
+ case TYPE_RAW:
+ fprintf(output, "%s\n", data.Name);
+ continue;
+
+ case TYPE_CONSTANT:
+ if (ms_format)
+ {
+ fprintf(output, "%s equ %"PRIx64"h\n", data.Name,
data.Value);
+ }
+ else
+ {
+ fprintf(output, "%s = 0x%"PRIx64"\n", data.Name,
data.Value);
+ }
+ continue;
+
+ case TYPE_HEADER:
+ if (ms_format)
+ {
+ fprintf(output, "\n; %s\n", data.Name);
+ }
+ else
+ {
+ fprintf(output, "\n/* %s */\n", data.Name);
+ }
+ continue;
+ }
+
+ break;
+ }
+
+ result = 0;
+
+quit:
+ /* Close files */
+ fclose(input);
+ fclose(output);
+
+ return result;
}