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