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 <ntdef.h>
11 #include <reactos/rossym.h>
12 #include "rossympriv.h"
13 #include <ntimage.h>
14
15 #define NDEBUG
16 #include <debug.h>
17
18 BOOLEAN
RosSymCreateFromMem(PVOID ImageStart,ULONG_PTR ImageSize,PROSSYM_INFO * RosSymInfo)19 RosSymCreateFromMem(PVOID ImageStart, ULONG_PTR ImageSize, PROSSYM_INFO *RosSymInfo)
20 {
21 PIMAGE_DOS_HEADER DosHeader;
22 PIMAGE_NT_HEADERS NtHeaders;
23 PIMAGE_SECTION_HEADER SectionHeader;
24 ULONG SectionIndex;
25 BOOLEAN RosSymSectionFound = FALSE;
26 CHAR SectionName[IMAGE_SIZEOF_SHORT_NAME];
27
28 /* Check if MZ header is valid */
29 DosHeader = (PIMAGE_DOS_HEADER) ImageStart;
30 if (ImageSize < sizeof(IMAGE_DOS_HEADER)
31 || ! ROSSYM_IS_VALID_DOS_HEADER(DosHeader))
32 {
33 DPRINT1("Image doesn't have a valid DOS header\n");
34 return FALSE;
35 }
36
37 /* Locate NT header */
38 NtHeaders = (PIMAGE_NT_HEADERS)((char *) ImageStart + DosHeader->e_lfanew);
39 if (ImageSize < DosHeader->e_lfanew + sizeof(IMAGE_NT_HEADERS)
40 || ! ROSSYM_IS_VALID_NT_HEADERS(NtHeaders))
41 {
42 DPRINT1("Image doesn't have a valid PE header\n");
43 return FALSE;
44 }
45
46 /* Search for the section header */
47 SectionHeader = IMAGE_FIRST_SECTION(NtHeaders);
48 if (ImageSize < (ULONG_PTR)((char *) (SectionHeader + NtHeaders->FileHeader.NumberOfSections)
49 - (char *) ImageStart))
50 {
51 DPRINT1("Image doesn't have valid section headers\n");
52 return FALSE;
53 }
54 strncpy(SectionName, ROSSYM_SECTION_NAME, IMAGE_SIZEOF_SHORT_NAME);
55 for (SectionIndex = 0; SectionIndex < NtHeaders->FileHeader.NumberOfSections; SectionIndex++)
56 {
57 if (0 == memcmp(SectionName, SectionHeader->Name, IMAGE_SIZEOF_SHORT_NAME))
58 {
59 RosSymSectionFound = TRUE;
60 break;
61 }
62 SectionHeader++;
63 }
64
65 if (!RosSymSectionFound)
66 {
67 DPRINT("No %s section found\n", ROSSYM_SECTION_NAME);
68 return FALSE;
69 }
70
71 /* Locate the section itself */
72 if (ImageSize < SectionHeader->PointerToRawData + SectionHeader->SizeOfRawData
73 || SectionHeader->SizeOfRawData < sizeof(ROSSYM_HEADER))
74 {
75 DPRINT("Invalid %s section\n", ROSSYM_SECTION_NAME);
76 return FALSE;
77 }
78
79 if (SectionHeader->VirtualAddress + SectionHeader->Misc.VirtualSize > ImageSize)
80 {
81 DPRINT("Bad %s section virtual size!\n", ROSSYM_SECTION_NAME);
82 return FALSE;
83 }
84
85 /* Load it */
86 return RosSymCreateFromRaw((char *) ImageStart + SectionHeader->VirtualAddress,
87 SectionHeader->SizeOfRawData, RosSymInfo);
88 }
89
90 /* EOF */
91