2011-05-16 13:12:07 +00:00
|
|
|
/*
|
|
|
|
* COPYRIGHT: See COPYING in the top level directory
|
|
|
|
* PROJECT: ReactOS kernel
|
|
|
|
* FILE: lib/rossym/fromfile.c
|
|
|
|
* PURPOSE: Creating rossym info from a file
|
|
|
|
*
|
|
|
|
* PROGRAMMERS: Ge van Geldorp (gvg@reactos.com)
|
|
|
|
*/
|
|
|
|
|
2011-08-14 18:12:05 +00:00
|
|
|
#include <precomp.h>
|
2011-05-16 13:12:07 +00:00
|
|
|
|
2011-08-14 21:39:37 +00:00
|
|
|
#define NDEBUG
|
|
|
|
#include <debug.h>
|
|
|
|
|
2011-06-01 20:36:40 +00:00
|
|
|
#define SYMBOL_SIZE 18
|
|
|
|
|
2011-05-16 13:12:07 +00:00
|
|
|
extern NTSTATUS RosSymStatus;
|
|
|
|
|
|
|
|
BOOLEAN
|
|
|
|
RosSymCreateFromFile(PVOID FileContext, PROSSYM_INFO *RosSymInfo)
|
|
|
|
{
|
2011-06-01 20:36:40 +00:00
|
|
|
IMAGE_DOS_HEADER DosHeader;
|
|
|
|
IMAGE_NT_HEADERS NtHeaders;
|
|
|
|
PIMAGE_SECTION_HEADER SectionHeaders;
|
|
|
|
unsigned SectionIndex;
|
|
|
|
unsigned SymbolTable, NumSymbols;
|
|
|
|
|
|
|
|
/* Load DOS header */
|
|
|
|
if (! RosSymSeekFile(FileContext, 0))
|
2011-05-16 13:12:07 +00:00
|
|
|
{
|
2011-06-01 20:36:40 +00:00
|
|
|
werrstr("Could not rewind file\n");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (! RosSymReadFile(FileContext, &DosHeader, sizeof(IMAGE_DOS_HEADER)))
|
2011-05-16 13:12:07 +00:00
|
|
|
{
|
2011-06-01 20:36:40 +00:00
|
|
|
werrstr("Failed to read DOS header %x\n", RosSymStatus);
|
|
|
|
return FALSE;
|
2011-05-16 13:12:07 +00:00
|
|
|
}
|
2011-06-01 20:36:40 +00:00
|
|
|
if (! ROSSYM_IS_VALID_DOS_HEADER(&DosHeader))
|
2011-05-16 13:12:07 +00:00
|
|
|
{
|
2011-06-01 20:36:40 +00:00
|
|
|
werrstr("Image doesn't have a valid DOS header\n");
|
|
|
|
return FALSE;
|
2011-05-16 13:12:07 +00:00
|
|
|
}
|
|
|
|
|
2011-06-01 20:36:40 +00:00
|
|
|
/* Load NT headers */
|
|
|
|
if (! RosSymSeekFile(FileContext, DosHeader.e_lfanew))
|
2011-05-16 13:12:07 +00:00
|
|
|
{
|
2011-06-01 20:36:40 +00:00
|
|
|
werrstr("Failed seeking to NT headers\n");
|
|
|
|
return FALSE;
|
2011-05-16 13:12:07 +00:00
|
|
|
}
|
2011-06-01 20:36:40 +00:00
|
|
|
if (! RosSymReadFile(FileContext, &NtHeaders, sizeof(IMAGE_NT_HEADERS)))
|
2011-05-16 13:12:07 +00:00
|
|
|
{
|
2011-06-01 20:36:40 +00:00
|
|
|
werrstr("Failed to read NT headers\n");
|
|
|
|
return FALSE;
|
2011-05-16 13:12:07 +00:00
|
|
|
}
|
2011-06-01 20:36:40 +00:00
|
|
|
if (! ROSSYM_IS_VALID_NT_HEADERS(&NtHeaders))
|
2011-05-16 13:12:07 +00:00
|
|
|
{
|
2011-06-01 20:36:40 +00:00
|
|
|
werrstr("Image doesn't have a valid PE header\n");
|
|
|
|
return FALSE;
|
2011-05-16 13:12:07 +00:00
|
|
|
}
|
|
|
|
|
2011-06-01 20:36:40 +00:00
|
|
|
SymbolTable = NtHeaders.FileHeader.PointerToSymbolTable;
|
|
|
|
NumSymbols = NtHeaders.FileHeader.NumberOfSymbols;
|
2011-05-16 13:12:07 +00:00
|
|
|
|
2011-06-01 20:36:40 +00:00
|
|
|
if (!NumSymbols)
|
2011-05-16 13:12:07 +00:00
|
|
|
{
|
2011-06-01 20:36:40 +00:00
|
|
|
werrstr("Image doesn't have debug symbols\n");
|
|
|
|
return FALSE;
|
2011-05-16 13:12:07 +00:00
|
|
|
}
|
|
|
|
|
2011-06-01 20:36:40 +00:00
|
|
|
DPRINT("SymbolTable %x NumSymbols %x\n", SymbolTable, NumSymbols);
|
2011-05-16 13:12:07 +00:00
|
|
|
|
2011-06-01 20:36:40 +00:00
|
|
|
/* Load section headers */
|
|
|
|
if (! RosSymSeekFile(FileContext, (char *) IMAGE_FIRST_SECTION(&NtHeaders) -
|
|
|
|
(char *) &NtHeaders + DosHeader.e_lfanew))
|
2011-05-16 13:12:07 +00:00
|
|
|
{
|
2011-06-01 20:36:40 +00:00
|
|
|
werrstr("Failed seeking to section headers\n");
|
|
|
|
return FALSE;
|
2011-05-16 13:12:07 +00:00
|
|
|
}
|
2011-06-01 20:36:40 +00:00
|
|
|
DPRINT("Alloc section headers\n");
|
|
|
|
SectionHeaders = RosSymAllocMem(NtHeaders.FileHeader.NumberOfSections
|
|
|
|
* sizeof(IMAGE_SECTION_HEADER));
|
|
|
|
if (NULL == SectionHeaders)
|
2011-05-16 13:12:07 +00:00
|
|
|
{
|
2011-06-01 20:36:40 +00:00
|
|
|
werrstr("Failed to allocate memory for %u section headers\n",
|
|
|
|
NtHeaders.FileHeader.NumberOfSections);
|
|
|
|
return FALSE;
|
2011-05-16 13:12:07 +00:00
|
|
|
}
|
2011-06-01 20:36:40 +00:00
|
|
|
if (! RosSymReadFile(FileContext, SectionHeaders,
|
|
|
|
NtHeaders.FileHeader.NumberOfSections
|
|
|
|
* sizeof(IMAGE_SECTION_HEADER)))
|
|
|
|
{
|
|
|
|
RosSymFreeMem(SectionHeaders);
|
|
|
|
werrstr("Failed to read section headers\n");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert names to ANSI_STRINGs
|
|
|
|
for (SectionIndex = 0; SectionIndex < NtHeaders.FileHeader.NumberOfSections;
|
2021-06-11 12:29:21 +00:00
|
|
|
SectionIndex++)
|
2011-05-16 13:12:07 +00:00
|
|
|
{
|
2011-06-01 20:36:40 +00:00
|
|
|
ANSI_STRING astr;
|
|
|
|
if (SectionHeaders[SectionIndex].Name[0] != '/') {
|
|
|
|
DPRINT("Short name string %d, %s\n", SectionIndex, SectionHeaders[SectionIndex].Name);
|
|
|
|
astr.Buffer = RosSymAllocMem(IMAGE_SIZEOF_SHORT_NAME);
|
|
|
|
memcpy(astr.Buffer, SectionHeaders[SectionIndex].Name, IMAGE_SIZEOF_SHORT_NAME);
|
|
|
|
astr.MaximumLength = IMAGE_SIZEOF_SHORT_NAME;
|
|
|
|
astr.Length = GetStrnlen(astr.Buffer, IMAGE_SIZEOF_SHORT_NAME);
|
|
|
|
} else {
|
|
|
|
UNICODE_STRING intConv;
|
|
|
|
NTSTATUS Status;
|
|
|
|
ULONG StringOffset;
|
|
|
|
|
2012-07-12 19:48:30 +00:00
|
|
|
if (!RtlCreateUnicodeStringFromAsciiz(&intConv, (PCSZ)SectionHeaders[SectionIndex].Name + 1))
|
|
|
|
goto freeall;
|
2011-06-01 20:36:40 +00:00
|
|
|
Status = RtlUnicodeStringToInteger(&intConv, 10, &StringOffset);
|
|
|
|
RtlFreeUnicodeString(&intConv);
|
|
|
|
if (!NT_SUCCESS(Status)) goto freeall;
|
|
|
|
if (!RosSymSeekFile(FileContext, SymbolTable + NumSymbols * SYMBOL_SIZE + StringOffset))
|
|
|
|
goto freeall;
|
|
|
|
astr.Buffer = RosSymAllocMem(MAXIMUM_DWARF_NAME_SIZE);
|
|
|
|
if (!RosSymReadFile(FileContext, astr.Buffer, MAXIMUM_DWARF_NAME_SIZE))
|
|
|
|
goto freeall;
|
|
|
|
astr.Length = GetStrnlen(astr.Buffer, MAXIMUM_DWARF_NAME_SIZE);
|
2021-06-11 12:29:21 +00:00
|
|
|
astr.MaximumLength = MAXIMUM_DWARF_NAME_SIZE;
|
2011-06-01 20:36:40 +00:00
|
|
|
DPRINT("Long name %d, %s\n", SectionIndex, astr.Buffer);
|
|
|
|
}
|
|
|
|
*ANSI_NAME_STRING(&SectionHeaders[SectionIndex]) = astr;
|
2011-05-16 13:12:07 +00:00
|
|
|
}
|
|
|
|
|
2011-06-01 20:36:40 +00:00
|
|
|
DPRINT("Done with sections\n");
|
|
|
|
Pe *pe = RosSymAllocMem(sizeof(*pe));
|
|
|
|
pe->fd = FileContext;
|
|
|
|
pe->e2 = peget2;
|
|
|
|
pe->e4 = peget4;
|
|
|
|
pe->e8 = peget8;
|
|
|
|
pe->nsections = NtHeaders.FileHeader.NumberOfSections;
|
|
|
|
pe->sect = SectionHeaders;
|
|
|
|
pe->imagebase = pe->loadbase = NtHeaders.OptionalHeader.ImageBase;
|
|
|
|
pe->imagesize = NtHeaders.OptionalHeader.SizeOfImage;
|
2011-07-08 11:16:26 +00:00
|
|
|
pe->codestart = NtHeaders.OptionalHeader.BaseOfCode;
|
|
|
|
pe->datastart = NtHeaders.OptionalHeader.BaseOfData;
|
2011-06-01 20:36:40 +00:00
|
|
|
pe->loadsection = loaddisksection;
|
|
|
|
*RosSymInfo = dwarfopen(pe);
|
|
|
|
|
|
|
|
return TRUE;
|
2011-05-16 13:12:07 +00:00
|
|
|
|
|
|
|
freeall:
|
2011-06-01 20:36:40 +00:00
|
|
|
for (SectionIndex = 0; SectionIndex < NtHeaders.FileHeader.NumberOfSections;
|
|
|
|
SectionIndex++)
|
|
|
|
RtlFreeAnsiString(ANSI_NAME_STRING(&SectionHeaders[SectionIndex]));
|
|
|
|
RosSymFreeMem(SectionHeaders);
|
2011-05-16 13:12:07 +00:00
|
|
|
|
2011-06-01 20:36:40 +00:00
|
|
|
return FALSE;
|
2011-05-16 13:12:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* EOF */
|