4 #include <reactos/rossym.h>
5 #include "rossympriv.h"
13 #include "rossympriv.h"
15 PeSect
*pesection(Pe
*pe
, const char *name
)
19 RtlInitAnsiString(&WantName
, name
);
20 DPRINT("Searching for section %s\n", name
);
21 for (i
= 0; i
< pe
->nsections
; i
++) {
22 PANSI_STRING AnsiString
= ANSI_NAME_STRING(&pe
->sect
[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
);
31 DPRINT("%s not found\n", name
);
35 u16int
peget2(const unsigned char *ptr
) {
36 return *((u16int
*)ptr
);
39 u32int
peget4(const unsigned char *ptr
) {
40 return *((u32int
*)ptr
);
43 u64int
peget8(const unsigned char *ptr
) {
44 return *((u64int
*)ptr
);
47 int readn(void *filectx
, char *buffer
, ulong size
) {
48 return RosSymReadFile(filectx
, buffer
, size
);
51 int seek(void *filectx
, ulong position
, int origin
) {
53 return RosSymSeekFile(filectx
, position
);
57 readblock(void *fd
, DwarfBlock
*b
, ulong off
, ulong len
)
59 b
->data
= malloc(len
);
62 if(!seek(fd
, off
, 0) || !readn(fd
, (char *)b
->data
, len
)){
72 loaddisksection(Pe
*pe
, char *name
, DwarfBlock
*b
)
75 if((s
= pesection(pe
, name
)) == nil
)
77 return readblock(pe
->fd
, b
, s
->PointerToRawData
, s
->SizeOfRawData
);
81 loadmemsection(Pe
*pe
, char *name
, DwarfBlock
*b
)
85 if((s
= pesection(pe
, name
)) == nil
)
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
);
94 return s
->SizeOfRawData
;
97 void *RosSymAllocMemZero(ulong size
, ulong count
) {
98 void *res
= RosSymAllocMem(size
* count
);
99 if (res
) memset(res
, 0, size
* count
);
103 int GetStrnlen(const char *string
, int maxlen
) {
105 for (i
= 0; i
< maxlen
&& string
[i
]; i
++);
109 void pefree(Pe
*pe
) {
111 for (i
= 0; i
< pe
->nsections
; i
++) {
112 RtlFreeAnsiString(ANSI_NAME_STRING(&pe
->sect
[i
]));
114 for (i
= 0; i
< pe
->nsymbols
; i
++) {
115 free(pe
->symtab
[i
].name
);
122 void xfree(void *v
) {
123 if (v
) RosSymFreeMem(v
);
126 ulong
pefindrva(struct _IMAGE_SECTION_HEADER
*SectionHeaders
, int NumberOfSections
, ulong TargetPhysical
) {
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",
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
;