#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include "../pecoff.h"
+#include <typedefs.h>
+#include <pecoff.h>
static
void
break;
default:
- printf("Unknown relocatation type %ld address %ld\n",
- pReloc->Type, pReloc->VirtualAddress);
+ printf("Unknown relocation type %u, address 0x%x\n",
+ pReloc->Type, (unsigned)pReloc->VirtualAddress);
}
pReloc++;
{
char *pszSourceFile;
char *pszDestFile;
- unsigned long nFileSize, nBaseAddress, nOffsetSectionHeaders;
+ unsigned long nFileSize, nBaseAddress;
FILE *pSourceFile, *pDestFile;
IMAGE_FILE_HEADER *pFileHeader;
IMAGE_SECTION_HEADER *pSectionHeader;
unsigned int i;
- size_t nSize;
char *pData;
- PIMAGE_RELOCATION pReloc;
PIMAGE_SYMBOL pSymbols;
if ((argc != 4) || (strcmp(argv[1], "--help") == 0))
pData = malloc(nFileSize);
if (!pData)
{
- fprintf(stderr, "Failed to allocate %ld bytes\n", nFileSize);
+ fclose(pSourceFile);
+ fprintf(stderr, "Failed to allocate %lu bytes\n", nFileSize);
return -3;
}
/* Read the whole source file */
if (!fread(pData, nFileSize, 1, pSourceFile))
{
- fprintf(stderr, "Failed to read source file: %ld\n", nFileSize);
+ free(pData);
+ fclose(pSourceFile);
+ fprintf(stderr, "Failed to read %lu bytes from source file\n", nFileSize);
return -4;
}
/* Open the destination file */
pDestFile = fopen(pszDestFile, "wb");
- if (!pszDestFile)
+ if (!pDestFile)
{
- fprintf(stderr, "Couldn't open dest file '%s'\n", pszDestFile);
+ free(pData);
+ fprintf(stderr, "Couldn't open destination file '%s'\n", pszDestFile);
return -5;
}
/* Loop all sections */
for (i = 0; i < pFileHeader->NumberOfSections; i++)
{
- /* Skip empty sections */
- if (pSectionHeader->SizeOfRawData == 0) continue;
-
/* Check if this is '.text' section */
- if (strcmp(pSectionHeader->Name, ".text") == 0)
+ if ((strcmp((char*)pSectionHeader->Name, ".text") == 0) &&
+ (pSectionHeader->SizeOfRawData != 0))
{
RelocateSection(pData,
pSectionHeader,
if (!fwrite(pData + pSectionHeader->PointerToRawData,
pSectionHeader->SizeOfRawData, 1, pDestFile))
{
- fprintf(stderr, "Failed to write data %ld\n",
- pSectionHeader->SizeOfRawData);
+ free(pData);
+ fclose(pDestFile);
+ fprintf(stderr, "Failed to write %u bytes to destination file\n",
+ (unsigned int)pSectionHeader->SizeOfRawData);
return -6;
}
pSectionHeader++;
}
+ free(pData);
fclose(pDestFile);
return 0;
}
-