Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygenobj2bin.c
Go to the documentation of this file.
00001 #include <stdio.h> 00002 #include <stdlib.h> 00003 #include <string.h> 00004 #include "../pecoff.h" 00005 00006 static 00007 void 00008 Usage(void) 00009 { 00010 printf("Converts a coff object file into a raw binary file.\n" 00011 "Syntax: obj2bin <source file> <dest file> <base address>\n"); 00012 } 00013 00014 static 00015 void 00016 RelocateSection( 00017 char *pData, 00018 IMAGE_SECTION_HEADER *pSectionHeader, 00019 PIMAGE_SYMBOL pSymbols, 00020 unsigned int iOffset) 00021 { 00022 unsigned int i, nOffset; 00023 PIMAGE_RELOCATION pReloc; 00024 char *pSection; 00025 WORD *p16; 00026 DWORD *p32; 00027 00028 pSection = pData + pSectionHeader->PointerToRawData; 00029 00030 /* Calculate pointer to relocation table */ 00031 pReloc = (PIMAGE_RELOCATION)(pData + pSectionHeader->PointerToRelocations); 00032 00033 /* Loop all relocations */ 00034 for (i = 0; i < pSectionHeader->NumberOfRelocations; i++) 00035 { 00036 nOffset = pReloc->VirtualAddress - pSectionHeader->VirtualAddress; 00037 00038 if (nOffset > pSectionHeader->SizeOfRawData) continue; 00039 00040 switch (pReloc->Type) 00041 { 00042 case IMAGE_REL_I386_ABSOLUTE: 00043 case 16: 00044 p16 = (void*)(pSection + nOffset); 00045 *p16 += (WORD)(pSymbols[pReloc->SymbolTableIndex].Value + iOffset); 00046 break; 00047 00048 case IMAGE_REL_I386_DIR32: 00049 p32 = (void*)(pSection + nOffset); 00050 *p32 += (DWORD)(pSymbols[pReloc->SymbolTableIndex].Value + iOffset); 00051 break; 00052 00053 default: 00054 printf("Unknown relocatation type %ld address %ld\n", 00055 pReloc->Type, pReloc->VirtualAddress); 00056 } 00057 00058 pReloc++; 00059 } 00060 } 00061 00062 int main(int argc, char *argv[]) 00063 { 00064 char *pszSourceFile; 00065 char *pszDestFile; 00066 unsigned long nFileSize, nBaseAddress; 00067 FILE *pSourceFile, *pDestFile; 00068 IMAGE_FILE_HEADER *pFileHeader; 00069 IMAGE_SECTION_HEADER *pSectionHeader; 00070 unsigned int i; 00071 char *pData; 00072 PIMAGE_SYMBOL pSymbols; 00073 00074 if ((argc != 4) || (strcmp(argv[1], "--help") == 0)) 00075 { 00076 Usage(); 00077 return -1; 00078 } 00079 00080 pszSourceFile = argv[1]; 00081 pszDestFile = argv[2]; 00082 nBaseAddress = strtol(argv[3], 0, 16); 00083 00084 pSourceFile = fopen(pszSourceFile, "rb"); 00085 if (!pSourceFile) 00086 { 00087 fprintf(stderr, "Couldn't open source file '%s'\n", pszSourceFile); 00088 return -2; 00089 } 00090 00091 /* Get file size */ 00092 fseek(pSourceFile, 0, SEEK_END); 00093 nFileSize = ftell(pSourceFile); 00094 rewind(pSourceFile); 00095 00096 /* Allocate memory for the file */ 00097 pData = malloc(nFileSize); 00098 if (!pData) 00099 { 00100 fclose(pSourceFile); 00101 fprintf(stderr, "Failed to allocate %ld bytes\n", nFileSize); 00102 return -3; 00103 } 00104 00105 /* Read the whole source file */ 00106 if (!fread(pData, nFileSize, 1, pSourceFile)) 00107 { 00108 free(pData); 00109 fclose(pSourceFile); 00110 fprintf(stderr, "Failed to read source file: %ld\n", nFileSize); 00111 return -4; 00112 } 00113 00114 /* Close source file */ 00115 fclose(pSourceFile); 00116 00117 /* Open the destination file */ 00118 pDestFile = fopen(pszDestFile, "wb"); 00119 if (!pDestFile) 00120 { 00121 free(pData); 00122 fprintf(stderr, "Couldn't open dest file '%s'\n", pszDestFile); 00123 return -5; 00124 } 00125 00126 /* Calculate table pointers */ 00127 pFileHeader = (IMAGE_FILE_HEADER*)pData; 00128 pSymbols = (void*)(pData + pFileHeader->PointerToSymbolTable); 00129 pSectionHeader = (void*)(((char*)(pFileHeader + 1)) + pFileHeader->SizeOfOptionalHeader); 00130 00131 /* Loop all sections */ 00132 for (i = 0; i < pFileHeader->NumberOfSections; i++) 00133 { 00134 /* Check if this is '.text' section */ 00135 if ((strcmp(pSectionHeader->Name, ".text") == 0) && 00136 (pSectionHeader->SizeOfRawData != 0)) 00137 { 00138 RelocateSection(pData, 00139 pSectionHeader, 00140 pSymbols, 00141 nBaseAddress); 00142 00143 /* Write the section to the destination file */ 00144 if (!fwrite(pData + pSectionHeader->PointerToRawData, 00145 pSectionHeader->SizeOfRawData, 1, pDestFile)) 00146 { 00147 free(pData); 00148 fclose(pDestFile); 00149 fprintf(stderr, "Failed to write data %ld\n", 00150 pSectionHeader->SizeOfRawData); 00151 return -6; 00152 } 00153 00154 nBaseAddress += pSectionHeader->SizeOfRawData; 00155 } 00156 00157 pSectionHeader++; 00158 } 00159 00160 free(pData); 00161 fclose(pDestFile); 00162 00163 return 0; 00164 } 00165 Generated on Sun May 27 2012 04:37:46 for ReactOS by
1.7.6.1
|