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
30 #include "dbghelp_private.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
36 /******************************************************************
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;
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),
72 RtlImageRvaToVa(nth, (void*)mapping, stabstr, NULL),
78 static BOOL CALLBACK dbg_match(const char* file, void* user)
80 /* accept first file */
84 /******************************************************************
89 static BOOL pe_load_dbg_file(const struct process* pcs, struct module* module,
90 const char* dbg_name, DWORD timestamp)
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;
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",
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));
136 ERR("Wrong signature in .DBG file %s\n", debugstr_a(tmp));
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);
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)
157 const IMAGE_DATA_DIRECTORY* dir;
158 const IMAGE_DEBUG_DIRECTORY*dbg = NULL;
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));
183 ret = pe_load_dbg_file(pcs, module, (const char*)misc->Data, nth->FileHeader.TimeDateStamp);
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);
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)
205 const IMAGE_EXPORT_DIRECTORY* exports;
206 DWORD base = module->module.BaseOfImage;
209 if (dbghelp_options & SYMOPT_NO_PUBLICS) return TRUE;
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 */);
218 /* Add entry point */
219 symt_new_public(module, NULL, "EntryPoint",
220 base + nth->OptionalHeader.AddressOfEntryPoint, 1,
223 /* FIXME: we'd better store addresses linked to sections rather than
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 */);
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;
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 if (functions && ordinals && names)
253 for (i = 0; i < exports->NumberOfNames; i++)
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 */);
262 for (i = 0; i < exports->NumberOfFunctions; i++)
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 */);
275 /* no real debug info, only entry points */
276 if (module->module.SymType == SymDeferred)
277 module->module.SymType = SymExport;
281 /******************************************************************
285 BOOL pe_load_debug_info(const struct process* pcs, struct module* module)
291 IMAGE_NT_HEADERS* nth;
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)
298 if ((mapping = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) != NULL)
300 nth = RtlImageNtHeader(mapping);
302 if (!(dbghelp_options & SYMOPT_PUBLICS_ONLY))
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
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)
314 UnmapViewOfFile(mapping);
323 /******************************************************************
324 * pe_load_native_module
327 struct module* pe_load_native_module(struct process* pcs, const WCHAR* name,
328 HANDLE hFile, DWORD base, DWORD size)
330 struct module* module = NULL;
333 WCHAR loaded_name[MAX_PATH];
335 loaded_name[0] = '\0';
341 if ((hFile = FindExecutableImageExW(name, pcs->search_path, loaded_name, NULL, NULL)) == NULL)
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");
349 if ((hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) != NULL)
353 if ((mapping = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) != NULL)
355 IMAGE_NT_HEADERS* nth = RtlImageNtHeader(mapping);
359 if (!base) base = nth->OptionalHeader.ImageBase;
360 if (!size) size = nth->OptionalHeader.SizeOfImage;
362 module = module_new(pcs, loaded_name, DMT_PE, FALSE, base, size,
363 nth->FileHeader.TimeDateStamp,
364 nth->OptionalHeader.CheckSum);
367 if (dbghelp_options & SYMOPT_DEFERRED_LOADS)
368 module->module.SymType = SymDeferred;
370 pe_load_debug_info(pcs, module);
373 UnmapViewOfFile(mapping);
377 if (opened) CloseHandle(hFile);
382 /******************************************************************
386 BOOL pe_load_nt_header(HANDLE hProc, DWORD base, IMAGE_NT_HEADERS* nth)
388 IMAGE_DOS_HEADER dos;
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;
397 /******************************************************************
398 * pe_load_builtin_module
401 struct module* pe_load_builtin_module(struct process* pcs, const WCHAR* name,
402 DWORD base, DWORD size)
404 struct module* module = NULL;
406 if (base && pcs->dbg_hdr_addr)
408 IMAGE_NT_HEADERS nth;
410 if (pe_load_nt_header(pcs->handle, base, &nth))
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);