6 PeSect
*pesection(Pe
*pe
, const char *name
)
10 RtlInitAnsiString(&WantName
, name
);
11 werrstr("Searching for section %s\n", name
);
12 for (i
= 0; i
< pe
->nsections
; i
++) {
13 PANSI_STRING AnsiString
= ANSI_NAME_STRING(&pe
->sect
[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
);
22 werrstr("%s not found\n", name
);
26 u16int
peget2(const unsigned char *ptr
) {
27 return *((u16int
*)ptr
);
30 u32int
peget4(const unsigned char *ptr
) {
31 return *((u32int
*)ptr
);
34 u64int
peget8(const unsigned char *ptr
) {
35 return *((u64int
*)ptr
);
38 int readn(void *filectx
, char *buffer
, ulong size
) {
39 return RosSymReadFile(filectx
, buffer
, size
);
42 int seek(void *filectx
, ulong position
, int origin
) {
44 return RosSymSeekFile(filectx
, position
);
48 readblock(void *fd
, DwarfBlock
*b
, ulong off
, ulong len
)
50 b
->data
= malloc(len
);
53 if(!seek(fd
, off
, 0) || !readn(fd
, (char *)b
->data
, len
)){
63 loaddisksection(Pe
*pe
, char *name
, DwarfBlock
*b
)
66 if((s
= pesection(pe
, name
)) == nil
)
68 return readblock(pe
->fd
, b
, s
->PointerToRawData
, s
->SizeOfRawData
);
72 loadmemsection(Pe
*pe
, char *name
, DwarfBlock
*b
)
76 if((s
= pesection(pe
, name
)) == nil
)
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
);
85 return s
->SizeOfRawData
;
88 void *RosSymAllocMemZero(ulong size
, ulong count
) {
89 void *res
= RosSymAllocMem(size
* count
);
90 if (res
) memset(res
, 0, size
* count
);
94 int GetStrnlen(const char *string
, int maxlen
) {
96 for (i
= 0; i
< maxlen
&& string
[i
]; i
++);
100 void pefree(Pe
*pe
) {
102 for (i
= 0; i
< pe
->nsections
; i
++) {
103 RtlFreeAnsiString(ANSI_NAME_STRING(&pe
->sect
[i
]));
109 void xfree(void *v
) {
110 if (v
) RosSymFreeMem(v
);
113 ulong
pefindrva(struct _IMAGE_SECTION_HEADER
*SectionHeaders
, int NumberOfSections
, ulong TargetPhysical
) {
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",
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
;