Create a branch for network fixes.
[reactos.git] / dll / win32 / dbghelp / pe_module.c
1 /*
2 * File pe_module.c - handle PE module information
3 *
4 * Copyright (C) 1996, Eric Youngdale.
5 * Copyright (C) 1999-2000, Ulrich Weigand.
6 * Copyright (C) 2004-2007, Eric Pouech.
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 *
22 */
23
24 #include "config.h"
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <assert.h>
29
30 #include "dbghelp_private.h"
31 #include "winternl.h"
32 #include "wine/debug.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
35
36 /******************************************************************
37 * pe_load_stabs
38 *
39 * look for stabs information in PE header (it's how the mingw compiler provides
40 * its debugging information)
41 */
42 static BOOL pe_load_stabs(const struct process* pcs, struct module* module,
43 const void* mapping, IMAGE_NT_HEADERS* nth)
44 {
45 IMAGE_SECTION_HEADER* section;
46 int i, stabsize = 0, stabstrsize = 0;
47 unsigned int stabs = 0, stabstr = 0;
48 BOOL ret = FALSE;
49
50 section = (IMAGE_SECTION_HEADER*)
51 ((char*)&nth->OptionalHeader + nth->FileHeader.SizeOfOptionalHeader);
52 for (i = 0; i < nth->FileHeader.NumberOfSections; i++, section++)
53 {
54 if (!strcasecmp((const char*)section->Name, ".stab"))
55 {
56 stabs = section->VirtualAddress;
57 stabsize = section->SizeOfRawData;
58 }
59 else if (!strncasecmp((const char*)section->Name, ".stabstr", 8))
60 {
61 stabstr = section->VirtualAddress;
62 stabstrsize = section->SizeOfRawData;
63 }
64 }
65
66 if (stabstrsize && stabsize)
67 {
68 ret = stabs_parse(module,
69 module->module.BaseOfImage - nth->OptionalHeader.ImageBase,
70 RtlImageRvaToVa(nth, (void*)mapping, stabs, NULL),
71 stabsize,
72 RtlImageRvaToVa(nth, (void*)mapping, stabstr, NULL),
73 stabstrsize);
74 }
75 return ret;
76 }
77
78 static BOOL CALLBACK dbg_match(const char* file, void* user)
79 {
80 /* accept first file */
81 return FALSE;
82 }
83
84 /******************************************************************
85 * pe_load_dbg_file
86 *
87 * loads a .dbg file
88 */
89 static BOOL pe_load_dbg_file(const struct process* pcs, struct module* module,
90 const char* dbg_name, DWORD timestamp)
91 {
92 char tmp[MAX_PATH];
93 HANDLE hFile = INVALID_HANDLE_VALUE, hMap = 0;
94 const BYTE* dbg_mapping = NULL;
95 const IMAGE_SEPARATE_DEBUG_HEADER* hdr;
96 const IMAGE_DEBUG_DIRECTORY* dbg;
97 BOOL ret = FALSE;
98
99 WINE_TRACE("Processing DBG file %s\n", dbg_name);
100
101 if (SymFindFileInPath(pcs->handle, NULL, dbg_name, NULL, 0, 0, 0, tmp, dbg_match, NULL) &&
102 (hFile = CreateFileA(tmp, GENERIC_READ, FILE_SHARE_READ, NULL,
103 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) != INVALID_HANDLE_VALUE &&
104 ((hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) != 0) &&
105 ((dbg_mapping = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) != NULL))
106 {
107 hdr = (const IMAGE_SEPARATE_DEBUG_HEADER*)dbg_mapping;
108 if (hdr->TimeDateStamp != timestamp)
109 {
110 WINE_ERR("Warning - %s has incorrect internal timestamp\n",
111 dbg_name);
112 /*
113 * Well, sometimes this happens to DBG files which ARE REALLY the
114 * right .DBG files but nonetheless this check fails. Anyway,
115 * WINDBG (debugger for Windows by Microsoft) loads debug symbols
116 * which have incorrect timestamps.
117 */
118 }
119 if (hdr->Signature == IMAGE_SEPARATE_DEBUG_SIGNATURE)
120 {
121 /* section headers come immediately after debug header */
122 const IMAGE_SECTION_HEADER *sectp =
123 (const IMAGE_SECTION_HEADER*)(hdr + 1);
124 /* and after that and the exported names comes the debug directory */
125 dbg = (const IMAGE_DEBUG_DIRECTORY*)
126 (dbg_mapping + sizeof(*hdr) +
127 hdr->NumberOfSections * sizeof(IMAGE_SECTION_HEADER) +
128 hdr->ExportedNamesSize);
129
130
131 ret = pe_load_debug_directory(pcs, module, dbg_mapping, sectp,
132 hdr->NumberOfSections, dbg,
133 hdr->DebugDirectorySize / sizeof(*dbg));
134 }
135 else
136 ERR("Wrong signature in .DBG file %s\n", debugstr_a(tmp));
137 }
138 else
139 WINE_ERR("-Unable to peruse .DBG file %s (%s)\n", dbg_name, debugstr_a(tmp));
140
141 if (dbg_mapping) UnmapViewOfFile(dbg_mapping);
142 if (hMap) CloseHandle(hMap);
143 if (hFile != INVALID_HANDLE_VALUE) CloseHandle(hFile);
144 return ret;
145 }
146
147 /******************************************************************
148 * pe_load_msc_debug_info
149 *
150 * Process MSC debug information in PE file.
151 */
152 static BOOL pe_load_msc_debug_info(const struct process* pcs,
153 struct module* module,
154 const void* mapping, IMAGE_NT_HEADERS* nth)
155 {
156 BOOL ret = FALSE;
157 const IMAGE_DATA_DIRECTORY* dir;
158 const IMAGE_DEBUG_DIRECTORY*dbg = NULL;
159 int nDbg;
160
161 /* Read in debug directory */
162 dir = nth->OptionalHeader.DataDirectory + IMAGE_DIRECTORY_ENTRY_DEBUG;
163 nDbg = dir->Size / sizeof(IMAGE_DEBUG_DIRECTORY);
164 if (!nDbg) return FALSE;
165
166 dbg = RtlImageRvaToVa(nth, (void*)mapping, dir->VirtualAddress, NULL);
167
168 /* Parse debug directory */
169 if (nth->FileHeader.Characteristics & IMAGE_FILE_DEBUG_STRIPPED)
170 {
171 /* Debug info is stripped to .DBG file */
172 const IMAGE_DEBUG_MISC* misc = (const IMAGE_DEBUG_MISC*)
173 ((const char*)mapping + dbg->PointerToRawData);
174
175 if (nDbg != 1 || dbg->Type != IMAGE_DEBUG_TYPE_MISC ||
176 misc->DataType != IMAGE_DEBUG_MISC_EXENAME)
177 {
178 WINE_ERR("-Debug info stripped, but no .DBG file in module %s\n",
179 debugstr_w(module->module.ModuleName));
180 }
181 else
182 {
183 ret = pe_load_dbg_file(pcs, module, (const char*)misc->Data, nth->FileHeader.TimeDateStamp);
184 }
185 }
186 else
187 {
188 const IMAGE_SECTION_HEADER *sectp = (const IMAGE_SECTION_HEADER*)((const char*)&nth->OptionalHeader + nth->FileHeader.SizeOfOptionalHeader);
189 /* Debug info is embedded into PE module */
190 ret = pe_load_debug_directory(pcs, module, mapping, sectp,
191 nth->FileHeader.NumberOfSections, dbg, nDbg);
192 }
193
194 return ret;
195 }
196
197 /***********************************************************************
198 * pe_load_export_debug_info
199 */
200 static BOOL pe_load_export_debug_info(const struct process* pcs,
201 struct module* module,
202 const void* mapping, IMAGE_NT_HEADERS* nth)
203 {
204 unsigned int i;
205 const IMAGE_EXPORT_DIRECTORY* exports;
206 DWORD base = module->module.BaseOfImage;
207 DWORD size;
208
209 if (dbghelp_options & SYMOPT_NO_PUBLICS) return TRUE;
210
211 #if 0
212 /* Add start of DLL (better use the (yet unimplemented) Exe SymTag for this) */
213 /* FIXME: module.ModuleName isn't correctly set yet if it's passed in SymLoadModule */
214 symt_new_public(module, NULL, module->module.ModuleName, base, 1,
215 TRUE /* FIXME */, TRUE /* FIXME */);
216 #endif
217
218 /* Add entry point */
219 symt_new_public(module, NULL, "EntryPoint",
220 base + nth->OptionalHeader.AddressOfEntryPoint, 1,
221 TRUE, TRUE);
222 #if 0
223 /* FIXME: we'd better store addresses linked to sections rather than
224 absolute values */
225 IMAGE_SECTION_HEADER* section;
226 /* Add start of sections */
227 section = (IMAGE_SECTION_HEADER*)
228 ((char*)&nth->OptionalHeader + nth->FileHeader.SizeOfOptionalHeader);
229 for (i = 0; i < nth->FileHeader.NumberOfSections; i++, section++)
230 {
231 symt_new_public(module, NULL, section->Name,
232 RtlImageRvaToVa(nth, (void*)mapping, section->VirtualAddress, NULL),
233 1, TRUE /* FIXME */, TRUE /* FIXME */);
234 }
235 #endif
236
237 /* Add exported functions */
238 if ((exports = RtlImageDirectoryEntryToData((void*)mapping, FALSE,
239 IMAGE_DIRECTORY_ENTRY_EXPORT, &size)))
240 {
241 const WORD* ordinals = NULL;
242 const DWORD_PTR* functions = NULL;
243 const DWORD* names = NULL;
244 unsigned int j;
245 char buffer[16];
246
247 functions = RtlImageRvaToVa(nth, (void*)mapping, exports->AddressOfFunctions, NULL);
248 ordinals = RtlImageRvaToVa(nth, (void*)mapping, exports->AddressOfNameOrdinals, NULL);
249 names = RtlImageRvaToVa(nth, (void*)mapping, exports->AddressOfNames, NULL);
250
251 if (functions && ordinals && names)
252 {
253 for (i = 0; i < exports->NumberOfNames; i++)
254 {
255 if (!names[i]) continue;
256 symt_new_public(module, NULL,
257 RtlImageRvaToVa(nth, (void*)mapping, names[i], NULL),
258 base + functions[ordinals[i]],
259 1, TRUE /* FIXME */, TRUE /* FIXME */);
260 }
261
262 for (i = 0; i < exports->NumberOfFunctions; i++)
263 {
264 if (!functions[i]) continue;
265 /* Check if we already added it with a name */
266 for (j = 0; j < exports->NumberOfNames; j++)
267 if ((ordinals[j] == i) && names[j]) break;
268 if (j < exports->NumberOfNames) continue;
269 snprintf(buffer, sizeof(buffer), "%d", i + exports->Base);
270 symt_new_public(module, NULL, buffer, base + (DWORD)functions[i], 1,
271 TRUE /* FIXME */, TRUE /* FIXME */);
272 }
273 }
274 }
275 /* no real debug info, only entry points */
276 if (module->module.SymType == SymDeferred)
277 module->module.SymType = SymExport;
278 return TRUE;
279 }
280
281 /******************************************************************
282 * pe_load_debug_info
283 *
284 */
285 BOOL pe_load_debug_info(const struct process* pcs, struct module* module)
286 {
287 BOOL ret = FALSE;
288 HANDLE hFile;
289 HANDLE hMap;
290 void* mapping;
291 IMAGE_NT_HEADERS* nth;
292
293 hFile = CreateFileW(module->module.LoadedImageName, GENERIC_READ, FILE_SHARE_READ,
294 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
295 if (hFile == INVALID_HANDLE_VALUE) return ret;
296 if ((hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) != 0)
297 {
298 if ((mapping = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) != NULL)
299 {
300 nth = RtlImageNtHeader(mapping);
301
302 if (!(dbghelp_options & SYMOPT_PUBLICS_ONLY))
303 {
304 ret = pe_load_stabs(pcs, module, mapping, nth) ||
305 pe_load_msc_debug_info(pcs, module, mapping, nth);
306 /* if we still have no debug info (we could only get SymExport at this
307 * point), then do the SymExport except if we have an ELF container,
308 * in which case we'll rely on the export's on the ELF side
309 */
310 }
311 /* FIXME shouldn't we check that? if (!module_get_debug(pcs, module))l */
312 if (pe_load_export_debug_info(pcs, module, mapping, nth) && !ret)
313 ret = TRUE;
314 UnmapViewOfFile(mapping);
315 }
316 CloseHandle(hMap);
317 }
318 CloseHandle(hFile);
319
320 return ret;
321 }
322
323 /******************************************************************
324 * pe_load_native_module
325 *
326 */
327 struct module* pe_load_native_module(struct process* pcs, const WCHAR* name,
328 HANDLE hFile, DWORD base, DWORD size)
329 {
330 struct module* module = NULL;
331 BOOL opened = FALSE;
332 HANDLE hMap;
333 WCHAR loaded_name[MAX_PATH];
334
335 loaded_name[0] = '\0';
336 if (!hFile)
337 {
338
339 assert(name);
340
341 if ((hFile = FindExecutableImageExW(name, pcs->search_path, loaded_name, NULL, NULL)) == NULL)
342 return NULL;
343 opened = TRUE;
344 }
345 else if (name) strcpyW(loaded_name, name);
346 else if (dbghelp_options & SYMOPT_DEFERRED_LOADS)
347 FIXME("Trouble ahead (no module name passed in deferred mode)\n");
348
349 if ((hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) != NULL)
350 {
351 void* mapping;
352
353 if ((mapping = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) != NULL)
354 {
355 IMAGE_NT_HEADERS* nth = RtlImageNtHeader(mapping);
356
357 if (nth)
358 {
359 if (!base) base = nth->OptionalHeader.ImageBase;
360 if (!size) size = nth->OptionalHeader.SizeOfImage;
361
362 module = module_new(pcs, loaded_name, DMT_PE, FALSE, base, size,
363 nth->FileHeader.TimeDateStamp,
364 nth->OptionalHeader.CheckSum);
365 if (module)
366 {
367 if (dbghelp_options & SYMOPT_DEFERRED_LOADS)
368 module->module.SymType = SymDeferred;
369 else
370 pe_load_debug_info(pcs, module);
371 }
372 }
373 UnmapViewOfFile(mapping);
374 }
375 CloseHandle(hMap);
376 }
377 if (opened) CloseHandle(hFile);
378
379 return module;
380 }
381
382 /******************************************************************
383 * pe_load_nt_header
384 *
385 */
386 BOOL pe_load_nt_header(HANDLE hProc, DWORD base, IMAGE_NT_HEADERS* nth)
387 {
388 IMAGE_DOS_HEADER dos;
389
390 return ReadProcessMemory(hProc, (char*)base, &dos, sizeof(dos), NULL) &&
391 dos.e_magic == IMAGE_DOS_SIGNATURE &&
392 ReadProcessMemory(hProc, (char*)(base + dos.e_lfanew),
393 nth, sizeof(*nth), NULL) &&
394 nth->Signature == IMAGE_NT_SIGNATURE;
395 }
396
397 /******************************************************************
398 * pe_load_builtin_module
399 *
400 */
401 struct module* pe_load_builtin_module(struct process* pcs, const WCHAR* name,
402 DWORD base, DWORD size)
403 {
404 struct module* module = NULL;
405
406 if (base && pcs->dbg_hdr_addr)
407 {
408 IMAGE_NT_HEADERS nth;
409
410 if (pe_load_nt_header(pcs->handle, base, &nth))
411 {
412 if (!size) size = nth.OptionalHeader.SizeOfImage;
413 module = module_new(pcs, name, DMT_PE, FALSE, base, size,
414 nth.FileHeader.TimeDateStamp,
415 nth.OptionalHeader.CheckSum);
416 }
417 }
418 return module;
419 }