2011-08-14 18:12:05 +00:00
|
|
|
#include <precomp.h>
|
2011-05-16 13:12:07 +00:00
|
|
|
|
|
|
|
#define NDEBUG
|
2011-08-14 21:17:14 +00:00
|
|
|
#include <debug.h>
|
2011-05-16 13:12:07 +00:00
|
|
|
|
2021-06-11 12:29:21 +00:00
|
|
|
PeSect *pesection(Pe *pe, const char *name)
|
2011-05-16 13:12:07 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
ANSI_STRING WantName;
|
|
|
|
RtlInitAnsiString(&WantName, name);
|
2011-07-08 11:16:26 +00:00
|
|
|
werrstr("Searching for section %s\n", name);
|
2011-05-16 13:12:07 +00:00
|
|
|
for (i = 0; i < pe->nsections; i++) {
|
|
|
|
PANSI_STRING AnsiString = ANSI_NAME_STRING(&pe->sect[i]);
|
|
|
|
if (WantName.Length == AnsiString->Length &&
|
|
|
|
!memcmp(AnsiString->Buffer, name, WantName.Length)) {
|
2021-06-11 12:29:21 +00:00
|
|
|
werrstr("Found %s (%d) @ %x (%x)\n", name, i,
|
2011-05-16 13:12:07 +00:00
|
|
|
((PCHAR)pe->imagebase)+pe->sect[i].VirtualAddress,
|
|
|
|
pe->sect[i].SizeOfRawData);
|
|
|
|
return &pe->sect[i];
|
|
|
|
}
|
|
|
|
}
|
2011-07-08 11:16:26 +00:00
|
|
|
werrstr("%s not found\n", name);
|
2011-05-16 13:12:07 +00:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
u16int peget2(const unsigned char *ptr) {
|
|
|
|
return *((u16int*)ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
u32int peget4(const unsigned char *ptr) {
|
|
|
|
return *((u32int*)ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
u64int peget8(const unsigned char *ptr) {
|
|
|
|
return *((u64int*)ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
int readn(void *filectx, char *buffer, ulong size) {
|
|
|
|
return RosSymReadFile(filectx, buffer, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
int seek(void *filectx, ulong position, int origin) {
|
|
|
|
assert(origin == 0);
|
|
|
|
return RosSymSeekFile(filectx, position);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
readblock(void *fd, DwarfBlock *b, ulong off, ulong len)
|
|
|
|
{
|
|
|
|
b->data = malloc(len);
|
|
|
|
if(b->data == nil)
|
|
|
|
return -1;
|
|
|
|
if(!seek(fd, off, 0) || !readn(fd, (char *)b->data, len)){
|
|
|
|
free(b->data);
|
|
|
|
b->data = nil;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
b->len = len;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
loaddisksection(Pe *pe, char *name, DwarfBlock *b)
|
|
|
|
{
|
|
|
|
PeSect *s;
|
|
|
|
if((s = pesection(pe, name)) == nil)
|
|
|
|
return -1;
|
|
|
|
return readblock(pe->fd, b, s->PointerToRawData, s->SizeOfRawData);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
loadmemsection(Pe *pe, char *name, DwarfBlock *b)
|
|
|
|
{
|
|
|
|
PeSect *s;
|
|
|
|
|
|
|
|
if((s = pesection(pe, name)) == nil)
|
|
|
|
return -1;
|
2011-07-08 11:16:26 +00:00
|
|
|
werrstr("Loading section %s (ImageBase %x RVA %x)\n", name, pe->fd, s->VirtualAddress);
|
2011-05-16 13:12:07 +00:00
|
|
|
b->data = RosSymAllocMem(s->SizeOfRawData);
|
|
|
|
b->len = s->SizeOfRawData;
|
|
|
|
PCHAR DataSource = ((char *)pe->fd) + s->VirtualAddress;
|
2011-07-08 11:16:26 +00:00
|
|
|
werrstr("Copying to %x from %x (%x)\n", DataSource, b->data, b->len);
|
2011-05-16 13:12:07 +00:00
|
|
|
RtlCopyMemory(b->data, DataSource, s->SizeOfRawData);
|
2021-06-11 12:29:21 +00:00
|
|
|
|
2011-05-16 13:12:07 +00:00
|
|
|
return s->SizeOfRawData;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *RosSymAllocMemZero(ulong size, ulong count) {
|
|
|
|
void *res = RosSymAllocMem(size * count);
|
|
|
|
if (res) memset(res, 0, size * count);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
int GetStrnlen(const char *string, int maxlen) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < maxlen && string[i]; i++);
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
void pefree(Pe *pe) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < pe->nsections; i++) {
|
|
|
|
RtlFreeAnsiString(ANSI_NAME_STRING(&pe->sect[i]));
|
|
|
|
}
|
|
|
|
free(pe->sect);
|
|
|
|
free(pe);
|
|
|
|
}
|
|
|
|
|
|
|
|
void xfree(void *v) {
|
|
|
|
if (v) RosSymFreeMem(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
ulong pefindrva(struct _IMAGE_SECTION_HEADER *SectionHeaders, int NumberOfSections, ulong TargetPhysical) {
|
|
|
|
int i;
|
2011-07-08 11:16:26 +00:00
|
|
|
werrstr("Finding RVA for Physical %x\n", TargetPhysical);
|
2011-05-16 13:12:07 +00:00
|
|
|
for (i = 0; i < NumberOfSections; i++) {
|
2011-07-08 11:16:26 +00:00
|
|
|
werrstr("Section %d name %s Raw %x Virt %x\n",
|
2021-06-11 12:29:21 +00:00
|
|
|
i,
|
|
|
|
ANSI_NAME_STRING(&SectionHeaders[i])->Buffer,
|
2011-05-16 13:12:07 +00:00
|
|
|
SectionHeaders[i].PointerToRawData,
|
|
|
|
SectionHeaders[i].VirtualAddress);
|
2021-06-11 12:29:21 +00:00
|
|
|
if (TargetPhysical >= SectionHeaders[i].PointerToRawData &&
|
2011-05-16 13:12:07 +00:00
|
|
|
TargetPhysical < SectionHeaders[i].PointerToRawData + SectionHeaders[i].SizeOfRawData) {
|
2011-07-08 11:16:26 +00:00
|
|
|
werrstr("RVA %x\n", TargetPhysical - SectionHeaders[i].PointerToRawData + SectionHeaders[i].VirtualAddress);
|
2011-05-16 13:12:07 +00:00
|
|
|
return TargetPhysical - SectionHeaders[i].PointerToRawData + SectionHeaders[i].VirtualAddress;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil;
|
|
|
|
}
|