dsound: Fix locking in mixer.
[wine.git] / dlls / dbghelp / pe_module.c
bloba42ade17815d1b4ebbff941784135f2466a43795
1 /*
2 * File pe_module.c - handle PE module information
4 * Copyright (C) 1996, Eric Youngdale.
5 * Copyright (C) 1999-2000, Ulrich Weigand.
6 * Copyright (C) 2004-2007, Eric Pouech.
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.
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.
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
24 #include "config.h"
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <assert.h>
30 #include "dbghelp_private.h"
31 #include "winternl.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
36 /******************************************************************
37 * pe_load_stabs
39 * look for stabs information in PE header (it's how the mingw compiler provides
40 * its debugging information)
42 static BOOL pe_load_stabs(const struct process* pcs, struct module* module,
43 const void* mapping, IMAGE_NT_HEADERS* nth)
45 IMAGE_SECTION_HEADER* section;
46 int i, stabsize = 0, stabstrsize = 0;
47 unsigned int stabs = 0, stabstr = 0;
48 BOOL ret = FALSE;
50 section = (IMAGE_SECTION_HEADER*)
51 ((char*)&nth->OptionalHeader + nth->FileHeader.SizeOfOptionalHeader);
52 for (i = 0; i < nth->FileHeader.NumberOfSections; i++, section++)
54 if (!strcasecmp((const char*)section->Name, ".stab"))
56 stabs = section->VirtualAddress;
57 stabsize = section->SizeOfRawData;
59 else if (!strncasecmp((const char*)section->Name, ".stabstr", 8))
61 stabstr = section->VirtualAddress;
62 stabstrsize = section->SizeOfRawData;
66 if (stabstrsize && stabsize)
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);
75 return ret;
78 static BOOL CALLBACK dbg_match(const char* file, void* user)
80 /* accept first file */
81 return FALSE;
84 /******************************************************************
85 * pe_load_dbg_file
87 * loads a .dbg file
89 static BOOL pe_load_dbg_file(const struct process* pcs, struct module* module,
90 const char* dbg_name, DWORD timestamp)
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;
99 WINE_TRACE("Processing DBG file %s\n", dbg_name);
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))
107 hdr = (const IMAGE_SEPARATE_DEBUG_HEADER*)dbg_mapping;
108 if (hdr->TimeDateStamp != timestamp)
110 WINE_ERR("Warning - %s has incorrect internal timestamp\n",
111 dbg_name);
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.
119 if (hdr->Signature == IMAGE_SEPARATE_DEBUG_SIGNATURE)
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);
131 ret = pe_load_debug_directory(pcs, module, dbg_mapping, sectp,
132 hdr->NumberOfSections, dbg,
133 hdr->DebugDirectorySize / sizeof(*dbg));
135 else
136 ERR("Wrong signature in .DBG file %s\n", debugstr_a(tmp));
138 else
139 WINE_ERR("-Unable to peruse .DBG file %s (%s)\n", dbg_name, debugstr_a(tmp));
141 if (dbg_mapping) UnmapViewOfFile(dbg_mapping);
142 if (hMap) CloseHandle(hMap);
143 if (hFile != INVALID_HANDLE_VALUE) CloseHandle(hFile);
144 return ret;
147 /******************************************************************
148 * pe_load_msc_debug_info
150 * Process MSC debug information in PE file.
152 static BOOL pe_load_msc_debug_info(const struct process* pcs,
153 struct module* module,
154 const void* mapping, IMAGE_NT_HEADERS* nth)
156 BOOL ret = FALSE;
157 const IMAGE_DATA_DIRECTORY* dir;
158 const IMAGE_DEBUG_DIRECTORY*dbg = NULL;
159 int nDbg;
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;
166 dbg = RtlImageRvaToVa(nth, (void*)mapping, dir->VirtualAddress, NULL);
168 /* Parse debug directory */
169 if (nth->FileHeader.Characteristics & IMAGE_FILE_DEBUG_STRIPPED)
171 /* Debug info is stripped to .DBG file */
172 const IMAGE_DEBUG_MISC* misc = (const IMAGE_DEBUG_MISC*)
173 ((const char*)mapping + dbg->PointerToRawData);
175 if (nDbg != 1 || dbg->Type != IMAGE_DEBUG_TYPE_MISC ||
176 misc->DataType != IMAGE_DEBUG_MISC_EXENAME)
178 WINE_ERR("-Debug info stripped, but no .DBG file in module %s\n",
179 debugstr_w(module->module.ModuleName));
181 else
183 ret = pe_load_dbg_file(pcs, module, (const char*)misc->Data, nth->FileHeader.TimeDateStamp);
186 else
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);
194 return ret;
197 /***********************************************************************
198 * pe_load_export_debug_info
200 static BOOL pe_load_export_debug_info(const struct process* pcs,
201 struct module* module,
202 const void* mapping, IMAGE_NT_HEADERS* nth)
204 unsigned int i;
205 const IMAGE_EXPORT_DIRECTORY* exports;
206 DWORD base = module->module.BaseOfImage;
207 DWORD size;
209 if (dbghelp_options & SYMOPT_NO_PUBLICS) return TRUE;
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
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++)
231 symt_new_public(module, NULL, section->Name,
232 RtlImageRvaToVa(nth, (void*)mapping, section->VirtualAddress, NULL),
233 1, TRUE /* FIXME */, TRUE /* FIXME */);
235 #endif
237 /* Add exported functions */
238 if ((exports = RtlImageDirectoryEntryToData((void*)mapping, FALSE,
239 IMAGE_DIRECTORY_ENTRY_EXPORT, &size)))
241 const WORD* ordinals = NULL;
242 const DWORD_PTR* functions = NULL;
243 const DWORD* names = NULL;
244 unsigned int j;
245 char buffer[16];
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);
251 for (i = 0; i < exports->NumberOfNames; i++)
253 if (!names[i]) continue;
254 symt_new_public(module, NULL,
255 RtlImageRvaToVa(nth, (void*)mapping, names[i], NULL),
256 base + functions[ordinals[i]],
257 1, TRUE /* FIXME */, TRUE /* FIXME */);
260 for (i = 0; i < exports->NumberOfFunctions; i++)
262 if (!functions[i]) continue;
263 /* Check if we already added it with a name */
264 for (j = 0; j < exports->NumberOfNames; j++)
265 if ((ordinals[j] == i) && names[j]) break;
266 if (j < exports->NumberOfNames) continue;
267 snprintf(buffer, sizeof(buffer), "%d", i + exports->Base);
268 symt_new_public(module, NULL, buffer, base + (DWORD)functions[i], 1,
269 TRUE /* FIXME */, TRUE /* FIXME */);
272 /* no real debug info, only entry points */
273 if (module->module.SymType == SymDeferred)
274 module->module.SymType = SymExport;
275 return TRUE;
278 /******************************************************************
279 * pe_load_debug_info
282 BOOL pe_load_debug_info(const struct process* pcs, struct module* module)
284 BOOL ret = FALSE;
285 HANDLE hFile;
286 HANDLE hMap;
287 void* mapping;
288 IMAGE_NT_HEADERS* nth;
290 hFile = CreateFileW(module->module.LoadedImageName, GENERIC_READ, FILE_SHARE_READ,
291 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
292 if (hFile == INVALID_HANDLE_VALUE) return ret;
293 if ((hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) != 0)
295 if ((mapping = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) != NULL)
297 nth = RtlImageNtHeader(mapping);
299 if (!(dbghelp_options & SYMOPT_PUBLICS_ONLY))
301 ret = pe_load_stabs(pcs, module, mapping, nth) ||
302 pe_load_msc_debug_info(pcs, module, mapping, nth);
303 /* if we still have no debug info (we could only get SymExport at this
304 * point), then do the SymExport except if we have an ELF container,
305 * in which case we'll rely on the export's on the ELF side
308 /* FIXME shouldn't we check that? if (!module_get_debug(pcs, module))l */
309 if (pe_load_export_debug_info(pcs, module, mapping, nth) && !ret)
310 ret = TRUE;
311 UnmapViewOfFile(mapping);
313 CloseHandle(hMap);
315 CloseHandle(hFile);
317 return ret;
320 /******************************************************************
321 * pe_load_native_module
324 struct module* pe_load_native_module(struct process* pcs, const WCHAR* name,
325 HANDLE hFile, DWORD base, DWORD size)
327 struct module* module = NULL;
328 BOOL opened = FALSE;
329 HANDLE hMap;
330 WCHAR loaded_name[MAX_PATH];
332 loaded_name[0] = '\0';
333 if (!hFile)
336 assert(name);
338 if ((hFile = FindExecutableImageExW(name, pcs->search_path, loaded_name, NULL, NULL)) == NULL)
339 return NULL;
340 opened = TRUE;
342 else if (name) strcpyW(loaded_name, name);
343 else if (dbghelp_options & SYMOPT_DEFERRED_LOADS)
344 FIXME("Trouble ahead (no module name passed in deferred mode)\n");
346 if ((hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) != NULL)
348 void* mapping;
350 if ((mapping = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) != NULL)
352 IMAGE_NT_HEADERS* nth = RtlImageNtHeader(mapping);
354 if (nth)
356 if (!base) base = nth->OptionalHeader.ImageBase;
357 if (!size) size = nth->OptionalHeader.SizeOfImage;
359 module = module_new(pcs, loaded_name, DMT_PE, FALSE, base, size,
360 nth->FileHeader.TimeDateStamp,
361 nth->OptionalHeader.CheckSum);
362 if (module)
364 if (dbghelp_options & SYMOPT_DEFERRED_LOADS)
365 module->module.SymType = SymDeferred;
366 else
367 pe_load_debug_info(pcs, module);
370 UnmapViewOfFile(mapping);
372 CloseHandle(hMap);
374 if (opened) CloseHandle(hFile);
376 return module;
379 /******************************************************************
380 * pe_load_nt_header
383 BOOL pe_load_nt_header(HANDLE hProc, DWORD base, IMAGE_NT_HEADERS* nth)
385 IMAGE_DOS_HEADER dos;
387 return ReadProcessMemory(hProc, (char*)base, &dos, sizeof(dos), NULL) &&
388 dos.e_magic == IMAGE_DOS_SIGNATURE &&
389 ReadProcessMemory(hProc, (char*)(base + dos.e_lfanew),
390 nth, sizeof(*nth), NULL) &&
391 nth->Signature == IMAGE_NT_SIGNATURE;
394 /******************************************************************
395 * pe_load_builtin_module
398 struct module* pe_load_builtin_module(struct process* pcs, const WCHAR* name,
399 DWORD base, DWORD size)
401 struct module* module = NULL;
403 if (base && pcs->dbg_hdr_addr)
405 IMAGE_NT_HEADERS nth;
407 if (pe_load_nt_header(pcs->handle, base, &nth))
409 if (!size) size = nth.OptionalHeader.SizeOfImage;
410 module = module_new(pcs, name, DMT_PE, FALSE, base, size,
411 nth.FileHeader.TimeDateStamp,
412 nth.OptionalHeader.CheckSum);
415 return module;