2011-05-16 13:12:07 +00:00
|
|
|
/*
|
|
|
|
* Usage: rsym input-file output-file
|
|
|
|
*
|
|
|
|
* There are two sources of information: the .stab/.stabstr
|
|
|
|
* sections of the executable and the COFF symbol table. Most
|
|
|
|
* of the information is in the .stab/.stabstr sections.
|
|
|
|
* However, most of our asm files don't contain .stab directives,
|
|
|
|
* so routines implemented in assembler won't show up in the
|
|
|
|
* .stab section. They are present in the COFF symbol table.
|
|
|
|
* So, we mostly use the .stab/.stabstr sections, but we augment
|
|
|
|
* the info there with info from the COFF symbol table when
|
|
|
|
* possible.
|
|
|
|
*
|
|
|
|
* This is a tool and is compiled using the host compiler,
|
|
|
|
* i.e. on Linux gcc and not mingw-gcc (cross-compiler).
|
|
|
|
* Therefore we can't include SDK headers and we have to
|
|
|
|
* duplicate some definitions here.
|
|
|
|
* Also note that the internal functions are "old C-style",
|
|
|
|
* returning an int, where a return of 0 means success and
|
|
|
|
* non-zero is failure.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include "rsym.h"
|
|
|
|
|
2012-09-29 15:28:46 +00:00
|
|
|
int
|
|
|
|
IsDebugSection(PIMAGE_SECTION_HEADER Section)
|
|
|
|
{
|
|
|
|
/* This is a hack, but works for us */
|
|
|
|
return (Section->Name[0] == '/');
|
|
|
|
}
|
|
|
|
|
2011-05-16 13:12:07 +00:00
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
2012-09-29 15:28:46 +00:00
|
|
|
unsigned int i;
|
2012-09-29 14:13:16 +00:00
|
|
|
PSYMBOLFILE_HEADER SymbolFileHeader;
|
2012-09-29 15:28:46 +00:00
|
|
|
PIMAGE_NT_HEADERS NtHeaders;
|
|
|
|
PIMAGE_DOS_HEADER DosHeader;
|
|
|
|
PIMAGE_FILE_HEADER FileHeader;
|
|
|
|
PIMAGE_OPTIONAL_HEADER OptionalHeader;
|
|
|
|
PIMAGE_SECTION_HEADER SectionHeaders, LastSection;
|
2012-09-29 14:13:16 +00:00
|
|
|
char* path1;
|
|
|
|
char* path2;
|
|
|
|
FILE* out;
|
|
|
|
size_t FileSize;
|
|
|
|
void *FileData;
|
|
|
|
char elfhdr[] = { '\377', 'E', 'L', 'F' };
|
|
|
|
|
2012-09-29 15:28:46 +00:00
|
|
|
if (argc != 3)
|
2011-05-16 13:12:07 +00:00
|
|
|
{
|
2012-09-29 14:13:16 +00:00
|
|
|
fprintf(stderr, "Usage: rsym <exefile> <symfile>\n");
|
|
|
|
exit(1);
|
2011-05-16 13:12:07 +00:00
|
|
|
}
|
|
|
|
|
2012-09-29 14:13:16 +00:00
|
|
|
path1 = convert_path(argv[1]);
|
|
|
|
path2 = convert_path(argv[2]);
|
2011-05-16 13:12:07 +00:00
|
|
|
|
2012-09-29 15:28:46 +00:00
|
|
|
/* Load the input file into memory */
|
|
|
|
FileData = load_file( path1, &FileSize);
|
2012-09-29 14:13:16 +00:00
|
|
|
if ( !FileData )
|
|
|
|
{
|
2012-09-29 15:28:46 +00:00
|
|
|
fprintf(stderr, "An error occured loading '%s'\n", path1);
|
2012-09-29 14:13:16 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if MZ header exists */
|
2012-09-29 15:28:46 +00:00
|
|
|
DosHeader = (PIMAGE_DOS_HEADER) FileData;
|
|
|
|
if (DosHeader->e_magic != IMAGE_DOS_MAGIC || DosHeader->e_lfanew == 0L)
|
2012-09-29 14:13:16 +00:00
|
|
|
{
|
|
|
|
/* Ignore elf */
|
2012-09-29 15:28:46 +00:00
|
|
|
if (!memcmp(DosHeader, elfhdr, sizeof(elfhdr)))
|
2012-09-29 14:13:16 +00:00
|
|
|
exit(0);
|
|
|
|
perror("Input file is not a PE image.\n");
|
|
|
|
free(FileData);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2012-09-29 15:28:46 +00:00
|
|
|
/* Locate the headers */
|
|
|
|
NtHeaders = (PIMAGE_NT_HEADERS)((char*)FileData + DosHeader->e_lfanew);
|
|
|
|
FileHeader = &NtHeaders->FileHeader;
|
|
|
|
OptionalHeader = &NtHeaders->OptionalHeader;
|
2012-09-29 14:13:16 +00:00
|
|
|
|
|
|
|
/* Locate PE section headers */
|
2012-09-29 15:28:46 +00:00
|
|
|
SectionHeaders = (PIMAGE_SECTION_HEADER)((char*)OptionalHeader +
|
|
|
|
FileHeader->SizeOfOptionalHeader);
|
2012-09-29 14:13:16 +00:00
|
|
|
|
2012-09-29 15:28:46 +00:00
|
|
|
/* Loop all sections */
|
|
|
|
for (i = 0; i < FileHeader->NumberOfSections; i++)
|
2012-09-29 14:13:16 +00:00
|
|
|
{
|
2012-09-29 15:28:46 +00:00
|
|
|
/* Check if this is a debug section */
|
|
|
|
if (IsDebugSection(&SectionHeaders[i]))
|
2012-09-29 14:13:16 +00:00
|
|
|
{
|
2012-09-29 15:28:46 +00:00
|
|
|
/* Make sure we have the correct characteristics */
|
|
|
|
SectionHeaders[i].Characteristics |= IMAGE_SCN_CNT_INITIALIZED_DATA;
|
|
|
|
SectionHeaders[i].Characteristics &= ~(IMAGE_SCN_MEM_PURGEABLE | IMAGE_SCN_MEM_DISCARDABLE);
|
2012-09-29 14:13:16 +00:00
|
|
|
}
|
|
|
|
}
|
2011-05-16 13:12:07 +00:00
|
|
|
|
2012-09-29 15:28:46 +00:00
|
|
|
/* Get a pointer to the last section header */
|
|
|
|
LastSection = &SectionHeaders[FileHeader->NumberOfSections - 1];
|
|
|
|
|
|
|
|
/* Set the size of the last section to cover the rest of the PE */
|
|
|
|
LastSection->SizeOfRawData = FileSize - LastSection->PointerToRawData;
|
|
|
|
|
|
|
|
/* Check if the virtual section size is smaller than the raw data */
|
|
|
|
if (LastSection->Misc.VirtualSize < LastSection->SizeOfRawData)
|
2011-05-16 13:12:07 +00:00
|
|
|
{
|
2012-09-29 15:28:46 +00:00
|
|
|
/* Make sure the virtual size of the section cover the raw data */
|
|
|
|
LastSection->Misc.VirtualSize = ROUND_UP(LastSection->SizeOfRawData,
|
|
|
|
OptionalHeader->SectionAlignment);
|
|
|
|
|
|
|
|
/* Fix up image size */
|
|
|
|
OptionalHeader->SizeOfImage = LastSection->VirtualAddress +
|
|
|
|
LastSection->Misc.VirtualSize;
|
2011-05-16 13:12:07 +00:00
|
|
|
}
|
|
|
|
|
2012-09-29 15:28:46 +00:00
|
|
|
/* Open the output file */
|
2012-09-29 14:13:16 +00:00
|
|
|
out = fopen(path2, "wb");
|
|
|
|
if (out == NULL)
|
2011-05-16 13:12:07 +00:00
|
|
|
{
|
2012-09-29 14:13:16 +00:00
|
|
|
perror("Cannot open output file");
|
|
|
|
free(FileData);
|
|
|
|
exit(1);
|
2011-05-16 13:12:07 +00:00
|
|
|
}
|
|
|
|
|
2012-09-29 15:28:46 +00:00
|
|
|
/* Write the output file */
|
2012-09-29 14:13:16 +00:00
|
|
|
fwrite(FileData, 1, FileSize, out);
|
|
|
|
fclose(out);
|
|
|
|
free(FileData);
|
2011-05-16 13:12:07 +00:00
|
|
|
|
2012-09-29 14:13:16 +00:00
|
|
|
return 0;
|
2011-05-16 13:12:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* EOF */
|