1 #include <precomp.h> 2 3 #define NDEBUG 4 #include <debug.h> 5 6 PeSect *pesection(Pe *pe, const char *name) 7 { 8 int i; 9 ANSI_STRING WantName; 10 RtlInitAnsiString(&WantName, name); 11 werrstr("Searching for section %s\n", name); 12 for (i = 0; i < pe->nsections; i++) { 13 PANSI_STRING AnsiString = ANSI_NAME_STRING(&pe->sect[i]); 14 if (WantName.Length == AnsiString->Length && 15 !memcmp(AnsiString->Buffer, name, WantName.Length)) { 16 werrstr("Found %s (%d) @ %x (%x)\n", name, i, 17 ((PCHAR)pe->imagebase)+pe->sect[i].VirtualAddress, 18 pe->sect[i].SizeOfRawData); 19 return &pe->sect[i]; 20 } 21 } 22 werrstr("%s not found\n", name); 23 return nil; 24 } 25 26 u16int peget2(const unsigned char *ptr) { 27 return *((u16int*)ptr); 28 } 29 30 u32int peget4(const unsigned char *ptr) { 31 return *((u32int*)ptr); 32 } 33 34 u64int peget8(const unsigned char *ptr) { 35 return *((u64int*)ptr); 36 } 37 38 int readn(void *filectx, char *buffer, ulong size) { 39 return RosSymReadFile(filectx, buffer, size); 40 } 41 42 int seek(void *filectx, ulong position, int origin) { 43 assert(origin == 0); 44 return RosSymSeekFile(filectx, position); 45 } 46 47 static int 48 readblock(void *fd, DwarfBlock *b, ulong off, ulong len) 49 { 50 b->data = malloc(len); 51 if(b->data == nil) 52 return -1; 53 if(!seek(fd, off, 0) || !readn(fd, (char *)b->data, len)){ 54 free(b->data); 55 b->data = nil; 56 return -1; 57 } 58 b->len = len; 59 return 0; 60 } 61 62 int 63 loaddisksection(Pe *pe, char *name, DwarfBlock *b) 64 { 65 PeSect *s; 66 if((s = pesection(pe, name)) == nil) 67 return -1; 68 return readblock(pe->fd, b, s->PointerToRawData, s->SizeOfRawData); 69 } 70 71 int 72 loadmemsection(Pe *pe, char *name, DwarfBlock *b) 73 { 74 PeSect *s; 75 76 if((s = pesection(pe, name)) == nil) 77 return -1; 78 werrstr("Loading section %s (ImageBase %x RVA %x)\n", name, pe->fd, s->VirtualAddress); 79 b->data = RosSymAllocMem(s->SizeOfRawData); 80 b->len = s->SizeOfRawData; 81 PCHAR DataSource = ((char *)pe->fd) + s->VirtualAddress; 82 werrstr("Copying to %x from %x (%x)\n", DataSource, b->data, b->len); 83 RtlCopyMemory(b->data, DataSource, s->SizeOfRawData); 84 85 return s->SizeOfRawData; 86 } 87 88 void *RosSymAllocMemZero(ulong size, ulong count) { 89 void *res = RosSymAllocMem(size * count); 90 if (res) memset(res, 0, size * count); 91 return res; 92 } 93 94 int GetStrnlen(const char *string, int maxlen) { 95 int i; 96 for (i = 0; i < maxlen && string[i]; i++); 97 return i; 98 } 99 100 void pefree(Pe *pe) { 101 int i; 102 for (i = 0; i < pe->nsections; i++) { 103 RtlFreeAnsiString(ANSI_NAME_STRING(&pe->sect[i])); 104 } 105 free(pe->sect); 106 free(pe); 107 } 108 109 void xfree(void *v) { 110 if (v) RosSymFreeMem(v); 111 } 112 113 ulong pefindrva(struct _IMAGE_SECTION_HEADER *SectionHeaders, int NumberOfSections, ulong TargetPhysical) { 114 int i; 115 werrstr("Finding RVA for Physical %x\n", TargetPhysical); 116 for (i = 0; i < NumberOfSections; i++) { 117 werrstr("Section %d name %s Raw %x Virt %x\n", 118 i, 119 ANSI_NAME_STRING(&SectionHeaders[i])->Buffer, 120 SectionHeaders[i].PointerToRawData, 121 SectionHeaders[i].VirtualAddress); 122 if (TargetPhysical >= SectionHeaders[i].PointerToRawData && 123 TargetPhysical < SectionHeaders[i].PointerToRawData + SectionHeaders[i].SizeOfRawData) { 124 werrstr("RVA %x\n", TargetPhysical - SectionHeaders[i].PointerToRawData + SectionHeaders[i].VirtualAddress); 125 return TargetPhysical - SectionHeaders[i].PointerToRawData + SectionHeaders[i].VirtualAddress; 126 } 127 } 128 return nil; 129 } 130