comctl32/tests: Flush events before testing edit control IME messages.
[wine.git] / dlls / dbghelp / pe_module.c
blob4a7e68d5483bbd4a6eb9c263b060bcab5176ba47
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 "dbghelp_private.h"
30 #include "image_private.h"
31 #include "winternl.h"
32 #include "wine/debug.h"
33 #include "wine/heap.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
37 struct pe_module_info
39 struct image_file_map fmap;
42 static const char builtin_signature[] = "Wine builtin DLL";
44 static void* pe_map_full(struct image_file_map* fmap, IMAGE_NT_HEADERS** nth)
46 if (!fmap->u.pe.full_map)
48 fmap->u.pe.full_map = MapViewOfFile(fmap->u.pe.hMap, FILE_MAP_READ, 0, 0, 0);
50 if (fmap->u.pe.full_map)
52 if (nth) *nth = RtlImageNtHeader(fmap->u.pe.full_map);
53 fmap->u.pe.full_count++;
54 return fmap->u.pe.full_map;
56 return NULL;
59 static void pe_unmap_full(struct image_file_map* fmap)
61 if (fmap->u.pe.full_count && !--fmap->u.pe.full_count)
63 UnmapViewOfFile(fmap->u.pe.full_map);
64 fmap->u.pe.full_map = NULL;
68 /* as we store either IMAGE_OPTIONAL_HEADER(32|64) inside pe_file_map,
69 * this helper will read to any field 'field' inside such an header
71 #define PE_FROM_OPTHDR(fmap, field) (((fmap)->addr_size == 32) ? ((fmap)->u.pe.opt.header32. field) : ((fmap)->u.pe.opt.header64. field))
73 /******************************************************************
74 * pe_map_section
76 * Maps a single section into memory from an PE file
78 static const char* pe_map_section(struct image_section_map* ism)
80 void* mapping;
81 struct pe_file_map* fmap = &ism->fmap->u.pe;
83 if (ism->sidx >= 0 && ism->sidx < fmap->file_header.NumberOfSections &&
84 fmap->sect[ism->sidx].mapped == IMAGE_NO_MAP)
86 IMAGE_NT_HEADERS* nth;
88 if (fmap->sect[ism->sidx].shdr.Misc.VirtualSize > fmap->sect[ism->sidx].shdr.SizeOfRawData)
90 FIXME("Section %Id: virtual (0x%lx) > raw (0x%lx) size - not supported\n",
91 ism->sidx, fmap->sect[ism->sidx].shdr.Misc.VirtualSize,
92 fmap->sect[ism->sidx].shdr.SizeOfRawData);
93 return IMAGE_NO_MAP;
95 /* FIXME: that's rather drastic, but that will do for now
96 * that's ok if the full file map exists, but we could be less aggressive otherwise and
97 * only map the relevant section
99 if ((mapping = pe_map_full(ism->fmap, &nth)))
101 fmap->sect[ism->sidx].mapped = RtlImageRvaToVa(nth, mapping,
102 fmap->sect[ism->sidx].shdr.VirtualAddress,
103 NULL);
104 return fmap->sect[ism->sidx].mapped;
107 return IMAGE_NO_MAP;
110 /******************************************************************
111 * pe_find_section
113 * Finds a section by name (and type) into memory from an PE file
114 * or its alternate if any
116 static BOOL pe_find_section(struct image_file_map* fmap, const char* name,
117 struct image_section_map* ism)
119 const char* sectname;
120 unsigned i;
121 char tmp[IMAGE_SIZEOF_SHORT_NAME + 1];
123 for (i = 0; i < fmap->u.pe.file_header.NumberOfSections; i++)
125 sectname = (const char*)fmap->u.pe.sect[i].shdr.Name;
126 /* long section names start with a '/' (at least on MinGW32) */
127 if (sectname[0] == '/' && fmap->u.pe.strtable)
128 sectname = fmap->u.pe.strtable + atoi(sectname + 1);
129 else
131 /* the section name may not be null terminated */
132 sectname = memcpy(tmp, sectname, IMAGE_SIZEOF_SHORT_NAME);
133 tmp[IMAGE_SIZEOF_SHORT_NAME] = '\0';
135 if (!stricmp(sectname, name))
137 ism->fmap = fmap;
138 ism->sidx = i;
139 return TRUE;
142 ism->fmap = NULL;
143 ism->sidx = -1;
145 return FALSE;
148 /******************************************************************
149 * pe_unmap_section
151 * Unmaps a single section from memory
153 static void pe_unmap_section(struct image_section_map* ism)
155 if (ism->sidx >= 0 && ism->sidx < ism->fmap->u.pe.file_header.NumberOfSections &&
156 ism->fmap->u.pe.sect[ism->sidx].mapped != IMAGE_NO_MAP)
158 pe_unmap_full(ism->fmap);
159 ism->fmap->u.pe.sect[ism->sidx].mapped = IMAGE_NO_MAP;
163 /******************************************************************
164 * pe_get_map_rva
166 * Get the RVA of an PE section
168 static DWORD_PTR pe_get_map_rva(const struct image_section_map* ism)
170 if (ism->sidx < 0 || ism->sidx >= ism->fmap->u.pe.file_header.NumberOfSections)
171 return 0;
172 return ism->fmap->u.pe.sect[ism->sidx].shdr.VirtualAddress;
175 /******************************************************************
176 * pe_get_map_size
178 * Get the size of a PE section
180 static unsigned pe_get_map_size(const struct image_section_map* ism)
182 if (ism->sidx < 0 || ism->sidx >= ism->fmap->u.pe.file_header.NumberOfSections)
183 return 0;
184 return ism->fmap->u.pe.sect[ism->sidx].shdr.Misc.VirtualSize;
187 /******************************************************************
188 * pe_unmap_file
190 * Unmaps an PE file from memory (previously mapped with pe_map_file)
192 static void pe_unmap_file(struct image_file_map* fmap)
194 if (fmap->u.pe.hMap != 0)
196 struct image_section_map ism;
197 ism.fmap = fmap;
198 for (ism.sidx = 0; ism.sidx < fmap->u.pe.file_header.NumberOfSections; ism.sidx++)
200 pe_unmap_section(&ism);
202 while (fmap->u.pe.full_count) pe_unmap_full(fmap);
203 HeapFree(GetProcessHeap(), 0, fmap->u.pe.sect);
204 HeapFree(GetProcessHeap(), 0, (void*)fmap->u.pe.strtable); /* FIXME ugly (see pe_map_file) */
205 CloseHandle(fmap->u.pe.hMap);
206 fmap->u.pe.hMap = NULL;
210 static const struct image_file_map_ops pe_file_map_ops =
212 pe_map_section,
213 pe_unmap_section,
214 pe_find_section,
215 pe_get_map_rva,
216 pe_get_map_size,
217 pe_unmap_file,
220 /******************************************************************
221 * pe_is_valid_pointer_table
223 * Checks whether the PointerToSymbolTable and NumberOfSymbols in file_header contain
224 * valid information.
226 static BOOL pe_is_valid_pointer_table(const IMAGE_NT_HEADERS* nthdr, const void* mapping, DWORD64 sz)
228 DWORD64 offset;
230 /* is the iSym table inside file size ? (including first DWORD of string table, which is its size) */
231 offset = (DWORD64)nthdr->FileHeader.PointerToSymbolTable;
232 offset += (DWORD64)nthdr->FileHeader.NumberOfSymbols * sizeof(IMAGE_SYMBOL);
233 if (offset + sizeof(DWORD) > sz) return FALSE;
234 /* is string table (following iSym table) inside file size ? */
235 offset += *(DWORD*)((const char*)mapping + offset);
236 return offset <= sz;
239 /******************************************************************
240 * pe_map_file
242 * Maps an PE file into memory (and checks it's a real PE file)
244 BOOL pe_map_file(HANDLE file, struct image_file_map* fmap, enum module_type mt)
246 void* mapping;
248 fmap->modtype = mt;
249 fmap->ops = &pe_file_map_ops;
250 fmap->alternate = NULL;
251 fmap->u.pe.hMap = CreateFileMappingW(file, NULL, PAGE_READONLY, 0, 0, NULL);
252 if (fmap->u.pe.hMap == 0) return FALSE;
253 fmap->u.pe.full_count = 0;
254 fmap->u.pe.full_map = NULL;
255 if (!(mapping = pe_map_full(fmap, NULL))) goto error;
257 switch (mt)
259 case DMT_PE:
261 IMAGE_NT_HEADERS* nthdr;
262 IMAGE_SECTION_HEADER* section;
263 unsigned i;
265 if (!(nthdr = RtlImageNtHeader(mapping))) goto error;
266 memcpy(&fmap->u.pe.file_header, &nthdr->FileHeader, sizeof(fmap->u.pe.file_header));
267 switch (nthdr->OptionalHeader.Magic)
269 case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
270 if (sizeof(void*) == 8 && !(SymGetOptions() & SYMOPT_INCLUDE_32BIT_MODULES))
272 TRACE("Won't load 32bit module in 64bit dbghelp when options don't ask for it\n");
273 goto error;
275 fmap->addr_size = 32;
276 memcpy(&fmap->u.pe.opt.header32, &nthdr->OptionalHeader, sizeof(fmap->u.pe.opt.header32));
277 break;
278 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
279 if (sizeof(void*) == 4) return FALSE;
280 fmap->addr_size = 64;
281 memcpy(&fmap->u.pe.opt.header64, &nthdr->OptionalHeader, sizeof(fmap->u.pe.opt.header64));
282 break;
283 default:
284 return FALSE;
287 fmap->u.pe.builtin = !memcmp((const IMAGE_DOS_HEADER*)mapping + 1, builtin_signature, sizeof(builtin_signature));
288 section = (IMAGE_SECTION_HEADER*)
289 ((char*)&nthdr->OptionalHeader + nthdr->FileHeader.SizeOfOptionalHeader);
290 fmap->u.pe.sect = HeapAlloc(GetProcessHeap(), 0,
291 nthdr->FileHeader.NumberOfSections * sizeof(fmap->u.pe.sect[0]));
292 if (!fmap->u.pe.sect) goto error;
293 for (i = 0; i < nthdr->FileHeader.NumberOfSections; i++)
295 memcpy(&fmap->u.pe.sect[i].shdr, section + i, sizeof(IMAGE_SECTION_HEADER));
296 fmap->u.pe.sect[i].mapped = IMAGE_NO_MAP;
298 if (nthdr->FileHeader.PointerToSymbolTable && nthdr->FileHeader.NumberOfSymbols)
300 LARGE_INTEGER li;
302 if (GetFileSizeEx(file, &li) && pe_is_valid_pointer_table(nthdr, mapping, li.QuadPart))
304 /* FIXME ugly: should rather map the relevant content instead of copying it */
305 const char* src = (const char*)mapping +
306 nthdr->FileHeader.PointerToSymbolTable +
307 nthdr->FileHeader.NumberOfSymbols * sizeof(IMAGE_SYMBOL);
308 char* dst;
309 DWORD sz = *(DWORD*)src;
311 if ((dst = HeapAlloc(GetProcessHeap(), 0, sz)))
312 memcpy(dst, src, sz);
313 fmap->u.pe.strtable = dst;
315 else
317 WARN("Bad coff table... wipping out\n");
318 /* we have bad information here, wipe it out */
319 fmap->u.pe.file_header.PointerToSymbolTable = 0;
320 fmap->u.pe.file_header.NumberOfSymbols = 0;
321 fmap->u.pe.strtable = NULL;
324 else fmap->u.pe.strtable = NULL;
326 break;
327 default: assert(0); goto error;
329 pe_unmap_full(fmap);
331 return TRUE;
332 error:
333 pe_unmap_full(fmap);
334 CloseHandle(fmap->u.pe.hMap);
335 return FALSE;
338 /******************************************************************
339 * pe_map_directory
341 * Maps a directory content out of a PE file
343 const char* pe_map_directory(struct module* module, int dirno, DWORD* size)
345 IMAGE_NT_HEADERS* nth;
346 void* mapping;
348 if (module->type != DMT_PE || !module->format_info[DFI_PE]) return NULL;
349 if (dirno >= IMAGE_NUMBEROF_DIRECTORY_ENTRIES ||
350 !(mapping = pe_map_full(&module->format_info[DFI_PE]->u.pe_info->fmap, &nth)))
351 return NULL;
352 if (size) *size = nth->OptionalHeader.DataDirectory[dirno].Size;
353 return RtlImageRvaToVa(nth, mapping,
354 nth->OptionalHeader.DataDirectory[dirno].VirtualAddress, NULL);
357 static void pe_module_remove(struct process* pcs, struct module_format* modfmt)
359 image_unmap_file(&modfmt->u.pe_info->fmap);
360 HeapFree(GetProcessHeap(), 0, modfmt);
363 /******************************************************************
364 * pe_locate_with_coff_symbol_table
366 * Use the COFF symbol table (if any) from the IMAGE_FILE_HEADER to set the absolute address
367 * of global symbols.
368 * Mingw32 requires this for stabs debug information as address for global variables isn't filled in
369 * (this is similar to what is done in elf_module.c when using the .symtab ELF section)
371 static BOOL pe_locate_with_coff_symbol_table(struct module* module)
373 struct image_file_map* fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
374 const IMAGE_SYMBOL* isym;
375 int i, numsym, naux;
376 char tmp[9];
377 const char* name;
378 struct hash_table_iter hti;
379 void* ptr;
380 struct symt_data* sym;
381 const char* mapping;
383 numsym = fmap->u.pe.file_header.NumberOfSymbols;
384 if (!fmap->u.pe.file_header.PointerToSymbolTable || !numsym)
385 return TRUE;
386 if (!(mapping = pe_map_full(fmap, NULL))) return FALSE;
387 isym = (const IMAGE_SYMBOL*)(mapping + fmap->u.pe.file_header.PointerToSymbolTable);
389 for (i = 0; i < numsym; i+= naux, isym += naux)
391 if (isym->StorageClass == IMAGE_SYM_CLASS_EXTERNAL &&
392 isym->SectionNumber > 0 && isym->SectionNumber <= fmap->u.pe.file_header.NumberOfSections)
394 if (isym->N.Name.Short)
396 name = memcpy(tmp, isym->N.ShortName, 8);
397 tmp[8] = '\0';
399 else name = fmap->u.pe.strtable + isym->N.Name.Long;
400 if (name[0] == '_') name++;
401 hash_table_iter_init(&module->ht_symbols, &hti, name);
402 while ((ptr = hash_table_iter_up(&hti)))
404 sym = CONTAINING_RECORD(ptr, struct symt_data, hash_elt);
405 if (sym->symt.tag == SymTagData &&
406 (sym->kind == DataIsGlobal || sym->kind == DataIsFileStatic) &&
407 sym->u.var.kind == loc_absolute &&
408 !strcmp(sym->hash_elt.name, name))
410 TRACE("Changing absolute address for %d.%s: %Ix -> %I64x\n",
411 isym->SectionNumber, name, sym->u.var.offset,
412 module->module.BaseOfImage +
413 fmap->u.pe.sect[isym->SectionNumber - 1].shdr.VirtualAddress +
414 isym->Value);
415 sym->u.var.offset = module->module.BaseOfImage +
416 fmap->u.pe.sect[isym->SectionNumber - 1].shdr.VirtualAddress + isym->Value;
417 break;
421 naux = isym->NumberOfAuxSymbols + 1;
423 pe_unmap_full(fmap);
424 return TRUE;
427 /******************************************************************
428 * pe_load_coff_symbol_table
430 * Load public symbols out of the COFF symbol table (if any).
432 static BOOL pe_load_coff_symbol_table(struct module* module)
434 struct image_file_map* fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
435 const IMAGE_SYMBOL* isym;
436 int i, numsym, naux;
437 const char* strtable;
438 char tmp[9];
439 const char* name;
440 const char* lastfilename = NULL;
441 struct symt_compiland* compiland = NULL;
442 const IMAGE_SECTION_HEADER* sect;
443 const char* mapping;
445 numsym = fmap->u.pe.file_header.NumberOfSymbols;
446 if (!fmap->u.pe.file_header.PointerToSymbolTable || !numsym)
447 return TRUE;
448 if (!(mapping = pe_map_full(fmap, NULL))) return FALSE;
449 isym = (const IMAGE_SYMBOL*)(mapping + fmap->u.pe.file_header.PointerToSymbolTable);
450 /* FIXME: no way to get strtable size */
451 strtable = (const char*)&isym[numsym];
452 sect = IMAGE_FIRST_SECTION(RtlImageNtHeader((HMODULE)mapping));
454 for (i = 0; i < numsym; i+= naux, isym += naux)
456 if (isym->StorageClass == IMAGE_SYM_CLASS_FILE)
458 lastfilename = (const char*)(isym + 1);
459 compiland = NULL;
461 if (isym->StorageClass == IMAGE_SYM_CLASS_EXTERNAL &&
462 isym->SectionNumber > 0 && isym->SectionNumber <= fmap->u.pe.file_header.NumberOfSections)
464 if (isym->N.Name.Short)
466 name = memcpy(tmp, isym->N.ShortName, 8);
467 tmp[8] = '\0';
469 else name = strtable + isym->N.Name.Long;
470 if (name[0] == '_') name++;
472 if (!compiland && lastfilename)
473 compiland = symt_new_compiland(module, source_new(module, NULL, lastfilename));
475 if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
476 symt_new_public(module, compiland, name, FALSE,
477 module->module.BaseOfImage + sect[isym->SectionNumber - 1].VirtualAddress +
478 isym->Value,
481 naux = isym->NumberOfAuxSymbols + 1;
483 module->module.SymType = SymCoff;
484 module->module.LineNumbers = FALSE;
485 module->module.GlobalSymbols = FALSE;
486 module->module.TypeInfo = FALSE;
487 module->module.SourceIndexed = FALSE;
488 module->module.Publics = TRUE;
489 pe_unmap_full(fmap);
491 return TRUE;
494 /******************************************************************
495 * pe_load_stabs
497 * look for stabs information in PE header (it's how the mingw compiler provides
498 * its debugging information)
500 static BOOL pe_load_stabs(const struct process* pcs, struct module* module)
502 struct image_file_map* fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
503 struct image_section_map sect_stabs, sect_stabstr;
504 BOOL ret = FALSE;
506 if (pe_find_section(fmap, ".stab", &sect_stabs) && pe_find_section(fmap, ".stabstr", &sect_stabstr))
508 const char* stab;
509 const char* stabstr;
511 stab = image_map_section(&sect_stabs);
512 stabstr = image_map_section(&sect_stabstr);
513 if (stab != IMAGE_NO_MAP && stabstr != IMAGE_NO_MAP)
515 ret = stabs_parse(module,
516 module->module.BaseOfImage - PE_FROM_OPTHDR(fmap, ImageBase),
517 stab, image_get_map_size(&sect_stabs) / sizeof(struct stab_nlist), sizeof(struct stab_nlist),
518 stabstr, image_get_map_size(&sect_stabstr),
519 NULL, NULL);
521 image_unmap_section(&sect_stabs);
522 image_unmap_section(&sect_stabstr);
523 if (ret) pe_locate_with_coff_symbol_table(module);
525 TRACE("%s the STABS debug info\n", ret ? "successfully loaded" : "failed to load");
527 return ret;
530 /******************************************************************
531 * pe_load_dwarf
533 * look for dwarf information in PE header (it's also a way for the mingw compiler
534 * to provide its debugging information)
536 static BOOL pe_load_dwarf(struct module* module)
538 struct image_file_map* fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
539 BOOL ret;
541 ret = dwarf2_parse(module,
542 module->module.BaseOfImage - PE_FROM_OPTHDR(fmap, ImageBase),
543 NULL, /* FIXME: some thunks to deal with ? */
544 fmap);
545 TRACE("%s the DWARF debug info\n", ret ? "successfully loaded" : "failed to load");
547 return ret;
550 /******************************************************************
551 * pe_load_dbg_file
553 * loads a .dbg file
555 static BOOL pe_load_dbg_file(const struct process* pcs, struct module* module,
556 const char* dbg_name, DWORD timestamp)
558 WCHAR tmp[MAX_PATH];
559 HANDLE hFile = INVALID_HANDLE_VALUE, hMap = 0;
560 const BYTE* dbg_mapping = NULL;
561 BOOL ret = FALSE;
563 TRACE("Processing DBG file %s\n", debugstr_a(dbg_name));
565 if (path_find_symbol_file(pcs, module, dbg_name, DMT_DBG, NULL, timestamp, 0, tmp, &module->module.DbgUnmatched) &&
566 (hFile = CreateFileW(tmp, GENERIC_READ, FILE_SHARE_READ, NULL,
567 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) != INVALID_HANDLE_VALUE &&
568 ((hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) != 0) &&
569 ((dbg_mapping = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) != NULL))
571 const IMAGE_SEPARATE_DEBUG_HEADER* hdr;
572 const IMAGE_SECTION_HEADER* sectp;
573 const IMAGE_DEBUG_DIRECTORY* dbg;
575 hdr = (const IMAGE_SEPARATE_DEBUG_HEADER*)dbg_mapping;
576 /* section headers come immediately after debug header */
577 sectp = (const IMAGE_SECTION_HEADER*)(hdr + 1);
578 /* and after that and the exported names comes the debug directory */
579 dbg = (const IMAGE_DEBUG_DIRECTORY*)
580 (dbg_mapping + sizeof(*hdr) +
581 hdr->NumberOfSections * sizeof(IMAGE_SECTION_HEADER) +
582 hdr->ExportedNamesSize);
584 ret = pe_load_debug_directory(pcs, module, dbg_mapping, sectp,
585 hdr->NumberOfSections, dbg,
586 hdr->DebugDirectorySize / sizeof(*dbg));
588 else
589 ERR("Couldn't find .DBG file %s (%s)\n", debugstr_a(dbg_name), debugstr_w(tmp));
591 if (dbg_mapping) UnmapViewOfFile(dbg_mapping);
592 if (hMap) CloseHandle(hMap);
593 if (hFile != INVALID_HANDLE_VALUE) CloseHandle(hFile);
594 return ret;
597 /******************************************************************
598 * pe_load_msc_debug_info
600 * Process MSC debug information in PE file.
602 static BOOL pe_load_msc_debug_info(const struct process* pcs, struct module* module)
604 struct image_file_map* fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
605 BOOL ret = FALSE;
606 const IMAGE_DEBUG_DIRECTORY*dbg;
607 ULONG nDbg;
608 void* mapping;
609 IMAGE_NT_HEADERS* nth;
611 if (!(mapping = pe_map_full(fmap, &nth))) return FALSE;
612 /* Read in debug directory */
613 dbg = RtlImageDirectoryEntryToData( mapping, FALSE, IMAGE_DIRECTORY_ENTRY_DEBUG, &nDbg );
614 if (!dbg || !(nDbg /= sizeof(IMAGE_DEBUG_DIRECTORY))) goto done;
616 /* Parse debug directory */
617 if (nth->FileHeader.Characteristics & IMAGE_FILE_DEBUG_STRIPPED)
619 /* Debug info is stripped to .DBG file */
620 const IMAGE_DEBUG_MISC* misc = (const IMAGE_DEBUG_MISC*)
621 ((const char*)mapping + dbg->PointerToRawData);
623 if (nDbg != 1 || dbg->Type != IMAGE_DEBUG_TYPE_MISC ||
624 misc->DataType != IMAGE_DEBUG_MISC_EXENAME)
626 ERR("-Debug info stripped, but no .DBG file in module %s\n",
627 debugstr_w(module->modulename));
629 else
631 ret = pe_load_dbg_file(pcs, module, (const char*)misc->Data, nth->FileHeader.TimeDateStamp);
634 else
636 const IMAGE_SECTION_HEADER *sectp = (const IMAGE_SECTION_HEADER*)((const char*)&nth->OptionalHeader + nth->FileHeader.SizeOfOptionalHeader);
637 /* Debug info is embedded into PE module */
638 ret = pe_load_debug_directory(pcs, module, mapping, sectp,
639 nth->FileHeader.NumberOfSections, dbg, nDbg);
641 done:
642 pe_unmap_full(fmap);
643 return ret;
646 /***********************************************************************
647 * pe_load_export_debug_info
649 static BOOL pe_load_export_debug_info(const struct process* pcs, struct module* module)
651 struct image_file_map* fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
652 unsigned int i;
653 const IMAGE_EXPORT_DIRECTORY* exports;
654 DWORD_PTR base = module->module.BaseOfImage;
655 DWORD size;
656 IMAGE_NT_HEADERS* nth;
657 void* mapping;
659 if (dbghelp_options & SYMOPT_NO_PUBLICS) return TRUE;
661 if (!(mapping = pe_map_full(fmap, &nth))) return FALSE;
662 #if 0
663 /* Add start of DLL (better use the (yet unimplemented) Exe SymTag for this) */
664 /* FIXME: module.ModuleName isn't correctly set yet if it's passed in SymLoadModule */
665 symt_new_public(module, NULL, module->module.ModuleName, FALSE, base, 1);
666 #endif
668 /* Add entry point */
669 symt_new_public(module, NULL, "EntryPoint", FALSE,
670 base + nth->OptionalHeader.AddressOfEntryPoint, 1);
671 #if 0
672 /* FIXME: we'd better store addresses linked to sections rather than
673 absolute values */
674 IMAGE_SECTION_HEADER* section;
675 /* Add start of sections */
676 section = (IMAGE_SECTION_HEADER*)
677 ((char*)&nth->OptionalHeader + nth->FileHeader.SizeOfOptionalHeader);
678 for (i = 0; i < nth->FileHeader.NumberOfSections; i++, section++)
680 symt_new_public(module, NULL, section->Name, FALSE,
681 RtlImageRvaToVa(nth, mapping, section->VirtualAddress, NULL), 1);
683 #endif
685 /* Add exported functions */
686 if ((exports = RtlImageDirectoryEntryToData(mapping, FALSE,
687 IMAGE_DIRECTORY_ENTRY_EXPORT, &size)))
689 const WORD* ordinals = NULL;
690 const DWORD* functions = NULL;
691 const DWORD* names = NULL;
692 unsigned int j;
693 char buffer[16];
695 functions = RtlImageRvaToVa(nth, mapping, exports->AddressOfFunctions, NULL);
696 ordinals = RtlImageRvaToVa(nth, mapping, exports->AddressOfNameOrdinals, NULL);
697 names = RtlImageRvaToVa(nth, mapping, exports->AddressOfNames, NULL);
699 if (functions && ordinals && names)
701 for (i = 0; i < exports->NumberOfNames; i++)
703 if (!names[i]) continue;
704 symt_new_public(module, NULL,
705 RtlImageRvaToVa(nth, mapping, names[i], NULL),
706 FALSE,
707 base + functions[ordinals[i]], 1);
710 for (i = 0; i < exports->NumberOfFunctions; i++)
712 if (!functions[i]) continue;
713 /* Check if we already added it with a name */
714 for (j = 0; j < exports->NumberOfNames; j++)
715 if ((ordinals[j] == i) && names[j]) break;
716 if (j < exports->NumberOfNames) continue;
717 snprintf(buffer, sizeof(buffer), "%ld", i + exports->Base);
718 symt_new_public(module, NULL, buffer, FALSE, base + functions[i], 1);
722 /* no real debug info, only entry points */
723 if (module->module.SymType == SymDeferred)
724 module->module.SymType = SymExport;
725 pe_unmap_full(fmap);
727 return TRUE;
730 /******************************************************************
731 * pe_load_debug_info
734 BOOL pe_load_debug_info(const struct process* pcs, struct module* module)
736 BOOL ret = FALSE;
738 if (!(dbghelp_options & SYMOPT_PUBLICS_ONLY))
740 ret = image_check_alternate(&module->format_info[DFI_PE]->u.pe_info->fmap, module);
741 ret = pe_load_stabs(pcs, module) || ret;
742 ret = pe_load_dwarf(module) || ret;
743 ret = pe_load_msc_debug_info(pcs, module) || ret;
744 ret = ret || pe_load_coff_symbol_table(module); /* FIXME */
745 /* if we still have no debug info (we could only get SymExport at this
746 * point), then do the SymExport except if we have an ELF container,
747 * in which case we'll rely on the export's on the ELF side
750 /* FIXME shouldn't we check that? if (!module_get_debug(pcs, module)) */
751 if (pe_load_export_debug_info(pcs, module) && !ret)
752 ret = TRUE;
753 if (!ret) module->module.SymType = SymNone;
754 return ret;
757 struct builtin_search
759 WCHAR *path;
760 struct image_file_map fmap;
763 static BOOL search_builtin_pe(void *param, HANDLE handle, const WCHAR *path)
765 struct builtin_search *search = param;
766 size_t size;
768 if (!pe_map_file(handle, &search->fmap, DMT_PE)) return FALSE;
770 size = (lstrlenW(path) + 1) * sizeof(WCHAR);
771 if ((search->path = heap_alloc(size)))
772 memcpy(search->path, path, size);
773 return TRUE;
776 /******************************************************************
777 * pe_load_native_module
780 struct module* pe_load_native_module(struct process* pcs, const WCHAR* name,
781 HANDLE hFile, DWORD64 base, DWORD size)
783 struct module* module = NULL;
784 BOOL opened = FALSE;
785 struct module_format* modfmt;
786 WCHAR loaded_name[MAX_PATH];
788 loaded_name[0] = '\0';
789 if (!hFile)
791 assert(name);
793 if ((hFile = FindExecutableImageExW(name, pcs->search_path, loaded_name, NULL, NULL)) == NULL &&
794 (hFile = FindExecutableImageExW(name, L".", loaded_name, NULL, NULL)) == NULL)
795 return NULL;
796 opened = TRUE;
798 else if (name) lstrcpyW(loaded_name, name);
799 if (!(modfmt = HeapAlloc(GetProcessHeap(), 0, sizeof(struct module_format) + sizeof(struct pe_module_info))))
800 return NULL;
801 modfmt->u.pe_info = (struct pe_module_info*)(modfmt + 1);
802 if (pe_map_file(hFile, &modfmt->u.pe_info->fmap, DMT_PE))
804 struct builtin_search builtin = { NULL };
805 if (modfmt->u.pe_info->fmap.u.pe.builtin && search_dll_path(pcs, loaded_name, search_builtin_pe, &builtin))
807 TRACE("reloaded %s from %s\n", debugstr_w(loaded_name), debugstr_w(builtin.path));
808 image_unmap_file(&modfmt->u.pe_info->fmap);
809 modfmt->u.pe_info->fmap = builtin.fmap;
811 if (!base) base = PE_FROM_OPTHDR(&modfmt->u.pe_info->fmap, ImageBase);
812 if (!size) size = PE_FROM_OPTHDR(&modfmt->u.pe_info->fmap, SizeOfImage);
814 module = module_new(pcs, loaded_name, DMT_PE, FALSE, base, size,
815 modfmt->u.pe_info->fmap.u.pe.file_header.TimeDateStamp,
816 PE_FROM_OPTHDR(&modfmt->u.pe_info->fmap, CheckSum),
817 modfmt->u.pe_info->fmap.u.pe.file_header.Machine);
818 if (module)
820 module->real_path = builtin.path;
821 modfmt->module = module;
822 modfmt->remove = pe_module_remove;
823 modfmt->loc_compute = NULL;
824 module->format_info[DFI_PE] = modfmt;
825 module->reloc_delta = base - PE_FROM_OPTHDR(&modfmt->u.pe_info->fmap, ImageBase);
827 else
829 ERR("could not load the module '%s'\n", debugstr_w(loaded_name));
830 heap_free(builtin.path);
831 image_unmap_file(&modfmt->u.pe_info->fmap);
834 if (!module) HeapFree(GetProcessHeap(), 0, modfmt);
836 if (opened) CloseHandle(hFile);
838 return module;
841 /******************************************************************
842 * pe_load_nt_header
845 BOOL pe_load_nt_header(HANDLE hProc, DWORD64 base, IMAGE_NT_HEADERS* nth)
847 IMAGE_DOS_HEADER dos;
849 return ReadProcessMemory(hProc, (char*)(DWORD_PTR)base, &dos, sizeof(dos), NULL) &&
850 dos.e_magic == IMAGE_DOS_SIGNATURE &&
851 ReadProcessMemory(hProc, (char*)(DWORD_PTR)(base + dos.e_lfanew),
852 nth, sizeof(*nth), NULL) &&
853 nth->Signature == IMAGE_NT_SIGNATURE;
856 /******************************************************************
857 * pe_load_builtin_module
860 struct module* pe_load_builtin_module(struct process* pcs, const WCHAR* name,
861 DWORD64 base, DWORD64 size)
863 struct module* module = NULL;
865 if (base && pcs->dbg_hdr_addr)
867 IMAGE_NT_HEADERS nth;
869 if (pe_load_nt_header(pcs->handle, base, &nth))
871 if (!size) size = nth.OptionalHeader.SizeOfImage;
872 module = module_new(pcs, name, DMT_PE, FALSE, base, size,
873 nth.FileHeader.TimeDateStamp,
874 nth.OptionalHeader.CheckSum,
875 nth.FileHeader.Machine);
878 return module;
881 /***********************************************************************
882 * ImageDirectoryEntryToDataEx (DBGHELP.@)
884 * Search for specified directory in PE image
886 * PARAMS
888 * base [in] Image base address
889 * image [in] TRUE - image has been loaded by loader, FALSE - raw file image
890 * dir [in] Target directory index
891 * size [out] Receives directory size
892 * section [out] Receives pointer to section header of section containing directory data
894 * RETURNS
895 * Success: pointer to directory data
896 * Failure: NULL
899 PVOID WINAPI ImageDirectoryEntryToDataEx( PVOID base, BOOLEAN image, USHORT dir, PULONG size, PIMAGE_SECTION_HEADER *section )
901 const IMAGE_NT_HEADERS *nt;
902 DWORD_PTR addr;
904 *size = 0;
905 if (section) *section = NULL;
907 if (!(nt = RtlImageNtHeader( base ))) return NULL;
908 if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
910 const IMAGE_NT_HEADERS64 *nt64 = (const IMAGE_NT_HEADERS64 *)nt;
912 if (dir >= nt64->OptionalHeader.NumberOfRvaAndSizes) return NULL;
913 if (!(addr = nt64->OptionalHeader.DataDirectory[dir].VirtualAddress)) return NULL;
914 *size = nt64->OptionalHeader.DataDirectory[dir].Size;
915 if (image || addr < nt64->OptionalHeader.SizeOfHeaders) return (char *)base + addr;
917 else if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC)
919 const IMAGE_NT_HEADERS32 *nt32 = (const IMAGE_NT_HEADERS32 *)nt;
921 if (dir >= nt32->OptionalHeader.NumberOfRvaAndSizes) return NULL;
922 if (!(addr = nt32->OptionalHeader.DataDirectory[dir].VirtualAddress)) return NULL;
923 *size = nt32->OptionalHeader.DataDirectory[dir].Size;
924 if (image || addr < nt32->OptionalHeader.SizeOfHeaders) return (char *)base + addr;
926 else return NULL;
928 return RtlImageRvaToVa( nt, base, addr, section );
931 /***********************************************************************
932 * ImageDirectoryEntryToData (DBGHELP.@)
934 * NOTES
935 * See ImageDirectoryEntryToDataEx
937 PVOID WINAPI ImageDirectoryEntryToData( PVOID base, BOOLEAN image, USHORT dir, PULONG size )
939 return ImageDirectoryEntryToDataEx( base, image, dir, size, NULL );