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)
12 #include <reactos/rossym.h>
13 #include "rossympriv.h"
20 RosSymCreateFromFile(PVOID FileContext
, PROSSYM_INFO
*RosSymInfo
)
22 IMAGE_DOS_HEADER DosHeader
;
23 IMAGE_NT_HEADERS NtHeaders
;
24 PIMAGE_SECTION_HEADER SectionHeaders
, SectionHeader
;
25 unsigned SectionIndex
;
26 char SectionName
[IMAGE_SIZEOF_SHORT_NAME
];
27 ROSSYM_HEADER RosSymHeader
;
30 if (! RosSymReadFile(FileContext
, &DosHeader
, sizeof(IMAGE_DOS_HEADER
)))
32 DPRINT1("Failed to read DOS header\n");
35 if (! ROSSYM_IS_VALID_DOS_HEADER(&DosHeader
))
37 DPRINT1("Image doesn't have a valid DOS header\n");
42 if (! RosSymSeekFile(FileContext
, DosHeader
.e_lfanew
))
44 DPRINT1("Failed seeking to NT headers\n");
47 if (! RosSymReadFile(FileContext
, &NtHeaders
, sizeof(IMAGE_NT_HEADERS
)))
49 DPRINT1("Failed to read NT headers\n");
52 if (! ROSSYM_IS_VALID_NT_HEADERS(&NtHeaders
))
54 DPRINT1("Image doesn't have a valid PE header\n");
58 /* Load section headers */
59 if (! RosSymSeekFile(FileContext
, (char *) IMAGE_FIRST_SECTION(&NtHeaders
) -
60 (char *) &NtHeaders
+ DosHeader
.e_lfanew
))
62 DPRINT1("Failed seeking to section headers\n");
65 SectionHeaders
= RosSymAllocMem(NtHeaders
.FileHeader
.NumberOfSections
66 * sizeof(IMAGE_SECTION_HEADER
));
67 if (NULL
== SectionHeaders
)
69 DPRINT1("Failed to allocate memory for %u section headers\n",
70 NtHeaders
.FileHeader
.NumberOfSections
);
73 if (! RosSymReadFile(FileContext
, SectionHeaders
,
74 NtHeaders
.FileHeader
.NumberOfSections
75 * sizeof(IMAGE_SECTION_HEADER
)))
77 RosSymFreeMem(SectionHeaders
);
78 DPRINT1("Failed to read section headers\n");
82 /* Search for the section header */
83 strncpy(SectionName
, ROSSYM_SECTION_NAME
, IMAGE_SIZEOF_SHORT_NAME
);
84 SectionHeader
= SectionHeaders
;
85 for (SectionIndex
= 0; SectionIndex
< NtHeaders
.FileHeader
.NumberOfSections
; SectionIndex
++)
87 if (0 == memcmp(SectionName
, SectionHeader
->Name
, IMAGE_SIZEOF_SHORT_NAME
))
93 if (NtHeaders
.FileHeader
.NumberOfSections
<= SectionIndex
)
95 RosSymFreeMem(SectionHeaders
);
96 DPRINT("No %s section found\n", ROSSYM_SECTION_NAME
);
100 /* Load rossym header */
101 if (! RosSymSeekFile(FileContext
, SectionHeader
->PointerToRawData
))
103 RosSymFreeMem(SectionHeaders
);
104 DPRINT1("Failed seeking to section data\n");
107 RosSymFreeMem(SectionHeaders
);
108 if (! RosSymReadFile(FileContext
, &RosSymHeader
, sizeof(ROSSYM_HEADER
)))
110 DPRINT1("Failed to read rossym header\n");
113 if (RosSymHeader
.SymbolsOffset
< sizeof(ROSSYM_HEADER
)
114 || RosSymHeader
.StringsOffset
< RosSymHeader
.SymbolsOffset
+ RosSymHeader
.SymbolsLength
115 || 0 != (RosSymHeader
.SymbolsLength
% sizeof(ROSSYM_ENTRY
)))
117 DPRINT1("Invalid ROSSYM_HEADER\n");
121 *RosSymInfo
= RosSymAllocMem(sizeof(ROSSYM_INFO
) - sizeof(ROSSYM_HEADER
)
122 + RosSymHeader
.StringsOffset
+ RosSymHeader
.StringsLength
+ 1);
123 if (NULL
== *RosSymInfo
)
125 DPRINT1("Failed to allocate memory for rossym\n");
128 (*RosSymInfo
)->Symbols
= (PROSSYM_ENTRY
)((char *) *RosSymInfo
+ sizeof(ROSSYM_INFO
)
129 - sizeof(ROSSYM_HEADER
) + RosSymHeader
.SymbolsOffset
);
130 (*RosSymInfo
)->SymbolsCount
= RosSymHeader
.SymbolsLength
/ sizeof(ROSSYM_ENTRY
);
131 (*RosSymInfo
)->Strings
= (PCHAR
) *RosSymInfo
+ sizeof(ROSSYM_INFO
) - sizeof(ROSSYM_HEADER
)
132 + RosSymHeader
.StringsOffset
;
133 (*RosSymInfo
)->StringsLength
= RosSymHeader
.StringsLength
;
134 if (! RosSymReadFile(FileContext
, *RosSymInfo
+ 1,
135 RosSymHeader
.StringsOffset
+ RosSymHeader
.StringsLength
136 - sizeof(ROSSYM_HEADER
)))
138 DPRINT1("Failed to read rossym headers\n");
141 /* Make sure the last string is null terminated, we allocated an extra byte for that */
142 (*RosSymInfo
)->Strings
[(*RosSymInfo
)->StringsLength
] = '\0';