[OBJ2BIN]

- Fix relocation for gas build objetcs
- Protect from buffer overwrite

svn path=/trunk/; revision=52146
This commit is contained in:
Timo Kreuzer 2011-06-08 12:42:04 +00:00
parent fbbc07eda8
commit af201a5b6a

View file

@ -8,13 +8,14 @@ void
Usage(void) Usage(void)
{ {
printf("Converts a coff object file into a raw binary file.\n" printf("Converts a coff object file into a raw binary file.\n"
"Syntax: obj2bin <source file> <dest file>\n"); "Syntax: obj2bin <source file> <dest file> <base address>\n");
} }
static static
void void
RelocateImage( RelocateImage(
char *pData, char *pData,
unsigned int nSize,
PIMAGE_RELOCATION pReloc, PIMAGE_RELOCATION pReloc,
unsigned int cNumRelocs, unsigned int cNumRelocs,
PIMAGE_SYMBOL pSymbols, PIMAGE_SYMBOL pSymbols,
@ -25,15 +26,19 @@ RelocateImage(
for (i = 0; i < cNumRelocs; i++) for (i = 0; i < cNumRelocs; i++)
{ {
if (pReloc->VirtualAddress > nSize) continue;
switch (pReloc->Type) switch (pReloc->Type)
{ {
case IMAGE_REL_I386_ABSOLUTE: case IMAGE_REL_I386_ABSOLUTE:
case 16:
p16 = (void*)(pData + pReloc->VirtualAddress); p16 = (void*)(pData + pReloc->VirtualAddress);
*p16 = (WORD)(pSymbols[pReloc->SymbolTableIndex].Value + iOffset); *p16 += (WORD)(pSymbols[pReloc->SymbolTableIndex].Value + iOffset);
break; break;
default: default:
printf("Unknown relocatation type %ld\n", pReloc->Type); printf("Unknown relocatation type %ld address %ld\n",
pReloc->Type, pReloc->VirtualAddress);
} }
pReloc++; pReloc++;
@ -183,12 +188,14 @@ int main(int argc, char *argv[])
return -15; return -15;
} }
RelocateImage(pData, pReloc, SectionHeader.NumberOfRelocations, pSymbols, iOffset); RelocateImage(pData, SectionHeader.SizeOfRawData,
pReloc, SectionHeader.NumberOfRelocations, pSymbols, iOffset);
/* Write the section to the destination file */ /* Write the section to the destination file */
if (!fwrite(pData, SectionHeader.SizeOfRawData, 1, pDestFile)) if (!fwrite(pData, SectionHeader.SizeOfRawData, 1, pDestFile))
{ {
fprintf(stderr, "Failed to write data\n"); fprintf(stderr, "Failed to write data %ld\n",
SectionHeader.SizeOfRawData);
return -16; return -16;
} }