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
30 #include "dbghelp_private.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
37 /******************************************************************
40 * look for stabs information in PE header (it's how the mingw compiler provides
41 * its debugging information)
43 static SYM_TYPE 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 SYM_TYPE sym_type = SymNone;
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 sym_type = stabs_parse(module, mapping, module->module.BaseOfImage,
70 stabs, stabsize, stabstr, stabstrsize);
75 static BOOL CALLBACK dbg_match(char* file, void* user)
77 /* accept first file */
81 /******************************************************************
86 static SYM_TYPE pe_load_dbg_file(const struct process* pcs, struct module* module,
87 const char* dbg_name, DWORD timestamp)
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 SYM_TYPE sym_type = -1;
96 WINE_TRACE("Processing DBG file %s\n", dbg_name);
98 if (SymFindFileInPath(pcs->handle, NULL, (char*)dbg_name,
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",
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 sym_type = pe_load_debug_directory(pcs, module, dbg_mapping, dbg,
124 hdr->DebugDirectorySize / sizeof(*dbg));
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);
135 /******************************************************************
136 * pe_load_msc_debug_info
138 * Process MSC debug information in PE file.
140 static SYM_TYPE pe_load_msc_debug_info(const struct process* pcs,
141 struct module* module,
142 const void* mapping, IMAGE_NT_HEADERS* nth)
144 SYM_TYPE sym_type = -1;
145 const IMAGE_DATA_DIRECTORY* dir;
146 const IMAGE_DEBUG_DIRECTORY*dbg = NULL;
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 sym_type;
154 dbg = (const IMAGE_DEBUG_DIRECTORY*)((const char*)mapping + dir->VirtualAddress);
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);
171 sym_type = pe_load_dbg_file(pcs, module, misc->Data, nth->FileHeader.TimeDateStamp);
176 /* Debug info is embedded into PE module */
177 sym_type = pe_load_debug_directory(pcs, module, mapping, dbg, nDbg);
183 /***********************************************************************
184 * pe_load_export_debug_info
186 static SYM_TYPE pe_load_export_debug_info(const struct process* pcs,
187 struct module* module,
188 const void* mapping, IMAGE_NT_HEADERS* nth)
191 IMAGE_DATA_DIRECTORY* dir;
192 DWORD base = module->module.BaseOfImage;
195 addr.Mode = AddrModeFlat;
199 /* Add start of DLL (better use the (yet unimplemented) Exe SymTag for this) */
200 /* FIXME: module.ModuleName isn't correctly set yet if it's passed in SymLoadModule */
201 symt_new_public(module, NULL, module->module.ModuleName, base, 0,
202 TRUE /* FIXME */, TRUE /* FIXME */);
205 /* Add entry point */
206 symt_new_public(module, NULL, "EntryPoint",
207 base + nth->OptionalHeader.AddressOfEntryPoint, 0,
208 TRUE /* FIXME */, TRUE /* FIXME */);
211 /* FIXME: we'd better store addresses linked to sections rather than
213 IMAGE_SECTION_HEADER* section;
214 /* Add start of sections */
215 section = (IMAGE_SECTION_HEADER*)
216 ((char*)&nth->OptionalHeader + nth->FileHeader.SizeOfOptionalHeader);
217 for (i = 0; i < nth->FileHeader.NumberOfSections; i++, section++)
219 symt_new_public(module, NULL, section->Name, base + section->VirtualAddress, 0,
220 TRUE /* FIXME */, TRUE /* FIXME */);
224 /* Add exported functions */
225 if ((dir = RtlImageDirectoryEntryToData((void*)mapping, TRUE,
226 IMAGE_DIRECTORY_ENTRY_EXPORT, NULL)))
228 const IMAGE_EXPORT_DIRECTORY* exports;
229 const WORD* ordinals = NULL;
230 const void* const* functions = NULL;
231 const DWORD* names = NULL;
235 exports = (const void*)((const char*)mapping + dir->VirtualAddress);
236 functions = (const void*)((const char*)mapping + exports->AddressOfFunctions);
237 ordinals = (const void*)((const char*)mapping + exports->AddressOfNameOrdinals);
238 names = (const void*)((const char*)mapping + exports->AddressOfNames);
240 for (i = 0; i < exports->NumberOfNames; i++)
242 if (!names[i]) continue;
243 symt_new_public(module, NULL, (const char*)base + names[i],
244 base + (DWORD)functions[ordinals[i]], 0,
245 TRUE /* FIXME */, TRUE /* FIXME */);
248 for (i = 0; i < exports->NumberOfFunctions; i++)
250 if (!functions[i]) continue;
251 /* Check if we already added it with a name */
252 for (j = 0; j < exports->NumberOfNames; j++)
253 if ((ordinals[j] == i) && names[j]) break;
254 if (j < exports->NumberOfNames) continue;
255 snprintf(buffer, sizeof(buffer), "%ld", i + exports->Base);
256 symt_new_public(module, NULL, buffer, base + (DWORD)functions[i], 0,
257 TRUE /* FIXME */, TRUE /* FIXME */);
260 /* no real debug info, only entry points */
261 return module->module.SymType = SymExport;
264 /******************************************************************
268 SYM_TYPE pe_load_debug_info(const struct process* pcs, struct module* module)
270 SYM_TYPE sym_type = -1;
274 IMAGE_NT_HEADERS* nth;
276 hFile = CreateFileA(module->module.LoadedImageName, GENERIC_READ, FILE_SHARE_READ,
277 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
278 if (hFile == INVALID_HANDLE_VALUE) return -1;
279 if ((hMap = CreateFileMappingA(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) != 0)
281 if ((mapping = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) != NULL)
283 nth = RtlImageNtHeader(mapping);
285 if (!(dbghelp_options & SYMOPT_PUBLICS_ONLY))
287 sym_type = pe_load_stabs(pcs, module, mapping, nth);
288 if (sym_type <= SymNone)
289 sym_type = pe_load_msc_debug_info(pcs, module, mapping, nth);
290 /* if we still have no debug info (we could only get SymExport at this
291 * point), then do the SymExport except if we have an ELF container,
292 * in which case we'll rely on the export's on the ELF side
295 if (sym_type <= SymNone && !module_get_debug(pcs, module))
296 sym_type = pe_load_export_debug_info(pcs, module, mapping, nth);
297 UnmapViewOfFile(mapping);
303 module->module.SymType = (sym_type >= SymNone) ? sym_type : SymNone;
307 /******************************************************************
311 struct module* pe_load_module(struct process* pcs, char* name,
312 HANDLE hFile, DWORD base, DWORD size)
314 struct module* module = NULL;
318 char loaded_name[MAX_PATH];
320 loaded_name[0] = '\0';
325 /* FIXME SetLastError */
328 if ((hFile = FindExecutableImage(name, NULL, loaded_name)) == NULL)
332 else if (name) strcpy(loaded_name, name);
333 else if (dbghelp_options & SYMOPT_DEFERRED_LOADS)
334 FIXME("Trouble ahead (no module name passed in deferred mode)\n");
335 if (!(module = module_find_by_name(pcs, loaded_name, DMT_PE)) &&
336 (hMap = CreateFileMappingA(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) != NULL)
338 if ((mapping = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) != NULL)
340 IMAGE_NT_HEADERS* nth = RtlImageNtHeader(mapping);
344 if (!base) base = nth->OptionalHeader.ImageBase;
345 if (!size) size = nth->OptionalHeader.SizeOfImage;
347 module = module_new(pcs, loaded_name, DMT_PE, base, size,
348 nth->FileHeader.TimeDateStamp,
349 nth->OptionalHeader.CheckSum);
352 module->module.SymType = (dbghelp_options & SYMOPT_DEFERRED_LOADS) ?
353 SymDeferred : pe_load_debug_info(pcs, module);
356 UnmapViewOfFile(mapping);
360 if (opened) CloseHandle(hFile);
365 /******************************************************************
366 * pe_load_module_from_pcs
369 struct module* pe_load_module_from_pcs(struct process* pcs, const char* name,
370 const char* mod_name, DWORD base, DWORD size)
372 struct module* module;
375 if ((module = module_find_by_name(pcs, name, DMT_PE))) return module;
376 if (mod_name) ptr = mod_name;
379 for (ptr = name + strlen(name) - 1; ptr >= name; ptr--)
381 if (*ptr == '/' || *ptr == '\\')
388 if (ptr && (module = module_find_by_name(pcs, ptr, DMT_PE))) return module;
389 if (base && pcs->dbg_hdr_addr)
391 IMAGE_DOS_HEADER dos;
392 IMAGE_NT_HEADERS nth;
394 if (read_mem(pcs->handle, base, &dos, sizeof(dos)) &&
395 dos.e_magic == IMAGE_DOS_SIGNATURE &&
396 read_mem(pcs->handle, base + dos.e_lfanew, &nth, sizeof(nth)) &&
397 nth.Signature == IMAGE_NT_SIGNATURE)
399 if (!size) size = nth.OptionalHeader.SizeOfImage;
400 module = module_new(pcs, name, DMT_PE, base, size,
401 nth.FileHeader.TimeDateStamp, nth.OptionalHeader.CheckSum);