88c6e95d724b499bc9d16d35a65568ca47d438e8
11 printf("Converts a coff object file into a raw binary file.\n"
12 "Syntax: obj2bin <source file> <dest file> <base address>\n");
19 IMAGE_SECTION_HEADER
*pSectionHeader
,
20 PIMAGE_SYMBOL pSymbols
,
23 unsigned int i
, nOffset
;
24 PIMAGE_RELOCATION pReloc
;
29 pSection
= pData
+ pSectionHeader
->PointerToRawData
;
31 /* Calculate pointer to relocation table */
32 pReloc
= (PIMAGE_RELOCATION
)(pData
+ pSectionHeader
->PointerToRelocations
);
34 /* Loop all relocations */
35 for (i
= 0; i
< pSectionHeader
->NumberOfRelocations
; i
++)
37 nOffset
= pReloc
->VirtualAddress
- pSectionHeader
->VirtualAddress
;
39 if (nOffset
> pSectionHeader
->SizeOfRawData
) continue;
43 case IMAGE_REL_I386_ABSOLUTE
:
45 p16
= (void*)(pSection
+ nOffset
);
46 *p16
+= (WORD
)(pSymbols
[pReloc
->SymbolTableIndex
].Value
+ iOffset
);
49 case IMAGE_REL_I386_DIR32
:
50 p32
= (void*)(pSection
+ nOffset
);
51 *p32
+= (DWORD
)(pSymbols
[pReloc
->SymbolTableIndex
].Value
+ iOffset
);
55 printf("Unknown relocatation type %d, address 0x%lx\n",
56 pReloc
->Type
, pReloc
->VirtualAddress
);
63 int main(int argc
, char *argv
[])
67 unsigned long nFileSize
, nBaseAddress
;
68 FILE *pSourceFile
, *pDestFile
;
69 IMAGE_FILE_HEADER
*pFileHeader
;
70 IMAGE_SECTION_HEADER
*pSectionHeader
;
73 PIMAGE_SYMBOL pSymbols
;
75 if ((argc
!= 4) || (strcmp(argv
[1], "--help") == 0))
81 pszSourceFile
= argv
[1];
82 pszDestFile
= argv
[2];
83 nBaseAddress
= strtol(argv
[3], 0, 16);
85 pSourceFile
= fopen(pszSourceFile
, "rb");
88 fprintf(stderr
, "Couldn't open source file '%s'\n", pszSourceFile
);
93 fseek(pSourceFile
, 0, SEEK_END
);
94 nFileSize
= ftell(pSourceFile
);
97 /* Allocate memory for the file */
98 pData
= malloc(nFileSize
);
102 fprintf(stderr
, "Failed to allocate %ld bytes\n", nFileSize
);
106 /* Read the whole source file */
107 if (!fread(pData
, nFileSize
, 1, pSourceFile
))
111 fprintf(stderr
, "Failed to read %ld bytes from source file\n", nFileSize
);
115 /* Close source file */
118 /* Open the destination file */
119 pDestFile
= fopen(pszDestFile
, "wb");
123 fprintf(stderr
, "Couldn't open destination file '%s'\n", pszDestFile
);
127 /* Calculate table pointers */
128 pFileHeader
= (IMAGE_FILE_HEADER
*)pData
;
129 pSymbols
= (void*)(pData
+ pFileHeader
->PointerToSymbolTable
);
130 pSectionHeader
= (void*)(((char*)(pFileHeader
+ 1)) + pFileHeader
->SizeOfOptionalHeader
);
132 /* Loop all sections */
133 for (i
= 0; i
< pFileHeader
->NumberOfSections
; i
++)
135 /* Check if this is '.text' section */
136 if ((strcmp((char*)pSectionHeader
->Name
, ".text") == 0) &&
137 (pSectionHeader
->SizeOfRawData
!= 0))
139 RelocateSection(pData
,
144 /* Write the section to the destination file */
145 if (!fwrite(pData
+ pSectionHeader
->PointerToRawData
,
146 pSectionHeader
->SizeOfRawData
, 1, pDestFile
))
150 fprintf(stderr
, "Failed to write %ld bytes to destination file\n",
151 pSectionHeader
->SizeOfRawData
);
155 nBaseAddress
+= pSectionHeader
->SizeOfRawData
;