ReactOS 0.4.15-dev-8339-g4028de8
frommem.c
Go to the documentation of this file.
1/*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: lib/rossym/frommem.c
5 * PURPOSE: Creating rossym info from an in-memory image
6 *
7 * PROGRAMMERS: Ge van Geldorp (gvg@reactos.com)
8 */
9
10#include <ntifs.h>
11#include <ndk/ntndk.h>
12#include <reactos/rossym.h>
13#include "rossympriv.h"
14#include <ntimage.h>
15
16#define NDEBUG
17#include <debug.h>
18
19#include "dwarf.h"
20#include "pe.h"
21
23RosSymCreateFromMem(PVOID ImageStart, ULONG_PTR ImageSize, PROSSYM_INFO *RosSymInfo)
24{
25 ANSI_STRING AnsiNameString = { };
26 PIMAGE_DOS_HEADER DosHeader;
27 PIMAGE_NT_HEADERS NtHeaders;
28 PIMAGE_SECTION_HEADER SectionHeaders;
29 ULONG SectionIndex;
30 unsigned SymbolTable, NumSymbols;
31
32 /* Check if MZ header is valid */
33 DosHeader = (PIMAGE_DOS_HEADER) ImageStart;
34 if (ImageSize < sizeof(IMAGE_DOS_HEADER)
35 || ! ROSSYM_IS_VALID_DOS_HEADER(DosHeader))
36 {
37 DPRINT1("Image doesn't have a valid DOS header\n");
38 return FALSE;
39 }
40
41 /* Locate NT header */
42 NtHeaders = (PIMAGE_NT_HEADERS)((char *) ImageStart + DosHeader->e_lfanew);
43 if (ImageSize < DosHeader->e_lfanew + sizeof(IMAGE_NT_HEADERS)
44 || ! ROSSYM_IS_VALID_NT_HEADERS(NtHeaders))
45 {
46 DPRINT1("Image doesn't have a valid PE header\n");
47 return FALSE;
48 }
49
50 SymbolTable = NtHeaders->FileHeader.PointerToSymbolTable;
51 NumSymbols = NtHeaders->FileHeader.NumberOfSymbols;
52
53 /* Search for the section header */
54 ULONG SectionHeaderSize = NtHeaders->FileHeader.NumberOfSections * sizeof(IMAGE_SECTION_HEADER);
55 SectionHeaders = RosSymAllocMem(SectionHeaderSize);
56 RtlCopyMemory(SectionHeaders, IMAGE_FIRST_SECTION(NtHeaders), SectionHeaderSize);
57
58 // Convert names to ANSI_STRINGs
59 for (SectionIndex = 0; SectionIndex < NtHeaders->FileHeader.NumberOfSections;
60 SectionIndex++)
61 {
62 if (SectionHeaders[SectionIndex].Name[0] != '/') {
64 RtlCopyMemory(AnsiNameString.Buffer, SectionHeaders[SectionIndex].Name, IMAGE_SIZEOF_SHORT_NAME);
66 AnsiNameString.Length = GetStrnlen(AnsiNameString.Buffer, IMAGE_SIZEOF_SHORT_NAME);
67 } else {
68 UNICODE_STRING intConv;
70 ULONG StringOffset;
71
72 if (!RtlCreateUnicodeStringFromAsciiz(&intConv, (PCSZ)SectionHeaders[SectionIndex].Name + 1))
73 goto freeall;
74 Status = RtlUnicodeStringToInteger(&intConv, 10, &StringOffset);
75 RtlFreeUnicodeString(&intConv);
76 if (!NT_SUCCESS(Status)) goto freeall;
77 ULONG VirtualOffset = pefindrva(SectionHeaders, NtHeaders->FileHeader.NumberOfSections, SymbolTable+(NumSymbols*SYMBOL_SIZE)+StringOffset);
78 if (!VirtualOffset) goto freeall;
80 if (!AnsiNameString.Buffer) goto freeall;
81 PCHAR StringTarget = ((PCHAR)ImageStart)+VirtualOffset;
82 PCHAR EndOfImage = ((PCHAR)ImageStart) + NtHeaders->OptionalHeader.SizeOfImage;
83 if (StringTarget >= EndOfImage) goto freeall;
84 ULONG PossibleStringLength = EndOfImage - StringTarget;
85 if (PossibleStringLength > MAXIMUM_DWARF_NAME_SIZE)
86 PossibleStringLength = MAXIMUM_DWARF_NAME_SIZE;
87 RtlCopyMemory(AnsiNameString.Buffer, StringTarget, PossibleStringLength);
88 AnsiNameString.Length = strlen(AnsiNameString.Buffer);
90 }
91 memcpy
92 (&SectionHeaders[SectionIndex],
93 &AnsiNameString,
94 sizeof(AnsiNameString));
95 }
96
97 Pe *pe = RosSymAllocMem(sizeof(*pe));
98 pe->fd = ImageStart;
99 pe->e2 = peget2;
100 pe->e4 = peget4;
101 pe->e8 = peget8;
102 pe->loadbase = (ULONG)ImageStart;
103 pe->imagebase = NtHeaders->OptionalHeader.ImageBase;
104 pe->imagesize = NtHeaders->OptionalHeader.SizeOfImage;
105 pe->nsections = NtHeaders->FileHeader.NumberOfSections;
106 pe->sect = SectionHeaders;
107 pe->nsymbols = NtHeaders->FileHeader.NumberOfSymbols;
108 pe->symtab = malloc(pe->nsymbols * sizeof(CoffSymbol));
109 PSYMENT SymbolData = (PSYMENT)
110 (((PCHAR)ImageStart) +
112 (pe->sect,
113 pe->nsections,
114 NtHeaders->FileHeader.PointerToSymbolTable));
115 int i, j;
116 for (i = 0, j = 0; i < pe->nsymbols; i++) {
117 if ((SymbolData[i].e_scnum < 1) ||
118 (SymbolData[i].e_sclass != C_EXT &&
119 SymbolData[i].e_sclass != C_STAT))
120 continue;
121 int section = SymbolData[i].e_scnum - 1;
122 if (SymbolData[i].e.e.e_zeroes) {
123 pe->symtab[j].name = malloc(sizeof(SymbolData[i].e.e_name)+1);
124 strcpy(pe->symtab[j].name, SymbolData[i].e.e_name);
125 } else {
126 PCHAR SymbolName = ((PCHAR)ImageStart) +
128 (pe->sect,
129 pe->nsections,
131 (NtHeaders->FileHeader.NumberOfSymbols * 18) +
132 SymbolData[i].e.e.e_offset);
133 pe->symtab[j].name = malloc(strlen(SymbolName)+1);
134 strcpy(pe->symtab[j].name, SymbolName);
135 }
136 if (pe->symtab[j].name[0] == '.') {
137 free(pe->symtab[j].name);
138 continue;
139 }
140 pe->symtab[j].address = pe->sect[section].VirtualAddress + SymbolData[i].e_value;
141 j++;
142 }
143 pe->nsymbols = j;
145 *RosSymInfo = dwarfopen(pe);
146
147 return !!*RosSymInfo;
148
149freeall:
150 if (AnsiNameString.Buffer) RosSymFreeMem(AnsiNameString.Buffer);
151 for (SectionIndex = 0; SectionIndex < NtHeaders->FileHeader.NumberOfSections;
152 SectionIndex++)
153 RtlFreeAnsiString(ANSI_NAME_STRING(&SectionHeaders[SectionIndex]));
154 RosSymFreeMem(SectionHeaders);
155
156 return FALSE;
157}
158
159/* EOF */
unsigned char BOOLEAN
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
char * strcpy(char *DstString, const char *SrcString)
Definition: utclib.c:388
LONG NTSTATUS
Definition: precomp.h:26
#define DPRINT1
Definition: precomp.h:8
#define free
Definition: debug_ros.c:5
#define malloc
Definition: debug_ros.c:4
#define FALSE
Definition: types.h:117
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:33
PIMAGE_NT_HEADERS32 PIMAGE_NT_HEADERS
Definition: ntddk_ex.h:187
struct _IMAGE_DOS_HEADER * PIMAGE_DOS_HEADER
Status
Definition: gdiplustypes.h:25
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
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 GLint GLint j
Definition: glfuncs.h:250
#define e
Definition: ke_i.h:82
#define PCHAR
Definition: match.c:90
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
DWORD e_lfanew
Definition: crypt.c:1156
NTSYSAPI BOOLEAN NTAPI RtlCreateUnicodeStringFromAsciiz(_Out_ PUNICODE_STRING Destination, _In_ PCSZ Source)
NTSYSAPI VOID NTAPI RtlFreeAnsiString(PANSI_STRING AnsiString)
NTSYSAPI NTSTATUS NTAPI RtlUnicodeStringToInteger(PUNICODE_STRING String, ULONG Base, PULONG Value)
NTSYSAPI VOID NTAPI RtlFreeUnicodeString(PUNICODE_STRING UnicodeString)
#define IMAGE_FIRST_SECTION(NtHeader)
Definition: ntimage.h:427
#define IMAGE_SIZEOF_SHORT_NAME
Definition: pedump.c:277
struct _IMAGE_SECTION_HEADER IMAGE_SECTION_HEADER
BOOLEAN RosSymCreateFromMem(PVOID ImageStart, ULONG_PTR ImageSize, PROSSYM_INFO *RosSymInfo)
Definition: frommem.c:19
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
u16int peget2(const unsigned char *ptr)
Definition: pe.c:35
int GetStrnlen(const char *string, int maxlen)
Definition: pe.c:103
ulong pefindrva(struct _IMAGE_SECTION_HEADER *SectionHeaders, int NumberOfSections, ulong TargetPhysical)
Definition: pe.c:126
#define C_STAT
Definition: pe.h:46
#define C_EXT
Definition: pe.h:45
struct SYMENT * PSYMENT
#define ANSI_NAME_STRING(s)
Definition: pe.h:59
#define RosSymAllocMem(Size)
Definition: rossympriv.h:14
#define RosSymFreeMem(Area)
Definition: rossympriv.h:15
#define ROSSYM_IS_VALID_NT_HEADERS(NtHeaders)
Definition: rossympriv.h:24
#define ROSSYM_IS_VALID_DOS_HEADER(DosHeader)
Definition: rossympriv.h:22
#define MAXIMUM_DWARF_NAME_SIZE
Definition: dwarf.h:470
#define SYMBOL_SIZE
Definition: dwarf.h:469
Dwarf * dwarfopen(struct _Pe *elf)
Definition: dwarfopen.c:16
Definition: pe.h:29
struct SYMENT::@4981::@4982 e
unsigned char e_sclass
Definition: pe.h:40
unsigned long e_value
Definition: pe.h:37
short e_scnum
Definition: pe.h:38
USHORT MaximumLength
Definition: env_spec_w32.h:377
Definition: pe.h:9
ulong address
Definition: pe.h:10
char * name
Definition: pe.h:11
DWORD NumberOfSymbols
Definition: ntddk_ex.h:126
DWORD PointerToSymbolTable
Definition: ntddk_ex.h:125
IMAGE_OPTIONAL_HEADER32 OptionalHeader
Definition: ntddk_ex.h:184
IMAGE_FILE_HEADER FileHeader
Definition: ntddk_ex.h:183
BYTE Name[IMAGE_SIZEOF_SHORT_NAME]
Definition: pedump.c:281
Definition: pe.h:14
ulong imagesize
Definition: pe.h:19
struct _IMAGE_SECTION_HEADER * sect
Definition: pe.h:24
void * fd
Definition: pe.h:15
ulong loadbase
Definition: pe.h:19
CoffSymbol * symtab
Definition: pe.h:21
u16int(* e2)(const unsigned char *data)
Definition: pe.h:16
int(* loadsection)(struct _Pe *pe, char *name, struct DwarfBlock *b)
Definition: pe.h:22
ulong nsymbols
Definition: pe.h:20
u64int(* e8)(const unsigned char *data)
Definition: pe.h:18
u32int(* e4)(const unsigned char *data)
Definition: pe.h:17
ulong imagebase
Definition: pe.h:19
int nsections
Definition: pe.h:23
Definition: parser.c:56
#define RtlCopyMemory(Destination, Source, Length)
Definition: typedefs.h:263
uint32_t ULONG_PTR
Definition: typedefs.h:65
uint32_t ULONG
Definition: typedefs.h:59
char * PCHAR
Definition: typedefs.h:51
CONST char * PCSZ
Definition: umtypes.h:125