ReactOS  0.4.15-dev-2703-g05fb0f1
obj2bin.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <typedefs.h>
#include <pecoff.h>
Include dependency graph for obj2bin.c:

Go to the source code of this file.

Functions

static void Usage (void)
 
static int RelocateSection (char *pData, IMAGE_SECTION_HEADER *pSectionHeader, PIMAGE_SYMBOL pSymbols, unsigned int iOffset)
 
int main (int argc, char *argv[])
 

Function Documentation

◆ main()

int main ( int argc  ,
char argv[] 
)

hosttype.c Copyright (C) 2002 by Brian Palmer brian.nosp@m.p@sg.nosp@m.inet..nosp@m.com

Definition at line 71 of file obj2bin.c.

72 {
73  char *pszSourceFile;
74  char *pszDestFile;
75  unsigned long nFileSize, nBaseAddress;
76  FILE *pSourceFile, *pDestFile;
77  IMAGE_FILE_HEADER *pFileHeader;
78  IMAGE_SECTION_HEADER *pSectionHeader;
79  unsigned int i;
80  char *pData;
81  PIMAGE_SYMBOL pSymbols;
82 
83  if ((argc != 4) || (strcmp(argv[1], "--help") == 0))
84  {
85  Usage();
86  return -1;
87  }
88 
89  pszSourceFile = argv[1];
90  pszDestFile = argv[2];
91  nBaseAddress = strtol(argv[3], 0, 16);
92 
93  pSourceFile = fopen(pszSourceFile, "rb");
94  if (!pSourceFile)
95  {
96  fprintf(stderr, "Couldn't open source file '%s'\n", pszSourceFile);
97  return -2;
98  }
99 
100  /* Get file size */
101  fseek(pSourceFile, 0, SEEK_END);
102  nFileSize = ftell(pSourceFile);
103  rewind(pSourceFile);
104 
105  /* Allocate memory for the file */
106  pData = malloc(nFileSize);
107  if (!pData)
108  {
109  fclose(pSourceFile);
110  fprintf(stderr, "Failed to allocate %lu bytes\n", nFileSize);
111  return -3;
112  }
113 
114  /* Read the whole source file */
115  if (!fread(pData, nFileSize, 1, pSourceFile))
116  {
117  free(pData);
118  fclose(pSourceFile);
119  fprintf(stderr, "Failed to read %lu bytes from source file\n", nFileSize);
120  return -4;
121  }
122 
123  /* Close source file */
124  fclose(pSourceFile);
125 
126  /* Open the destination file */
127  pDestFile = fopen(pszDestFile, "wb");
128  if (!pDestFile)
129  {
130  free(pData);
131  fprintf(stderr, "Couldn't open destination file '%s'\n", pszDestFile);
132  return -5;
133  }
134 
135  /* Calculate table pointers */
136  pFileHeader = (IMAGE_FILE_HEADER*)pData;
137  pSymbols = (void*)(pData + pFileHeader->PointerToSymbolTable);
138  pSectionHeader = (void*)(((char*)(pFileHeader + 1)) + pFileHeader->SizeOfOptionalHeader);
139 
140  /* Loop all sections */
141  for (i = 0; i < pFileHeader->NumberOfSections; i++)
142  {
143  /* Check if this is '.text' section */
144  if ((strcmp((char*)pSectionHeader->Name, ".text") == 0) &&
145  (pSectionHeader->SizeOfRawData != 0))
146  {
147  if (!RelocateSection(pData,
148  pSectionHeader,
149  pSymbols,
150  nBaseAddress))
151  {
152  free(pData);
153  fclose(pDestFile);
154  return -7;
155  }
156 
157  /* Write the section to the destination file */
158  if (!fwrite(pData + pSectionHeader->PointerToRawData,
159  pSectionHeader->SizeOfRawData, 1, pDestFile))
160  {
161  free(pData);
162  fclose(pDestFile);
163  fprintf(stderr, "Failed to write %u bytes to destination file\n",
164  (unsigned int)pSectionHeader->SizeOfRawData);
165  return -6;
166  }
167 
168  nBaseAddress += pSectionHeader->SizeOfRawData;
169  }
170 
171  pSectionHeader++;
172  }
173 
174  free(pData);
175  fclose(pDestFile);
176 
177  return 0;
178 }
static int argc
Definition: ServiceArgs.c:12
static void Usage(void)
Definition: obj2bin.c:9
#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
_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)
_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)
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
DWORD PointerToSymbolTable
Definition: ntddk_ex.h:125
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
static int RelocateSection(char *pData, IMAGE_SECTION_HEADER *pSectionHeader, PIMAGE_SYMBOL pSymbols, unsigned int iOffset)
Definition: obj2bin.c:17
struct _IMAGE_SYMBOL UNALIGNED * PIMAGE_SYMBOL
Definition: pecoff.h:258

◆ RelocateSection()

static int RelocateSection ( char pData,
IMAGE_SECTION_HEADER pSectionHeader,
PIMAGE_SYMBOL  pSymbols,
unsigned int  iOffset 
)
static

Definition at line 17 of file obj2bin.c.

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  p16 = (void*)(pSection + nOffset);
51  *p16 += (WORD)(pSymbols[pReloc->SymbolTableIndex].Value + iOffset);
52  break;
53 
55  p32 = (void*)(pSection + nOffset);
56  *p32 += (DWORD)(pSymbols[pReloc->SymbolTableIndex].Value + iOffset);
57  break;
58 
59  default:
60  printf("Unknown relocation type %u, address 0x%x\n",
61  pReloc->Type, (unsigned)pReloc->VirtualAddress);
62  return 0;
63  }
64 
65  pReloc++;
66  }
67 
68  return 1;
69 }
WORD NumberOfRelocations
Definition: pedump.c:293
#define IMAGE_REL_I386_ABSOLUTE
Definition: pecoff.h:60
DWORD PointerToRawData
Definition: pedump.c:290
#define DWORD
Definition: nt_native.h:44
#define IMAGE_REL_I386_DIR32
Definition: pecoff.h:62
unsigned short WORD
Definition: ntddk_ex.h:93
unsigned long DWORD
Definition: ntddk_ex.h:95
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
struct _IMAGE_RELOCATION UNALIGNED * PIMAGE_RELOCATION
Definition: pecoff.h:239
#define IMAGE_REL_I386_REL16
Definition: pecoff.h:61
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

Referenced by main().

◆ Usage()

static void Usage ( void  )
static

Definition at line 9 of file obj2bin.c.

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 }
#define printf
Definition: config.h:203

Referenced by main().