2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: lib/rossym/fromfile.c
5 * PURPOSE: Creating rossym info from a file
7 * PROGRAMMERS: Ge van Geldorp (gvg@reactos.com)
11 #include <ddk/ntddk.h>
12 #include <reactos/rossym.h>
13 #include "rossympriv.h"
19 RosSymCreateFromFile(PVOID FileContext
, PROSSYM_INFO
*RosSymInfo
)
21 IMAGE_DOS_HEADER DosHeader
;
22 IMAGE_NT_HEADERS NtHeaders
;
23 PIMAGE_SECTION_HEADER SectionHeaders
, SectionHeader
;
24 unsigned SectionIndex
;
25 char SectionName
[IMAGE_SIZEOF_SHORT_NAME
];
26 ROSSYM_HEADER RosSymHeader
;
29 if (! RosSymReadFile(FileContext
, &DosHeader
, sizeof(IMAGE_DOS_HEADER
)))
31 DPRINT1("Failed to read DOS header\n");
34 if (! ROSSYM_IS_VALID_DOS_HEADER(&DosHeader
))
36 DPRINT1("Image doesn't have a valid DOS header\n");
41 if (! RosSymSeekFile(FileContext
, DosHeader
.e_lfanew
))
43 DPRINT1("Failed seeking to NT headers\n");
46 if (! RosSymReadFile(FileContext
, &NtHeaders
, sizeof(IMAGE_NT_HEADERS
)))
48 DPRINT1("Failed to read NT headers\n");
51 if (! ROSSYM_IS_VALID_NT_HEADERS(&NtHeaders
))
53 DPRINT1("Image doesn't have a valid PE header\n");
57 /* Load section headers */
58 if (! RosSymSeekFile(FileContext
, (char *) IMAGE_FIRST_SECTION(&NtHeaders
) -
59 (char *) &NtHeaders
+ DosHeader
.e_lfanew
))
61 DPRINT1("Failed seeking to section headers\n");
64 SectionHeaders
= RosSymAllocMem(NtHeaders
.FileHeader
.NumberOfSections
65 * sizeof(IMAGE_SECTION_HEADER
));
66 if (NULL
== SectionHeaders
)
68 DPRINT1("Failed to allocate memory for %u section headers\n",
69 NtHeaders
.FileHeader
.NumberOfSections
);
72 if (! RosSymReadFile(FileContext
, SectionHeaders
,
73 NtHeaders
.FileHeader
.NumberOfSections
74 * sizeof(IMAGE_SECTION_HEADER
)))
76 RosSymFreeMem(SectionHeaders
);
77 DPRINT1("Failed to read section headers\n");
81 /* Search for the section header */
82 strncpy(SectionName
, ROSSYM_SECTION_NAME
, IMAGE_SIZEOF_SHORT_NAME
);
83 SectionHeader
= SectionHeaders
;
84 for (SectionIndex
= 0; SectionIndex
< NtHeaders
.FileHeader
.NumberOfSections
; SectionIndex
++)
86 if (0 == memcmp(SectionName
, SectionHeader
->Name
, IMAGE_SIZEOF_SHORT_NAME
))
92 if (NtHeaders
.FileHeader
.NumberOfSections
<= SectionIndex
)
94 RosSymFreeMem(SectionHeaders
);
95 DPRINT("No %s section found\n", ROSSYM_SECTION_NAME
);
99 /* Load rossym header */
100 if (! RosSymSeekFile(FileContext
, SectionHeader
->PointerToRawData
))
102 RosSymFreeMem(SectionHeaders
);
103 DPRINT1("Failed seeking to section data\n");
106 RosSymFreeMem(SectionHeaders
);
107 if (! RosSymReadFile(FileContext
, &RosSymHeader
, sizeof(ROSSYM_HEADER
)))
109 DPRINT1("Failed to read rossym header\n");
112 if (RosSymHeader
.SymbolsOffset
< sizeof(ROSSYM_HEADER
)
113 || RosSymHeader
.StringsOffset
< RosSymHeader
.SymbolsOffset
+ RosSymHeader
.SymbolsLength
114 || 0 != (RosSymHeader
.SymbolsLength
% sizeof(ROSSYM_ENTRY
)))
116 DPRINT1("Invalid ROSSYM_HEADER\n");
120 *RosSymInfo
= RosSymAllocMem(sizeof(ROSSYM_INFO
) - sizeof(ROSSYM_HEADER
)
121 + RosSymHeader
.StringsOffset
+ RosSymHeader
.StringsLength
+ 1);
122 if (NULL
== *RosSymInfo
)
124 DPRINT1("Failed to allocate memory for rossym\n");
127 (*RosSymInfo
)->Symbols
= (PROSSYM_ENTRY
)((char *) *RosSymInfo
+ sizeof(ROSSYM_INFO
)
128 - sizeof(ROSSYM_HEADER
) + RosSymHeader
.SymbolsOffset
);
129 (*RosSymInfo
)->SymbolsCount
= RosSymHeader
.SymbolsLength
/ sizeof(ROSSYM_ENTRY
);
130 (*RosSymInfo
)->Strings
= (PCHAR
) *RosSymInfo
+ sizeof(ROSSYM_INFO
) - sizeof(ROSSYM_HEADER
)
131 + RosSymHeader
.StringsOffset
;
132 (*RosSymInfo
)->StringsLength
= RosSymHeader
.StringsLength
;
133 if (! RosSymReadFile(FileContext
, *RosSymInfo
+ 1,
134 RosSymHeader
.StringsOffset
+ RosSymHeader
.StringsLength
135 - sizeof(ROSSYM_HEADER
)))
137 DPRINT1("Failed to read rossym headers\n");
140 /* Make sure the last string is null terminated, we allocated an extra byte for that */
141 (*RosSymInfo
)->Strings
[(*RosSymInfo
)->StringsLength
] = '\0';