10 printf("Converts a coff object file into a raw binary file.\n"
11 "Syntax: obj2bin <source file> <dest file>\n");
18 PIMAGE_RELOCATION pReloc
,
19 unsigned int cNumRelocs
,
20 PIMAGE_SYMBOL pSymbols
,
26 for (i
= 0; i
< cNumRelocs
; i
++)
30 case IMAGE_REL_I386_ABSOLUTE
:
31 p16
= (void*)(pData
+ pReloc
->VirtualAddress
);
32 *p16
= (WORD
)(pSymbols
[pReloc
->SymbolTableIndex
].Value
+ iOffset
);
36 printf("Unknown relocatation type %ld\n", pReloc
->Type
);
43 int main(int argc
, char *argv
[])
47 unsigned long iOffset
;
48 FILE *pSourceFile
, *pDestFile
;
49 IMAGE_FILE_HEADER FileHeader
;
50 IMAGE_SECTION_HEADER SectionHeader
;
54 PIMAGE_RELOCATION pReloc
;
55 PIMAGE_SYMBOL pSymbols
;
57 if ((argc
!= 4) || (strcmp(argv
[1], "--help") == 0))
63 pszSourceFile
= argv
[1];
64 pszDestFile
= argv
[2];
66 pSourceFile
= fopen(pszSourceFile
, "rb");
69 fprintf(stderr
, "Couldn't open source file '%s'\n", pszSourceFile
);
73 pDestFile
= fopen(pszDestFile
, "wb");
76 fprintf(stderr
, "Couldn't open dest file '%s'\n", pszDestFile
);
80 iOffset
= strtol(argv
[3], 0, 16);
82 /* Load the coff header */
83 nSize
= fread(&FileHeader
, 1, sizeof(FileHeader
), pSourceFile
);
84 if (nSize
!= sizeof(FileHeader
))
86 fprintf(stderr
, "Failed to read source file\n");
90 /* Jump to section headers (skip optional header) */
91 if (fseek(pSourceFile
, FileHeader
.SizeOfOptionalHeader
, SEEK_CUR
))
93 fprintf(stderr
, "Failed to set file pointer\n");
97 /* Loop all sections */
98 for (i
= 0; i
< FileHeader
.NumberOfSections
; i
++)
100 /* Read section header */
101 nSize
= fread(&SectionHeader
, 1, sizeof(SectionHeader
), pSourceFile
);
102 if (nSize
!= sizeof(SectionHeader
))
104 fprintf(stderr
, "Failed to read section %ld file\n", i
);
108 /* Check if this is '.text' section */
109 if (strcmp(SectionHeader
.Name
, ".text") == 0) break;
112 if (i
== FileHeader
.NumberOfSections
)
114 fprintf(stderr
, "No .text section found\n");
118 /* Move file pointer to the symbol table */
119 if (fseek(pSourceFile
, FileHeader
.PointerToSymbolTable
, SEEK_SET
))
121 fprintf(stderr
, "Failed to set file pointer\n");
125 /* Allocate memory for the symbols */
126 nSize
= FileHeader
.NumberOfSymbols
* sizeof(IMAGE_SYMBOL
);
127 pSymbols
= malloc(nSize
);
130 fprintf(stderr
, "Failed to allocate %ld bytes\n", nSize
);
134 /* Read symbol data */
135 if (!fread(pSymbols
, nSize
, 1, pSourceFile
))
137 fprintf(stderr
, "Failed to read section %ld file\n", i
);
141 /* Move file pointer to the start of the section */
142 if (fseek(pSourceFile
, SectionHeader
.PointerToRawData
, SEEK_SET
))
144 fprintf(stderr
, "Failed to set file pointer\n");
148 /* Allocate memory for the section */
149 pData
= malloc(SectionHeader
.SizeOfRawData
);
152 fprintf(stderr
, "Failed to allocate %ld bytes\n", SectionHeader
.SizeOfRawData
);
156 /* Read section data */
157 if (!fread(pData
, SectionHeader
.SizeOfRawData
, 1, pSourceFile
))
159 fprintf(stderr
, "Failed to read section %ld file\n", i
);
163 /* Allocate memory for the relocation */
164 nSize
= SectionHeader
.NumberOfRelocations
* sizeof(IMAGE_RELOCATION
);
165 pReloc
= malloc(nSize
);
168 fprintf(stderr
, "Failed to allocate %ld bytes\n", nSize
);
172 /* Move file pointer to the relocation table */
173 if (fseek(pSourceFile
, SectionHeader
.PointerToRelocations
, SEEK_SET
))
175 fprintf(stderr
, "Failed to set file pointer\n");
179 /* Read relocation data */
180 if (!fread(pReloc
, nSize
, 1, pSourceFile
))
182 fprintf(stderr
, "Failed to read section %ld file\n", i
);
186 RelocateImage(pData
, pReloc
, SectionHeader
.NumberOfRelocations
, pSymbols
, iOffset
);
188 /* Write the section to the destination file */
189 if (!fwrite(pData
, SectionHeader
.SizeOfRawData
, 1, pDestFile
))
191 fprintf(stderr
, "Failed to write data\n");