ReactOS  0.4.15-dev-1647-g91fceab
pe.c
Go to the documentation of this file.
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++) {
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
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
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
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 
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++) {
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 }
signed char * PCHAR
Definition: retypes.h:7
#define werrstr(str,...)
Definition: compat.h:34
int readn(void *filectx, char *buffer, ulong size)
Definition: pe.c:47
void * RosSymAllocMemZero(ulong size, ulong count)
Definition: pe.c:97
int memcmp(void *Buffer1, void *Buffer2, ACPI_SIZE Count)
Definition: utclib.c:112
#define RosSymFreeMem(Area)
Definition: rossympriv.h:22
GLuint GLuint GLsizei count
Definition: gl.h:1545
#define free
Definition: debug_ros.c:5
void * fd
Definition: pe.h:15
#define RosSymReadFile(FileContext, Buffer, Size)
Definition: rossympriv.h:23
Definition: pe.h:14
#define assert(x)
Definition: debug.h:53
voidpf uLong int origin
Definition: ioapi.h:142
GLuint buffer
Definition: glext.h:5915
static int fd
Definition: io.c:51
int GetStrnlen(const char *string, int maxlen)
Definition: pe.c:103
DWORD PointerToRawData
Definition: pedump.c:290
PeSect * pesection(Pe *pe, const char *name)
Definition: pe.c:15
NTSYSAPI VOID NTAPI RtlInitAnsiString(PANSI_STRING DestinationString, PCSZ SourceString)
u64int peget8(const unsigned char *ptr)
Definition: pe.c:43
static PVOID ptr
Definition: dispmode.c:27
unsigned int u32int
Definition: compat.h:13
static int readblock(void *fd, DwarfBlock *b, ulong off, ulong len)
Definition: pe.c:48
Definition: bufpool.h:45
_Must_inspect_result_ _In_ WDFDMATRANSACTION _In_ PFN_WDF_PROGRAM_DMA _In_ WDF_DMA_DIRECTION _In_ PMDL _In_ PVOID VirtualAddress
u16int peget2(const unsigned char *ptr)
Definition: pe.c:35
#define RosSymAllocMem(Size)
Definition: rossympriv.h:21
int seek(void *filectx, ulong position, int origin)
Definition: pe.c:51
GLboolean GLboolean GLboolean b
Definition: glext.h:6204
GLsizeiptr size
Definition: glext.h:5919
void xfree(void *v)
Definition: pe.c:122
int loaddisksection(Pe *pe, char *name, DwarfBlock *b)
Definition: pe.c:72
NTSYSAPI VOID NTAPI RtlFreeAnsiString(PANSI_STRING AnsiString)
int nsections
Definition: pe.h:23
ulong imagebase
Definition: pe.h:19
ulong pefindrva(struct _IMAGE_SECTION_HEADER *SectionHeaders, int NumberOfSections, ulong TargetPhysical)
Definition: pe.c:126
GLenum GLsizei len
Definition: glext.h:6722
GLdouble s
Definition: gl.h:2039
unsigned short u16int
Definition: compat.h:11
unsigned long ulong
Definition: linux.h:275
const GLdouble * v
Definition: gl.h:2040
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define ANSI_NAME_STRING(s)
Definition: pe.h:59
struct _IMAGE_SECTION_HEADER * sect
Definition: pe.h:24
#define nil
Definition: compat.h:23
void pefree(Pe *pe)
Definition: pe.c:109
u32int peget4(const unsigned char *ptr)
Definition: pe.c:39
Definition: name.c:38
int loadmemsection(Pe *pe, char *name, DwarfBlock *b)
Definition: pe.c:81
GLuint res
Definition: glext.h:9613
#define RtlCopyMemory(Destination, Source, Length)
Definition: typedefs.h:263
#define malloc
Definition: debug_ros.c:4
#define memset(x, y, z)
Definition: compat.h:39
#define RosSymSeekFile(FileContext, Position)
Definition: rossympriv.h:24
unsigned long long u64int
Definition: compat.h:15