- Load symbols for NTOSKRNL and HAL as 1st and 2nd entries in LoadOrderList in LPB.
[reactos.git] / reactos / lib / rossym / frommem.c
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 #define NTOSAPI
11 #include <ntddk.h>
12 #include <reactos/rossym.h>
13 #include "rossympriv.h"
14 #ifdef _MSC_VER
15 #include "ntimage.h"
16 #endif
17
18 #define NDEBUG
19 #include <debug.h>
20
21 BOOLEAN
22 RosSymCreateFromMem(PVOID ImageStart, ULONG_PTR ImageSize, PROSSYM_INFO *RosSymInfo)
23 {
24 PIMAGE_DOS_HEADER DosHeader;
25 PIMAGE_NT_HEADERS NtHeaders;
26 PIMAGE_SECTION_HEADER SectionHeader;
27 unsigned SectionIndex;
28 char SectionName[IMAGE_SIZEOF_SHORT_NAME];
29
30 /* Check if MZ header is valid */
31 DosHeader = (PIMAGE_DOS_HEADER) ImageStart;
32 if (ImageSize < sizeof(IMAGE_DOS_HEADER)
33 || ! ROSSYM_IS_VALID_DOS_HEADER(DosHeader))
34 {
35 DPRINT1("Image doesn't have a valid DOS header\n");
36 return FALSE;
37 }
38
39 /* Locate NT header */
40 NtHeaders = (PIMAGE_NT_HEADERS)((char *) ImageStart + DosHeader->e_lfanew);
41 if (ImageSize < DosHeader->e_lfanew + sizeof(IMAGE_NT_HEADERS)
42 || ! ROSSYM_IS_VALID_NT_HEADERS(NtHeaders))
43 {
44 DPRINT1("Image doesn't have a valid PE header\n");
45 return FALSE;
46 }
47
48 /* Search for the section header */
49 SectionHeader = IMAGE_FIRST_SECTION(NtHeaders);
50 if (ImageSize < (ULONG_PTR)((char *) (SectionHeader + NtHeaders->FileHeader.NumberOfSections)
51 - (char *) ImageStart))
52 {
53 DPRINT1("Image doesn't have valid section headers\n");
54 return FALSE;
55 }
56 strncpy(SectionName, ROSSYM_SECTION_NAME, IMAGE_SIZEOF_SHORT_NAME);
57 for (SectionIndex = 0; SectionIndex < NtHeaders->FileHeader.NumberOfSections; SectionIndex++)
58 {
59 if (0 == memcmp(SectionName, SectionHeader->Name, IMAGE_SIZEOF_SHORT_NAME))
60 {
61 break;
62 }
63 SectionHeader++;
64 }
65 if (NtHeaders->FileHeader.NumberOfSections <= SectionIndex)
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 /* Load it */
80 return RosSymCreateFromRaw((char *) ImageStart + SectionHeader->VirtualAddress,
81 SectionHeader->SizeOfRawData, RosSymInfo);
82 }
83
84 /* EOF */