ReactOS  0.4.14-dev-552-g2fad488
pe.c
Go to the documentation of this file.
1 #define NTOSAPI
2 #include <ntifs.h>
3 #include <ndk/ntndk.h>
4 #include <reactos/rossym.h>
5 #include "rossympriv.h"
6 #include <ntimage.h>
7 
8 #define NDEBUG
9 #include <debug.h>
10 
11 #include "dwarf.h"
12 #include "pe.h"
13 #include "rossympriv.h"
14 
15 PeSect *pesection(Pe *pe, const char *name)
16 {
17  int i;
18  ANSI_STRING WantName;
19  RtlInitAnsiString(&WantName, name);
20  DPRINT("Searching for section %s\n", name);
21  for (i = 0; i < pe->nsections; i++) {
23  if (WantName.Length == AnsiString->Length &&
24  !memcmp(AnsiString->Buffer, name, WantName.Length)) {
25  DPRINT("Found %s (%d) @ %x (%x)\n", name, i,
26  ((PCHAR)pe->imagebase)+pe->sect[i].VirtualAddress,
27  pe->sect[i].SizeOfRawData);
28  return &pe->sect[i];
29  }
30  }
31  DPRINT("%s not found\n", name);
32  return nil;
33 }
34 
35 u16int peget2(const unsigned char *ptr) {
36  return *((u16int*)ptr);
37 }
38 
39 u32int peget4(const unsigned char *ptr) {
40  return *((u32int*)ptr);
41 }
42 
43 u64int peget8(const unsigned char *ptr) {
44  return *((u64int*)ptr);
45 }
46 
47 int readn(void *filectx, char *buffer, ulong size) {
48  return RosSymReadFile(filectx, buffer, size);
49 }
50 
51 int seek(void *filectx, ulong position, int origin) {
52  assert(origin == 0);
53  return RosSymSeekFile(filectx, position);
54 }
55 
56 static int
58 {
59  b->data = malloc(len);
60  if(b->data == nil)
61  return -1;
62  if(!seek(fd, off, 0) || !readn(fd, (char *)b->data, len)){
63  free(b->data);
64  b->data = nil;
65  return -1;
66  }
67  b->len = len;
68  return 0;
69 }
70 
71 int
73 {
74  PeSect *s;
75  if((s = pesection(pe, name)) == nil)
76  return -1;
77  return readblock(pe->fd, b, s->PointerToRawData, s->SizeOfRawData);
78 }
79 
80 int
82 {
83  PeSect *s;
84 
85  if((s = pesection(pe, name)) == nil)
86  return -1;
87  DPRINT("Loading section %s (ImageBase %x RVA %x)\n", name, pe->fd, s->VirtualAddress);
88  b->data = RosSymAllocMem(s->SizeOfRawData);
89  b->len = s->SizeOfRawData;
90  PCHAR DataSource = ((char *)pe->fd) + s->VirtualAddress;
91  DPRINT("Copying to %x from %x (%x)\n", DataSource, b->data, b->len);
92  RtlCopyMemory(b->data, DataSource, s->SizeOfRawData);
93 
94  return s->SizeOfRawData;
95 }
96 
98  void *res = RosSymAllocMem(size * count);
99  if (res) memset(res, 0, size * count);
100  return res;
101 }
102 
103 int GetStrnlen(const char *string, int maxlen) {
104  int i;
105  for (i = 0; i < maxlen && string[i]; i++);
106  return i;
107 }
108 
109 void pefree(Pe *pe) {
110  int i;
111  for (i = 0; i < pe->nsections; i++) {
113  }
114  for (i = 0; i < pe->nsymbols; i++) {
115  free(pe->symtab[i].name);
116  }
117  free(pe->symtab);
118  free(pe->sect);
119  free(pe);
120 }
121 
122 void xfree(void *v) {
123  if (v) RosSymFreeMem(v);
124 }
125 
126 ulong pefindrva(struct _IMAGE_SECTION_HEADER *SectionHeaders, int NumberOfSections, ulong TargetPhysical) {
127  int i;
128  DPRINT("Finding RVA for Physical %x\n", TargetPhysical);
129  for (i = 0; i < NumberOfSections; i++) {
130  DPRINT("Section %d name %s Raw %x Virt %x\n",
131  i,
132  ANSI_NAME_STRING(&SectionHeaders[i])->Buffer,
133  SectionHeaders[i].PointerToRawData,
134  SectionHeaders[i].VirtualAddress);
135  if (TargetPhysical >= SectionHeaders[i].PointerToRawData &&
136  TargetPhysical < SectionHeaders[i].PointerToRawData + SectionHeaders[i].SizeOfRawData) {
137  DPRINT("RVA %x\n", TargetPhysical - SectionHeaders[i].PointerToRawData + SectionHeaders[i].VirtualAddress);
138  return TargetPhysical - SectionHeaders[i].PointerToRawData + SectionHeaders[i].VirtualAddress;
139  }
140  }
141  return nil;
142 }
char * name
Definition: pe.h:11
signed char * PCHAR
Definition: retypes.h:7
int readn(void *filectx, char *buffer, ulong size)
Definition: pe.c:47
NTSYSAPI VOID NTAPI RtlCopyMemory(VOID UNALIGNED *Destination, CONST VOID UNALIGNED *Source, ULONG Length)
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
CoffSymbol * symtab
Definition: pe.h:21
static int readblock(void *fd, DwarfBlock *b, ulong off, ulong len)
Definition: pe.c:57
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)
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
u64int peget8(const unsigned char *ptr)
Definition: pe.c:43
static PVOID ptr
Definition: dispmode.c:27
ulong nsymbols
Definition: pe.h:20
unsigned int u32int
Definition: compat.h:13
void DPRINT(...)
Definition: polytest.cpp:61
Definition: bufpool.h:45
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
_In_ ULONG _In_ BOOLEAN _Must_inspect_result_ PVOID * VirtualAddress
Definition: ndis.h:3791
unsigned short u16int
Definition: compat.h:11
unsigned long ulong
Definition: linux.h:275
const GLdouble * v
Definition: gl.h:2040
#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 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
off
Definition: i386-dis.c:3909