Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygenpe.c
Go to the documentation of this file.
00001 #define NTOSAPI 00002 #include <ntifs.h> 00003 #include <ndk/ntndk.h> 00004 #include <reactos/rossym.h> 00005 #include "rossympriv.h" 00006 #include <ntimage.h> 00007 00008 #define NDEBUG 00009 #include <debug.h> 00010 00011 #include "dwarf.h" 00012 #include "pe.h" 00013 #include "rossympriv.h" 00014 00015 PeSect *pesection(Pe *pe, const char *name) 00016 { 00017 int i; 00018 ANSI_STRING WantName; 00019 RtlInitAnsiString(&WantName, name); 00020 DPRINT("Searching for section %s\n", name); 00021 for (i = 0; i < pe->nsections; i++) { 00022 PANSI_STRING AnsiString = ANSI_NAME_STRING(&pe->sect[i]); 00023 if (WantName.Length == AnsiString->Length && 00024 !memcmp(AnsiString->Buffer, name, WantName.Length)) { 00025 DPRINT("Found %s (%d) @ %x (%x)\n", name, i, 00026 ((PCHAR)pe->imagebase)+pe->sect[i].VirtualAddress, 00027 pe->sect[i].SizeOfRawData); 00028 return &pe->sect[i]; 00029 } 00030 } 00031 DPRINT("%s not found\n", name); 00032 return nil; 00033 } 00034 00035 u16int peget2(const unsigned char *ptr) { 00036 return *((u16int*)ptr); 00037 } 00038 00039 u32int peget4(const unsigned char *ptr) { 00040 return *((u32int*)ptr); 00041 } 00042 00043 u64int peget8(const unsigned char *ptr) { 00044 return *((u64int*)ptr); 00045 } 00046 00047 int readn(void *filectx, char *buffer, ulong size) { 00048 return RosSymReadFile(filectx, buffer, size); 00049 } 00050 00051 int seek(void *filectx, ulong position, int origin) { 00052 assert(origin == 0); 00053 return RosSymSeekFile(filectx, position); 00054 } 00055 00056 static int 00057 readblock(void *fd, DwarfBlock *b, ulong off, ulong len) 00058 { 00059 b->data = malloc(len); 00060 if(b->data == nil) 00061 return -1; 00062 if(!seek(fd, off, 0) || !readn(fd, (char *)b->data, len)){ 00063 free(b->data); 00064 b->data = nil; 00065 return -1; 00066 } 00067 b->len = len; 00068 return 0; 00069 } 00070 00071 int 00072 loaddisksection(Pe *pe, char *name, DwarfBlock *b) 00073 { 00074 PeSect *s; 00075 if((s = pesection(pe, name)) == nil) 00076 return -1; 00077 return readblock(pe->fd, b, s->PointerToRawData, s->SizeOfRawData); 00078 } 00079 00080 int 00081 loadmemsection(Pe *pe, char *name, DwarfBlock *b) 00082 { 00083 PeSect *s; 00084 00085 if((s = pesection(pe, name)) == nil) 00086 return -1; 00087 DPRINT("Loading section %s (ImageBase %x RVA %x)\n", name, pe->fd, s->VirtualAddress); 00088 b->data = RosSymAllocMem(s->SizeOfRawData); 00089 b->len = s->SizeOfRawData; 00090 PCHAR DataSource = ((char *)pe->fd) + s->VirtualAddress; 00091 DPRINT("Copying to %x from %x (%x)\n", DataSource, b->data, b->len); 00092 RtlCopyMemory(b->data, DataSource, s->SizeOfRawData); 00093 00094 return s->SizeOfRawData; 00095 } 00096 00097 void *RosSymAllocMemZero(ulong size, ulong count) { 00098 void *res = RosSymAllocMem(size * count); 00099 if (res) memset(res, 0, size * count); 00100 return res; 00101 } 00102 00103 int GetStrnlen(const char *string, int maxlen) { 00104 int i; 00105 for (i = 0; i < maxlen && string[i]; i++); 00106 return i; 00107 } 00108 00109 void pefree(Pe *pe) { 00110 int i; 00111 for (i = 0; i < pe->nsections; i++) { 00112 RtlFreeAnsiString(ANSI_NAME_STRING(&pe->sect[i])); 00113 } 00114 for (i = 0; i < pe->nsymbols; i++) { 00115 free(pe->symtab[i].name); 00116 } 00117 free(pe->symtab); 00118 free(pe->sect); 00119 free(pe); 00120 } 00121 00122 void xfree(void *v) { 00123 if (v) RosSymFreeMem(v); 00124 } 00125 00126 ulong pefindrva(struct _IMAGE_SECTION_HEADER *SectionHeaders, int NumberOfSections, ulong TargetPhysical) { 00127 int i; 00128 DPRINT("Finding RVA for Physical %x\n", TargetPhysical); 00129 for (i = 0; i < NumberOfSections; i++) { 00130 DPRINT("Section %d name %s Raw %x Virt %x\n", 00131 i, 00132 ANSI_NAME_STRING(&SectionHeaders[i])->Buffer, 00133 SectionHeaders[i].PointerToRawData, 00134 SectionHeaders[i].VirtualAddress); 00135 if (TargetPhysical >= SectionHeaders[i].PointerToRawData && 00136 TargetPhysical < SectionHeaders[i].PointerToRawData + SectionHeaders[i].SizeOfRawData) { 00137 DPRINT("RVA %x\n", TargetPhysical - SectionHeaders[i].PointerToRawData + SectionHeaders[i].VirtualAddress); 00138 return TargetPhysical - SectionHeaders[i].PointerToRawData + SectionHeaders[i].VirtualAddress; 00139 } 00140 } 00141 return nil; 00142 } Generated on Fri May 25 2012 04:34:49 for ReactOS by
1.7.6.1
|