ReactOS  0.4.14-dev-57-g333b8f1
obj2bin.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <typedefs.h>
5 #include <pecoff.h>
6 
7 static
8 void
9 Usage(void)
10 {
11  printf("Converts a coff object file into a raw binary file.\n"
12  "Syntax: obj2bin <source file> <dest file> <base address>\n");
13 }
14 
15 static
16 void
18  char *pData,
19  IMAGE_SECTION_HEADER *pSectionHeader,
20  PIMAGE_SYMBOL pSymbols,
21  unsigned int iOffset)
22 {
23  unsigned int i, nOffset;
24  PIMAGE_RELOCATION pReloc;
25  char *pSection;
26  WORD *p16;
27  DWORD *p32;
28 
29  pSection = pData + pSectionHeader->PointerToRawData;
30 
31  /* Calculate pointer to relocation table */
32  pReloc = (PIMAGE_RELOCATION)(pData + pSectionHeader->PointerToRelocations);
33 
34  /* Loop all relocations */
35  for (i = 0; i < pSectionHeader->NumberOfRelocations; i++)
36  {
37  nOffset = pReloc->VirtualAddress - pSectionHeader->VirtualAddress;
38 
39  if (nOffset > pSectionHeader->SizeOfRawData) continue;
40 
41  switch (pReloc->Type)
42  {
44  case 16:
45  p16 = (void*)(pSection + nOffset);
46  *p16 += (WORD)(pSymbols[pReloc->SymbolTableIndex].Value + iOffset);
47  break;
48 
50  p32 = (void*)(pSection + nOffset);
51  *p32 += (DWORD)(pSymbols[pReloc->SymbolTableIndex].Value + iOffset);
52  break;
53 
54  default:
55  printf("Unknown relocation type %u, address 0x%x\n",
56  pReloc->Type, (unsigned)pReloc->VirtualAddress);
57  }
58 
59  pReloc++;
60  }
61 }
62 
63 int main(int argc, char *argv[])
64 {
65  char *pszSourceFile;
66  char *pszDestFile;
67  unsigned long nFileSize, nBaseAddress;
68  FILE *pSourceFile, *pDestFile;
69  IMAGE_FILE_HEADER *pFileHeader;
70  IMAGE_SECTION_HEADER *pSectionHeader;
71  unsigned int i;
72  char *pData;
73  PIMAGE_SYMBOL pSymbols;
74 
75  if ((argc != 4) || (strcmp(argv[1], "--help") == 0))
76  {
77  Usage();
78  return -1;
79  }
80 
81  pszSourceFile = argv[1];
82  pszDestFile = argv[2];
83  nBaseAddress = strtol(argv[3], 0, 16);
84 
85  pSourceFile = fopen(pszSourceFile, "rb");
86  if (!pSourceFile)
87  {
88  fprintf(stderr, "Couldn't open source file '%s'\n", pszSourceFile);
89  return -2;
90  }
91 
92  /* Get file size */
93  fseek(pSourceFile, 0, SEEK_END);
94  nFileSize = ftell(pSourceFile);
95  rewind(pSourceFile);
96 
97  /* Allocate memory for the file */
98  pData = malloc(nFileSize);
99  if (!pData)
100  {
101  fclose(pSourceFile);
102  fprintf(stderr, "Failed to allocate %lu bytes\n", nFileSize);
103  return -3;
104  }
105 
106  /* Read the whole source file */
107  if (!fread(pData, nFileSize, 1, pSourceFile))
108  {
109  free(pData);
110  fclose(pSourceFile);
111  fprintf(stderr, "Failed to read %lu bytes from source file\n", nFileSize);
112  return -4;
113  }
114 
115  /* Close source file */
116  fclose(pSourceFile);
117 
118  /* Open the destination file */
119  pDestFile = fopen(pszDestFile, "wb");
120  if (!pDestFile)
121  {
122  free(pData);
123  fprintf(stderr, "Couldn't open destination file '%s'\n", pszDestFile);
124  return -5;
125  }
126 
127  /* Calculate table pointers */
128  pFileHeader = (IMAGE_FILE_HEADER*)pData;
129  pSymbols = (void*)(pData + pFileHeader->PointerToSymbolTable);
130  pSectionHeader = (void*)(((char*)(pFileHeader + 1)) + pFileHeader->SizeOfOptionalHeader);
131 
132  /* Loop all sections */
133  for (i = 0; i < pFileHeader->NumberOfSections; i++)
134  {
135  /* Check if this is '.text' section */
136  if ((strcmp((char*)pSectionHeader->Name, ".text") == 0) &&
137  (pSectionHeader->SizeOfRawData != 0))
138  {
140  pSectionHeader,
141  pSymbols,
142  nBaseAddress);
143 
144  /* Write the section to the destination file */
145  if (!fwrite(pData + pSectionHeader->PointerToRawData,
146  pSectionHeader->SizeOfRawData, 1, pDestFile))
147  {
148  free(pData);
149  fclose(pDestFile);
150  fprintf(stderr, "Failed to write %u bytes to destination file\n",
151  (unsigned int)pSectionHeader->SizeOfRawData);
152  return -6;
153  }
154 
155  nBaseAddress += pSectionHeader->SizeOfRawData;
156  }
157 
158  pSectionHeader++;
159  }
160 
161  free(pData);
162  fclose(pDestFile);
163 
164  return 0;
165 }
static int argc
Definition: ServiceArgs.c:12
static void Usage(void)
Definition: obj2bin.c:9
static void RelocateSection(char *pData, IMAGE_SECTION_HEADER *pSectionHeader, PIMAGE_SYMBOL pSymbols, unsigned int iOffset)
Definition: obj2bin.c:17
WORD NumberOfRelocations
Definition: pedump.c:293
#define IMAGE_REL_I386_ABSOLUTE
Definition: pecoff.h:44
#define free
Definition: debug_ros.c:5
DWORD PointerToRawData
Definition: pedump.c:290
_Check_return_opt_ _CRTIMP size_t __cdecl fwrite(_In_reads_bytes_(_Size *_Count) const void *_Str, _In_ size_t _Size, _In_ size_t _Count, _Inout_ FILE *_File)
#define argv
Definition: mplay32.c:18
#define DWORD
Definition: nt_native.h:44
int main(int argc, char *argv[])
Definition: obj2bin.c:63
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
_Check_return_opt_ _CRTIMP size_t __cdecl fread(_Out_writes_bytes_(_ElementSize *_Count) void *_DstBuf, _In_ size_t _ElementSize, _In_ size_t _Count, _Inout_ FILE *_File)
_Check_return_opt_ _CRTIMP int __cdecl fprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format,...)
_Check_return_opt_ _CRTIMP int __cdecl fseek(_Inout_ FILE *_File, _In_ long _Offset, _In_ int _Origin)
WORD SizeOfOptionalHeader
Definition: ntddk_ex.h:127
_CRTIMP void __cdecl rewind(_Inout_ FILE *_File)
#define IMAGE_REL_I386_DIR32
Definition: pecoff.h:45
unsigned short WORD
Definition: ntddk_ex.h:93
unsigned long DWORD
Definition: ntddk_ex.h:95
_Check_return_opt_ _CRTIMP int __cdecl fclose(_Inout_ FILE *_File)
_Check_return_ _CRTIMP FILE *__cdecl fopen(_In_z_ const char *_Filename, _In_z_ const char *_Mode)
DWORD PointerToSymbolTable
Definition: ntddk_ex.h:125
struct _IMAGE_RELOCATION UNALIGNED * PIMAGE_RELOCATION
Definition: pecoff.h:222
BYTE Name[IMAGE_SIZEOF_SHORT_NAME]
Definition: pedump.c:281
_Check_return_ long __cdecl strtol(_In_z_ const char *_Str, _Out_opt_ _Deref_post_z_ char **_EndPtr, _In_ int _Radix)
_Check_return_ _CRTIMP long __cdecl ftell(_Inout_ FILE *_File)
FILE * stderr
int strcmp(const char *String1, const char *String2)
Definition: utclib.c:469
#define SEEK_END
Definition: cabinet.c:27
#define malloc
Definition: debug_ros.c:4
TW_UINT32 TW_UINT16 TW_UINT16 TW_MEMREF pData
Definition: twain.h:1827
DWORD PointerToRelocations
Definition: pedump.c:291
#define printf
Definition: config.h:203
struct _IMAGE_SYMBOL UNALIGNED * PIMAGE_SYMBOL
Definition: pecoff.h:241