cmd: DIR command outputs free space for the path.
[wine.git] / dlls / dbghelp / pe_module.c
blob6c07bf5826a6a20a9aeaa23e3f18516bf414fdae
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 <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <assert.h>
29 #include "ntstatus.h"
30 #define WIN32_NO_STATUS
31 #include "dbghelp_private.h"
32 #include "image_private.h"
33 #include "winternl.h"
34 #include "wine/debug.h"
35 #include "wine/heap.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
39 struct pe_module_info
41 struct image_file_map fmap;
44 static const char builtin_signature[] = "Wine builtin DLL";
46 static void* pe_map_full(struct image_file_map* fmap, IMAGE_NT_HEADERS** nth)
48 if (!fmap->u.pe.full_map)
50 fmap->u.pe.full_map = MapViewOfFile(fmap->u.pe.hMap, FILE_MAP_READ, 0, 0, 0);
52 if (fmap->u.pe.full_map)
54 if (nth) *nth = RtlImageNtHeader(fmap->u.pe.full_map);
55 fmap->u.pe.full_count++;
56 return fmap->u.pe.full_map;
58 return NULL;
61 static void pe_unmap_full(struct image_file_map* fmap)
63 if (fmap->u.pe.full_count && !--fmap->u.pe.full_count)
65 UnmapViewOfFile(fmap->u.pe.full_map);
66 fmap->u.pe.full_map = NULL;
70 /* as we store either IMAGE_OPTIONAL_HEADER(32|64) inside pe_file_map,
71 * this helper will read to any field 'field' inside such an header
73 #define PE_FROM_OPTHDR(fmap, field) (((fmap)->addr_size == 32) ? ((fmap)->u.pe.opt.header32. field) : ((fmap)->u.pe.opt.header64. field))
75 /******************************************************************
76 * pe_map_section
78 * Maps a single section into memory from an PE file
80 static const char* pe_map_section(struct image_section_map* ism)
82 void* mapping;
83 struct pe_file_map* fmap = &ism->fmap->u.pe;
85 if (ism->sidx >= 0 && ism->sidx < fmap->file_header.NumberOfSections &&
86 fmap->sect[ism->sidx].mapped == IMAGE_NO_MAP)
88 IMAGE_NT_HEADERS* nth;
90 if (fmap->sect[ism->sidx].shdr.Misc.VirtualSize > fmap->sect[ism->sidx].shdr.SizeOfRawData)
92 FIXME("Section %Id: virtual (0x%lx) > raw (0x%lx) size - not supported\n",
93 ism->sidx, fmap->sect[ism->sidx].shdr.Misc.VirtualSize,
94 fmap->sect[ism->sidx].shdr.SizeOfRawData);
95 return IMAGE_NO_MAP;
97 /* FIXME: that's rather drastic, but that will do for now
98 * that's ok if the full file map exists, but we could be less aggressive otherwise and
99 * only map the relevant section
101 if ((mapping = pe_map_full(ism->fmap, &nth)))
103 fmap->sect[ism->sidx].mapped = RtlImageRvaToVa(nth, mapping,
104 fmap->sect[ism->sidx].shdr.VirtualAddress,
105 NULL);
106 return fmap->sect[ism->sidx].mapped;
109 return IMAGE_NO_MAP;
112 /******************************************************************
113 * pe_find_section
115 * Finds a section by name (and type) into memory from an PE file
116 * or its alternate if any
118 static BOOL pe_find_section(struct image_file_map* fmap, const char* name,
119 struct image_section_map* ism)
121 const char* sectname;
122 unsigned i;
123 char tmp[IMAGE_SIZEOF_SHORT_NAME + 1];
125 for (i = 0; i < fmap->u.pe.file_header.NumberOfSections; i++)
127 sectname = (const char*)fmap->u.pe.sect[i].shdr.Name;
128 /* long section names start with a '/' (at least on MinGW32) */
129 if (sectname[0] == '/' && fmap->u.pe.strtable)
130 sectname = fmap->u.pe.strtable + atoi(sectname + 1);
131 else
133 /* the section name may not be null terminated */
134 sectname = memcpy(tmp, sectname, IMAGE_SIZEOF_SHORT_NAME);
135 tmp[IMAGE_SIZEOF_SHORT_NAME] = '\0';
137 if (!stricmp(sectname, name))
139 ism->fmap = fmap;
140 ism->sidx = i;
141 return TRUE;
144 ism->fmap = NULL;
145 ism->sidx = -1;
147 return FALSE;
150 /******************************************************************
151 * pe_unmap_section
153 * Unmaps a single section from memory
155 static void pe_unmap_section(struct image_section_map* ism)
157 if (ism->sidx >= 0 && ism->sidx < ism->fmap->u.pe.file_header.NumberOfSections &&
158 ism->fmap->u.pe.sect[ism->sidx].mapped != IMAGE_NO_MAP)
160 pe_unmap_full(ism->fmap);
161 ism->fmap->u.pe.sect[ism->sidx].mapped = IMAGE_NO_MAP;
165 /******************************************************************
166 * pe_get_map_rva
168 * Get the RVA of an PE section
170 static DWORD_PTR pe_get_map_rva(const struct image_section_map* ism)
172 if (ism->sidx < 0 || ism->sidx >= ism->fmap->u.pe.file_header.NumberOfSections)
173 return 0;
174 return ism->fmap->u.pe.sect[ism->sidx].shdr.VirtualAddress;
177 /******************************************************************
178 * pe_get_map_size
180 * Get the size of a PE section
182 static unsigned pe_get_map_size(const struct image_section_map* ism)
184 if (ism->sidx < 0 || ism->sidx >= ism->fmap->u.pe.file_header.NumberOfSections)
185 return 0;
186 return ism->fmap->u.pe.sect[ism->sidx].shdr.Misc.VirtualSize;
189 /******************************************************************
190 * pe_unmap_file
192 * Unmaps an PE file from memory (previously mapped with pe_map_file)
194 static void pe_unmap_file(struct image_file_map* fmap)
196 if (fmap->u.pe.hMap != 0)
198 struct image_section_map ism;
199 ism.fmap = fmap;
200 for (ism.sidx = 0; ism.sidx < fmap->u.pe.file_header.NumberOfSections; ism.sidx++)
202 pe_unmap_section(&ism);
204 while (fmap->u.pe.full_count) pe_unmap_full(fmap);
205 HeapFree(GetProcessHeap(), 0, fmap->u.pe.sect);
206 HeapFree(GetProcessHeap(), 0, (void*)fmap->u.pe.strtable); /* FIXME ugly (see pe_map_file) */
207 CloseHandle(fmap->u.pe.hMap);
208 fmap->u.pe.hMap = NULL;
212 static const struct image_file_map_ops pe_file_map_ops =
214 pe_map_section,
215 pe_unmap_section,
216 pe_find_section,
217 pe_get_map_rva,
218 pe_get_map_size,
219 pe_unmap_file,
222 /******************************************************************
223 * pe_is_valid_pointer_table
225 * Checks whether the PointerToSymbolTable and NumberOfSymbols in file_header contain
226 * valid information.
228 static BOOL pe_is_valid_pointer_table(const IMAGE_NT_HEADERS* nthdr, const void* mapping, DWORD64 sz)
230 DWORD64 offset;
232 /* is the iSym table inside file size ? (including first DWORD of string table, which is its size) */
233 offset = (DWORD64)nthdr->FileHeader.PointerToSymbolTable;
234 offset += (DWORD64)nthdr->FileHeader.NumberOfSymbols * sizeof(IMAGE_SYMBOL);
235 if (offset + sizeof(DWORD) > sz) return FALSE;
236 /* is string table (following iSym table) inside file size ? */
237 offset += *(DWORD*)((const char*)mapping + offset);
238 return offset <= sz;
241 /******************************************************************
242 * pe_map_file
244 * Maps an PE file into memory (and checks it's a real PE file)
246 BOOL pe_map_file(HANDLE file, struct image_file_map* fmap, enum module_type mt)
248 void* mapping;
250 fmap->modtype = mt;
251 fmap->ops = &pe_file_map_ops;
252 fmap->alternate = NULL;
253 fmap->u.pe.hMap = CreateFileMappingW(file, NULL, PAGE_READONLY, 0, 0, NULL);
254 if (fmap->u.pe.hMap == 0) return FALSE;
255 fmap->u.pe.full_count = 0;
256 fmap->u.pe.full_map = NULL;
257 if (!(mapping = pe_map_full(fmap, NULL))) goto error;
259 switch (mt)
261 case DMT_PE:
263 IMAGE_NT_HEADERS* nthdr;
264 IMAGE_SECTION_HEADER* section;
265 unsigned i;
267 if (!(nthdr = RtlImageNtHeader(mapping))) goto error;
268 memcpy(&fmap->u.pe.file_header, &nthdr->FileHeader, sizeof(fmap->u.pe.file_header));
269 switch (nthdr->OptionalHeader.Magic)
271 case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
272 fmap->addr_size = 32;
273 memcpy(&fmap->u.pe.opt.header32, &nthdr->OptionalHeader, sizeof(fmap->u.pe.opt.header32));
274 break;
275 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
276 if (sizeof(void*) == 4) return FALSE;
277 fmap->addr_size = 64;
278 memcpy(&fmap->u.pe.opt.header64, &nthdr->OptionalHeader, sizeof(fmap->u.pe.opt.header64));
279 break;
280 default:
281 return FALSE;
284 fmap->u.pe.builtin = !memcmp((const IMAGE_DOS_HEADER*)mapping + 1, builtin_signature, sizeof(builtin_signature));
285 section = (IMAGE_SECTION_HEADER*)
286 ((char*)&nthdr->OptionalHeader + nthdr->FileHeader.SizeOfOptionalHeader);
287 fmap->u.pe.sect = HeapAlloc(GetProcessHeap(), 0,
288 nthdr->FileHeader.NumberOfSections * sizeof(fmap->u.pe.sect[0]));
289 if (!fmap->u.pe.sect) goto error;
290 for (i = 0; i < nthdr->FileHeader.NumberOfSections; i++)
292 memcpy(&fmap->u.pe.sect[i].shdr, section + i, sizeof(IMAGE_SECTION_HEADER));
293 fmap->u.pe.sect[i].mapped = IMAGE_NO_MAP;
295 if (nthdr->FileHeader.PointerToSymbolTable && nthdr->FileHeader.NumberOfSymbols)
297 LARGE_INTEGER li;
299 if (GetFileSizeEx(file, &li) && pe_is_valid_pointer_table(nthdr, mapping, li.QuadPart))
301 /* FIXME ugly: should rather map the relevant content instead of copying it */
302 const char* src = (const char*)mapping +
303 nthdr->FileHeader.PointerToSymbolTable +
304 nthdr->FileHeader.NumberOfSymbols * sizeof(IMAGE_SYMBOL);
305 char* dst;
306 DWORD sz = *(DWORD*)src;
308 if ((dst = HeapAlloc(GetProcessHeap(), 0, sz)))
309 memcpy(dst, src, sz);
310 fmap->u.pe.strtable = dst;
312 else
314 WARN("Bad coff table... wipping out\n");
315 /* we have bad information here, wipe it out */
316 fmap->u.pe.file_header.PointerToSymbolTable = 0;
317 fmap->u.pe.file_header.NumberOfSymbols = 0;
318 fmap->u.pe.strtable = NULL;
321 else fmap->u.pe.strtable = NULL;
323 break;
324 default: assert(0); goto error;
326 pe_unmap_full(fmap);
328 return TRUE;
329 error:
330 pe_unmap_full(fmap);
331 CloseHandle(fmap->u.pe.hMap);
332 return FALSE;
335 /******************************************************************
336 * pe_map_directory
338 * Maps a directory content out of a PE file
340 const char* pe_map_directory(struct module* module, int dirno, DWORD* size)
342 IMAGE_NT_HEADERS* nth;
343 void* mapping;
345 if (module->type != DMT_PE || !module->format_info[DFI_PE]) return NULL;
346 if (dirno >= IMAGE_NUMBEROF_DIRECTORY_ENTRIES ||
347 !(mapping = pe_map_full(&module->format_info[DFI_PE]->u.pe_info->fmap, &nth)))
348 return NULL;
349 if (size) *size = nth->OptionalHeader.DataDirectory[dirno].Size;
350 return RtlImageRvaToVa(nth, mapping,
351 nth->OptionalHeader.DataDirectory[dirno].VirtualAddress, NULL);
354 static void pe_module_remove(struct process* pcs, struct module_format* modfmt)
356 image_unmap_file(&modfmt->u.pe_info->fmap);
357 HeapFree(GetProcessHeap(), 0, modfmt);
360 /******************************************************************
361 * pe_locate_with_coff_symbol_table
363 * Use the COFF symbol table (if any) from the IMAGE_FILE_HEADER to set the absolute address
364 * of global symbols.
365 * Mingw32 requires this for stabs debug information as address for global variables isn't filled in
366 * (this is similar to what is done in elf_module.c when using the .symtab ELF section)
368 static BOOL pe_locate_with_coff_symbol_table(struct module* module)
370 struct image_file_map* fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
371 const IMAGE_SYMBOL* isym;
372 int i, numsym, naux;
373 char tmp[9];
374 const char* name;
375 struct hash_table_iter hti;
376 void* ptr;
377 struct symt_data* sym;
378 const char* mapping;
380 numsym = fmap->u.pe.file_header.NumberOfSymbols;
381 if (!fmap->u.pe.file_header.PointerToSymbolTable || !numsym)
382 return TRUE;
383 if (!(mapping = pe_map_full(fmap, NULL))) return FALSE;
384 isym = (const IMAGE_SYMBOL*)(mapping + fmap->u.pe.file_header.PointerToSymbolTable);
386 for (i = 0; i < numsym; i+= naux, isym += naux)
388 if (isym->StorageClass == IMAGE_SYM_CLASS_EXTERNAL &&
389 isym->SectionNumber > 0 && isym->SectionNumber <= fmap->u.pe.file_header.NumberOfSections)
391 if (isym->N.Name.Short)
393 name = memcpy(tmp, isym->N.ShortName, 8);
394 tmp[8] = '\0';
396 else name = fmap->u.pe.strtable + isym->N.Name.Long;
397 if (name[0] == '_') name++;
398 hash_table_iter_init(&module->ht_symbols, &hti, name);
399 while ((ptr = hash_table_iter_up(&hti)))
401 sym = CONTAINING_RECORD(ptr, struct symt_data, hash_elt);
402 if (sym->symt.tag == SymTagData &&
403 (sym->kind == DataIsGlobal || sym->kind == DataIsFileStatic) &&
404 sym->u.var.kind == loc_absolute &&
405 !strcmp(sym->hash_elt.name, name))
407 TRACE("Changing absolute address for %d.%s: %Ix -> %I64x\n",
408 isym->SectionNumber, name, sym->u.var.offset,
409 module->module.BaseOfImage +
410 fmap->u.pe.sect[isym->SectionNumber - 1].shdr.VirtualAddress +
411 isym->Value);
412 sym->u.var.offset = module->module.BaseOfImage +
413 fmap->u.pe.sect[isym->SectionNumber - 1].shdr.VirtualAddress + isym->Value;
414 break;
418 naux = isym->NumberOfAuxSymbols + 1;
420 pe_unmap_full(fmap);
421 return TRUE;
424 /******************************************************************
425 * pe_load_coff_symbol_table
427 * Load public symbols out of the COFF symbol table (if any).
429 static BOOL pe_load_coff_symbol_table(struct module* module)
431 struct image_file_map* fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
432 const IMAGE_SYMBOL* isym;
433 int i, numsym, naux;
434 const char* strtable;
435 char tmp[9];
436 const char* name;
437 const char* lastfilename = NULL;
438 struct symt_compiland* compiland = NULL;
439 const IMAGE_SECTION_HEADER* sect;
440 const char* mapping;
442 numsym = fmap->u.pe.file_header.NumberOfSymbols;
443 if (!fmap->u.pe.file_header.PointerToSymbolTable || !numsym)
444 return TRUE;
445 if (!(mapping = pe_map_full(fmap, NULL))) return FALSE;
446 isym = (const IMAGE_SYMBOL*)(mapping + fmap->u.pe.file_header.PointerToSymbolTable);
447 /* FIXME: no way to get strtable size */
448 strtable = (const char*)&isym[numsym];
449 sect = IMAGE_FIRST_SECTION(RtlImageNtHeader((HMODULE)mapping));
451 for (i = 0; i < numsym; i+= naux, isym += naux)
453 if (isym->StorageClass == IMAGE_SYM_CLASS_FILE)
455 lastfilename = (const char*)(isym + 1);
456 compiland = NULL;
458 if (isym->StorageClass == IMAGE_SYM_CLASS_EXTERNAL &&
459 isym->SectionNumber > 0 && isym->SectionNumber <= fmap->u.pe.file_header.NumberOfSections)
461 if (isym->N.Name.Short)
463 name = memcpy(tmp, isym->N.ShortName, 8);
464 tmp[8] = '\0';
466 else name = strtable + isym->N.Name.Long;
467 if (name[0] == '_') name++;
469 if (!compiland && lastfilename)
470 compiland = symt_new_compiland(module, source_new(module, NULL, lastfilename));
472 if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
473 symt_new_public(module, compiland, name, FALSE,
474 module->module.BaseOfImage + sect[isym->SectionNumber - 1].VirtualAddress +
475 isym->Value,
478 naux = isym->NumberOfAuxSymbols + 1;
480 module->module.SymType = SymCoff;
481 module->module.LineNumbers = FALSE;
482 module->module.GlobalSymbols = FALSE;
483 module->module.TypeInfo = FALSE;
484 module->module.SourceIndexed = FALSE;
485 module->module.Publics = TRUE;
486 pe_unmap_full(fmap);
488 return TRUE;
491 /******************************************************************
492 * pe_load_stabs
494 * look for stabs information in PE header (it's how the mingw compiler provides
495 * its debugging information)
497 static BOOL pe_load_stabs(const struct process* pcs, struct module* module)
499 struct image_file_map* fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
500 struct image_section_map sect_stabs, sect_stabstr;
501 BOOL ret = FALSE;
503 if (pe_find_section(fmap, ".stab", &sect_stabs) && pe_find_section(fmap, ".stabstr", &sect_stabstr))
505 const char* stab;
506 const char* stabstr;
508 stab = image_map_section(&sect_stabs);
509 stabstr = image_map_section(&sect_stabstr);
510 if (stab != IMAGE_NO_MAP && stabstr != IMAGE_NO_MAP)
512 ret = stabs_parse(module,
513 module->module.BaseOfImage - PE_FROM_OPTHDR(fmap, ImageBase),
514 stab, image_get_map_size(&sect_stabs) / sizeof(struct stab_nlist), sizeof(struct stab_nlist),
515 stabstr, image_get_map_size(&sect_stabstr),
516 NULL, NULL);
518 image_unmap_section(&sect_stabs);
519 image_unmap_section(&sect_stabstr);
520 if (ret) pe_locate_with_coff_symbol_table(module);
522 TRACE("%s the STABS debug info\n", ret ? "successfully loaded" : "failed to load");
524 return ret;
527 /******************************************************************
528 * pe_load_dwarf
530 * look for dwarf information in PE header (it's also a way for the mingw compiler
531 * to provide its debugging information)
533 static BOOL pe_load_dwarf(struct module* module)
535 struct image_file_map* fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
536 BOOL ret;
538 ret = dwarf2_parse(module,
539 module->module.BaseOfImage - PE_FROM_OPTHDR(fmap, ImageBase),
540 NULL, /* FIXME: some thunks to deal with ? */
541 fmap);
542 TRACE("%s the DWARF debug info\n", ret ? "successfully loaded" : "failed to load");
544 return ret;
547 /******************************************************************
548 * pe_load_dbg_file
550 * loads a .dbg file
552 static BOOL pe_load_dbg_file(const struct process* pcs, struct module* module,
553 const char* dbg_name, DWORD timestamp)
555 WCHAR tmp[MAX_PATH];
556 HANDLE hFile = INVALID_HANDLE_VALUE, hMap = 0;
557 const BYTE* dbg_mapping = NULL;
558 BOOL ret = FALSE;
560 TRACE("Processing DBG file %s\n", debugstr_a(dbg_name));
562 if (path_find_symbol_file(pcs, module, dbg_name, DMT_DBG, NULL, timestamp, 0, tmp, &module->module.DbgUnmatched) &&
563 (hFile = CreateFileW(tmp, GENERIC_READ, FILE_SHARE_READ, NULL,
564 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) != INVALID_HANDLE_VALUE &&
565 ((hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) != 0) &&
566 ((dbg_mapping = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) != NULL))
568 const IMAGE_SEPARATE_DEBUG_HEADER* hdr;
569 const IMAGE_SECTION_HEADER* sectp;
570 const IMAGE_DEBUG_DIRECTORY* dbg;
572 hdr = (const IMAGE_SEPARATE_DEBUG_HEADER*)dbg_mapping;
573 /* section headers come immediately after debug header */
574 sectp = (const IMAGE_SECTION_HEADER*)(hdr + 1);
575 /* and after that and the exported names comes the debug directory */
576 dbg = (const IMAGE_DEBUG_DIRECTORY*)
577 (dbg_mapping + sizeof(*hdr) +
578 hdr->NumberOfSections * sizeof(IMAGE_SECTION_HEADER) +
579 hdr->ExportedNamesSize);
581 ret = pe_load_debug_directory(pcs, module, dbg_mapping, sectp,
582 hdr->NumberOfSections, dbg,
583 hdr->DebugDirectorySize / sizeof(*dbg));
585 else
586 ERR("Couldn't find .DBG file %s (%s)\n", debugstr_a(dbg_name), debugstr_w(tmp));
588 if (dbg_mapping) UnmapViewOfFile(dbg_mapping);
589 if (hMap) CloseHandle(hMap);
590 if (hFile != INVALID_HANDLE_VALUE) CloseHandle(hFile);
591 return ret;
594 /******************************************************************
595 * pe_load_msc_debug_info
597 * Process MSC debug information in PE file.
599 static BOOL pe_load_msc_debug_info(const struct process* pcs, struct module* module)
601 struct image_file_map* fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
602 BOOL ret = FALSE;
603 const IMAGE_DEBUG_DIRECTORY*dbg;
604 ULONG nDbg;
605 void* mapping;
606 IMAGE_NT_HEADERS* nth;
608 if (!(mapping = pe_map_full(fmap, &nth))) return FALSE;
609 /* Read in debug directory */
610 dbg = RtlImageDirectoryEntryToData( mapping, FALSE, IMAGE_DIRECTORY_ENTRY_DEBUG, &nDbg );
611 if (!dbg || !(nDbg /= sizeof(IMAGE_DEBUG_DIRECTORY))) goto done;
613 /* Parse debug directory */
614 if (nth->FileHeader.Characteristics & IMAGE_FILE_DEBUG_STRIPPED)
616 /* Debug info is stripped to .DBG file */
617 const IMAGE_DEBUG_MISC* misc = (const IMAGE_DEBUG_MISC*)
618 ((const char*)mapping + dbg->PointerToRawData);
620 if (nDbg != 1 || dbg->Type != IMAGE_DEBUG_TYPE_MISC ||
621 misc->DataType != IMAGE_DEBUG_MISC_EXENAME)
623 ERR("-Debug info stripped, but no .DBG file in module %s\n",
624 debugstr_w(module->modulename));
626 else
628 ret = pe_load_dbg_file(pcs, module, (const char*)misc->Data, nth->FileHeader.TimeDateStamp);
631 else
633 const IMAGE_SECTION_HEADER *sectp = (const IMAGE_SECTION_HEADER*)((const char*)&nth->OptionalHeader + nth->FileHeader.SizeOfOptionalHeader);
634 /* Debug info is embedded into PE module */
635 ret = pe_load_debug_directory(pcs, module, mapping, sectp,
636 nth->FileHeader.NumberOfSections, dbg, nDbg);
638 done:
639 pe_unmap_full(fmap);
640 return ret;
643 /***********************************************************************
644 * pe_load_export_debug_info
646 static BOOL pe_load_export_debug_info(const struct process* pcs, struct module* module)
648 struct image_file_map* fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
649 unsigned int i;
650 const IMAGE_EXPORT_DIRECTORY* exports;
651 DWORD_PTR base = module->module.BaseOfImage;
652 DWORD size;
653 IMAGE_NT_HEADERS* nth;
654 void* mapping;
656 if (dbghelp_options & SYMOPT_NO_PUBLICS) return TRUE;
658 if (!(mapping = pe_map_full(fmap, &nth))) return FALSE;
659 #if 0
660 /* Add start of DLL (better use the (yet unimplemented) Exe SymTag for this) */
661 /* FIXME: module.ModuleName isn't correctly set yet if it's passed in SymLoadModule */
662 symt_new_public(module, NULL, module->module.ModuleName, FALSE, base, 1);
663 #endif
665 /* Add entry point */
666 symt_new_public(module, NULL, "EntryPoint", FALSE,
667 base + nth->OptionalHeader.AddressOfEntryPoint, 1);
668 #if 0
669 /* FIXME: we'd better store addresses linked to sections rather than
670 absolute values */
671 IMAGE_SECTION_HEADER* section;
672 /* Add start of sections */
673 section = (IMAGE_SECTION_HEADER*)
674 ((char*)&nth->OptionalHeader + nth->FileHeader.SizeOfOptionalHeader);
675 for (i = 0; i < nth->FileHeader.NumberOfSections; i++, section++)
677 symt_new_public(module, NULL, section->Name, FALSE,
678 RtlImageRvaToVa(nth, mapping, section->VirtualAddress, NULL), 1);
680 #endif
682 /* Add exported functions */
683 if ((exports = RtlImageDirectoryEntryToData(mapping, FALSE,
684 IMAGE_DIRECTORY_ENTRY_EXPORT, &size)))
686 const WORD* ordinals = NULL;
687 const DWORD* functions = NULL;
688 const DWORD* names = NULL;
689 unsigned int j;
690 char buffer[16];
692 functions = RtlImageRvaToVa(nth, mapping, exports->AddressOfFunctions, NULL);
693 ordinals = RtlImageRvaToVa(nth, mapping, exports->AddressOfNameOrdinals, NULL);
694 names = RtlImageRvaToVa(nth, mapping, exports->AddressOfNames, NULL);
696 if (functions && ordinals && names)
698 for (i = 0; i < exports->NumberOfNames; i++)
700 if (!names[i]) continue;
701 symt_new_public(module, NULL,
702 RtlImageRvaToVa(nth, mapping, names[i], NULL),
703 FALSE,
704 base + functions[ordinals[i]], 1);
707 for (i = 0; i < exports->NumberOfFunctions; i++)
709 if (!functions[i]) continue;
710 /* Check if we already added it with a name */
711 for (j = 0; j < exports->NumberOfNames; j++)
712 if ((ordinals[j] == i) && names[j]) break;
713 if (j < exports->NumberOfNames) continue;
714 snprintf(buffer, sizeof(buffer), "%ld", i + exports->Base);
715 symt_new_public(module, NULL, buffer, FALSE, base + functions[i], 1);
719 /* no real debug info, only entry points */
720 if (module->module.SymType == SymDeferred)
721 module->module.SymType = SymExport;
722 pe_unmap_full(fmap);
724 return TRUE;
727 /******************************************************************
728 * pe_load_debug_info
731 BOOL pe_load_debug_info(const struct process* pcs, struct module* module)
733 BOOL ret = FALSE;
735 if (!(dbghelp_options & SYMOPT_PUBLICS_ONLY))
737 ret = image_check_alternate(&module->format_info[DFI_PE]->u.pe_info->fmap, module);
738 ret = pe_load_stabs(pcs, module) || ret;
739 ret = pe_load_dwarf(module) || ret;
740 ret = pe_load_msc_debug_info(pcs, module) || ret;
741 ret = ret || pe_load_coff_symbol_table(module); /* FIXME */
742 /* if we still have no debug info (we could only get SymExport at this
743 * point), then do the SymExport except if we have an ELF container,
744 * in which case we'll rely on the export's on the ELF side
747 /* FIXME shouldn't we check that? if (!module_get_debug(pcs, module)) */
748 if (pe_load_export_debug_info(pcs, module) && !ret)
749 ret = TRUE;
750 if (!ret) module->module.SymType = SymNone;
751 return ret;
754 struct builtin_search
756 WCHAR *path;
757 struct image_file_map fmap;
760 static BOOL search_builtin_pe(void *param, HANDLE handle, const WCHAR *path)
762 struct builtin_search *search = param;
764 if (!pe_map_file(handle, &search->fmap, DMT_PE)) return FALSE;
766 search->path = wcsdup(path);
767 return TRUE;
770 /******************************************************************
771 * pe_load_native_module
774 struct module* pe_load_native_module(struct process* pcs, const WCHAR* name,
775 HANDLE hFile, DWORD64 base, DWORD size)
777 struct module* module = NULL;
778 BOOL opened = FALSE;
779 struct module_format* modfmt;
780 WCHAR loaded_name[MAX_PATH];
781 WCHAR* real_path = NULL;
783 loaded_name[0] = '\0';
784 if (!hFile)
786 assert(name);
788 if ((hFile = FindExecutableImageExW(name, pcs->search_path, loaded_name, NULL, NULL)) == NULL &&
789 (hFile = FindExecutableImageExW(name, L".", loaded_name, NULL, NULL)) == NULL)
790 return NULL;
791 opened = TRUE;
793 else
795 ULONG sz = sizeof(OBJECT_NAME_INFORMATION) + MAX_PATH * sizeof(WCHAR), needed;
796 OBJECT_NAME_INFORMATION *obj_name;
797 NTSTATUS nts;
799 obj_name = RtlAllocateHeap(GetProcessHeap(), 0, sz);
800 if (obj_name)
802 nts = NtQueryObject(hFile, ObjectNameInformation, obj_name, sz, &needed);
803 if (nts == STATUS_BUFFER_OVERFLOW)
805 sz = needed;
806 obj_name = RtlReAllocateHeap(GetProcessHeap(), 0, obj_name, sz);
807 nts = NtQueryObject(hFile, ObjectNameInformation, obj_name, sz, &needed);
809 if (!nts)
811 obj_name->Name.Buffer[obj_name->Name.Length / sizeof(WCHAR)] = L'\0';
812 real_path = wcsdup(obj_name->Name.Buffer);
814 RtlFreeHeap(GetProcessHeap(), 0, obj_name);
816 if (name) lstrcpyW(loaded_name, name);
819 if ((modfmt = HeapAlloc(GetProcessHeap(), 0, sizeof(struct module_format) + sizeof(struct pe_module_info))))
821 modfmt->u.pe_info = (struct pe_module_info*)(modfmt + 1);
822 if (pe_map_file(hFile, &modfmt->u.pe_info->fmap, DMT_PE))
824 struct builtin_search builtin = { NULL };
825 if (opened && modfmt->u.pe_info->fmap.u.pe.builtin &&
826 search_dll_path(pcs, loaded_name, modfmt->u.pe_info->fmap.u.pe.file_header.Machine, search_builtin_pe, &builtin))
828 TRACE("reloaded %s from %s\n", debugstr_w(loaded_name), debugstr_w(builtin.path));
829 image_unmap_file(&modfmt->u.pe_info->fmap);
830 modfmt->u.pe_info->fmap = builtin.fmap;
831 real_path = builtin.path;
833 if (!base) base = PE_FROM_OPTHDR(&modfmt->u.pe_info->fmap, ImageBase);
834 if (!size) size = PE_FROM_OPTHDR(&modfmt->u.pe_info->fmap, SizeOfImage);
836 module = module_new(pcs, loaded_name, DMT_PE, FALSE, base, size,
837 modfmt->u.pe_info->fmap.u.pe.file_header.TimeDateStamp,
838 PE_FROM_OPTHDR(&modfmt->u.pe_info->fmap, CheckSum),
839 modfmt->u.pe_info->fmap.u.pe.file_header.Machine);
840 if (module)
842 module->real_path = real_path ? pool_wcsdup(&module->pool, real_path) : NULL;
843 modfmt->module = module;
844 modfmt->remove = pe_module_remove;
845 modfmt->loc_compute = NULL;
846 module->format_info[DFI_PE] = modfmt;
847 module->reloc_delta = base - PE_FROM_OPTHDR(&modfmt->u.pe_info->fmap, ImageBase);
849 else
851 ERR("could not load the module '%s'\n", debugstr_w(loaded_name));
852 image_unmap_file(&modfmt->u.pe_info->fmap);
855 if (!module) HeapFree(GetProcessHeap(), 0, modfmt);
858 if (opened) CloseHandle(hFile);
859 free(real_path);
860 return module;
863 /******************************************************************
864 * pe_load_nt_header
867 BOOL pe_load_nt_header(HANDLE hProc, DWORD64 base, IMAGE_NT_HEADERS* nth)
869 IMAGE_DOS_HEADER dos;
871 return ReadProcessMemory(hProc, (char*)(DWORD_PTR)base, &dos, sizeof(dos), NULL) &&
872 dos.e_magic == IMAGE_DOS_SIGNATURE &&
873 ReadProcessMemory(hProc, (char*)(DWORD_PTR)(base + dos.e_lfanew),
874 nth, sizeof(*nth), NULL) &&
875 nth->Signature == IMAGE_NT_SIGNATURE;
878 /******************************************************************
879 * pe_load_builtin_module
882 struct module* pe_load_builtin_module(struct process* pcs, const WCHAR* name,
883 DWORD64 base, DWORD64 size)
885 struct module* module = NULL;
887 if (base && pcs->dbg_hdr_addr)
889 IMAGE_NT_HEADERS nth;
891 if (pe_load_nt_header(pcs->handle, base, &nth))
893 if (!size) size = nth.OptionalHeader.SizeOfImage;
894 module = module_new(pcs, name, DMT_PE, FALSE, base, size,
895 nth.FileHeader.TimeDateStamp,
896 nth.OptionalHeader.CheckSum,
897 nth.FileHeader.Machine);
900 return module;
903 /***********************************************************************
904 * ImageDirectoryEntryToDataEx (DBGHELP.@)
906 * Search for specified directory in PE image
908 * PARAMS
910 * base [in] Image base address
911 * image [in] TRUE - image has been loaded by loader, FALSE - raw file image
912 * dir [in] Target directory index
913 * size [out] Receives directory size
914 * section [out] Receives pointer to section header of section containing directory data
916 * RETURNS
917 * Success: pointer to directory data
918 * Failure: NULL
921 PVOID WINAPI ImageDirectoryEntryToDataEx( PVOID base, BOOLEAN image, USHORT dir, PULONG size, PIMAGE_SECTION_HEADER *section )
923 const IMAGE_NT_HEADERS *nt;
924 DWORD_PTR addr;
926 *size = 0;
927 if (section) *section = NULL;
929 if (!(nt = RtlImageNtHeader( base ))) return NULL;
930 if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
932 const IMAGE_NT_HEADERS64 *nt64 = (const IMAGE_NT_HEADERS64 *)nt;
934 if (dir >= nt64->OptionalHeader.NumberOfRvaAndSizes) return NULL;
935 if (!(addr = nt64->OptionalHeader.DataDirectory[dir].VirtualAddress)) return NULL;
936 *size = nt64->OptionalHeader.DataDirectory[dir].Size;
937 if (image || addr < nt64->OptionalHeader.SizeOfHeaders) return (char *)base + addr;
939 else if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC)
941 const IMAGE_NT_HEADERS32 *nt32 = (const IMAGE_NT_HEADERS32 *)nt;
943 if (dir >= nt32->OptionalHeader.NumberOfRvaAndSizes) return NULL;
944 if (!(addr = nt32->OptionalHeader.DataDirectory[dir].VirtualAddress)) return NULL;
945 *size = nt32->OptionalHeader.DataDirectory[dir].Size;
946 if (image || addr < nt32->OptionalHeader.SizeOfHeaders) return (char *)base + addr;
948 else return NULL;
950 return RtlImageRvaToVa( nt, base, addr, section );
953 /***********************************************************************
954 * ImageDirectoryEntryToData (DBGHELP.@)
956 * NOTES
957 * See ImageDirectoryEntryToDataEx
959 PVOID WINAPI ImageDirectoryEntryToData( PVOID base, BOOLEAN image, USHORT dir, PULONG size )
961 return ImageDirectoryEntryToDataEx( base, image, dir, size, NULL );
964 DWORD pe_get_file_indexinfo(void* image, DWORD size, SYMSRV_INDEX_INFOW* info)
966 const IMAGE_NT_HEADERS* nthdr;
967 const IMAGE_DEBUG_DIRECTORY* dbg;
968 ULONG dirsize;
970 if (!(nthdr = RtlImageNtHeader(image))) return ERROR_BAD_FORMAT;
972 dbg = RtlImageDirectoryEntryToData(image, FALSE, IMAGE_DIRECTORY_ENTRY_DEBUG, &dirsize);
973 if (!dbg || dirsize < sizeof(dbg)) return ERROR_BAD_EXE_FORMAT;
975 /* fill in information from NT header */
976 info->timestamp = nthdr->FileHeader.TimeDateStamp;
977 info->stripped = (nthdr->FileHeader.Characteristics & IMAGE_FILE_DEBUG_STRIPPED) != 0;
978 if (nthdr->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
980 const IMAGE_NT_HEADERS64* nthdr64 = (const IMAGE_NT_HEADERS64*)nthdr;
981 info->size = nthdr64->OptionalHeader.SizeOfImage;
983 else if (nthdr->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC)
985 const IMAGE_NT_HEADERS32* nthdr32 = (const IMAGE_NT_HEADERS32*)nthdr;
986 info->size = nthdr32->OptionalHeader.SizeOfImage;
988 return msc_get_file_indexinfo(image, dbg, dirsize / sizeof(*dbg), info);