Authors: Robert Shearman <rob@codeweavers.com>, Eric Pouech <pouech-eric@wanadoo.fr>
[wine/multimedia.git] / dlls / dbghelp / pe_module.c
blobf7bb142d9b80e672601a7e5c443a1adfe980f670
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, 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 "winreg.h"
32 #include "winternl.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
37 /******************************************************************
38 * pe_load_stabs
40 * look for stabs information in PE header (it's how the mingw compiler provides
41 * its debugging information)
43 static BOOL pe_load_stabs(const struct process* pcs, struct module* module,
44 const void* mapping, IMAGE_NT_HEADERS* nth)
46 IMAGE_SECTION_HEADER* section;
47 int i, stabsize = 0, stabstrsize = 0;
48 unsigned int stabs = 0, stabstr = 0;
49 BOOL ret = FALSE;
51 section = (IMAGE_SECTION_HEADER*)
52 ((char*)&nth->OptionalHeader + nth->FileHeader.SizeOfOptionalHeader);
53 for (i = 0; i < nth->FileHeader.NumberOfSections; i++, section++)
55 if (!strcasecmp(section->Name, ".stab"))
57 stabs = section->VirtualAddress;
58 stabsize = section->SizeOfRawData;
60 else if (!strncasecmp(section->Name, ".stabstr", 8))
62 stabstr = section->VirtualAddress;
63 stabstrsize = section->SizeOfRawData;
67 if (stabstrsize && stabsize)
69 ret = stabs_parse(module, mapping, module->module.BaseOfImage,
70 stabs, stabsize, stabstr, stabstrsize);
72 return ret;
75 static BOOL CALLBACK dbg_match(char* file, void* user)
77 /* accept first file */
78 return FALSE;
81 /******************************************************************
82 * pe_load_dbg_file
84 * loads a .dbg file
86 static BOOL pe_load_dbg_file(const struct process* pcs, struct module* module,
87 const char* dbg_name, DWORD timestamp)
89 char tmp[MAX_PATH];
90 HANDLE hFile = INVALID_HANDLE_VALUE, hMap = 0;
91 const BYTE* dbg_mapping = NULL;
92 const IMAGE_SEPARATE_DEBUG_HEADER* hdr;
93 const IMAGE_DEBUG_DIRECTORY* dbg;
94 BOOL ret = FALSE;
96 WINE_TRACE("Processing DBG file %s\n", dbg_name);
98 if (SymFindFileInPath(pcs->handle, NULL, (char*)dbg_name,
99 NULL, 0, 0, 0,
100 tmp, dbg_match, NULL) &&
101 (hFile = CreateFileA(tmp, GENERIC_READ, FILE_SHARE_READ, NULL,
102 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) != INVALID_HANDLE_VALUE &&
103 ((hMap = CreateFileMappingA(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) != 0) &&
104 ((dbg_mapping = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) != NULL))
106 hdr = (const IMAGE_SEPARATE_DEBUG_HEADER*)dbg_mapping;
107 if (hdr->TimeDateStamp != timestamp)
109 WINE_ERR("Warning - %s has incorrect internal timestamp\n",
110 dbg_name);
112 * Well, sometimes this happens to DBG files which ARE REALLY the
113 * right .DBG files but nonetheless this check fails. Anyway,
114 * WINDBG (debugger for Windows by Microsoft) loads debug symbols
115 * which have incorrect timestamps.
118 dbg = (const IMAGE_DEBUG_DIRECTORY*)
119 (dbg_mapping + sizeof(*hdr) +
120 hdr->NumberOfSections * sizeof(IMAGE_SECTION_HEADER) +
121 hdr->ExportedNamesSize);
123 ret = pe_load_debug_directory(pcs, module, dbg_mapping, dbg,
124 hdr->DebugDirectorySize / sizeof(*dbg));
126 else
127 WINE_ERR("-Unable to peruse .DBG file %s (%s)\n", dbg_name, debugstr_a(tmp));
129 if (dbg_mapping) UnmapViewOfFile((void*)dbg_mapping);
130 if (hMap) CloseHandle(hMap);
131 if (hFile != NULL) CloseHandle(hFile);
132 return ret;
135 /******************************************************************
136 * pe_load_msc_debug_info
138 * Process MSC debug information in PE file.
140 static BOOL pe_load_msc_debug_info(const struct process* pcs,
141 struct module* module,
142 const void* mapping, IMAGE_NT_HEADERS* nth)
144 BOOL ret = FALSE;
145 const IMAGE_DATA_DIRECTORY* dir;
146 const IMAGE_DEBUG_DIRECTORY*dbg = NULL;
147 int nDbg;
149 /* Read in debug directory */
150 dir = nth->OptionalHeader.DataDirectory + IMAGE_DIRECTORY_ENTRY_DEBUG;
151 nDbg = dir->Size / sizeof(IMAGE_DEBUG_DIRECTORY);
152 if (!nDbg) return FALSE;
154 dbg = RtlImageRvaToVa(nth, (void*)mapping, dir->VirtualAddress, NULL);
156 /* Parse debug directory */
157 if (nth->FileHeader.Characteristics & IMAGE_FILE_DEBUG_STRIPPED)
159 /* Debug info is stripped to .DBG file */
160 const IMAGE_DEBUG_MISC* misc = (const IMAGE_DEBUG_MISC*)
161 ((const char*)mapping + dbg->PointerToRawData);
163 if (nDbg != 1 || dbg->Type != IMAGE_DEBUG_TYPE_MISC ||
164 misc->DataType != IMAGE_DEBUG_MISC_EXENAME)
166 WINE_ERR("-Debug info stripped, but no .DBG file in module %s\n",
167 module->module.ModuleName);
169 else
171 ret = pe_load_dbg_file(pcs, module, misc->Data, nth->FileHeader.TimeDateStamp);
174 else
176 /* Debug info is embedded into PE module */
177 ret = pe_load_debug_directory(pcs, module, mapping, dbg, nDbg);
180 return ret;
183 /***********************************************************************
184 * pe_load_export_debug_info
186 static BOOL pe_load_export_debug_info(const struct process* pcs,
187 struct module* module,
188 const void* mapping, IMAGE_NT_HEADERS* nth)
190 unsigned int i;
191 const IMAGE_EXPORT_DIRECTORY* exports;
192 DWORD base = module->module.BaseOfImage;
193 DWORD size;
195 if (dbghelp_options & SYMOPT_NO_PUBLICS) return TRUE;
197 #if 0
198 /* Add start of DLL (better use the (yet unimplemented) Exe SymTag for this) */
199 /* FIXME: module.ModuleName isn't correctly set yet if it's passed in SymLoadModule */
200 symt_new_public(module, NULL, module->module.ModuleName, base, 0,
201 TRUE /* FIXME */, TRUE /* FIXME */);
202 #endif
204 /* Add entry point */
205 symt_new_public(module, NULL, "EntryPoint",
206 base + nth->OptionalHeader.AddressOfEntryPoint, 0,
207 TRUE, TRUE);
208 #if 0
209 /* FIXME: we'd better store addresses linked to sections rather than
210 absolute values */
211 IMAGE_SECTION_HEADER* section;
212 /* Add start of sections */
213 section = (IMAGE_SECTION_HEADER*)
214 ((char*)&nth->OptionalHeader + nth->FileHeader.SizeOfOptionalHeader);
215 for (i = 0; i < nth->FileHeader.NumberOfSections; i++, section++)
217 symt_new_public(module, NULL, section->Name,
218 RtlImageRvaToVa(nth, (void*)mapping, section->VirtualAddress, NULL),
219 0, TRUE /* FIXME */, TRUE /* FIXME */);
221 #endif
223 /* Add exported functions */
224 if ((exports = RtlImageDirectoryEntryToData((void*)mapping, FALSE,
225 IMAGE_DIRECTORY_ENTRY_EXPORT, &size)))
227 const WORD* ordinals = NULL;
228 const DWORD_PTR* functions = NULL;
229 const DWORD* names = NULL;
230 unsigned int j;
231 char buffer[16];
233 functions = RtlImageRvaToVa(nth, (void*)mapping, exports->AddressOfFunctions, NULL);
234 ordinals = RtlImageRvaToVa(nth, (void*)mapping, exports->AddressOfNameOrdinals, NULL);
235 names = RtlImageRvaToVa(nth, (void*)mapping, exports->AddressOfNames, NULL);
237 for (i = 0; i < exports->NumberOfNames; i++)
239 if (!names[i]) continue;
240 symt_new_public(module, NULL,
241 RtlImageRvaToVa(nth, (void*)mapping, names[i], NULL),
242 base + functions[ordinals[i]],
243 0, TRUE /* FIXME */, TRUE /* FIXME */);
246 for (i = 0; i < exports->NumberOfFunctions; i++)
248 if (!functions[i]) continue;
249 /* Check if we already added it with a name */
250 for (j = 0; j < exports->NumberOfNames; j++)
251 if ((ordinals[j] == i) && names[j]) break;
252 if (j < exports->NumberOfNames) continue;
253 snprintf(buffer, sizeof(buffer), "%ld", i + exports->Base);
254 symt_new_public(module, NULL, buffer, base + (DWORD)functions[i], 0,
255 TRUE /* FIXME */, TRUE /* FIXME */);
258 /* no real debug info, only entry points */
259 module->module.SymType = SymExport;
260 return TRUE;
263 /******************************************************************
264 * pe_load_debug_info
267 BOOL pe_load_debug_info(const struct process* pcs, struct module* module)
269 BOOL ret = FALSE;
270 HANDLE hFile;
271 HANDLE hMap;
272 void* mapping;
273 IMAGE_NT_HEADERS* nth;
275 hFile = CreateFileA(module->module.LoadedImageName, GENERIC_READ, FILE_SHARE_READ,
276 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
277 if (hFile == INVALID_HANDLE_VALUE) return ret;
278 if ((hMap = CreateFileMappingA(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) != 0)
280 if ((mapping = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) != NULL)
282 nth = RtlImageNtHeader(mapping);
284 if (!(dbghelp_options & SYMOPT_PUBLICS_ONLY))
286 ret = pe_load_stabs(pcs, module, mapping, nth) ||
287 pe_load_msc_debug_info(pcs, module, mapping, nth);
288 /* if we still have no debug info (we could only get SymExport at this
289 * point), then do the SymExport except if we have an ELF container,
290 * in which case we'll rely on the export's on the ELF side
293 if (pe_load_export_debug_info(pcs, module, mapping, nth) && !ret)
294 ret = TRUE;
295 UnmapViewOfFile(mapping);
297 CloseHandle(hMap);
299 CloseHandle(hFile);
301 return ret;
304 /******************************************************************
305 * pe_load_module
308 struct module* pe_load_module(struct process* pcs, char* name,
309 HANDLE hFile, DWORD base, DWORD size)
311 struct module* module = NULL;
312 BOOL opened = FALSE;
313 HANDLE hMap;
314 void* mapping;
315 char loaded_name[MAX_PATH];
317 loaded_name[0] = '\0';
318 if (!hFile)
320 if (!name)
322 /* FIXME SetLastError */
323 return NULL;
325 if ((hFile = FindExecutableImage(name, NULL, loaded_name)) == NULL)
326 return NULL;
327 opened = TRUE;
329 else if (name) strcpy(loaded_name, name);
330 else if (dbghelp_options & SYMOPT_DEFERRED_LOADS)
331 FIXME("Trouble ahead (no module name passed in deferred mode)\n");
332 if (!(module = module_find_by_name(pcs, loaded_name, DMT_PE)) &&
333 (hMap = CreateFileMappingA(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) != NULL)
335 if ((mapping = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) != NULL)
337 IMAGE_NT_HEADERS* nth = RtlImageNtHeader(mapping);
339 if (nth)
341 if (!base) base = nth->OptionalHeader.ImageBase;
342 if (!size) size = nth->OptionalHeader.SizeOfImage;
344 module = module_new(pcs, loaded_name, DMT_PE, base, size,
345 nth->FileHeader.TimeDateStamp,
346 nth->OptionalHeader.CheckSum);
347 if (module)
349 if (dbghelp_options & SYMOPT_DEFERRED_LOADS)
350 module->module.SymType = SymDeferred;
351 else
352 pe_load_debug_info(pcs, module);
355 UnmapViewOfFile(mapping);
357 CloseHandle(hMap);
359 if (opened) CloseHandle(hFile);
361 return module;
364 /******************************************************************
365 * pe_load_module_from_pcs
368 struct module* pe_load_module_from_pcs(struct process* pcs, const char* name,
369 const char* mod_name, DWORD base, DWORD size)
371 struct module* module;
372 const char* ptr;
374 if ((module = module_find_by_name(pcs, name, DMT_PE))) return module;
375 if (mod_name) ptr = mod_name;
376 else
378 for (ptr = name + strlen(name) - 1; ptr >= name; ptr--)
380 if (*ptr == '/' || *ptr == '\\')
382 ptr++;
383 break;
387 if (ptr && (module = module_find_by_name(pcs, ptr, DMT_PE))) return module;
388 if (base && pcs->dbg_hdr_addr)
390 IMAGE_DOS_HEADER dos;
391 IMAGE_NT_HEADERS nth;
393 if (read_mem(pcs->handle, base, &dos, sizeof(dos)) &&
394 dos.e_magic == IMAGE_DOS_SIGNATURE &&
395 read_mem(pcs->handle, base + dos.e_lfanew, &nth, sizeof(nth)) &&
396 nth.Signature == IMAGE_NT_SIGNATURE)
398 if (!size) size = nth.OptionalHeader.SizeOfImage;
399 module = module_new(pcs, name, DMT_PE, base, size,
400 nth.FileHeader.TimeDateStamp, nth.OptionalHeader.CheckSum);
403 return module;