mirror of
https://github.com/reactos/reactos.git
synced 2025-08-05 20:23:34 +00:00
[RSYM_CMAKE]
Improve the code a bit. No functional change. svn path=/trunk/; revision=57433
This commit is contained in:
parent
c1abf5e96b
commit
c553d21849
2 changed files with 72 additions and 31 deletions
|
@ -186,6 +186,28 @@ typedef IMAGE_OPTIONAL_HEADER32 IMAGE_OPTIONAL_HEADER;
|
||||||
typedef PIMAGE_OPTIONAL_HEADER32 PIMAGE_OPTIONAL_HEADER;
|
typedef PIMAGE_OPTIONAL_HEADER32 PIMAGE_OPTIONAL_HEADER;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
typedef struct _IMAGE_NT_HEADERS32
|
||||||
|
{
|
||||||
|
DWORD Signature;
|
||||||
|
IMAGE_FILE_HEADER FileHeader;
|
||||||
|
IMAGE_OPTIONAL_HEADER32 OptionalHeader;
|
||||||
|
} IMAGE_NT_HEADERS32, *PIMAGE_NT_HEADERS32;
|
||||||
|
|
||||||
|
typedef struct _IMAGE_NT_HEADERS64
|
||||||
|
{
|
||||||
|
DWORD Signature;
|
||||||
|
IMAGE_FILE_HEADER FileHeader;
|
||||||
|
IMAGE_OPTIONAL_HEADER32 OptionalHeader;
|
||||||
|
} IMAGE_NT_HEADERS64, *PIMAGE_NT_HEADERS64;
|
||||||
|
|
||||||
|
#ifdef _TARGET_PE64
|
||||||
|
typedef IMAGE_NT_HEADERS64 IMAGE_NT_HEADERS;
|
||||||
|
typedef PIMAGE_NT_HEADERS64 PIMAGE_NT_HEADERS;
|
||||||
|
#else
|
||||||
|
typedef IMAGE_NT_HEADERS32 IMAGE_NT_HEADERS;
|
||||||
|
typedef PIMAGE_NT_HEADERS32 PIMAGE_NT_HEADERS;
|
||||||
|
#endif
|
||||||
|
|
||||||
typedef struct _IMAGE_SECTION_HEADER {
|
typedef struct _IMAGE_SECTION_HEADER {
|
||||||
BYTE Name[IMAGE_SIZEOF_SHORT_NAME];
|
BYTE Name[IMAGE_SIZEOF_SHORT_NAME];
|
||||||
union {
|
union {
|
||||||
|
|
|
@ -27,14 +27,22 @@
|
||||||
|
|
||||||
#include "rsym.h"
|
#include "rsym.h"
|
||||||
|
|
||||||
|
int
|
||||||
|
IsDebugSection(PIMAGE_SECTION_HEADER Section)
|
||||||
|
{
|
||||||
|
/* This is a hack, but works for us */
|
||||||
|
return (Section->Name[0] == '/');
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char* argv[])
|
int main(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
int i;
|
unsigned int i;
|
||||||
PSYMBOLFILE_HEADER SymbolFileHeader;
|
PSYMBOLFILE_HEADER SymbolFileHeader;
|
||||||
PIMAGE_DOS_HEADER PEDosHeader;
|
PIMAGE_NT_HEADERS NtHeaders;
|
||||||
PIMAGE_FILE_HEADER PEFileHeader;
|
PIMAGE_DOS_HEADER DosHeader;
|
||||||
PIMAGE_OPTIONAL_HEADER PEOptHeader;
|
PIMAGE_FILE_HEADER FileHeader;
|
||||||
PIMAGE_SECTION_HEADER PESectionHeaders;
|
PIMAGE_OPTIONAL_HEADER OptionalHeader;
|
||||||
|
PIMAGE_SECTION_HEADER SectionHeaders, LastSection;
|
||||||
char* path1;
|
char* path1;
|
||||||
char* path2;
|
char* path2;
|
||||||
FILE* out;
|
FILE* out;
|
||||||
|
@ -42,7 +50,7 @@ int main(int argc, char* argv[])
|
||||||
void *FileData;
|
void *FileData;
|
||||||
char elfhdr[] = { '\377', 'E', 'L', 'F' };
|
char elfhdr[] = { '\377', 'E', 'L', 'F' };
|
||||||
|
|
||||||
if (3 != argc)
|
if (argc != 3)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Usage: rsym <exefile> <symfile>\n");
|
fprintf(stderr, "Usage: rsym <exefile> <symfile>\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
|
@ -51,6 +59,7 @@ int main(int argc, char* argv[])
|
||||||
path1 = convert_path(argv[1]);
|
path1 = convert_path(argv[1]);
|
||||||
path2 = convert_path(argv[2]);
|
path2 = convert_path(argv[2]);
|
||||||
|
|
||||||
|
/* Load the input file into memory */
|
||||||
FileData = load_file( path1, &FileSize);
|
FileData = load_file( path1, &FileSize);
|
||||||
if ( !FileData )
|
if ( !FileData )
|
||||||
{
|
{
|
||||||
|
@ -59,48 +68,57 @@ int main(int argc, char* argv[])
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Check if MZ header exists */
|
/* Check if MZ header exists */
|
||||||
PEDosHeader = (PIMAGE_DOS_HEADER)FileData;
|
DosHeader = (PIMAGE_DOS_HEADER) FileData;
|
||||||
if (PEDosHeader->e_magic != IMAGE_DOS_MAGIC || PEDosHeader->e_lfanew == 0L)
|
if (DosHeader->e_magic != IMAGE_DOS_MAGIC || DosHeader->e_lfanew == 0L)
|
||||||
{
|
{
|
||||||
/* Ignore elf */
|
/* Ignore elf */
|
||||||
if (!memcmp(PEDosHeader, elfhdr, sizeof(elfhdr)))
|
if (!memcmp(DosHeader, elfhdr, sizeof(elfhdr)))
|
||||||
exit(0);
|
exit(0);
|
||||||
perror("Input file is not a PE image.\n");
|
perror("Input file is not a PE image.\n");
|
||||||
free(FileData);
|
free(FileData);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Locate PE file header */
|
/* Locate the headers */
|
||||||
/* sizeof(ULONG) = sizeof(MAGIC) */
|
NtHeaders = (PIMAGE_NT_HEADERS)((char*)FileData + DosHeader->e_lfanew);
|
||||||
PEFileHeader = (PIMAGE_FILE_HEADER)((char *) FileData + PEDosHeader->e_lfanew + sizeof(ULONG));
|
FileHeader = &NtHeaders->FileHeader;
|
||||||
|
OptionalHeader = &NtHeaders->OptionalHeader;
|
||||||
/* Locate optional header */
|
|
||||||
assert(sizeof(ULONG) == 4);
|
|
||||||
PEOptHeader = (PIMAGE_OPTIONAL_HEADER)(PEFileHeader + 1);
|
|
||||||
|
|
||||||
/* Locate PE section headers */
|
/* Locate PE section headers */
|
||||||
PESectionHeaders = (PIMAGE_SECTION_HEADER)((char *) PEOptHeader + PEFileHeader->SizeOfOptionalHeader);
|
SectionHeaders = (PIMAGE_SECTION_HEADER)((char*)OptionalHeader +
|
||||||
|
FileHeader->SizeOfOptionalHeader);
|
||||||
|
|
||||||
for (i = 0; i < PEFileHeader->NumberOfSections; i++)
|
/* Loop all sections */
|
||||||
|
for (i = 0; i < FileHeader->NumberOfSections; i++)
|
||||||
{
|
{
|
||||||
if (PESectionHeaders[i].Name[0] == '/')
|
/* Check if this is a debug section */
|
||||||
|
if (IsDebugSection(&SectionHeaders[i]))
|
||||||
{
|
{
|
||||||
PESectionHeaders[i].Characteristics |= IMAGE_SCN_CNT_INITIALIZED_DATA;
|
/* Make sure we have the correct characteristics */
|
||||||
PESectionHeaders[i].Characteristics &= ~(IMAGE_SCN_MEM_PURGEABLE | IMAGE_SCN_MEM_DISCARDABLE);
|
SectionHeaders[i].Characteristics |= IMAGE_SCN_CNT_INITIALIZED_DATA;
|
||||||
|
SectionHeaders[i].Characteristics &= ~(IMAGE_SCN_MEM_PURGEABLE | IMAGE_SCN_MEM_DISCARDABLE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
PESectionHeaders[PEFileHeader->NumberOfSections-1].SizeOfRawData =
|
/* Get a pointer to the last section header */
|
||||||
FileSize - PESectionHeaders[PEFileHeader->NumberOfSections-1].PointerToRawData;
|
LastSection = &SectionHeaders[FileHeader->NumberOfSections - 1];
|
||||||
if (PESectionHeaders[PEFileHeader->NumberOfSections-1].SizeOfRawData >
|
|
||||||
PESectionHeaders[PEFileHeader->NumberOfSections-1].Misc.VirtualSize)
|
/* 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)
|
||||||
{
|
{
|
||||||
PESectionHeaders[PEFileHeader->NumberOfSections-1].Misc.VirtualSize =
|
/* Make sure the virtual size of the section cover the raw data */
|
||||||
ROUND_UP(PESectionHeaders[PEFileHeader->NumberOfSections-1].SizeOfRawData,
|
LastSection->Misc.VirtualSize = ROUND_UP(LastSection->SizeOfRawData,
|
||||||
PEOptHeader->SectionAlignment);
|
OptionalHeader->SectionAlignment);
|
||||||
PEOptHeader->SizeOfImage = PESectionHeaders[PEFileHeader->NumberOfSections-1].VirtualAddress + PESectionHeaders[PEFileHeader->NumberOfSections-1].Misc.VirtualSize;
|
|
||||||
|
/* Fix up image size */
|
||||||
|
OptionalHeader->SizeOfImage = LastSection->VirtualAddress +
|
||||||
|
LastSection->Misc.VirtualSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Open the output file */
|
||||||
out = fopen(path2, "wb");
|
out = fopen(path2, "wb");
|
||||||
if (out == NULL)
|
if (out == NULL)
|
||||||
{
|
{
|
||||||
|
@ -109,6 +127,7 @@ int main(int argc, char* argv[])
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Write the output file */
|
||||||
fwrite(FileData, 1, FileSize, out);
|
fwrite(FileData, 1, FileSize, out);
|
||||||
fclose(out);
|
fclose(out);
|
||||||
free(FileData);
|
free(FileData);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue