ReactOS  0.4.14-dev-342-gdc047f9
frommem.c File Reference
#include <ntifs.h>
#include <ndk/ntndk.h>
#include <reactos/rossym.h>
#include "rossympriv.h"
#include <ntimage.h>
#include <debug.h>
#include "dwarf.h"
#include "pe.h"
Include dependency graph for frommem.c:

Go to the source code of this file.

Macros

#define NTOSAPI
 
#define NDEBUG
 

Functions

BOOLEAN RosSymCreateFromMem (PVOID ImageStart, ULONG_PTR ImageSize, PROSSYM_INFO *RosSymInfo)
 

Macro Definition Documentation

◆ NDEBUG

#define NDEBUG

Definition at line 17 of file frommem.c.

◆ NTOSAPI

#define NTOSAPI

Definition at line 10 of file frommem.c.

Function Documentation

◆ RosSymCreateFromMem()

BOOLEAN RosSymCreateFromMem ( PVOID  ImageStart,
ULONG_PTR  ImageSize,
PROSSYM_INFO RosSymInfo 
)

Definition at line 24 of file frommem.c.

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

Referenced by KdbSymProcessSymbols().