ReactOS 0.4.15-dev-8231-g29a56f3
pe.c
Go to the documentation of this file.
1
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
15PeSect *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
35u16int peget2(const unsigned char *ptr) {
36 return *((u16int*)ptr);
37}
38
39u32int peget4(const unsigned char *ptr) {
40 return *((u32int*)ptr);
41}
42
43u64int peget8(const unsigned char *ptr) {
44 return *((u64int*)ptr);
45}
46
47int readn(void *filectx, char *buffer, ulong size) {
48 return RosSymReadFile(filectx, buffer, size);
49}
50
51int seek(void *filectx, ulong position, int origin) {
52 assert(origin == 0);
53 return RosSymSeekFile(filectx, position);
54}
55
56static 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
71int
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
80int
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
103int GetStrnlen(const char *string, int maxlen) {
104 int i;
105 for (i = 0; i < maxlen && string[i]; i++);
106 return i;
107}
108
109void 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
122void xfree(void *v) {
123 if (v) RosSymFreeMem(v);
124}
125
126ulong 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}
int memcmp(void *Buffer1, void *Buffer2, ACPI_SIZE Count)
Definition: utclib.c:112
Definition: bufpool.h:45
#define free
Definition: debug_ros.c:5
#define malloc
Definition: debug_ros.c:4
#define assert(x)
Definition: debug.h:53
@ AnsiString
Definition: dnslib.h:19
unsigned long ulong
Definition: linux.h:275
const GLdouble * v
Definition: gl.h:2040
GLdouble s
Definition: gl.h:2039
GLuint GLuint GLsizei count
Definition: gl.h:1545
GLsizeiptr size
Definition: glext.h:5919
GLuint res
Definition: glext.h:9613
GLuint buffer
Definition: glext.h:5915
GLboolean GLboolean GLboolean b
Definition: glext.h:6204
GLenum GLsizei len
Definition: glext.h:6722
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
voidpf uLong int origin
Definition: ioapi.h:144
static PVOID ptr
Definition: dispmode.c:27
NTSYSAPI VOID NTAPI RtlFreeAnsiString(PANSI_STRING AnsiString)
NTSYSAPI VOID NTAPI RtlInitAnsiString(PANSI_STRING DestinationString, PCSZ SourceString)
u32int peget4(const unsigned char *ptr)
Definition: pe.c:39
u64int peget8(const unsigned char *ptr)
Definition: pe.c:43
int loadmemsection(Pe *pe, char *name, DwarfBlock *b)
Definition: pe.c:81
void xfree(void *v)
Definition: pe.c:122
void * RosSymAllocMemZero(ulong size, ulong count)
Definition: pe.c:97
PeSect * pesection(Pe *pe, const char *name)
Definition: pe.c:15
u16int peget2(const unsigned char *ptr)
Definition: pe.c:35
int GetStrnlen(const char *string, int maxlen)
Definition: pe.c:103
static int readblock(void *fd, DwarfBlock *b, ulong off, ulong len)
Definition: pe.c:57
int seek(void *filectx, ulong position, int origin)
Definition: pe.c:51
int loaddisksection(Pe *pe, char *name, DwarfBlock *b)
Definition: pe.c:72
int readn(void *filectx, char *buffer, ulong size)
Definition: pe.c:47
void pefree(Pe *pe)
Definition: pe.c:109
ulong pefindrva(struct _IMAGE_SECTION_HEADER *SectionHeaders, int NumberOfSections, ulong TargetPhysical)
Definition: pe.c:126
#define ANSI_NAME_STRING(s)
Definition: pe.h:59
#define RosSymSeekFile(FileContext, Position)
Definition: rossympriv.h:17
#define RosSymAllocMem(Size)
Definition: rossympriv.h:14
#define RosSymFreeMem(Area)
Definition: rossympriv.h:15
#define RosSymReadFile(FileContext, Buffer, Size)
Definition: rossympriv.h:16
static int fd
Definition: io.c:51
#define nil
Definition: compat.h:23
unsigned short u16int
Definition: compat.h:11
unsigned long long u64int
Definition: compat.h:15
unsigned int u32int
Definition: compat.h:13
#define memset(x, y, z)
Definition: compat.h:39
#define DPRINT
Definition: sndvol32.h:73
char * name
Definition: pe.h:11
DWORD PointerToRawData
Definition: pedump.c:290
Definition: pe.h:14
struct _IMAGE_SECTION_HEADER * sect
Definition: pe.h:24
void * fd
Definition: pe.h:15
CoffSymbol * symtab
Definition: pe.h:21
ulong nsymbols
Definition: pe.h:20
ulong imagebase
Definition: pe.h:19
int nsections
Definition: pe.h:23
Definition: name.c:39
#define RtlCopyMemory(Destination, Source, Length)
Definition: typedefs.h:263
char * PCHAR
Definition: typedefs.h:51
_Must_inspect_result_ _In_ WDFDMATRANSACTION _In_ PFN_WDF_PROGRAM_DMA _In_ WDF_DMA_DIRECTION _In_ PMDL _In_ PVOID VirtualAddress