gdiplus/tests: Comment out a test that corrupts the stack on Vista.
[wine/multimedia.git] / dlls / dbghelp / pe_module.c
blob18cec9a83693ed8259701a71d66d04a7d3a7ce80
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 "config.h"
25 #include "wine/port.h"
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <assert.h>
32 #include "dbghelp_private.h"
33 #include "image_private.h"
34 #include "winternl.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
39 struct pe_module_info
41 struct image_file_map fmap;
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 IMAGE_NO_MAP;
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 /******************************************************************
69 * pe_map_section
71 * Maps a single section into memory from an PE file
73 const char* pe_map_section(struct image_section_map* ism)
75 void* mapping;
76 struct pe_file_map* fmap = &ism->fmap->u.pe;
78 if (ism->sidx >= 0 && ism->sidx < fmap->ntheader.FileHeader.NumberOfSections &&
79 fmap->sect[ism->sidx].mapped == IMAGE_NO_MAP)
81 IMAGE_NT_HEADERS* nth;
82 /* FIXME: that's rather drastic, but that will do for now
83 * that's ok if the full file map exists, but we could be less agressive otherwise and
84 * only map the relevant section
86 if ((mapping = pe_map_full(ism->fmap, &nth)))
88 fmap->sect[ism->sidx].mapped = RtlImageRvaToVa(nth, mapping,
89 fmap->sect[ism->sidx].shdr.VirtualAddress,
90 NULL);
91 return fmap->sect[ism->sidx].mapped;
94 return IMAGE_NO_MAP;
97 /******************************************************************
98 * pe_find_section
100 * Finds a section by name (and type) into memory from an PE file
101 * or its alternate if any
103 BOOL pe_find_section(struct image_file_map* fmap, const char* name,
104 struct image_section_map* ism)
106 const char* sectname;
107 unsigned i;
108 char tmp[IMAGE_SIZEOF_SHORT_NAME + 1];
110 for (i = 0; i < fmap->u.pe.ntheader.FileHeader.NumberOfSections; i++)
112 sectname = (const char*)fmap->u.pe.sect[i].shdr.Name;
113 /* long section names start with a '/' (at least on MinGW32) */
114 if (sectname[0] == '/' && fmap->u.pe.strtable)
115 sectname = fmap->u.pe.strtable + atoi(sectname + 1);
116 else
118 /* the section name may not be null terminated */
119 sectname = memcpy(tmp, sectname, IMAGE_SIZEOF_SHORT_NAME);
120 tmp[IMAGE_SIZEOF_SHORT_NAME] = '\0';
122 if (!strcasecmp(sectname, name))
124 ism->fmap = fmap;
125 ism->sidx = i;
126 return TRUE;
129 ism->fmap = NULL;
130 ism->sidx = -1;
132 return FALSE;
135 /******************************************************************
136 * pe_unmap_section
138 * Unmaps a single section from memory
140 void pe_unmap_section(struct image_section_map* ism)
142 if (ism->sidx >= 0 && ism->sidx < ism->fmap->u.pe.ntheader.FileHeader.NumberOfSections &&
143 ism->fmap->u.pe.sect[ism->sidx].mapped != IMAGE_NO_MAP)
145 pe_unmap_full(ism->fmap);
146 ism->fmap->u.pe.sect[ism->sidx].mapped = IMAGE_NO_MAP;
150 /******************************************************************
151 * pe_get_map_rva
153 * Get the RVA of an PE section
155 DWORD_PTR pe_get_map_rva(const struct image_section_map* ism)
157 if (ism->sidx < 0 || ism->sidx >= ism->fmap->u.pe.ntheader.FileHeader.NumberOfSections)
158 return 0;
159 return ism->fmap->u.pe.sect[ism->sidx].shdr.VirtualAddress;
162 /******************************************************************
163 * pe_get_map_size
165 * Get the size of an PE section
167 unsigned pe_get_map_size(const struct image_section_map* ism)
169 if (ism->sidx < 0 || ism->sidx >= ism->fmap->u.pe.ntheader.FileHeader.NumberOfSections)
170 return 0;
171 return ism->fmap->u.pe.sect[ism->sidx].shdr.SizeOfRawData;
174 /******************************************************************
175 * pe_is_valid_pointer_table
177 * Checks whether the PointerToSymbolTable and NumberOfSymbols in file_header contain
178 * valid information.
180 static BOOL pe_is_valid_pointer_table(const IMAGE_NT_HEADERS* nthdr, const void* mapping, DWORD64 sz)
182 DWORD64 offset;
184 /* is the iSym table inside file size ? (including first DWORD of string table, which is its size) */
185 offset = (DWORD64)nthdr->FileHeader.PointerToSymbolTable;
186 offset += (DWORD64)nthdr->FileHeader.NumberOfSymbols * sizeof(IMAGE_SYMBOL);
187 if (offset + sizeof(DWORD) > sz) return FALSE;
188 /* is string table (following iSym table) inside file size ? */
189 offset += *(DWORD*)((const char*)mapping + offset);
190 return offset <= sz;
193 /******************************************************************
194 * pe_map_file
196 * Maps an PE file into memory (and checks it's a real PE file)
198 static BOOL pe_map_file(HANDLE file, struct image_file_map* fmap, enum module_type mt)
200 void* mapping;
202 fmap->modtype = mt;
203 fmap->u.pe.hMap = CreateFileMappingW(file, NULL, PAGE_READONLY, 0, 0, NULL);
204 if (fmap->u.pe.hMap == 0) return FALSE;
205 fmap->u.pe.full_count = 0;
206 fmap->u.pe.full_map = NULL;
207 if (!(mapping = pe_map_full(fmap, NULL))) goto error;
209 switch (mt)
211 case DMT_PE:
213 IMAGE_NT_HEADERS* nthdr;
214 IMAGE_SECTION_HEADER* section;
215 unsigned i;
217 if (!(nthdr = RtlImageNtHeader(mapping))) goto error;
218 memcpy(&fmap->u.pe.ntheader, nthdr, sizeof(fmap->u.pe.ntheader));
219 section = (IMAGE_SECTION_HEADER*)
220 ((char*)&nthdr->OptionalHeader + nthdr->FileHeader.SizeOfOptionalHeader);
221 fmap->u.pe.sect = HeapAlloc(GetProcessHeap(), 0,
222 nthdr->FileHeader.NumberOfSections * sizeof(fmap->u.pe.sect[0]));
223 if (!fmap->u.pe.sect) goto error;
224 for (i = 0; i < nthdr->FileHeader.NumberOfSections; i++)
226 memcpy(&fmap->u.pe.sect[i].shdr, section + i, sizeof(IMAGE_SECTION_HEADER));
227 fmap->u.pe.sect[i].mapped = IMAGE_NO_MAP;
229 if (nthdr->FileHeader.PointerToSymbolTable && nthdr->FileHeader.NumberOfSymbols)
231 LARGE_INTEGER li;
233 if (GetFileSizeEx(file, &li) && pe_is_valid_pointer_table(nthdr, mapping, li.QuadPart))
235 /* FIXME ugly: should rather map the relevant content instead of copying it */
236 const char* src = (const char*)mapping +
237 nthdr->FileHeader.PointerToSymbolTable +
238 nthdr->FileHeader.NumberOfSymbols * sizeof(IMAGE_SYMBOL);
239 char* dst;
240 DWORD sz = *(DWORD*)src;
242 if ((dst = HeapAlloc(GetProcessHeap(), 0, sz)))
243 memcpy(dst, src, sz);
244 fmap->u.pe.strtable = dst;
246 else
248 WARN("Bad coff table... wipping out\n");
249 /* we have bad information here, wipe it out */
250 fmap->u.pe.ntheader.FileHeader.PointerToSymbolTable = 0;
251 fmap->u.pe.ntheader.FileHeader.NumberOfSymbols = 0;
252 fmap->u.pe.strtable = NULL;
255 else fmap->u.pe.strtable = NULL;
257 break;
258 default: assert(0); goto error;
260 pe_unmap_full(fmap);
262 return TRUE;
263 error:
264 pe_unmap_full(fmap);
265 CloseHandle(fmap->u.pe.hMap);
266 return FALSE;
269 /******************************************************************
270 * pe_unmap_file
272 * Unmaps an PE file from memory (previously mapped with pe_map_file)
274 static void pe_unmap_file(struct image_file_map* fmap)
276 if (fmap->u.pe.hMap != 0)
278 struct image_section_map ism;
279 ism.fmap = fmap;
280 for (ism.sidx = 0; ism.sidx < fmap->u.pe.ntheader.FileHeader.NumberOfSections; ism.sidx++)
282 pe_unmap_section(&ism);
284 while (fmap->u.pe.full_count) pe_unmap_full(fmap);
285 HeapFree(GetProcessHeap(), 0, fmap->u.pe.sect);
286 HeapFree(GetProcessHeap(), 0, (void*)fmap->u.pe.strtable); /* FIXME ugly (see pe_map_file) */
287 CloseHandle(fmap->u.pe.hMap);
288 fmap->u.pe.hMap = NULL;
292 /******************************************************************
293 * pe_map_directory
295 * Maps a directory content out of a PE file
297 const char* pe_map_directory(struct module* module, int dirno, DWORD* size)
299 IMAGE_NT_HEADERS* nth;
300 void* mapping;
302 if (module->type != DMT_PE || !module->format_info[DFI_PE]) return NULL;
303 if (dirno >= IMAGE_NUMBEROF_DIRECTORY_ENTRIES ||
304 !(mapping = pe_map_full(&module->format_info[DFI_PE]->u.pe_info->fmap, &nth)))
305 return NULL;
306 if (size) *size = nth->OptionalHeader.DataDirectory[dirno].Size;
307 return RtlImageRvaToVa(nth, mapping,
308 nth->OptionalHeader.DataDirectory[dirno].VirtualAddress, NULL);
311 /******************************************************************
312 * pe_unmap_directory
314 * Unmaps a directory content
316 void pe_unmap_directory(struct image_file_map* fmap, int dirno)
318 pe_unmap_full(fmap);
321 static void pe_module_remove(struct process* pcs, struct module_format* modfmt)
323 pe_unmap_file(&modfmt->u.pe_info->fmap);
324 HeapFree(GetProcessHeap(), 0, modfmt);
327 /******************************************************************
328 * pe_locate_with_coff_symbol_table
330 * Use the COFF symbol table (if any) from the IMAGE_FILE_HEADER to set the absolute address
331 * of global symbols.
332 * Mingw32 requires this for stabs debug information as address for global variables isn't filled in
333 * (this is similar to what is done in elf_module.c when using the .symtab ELF section)
335 static BOOL pe_locate_with_coff_symbol_table(struct module* module)
337 struct image_file_map* fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
338 const IMAGE_SYMBOL* isym;
339 int i, numsym, naux;
340 char tmp[9];
341 const char* name;
342 struct hash_table_iter hti;
343 void* ptr;
344 struct symt_data* sym;
345 const char* mapping;
347 numsym = fmap->u.pe.ntheader.FileHeader.NumberOfSymbols;
348 if (!fmap->u.pe.ntheader.FileHeader.PointerToSymbolTable || !numsym)
349 return TRUE;
350 if (!(mapping = pe_map_full(fmap, NULL))) return FALSE;
351 isym = (const IMAGE_SYMBOL*)(mapping + fmap->u.pe.ntheader.FileHeader.PointerToSymbolTable);
353 for (i = 0; i < numsym; i+= naux, isym += naux)
355 if (isym->StorageClass == IMAGE_SYM_CLASS_EXTERNAL &&
356 isym->SectionNumber > 0 && isym->SectionNumber <= fmap->u.pe.ntheader.FileHeader.NumberOfSections)
358 if (isym->N.Name.Short)
360 name = memcpy(tmp, isym->N.ShortName, 8);
361 tmp[8] = '\0';
363 else name = fmap->u.pe.strtable + isym->N.Name.Long;
364 if (name[0] == '_') name++;
365 hash_table_iter_init(&module->ht_symbols, &hti, name);
366 while ((ptr = hash_table_iter_up(&hti)))
368 sym = GET_ENTRY(ptr, struct symt_data, hash_elt);
369 if (sym->symt.tag == SymTagData &&
370 (sym->kind == DataIsGlobal || sym->kind == DataIsFileStatic) &&
371 !strcmp(sym->hash_elt.name, name))
373 TRACE("Changing absolute address for %d.%s: %lx -> %s\n",
374 isym->SectionNumber, name, sym->u.var.offset,
375 wine_dbgstr_longlong(module->module.BaseOfImage +
376 fmap->u.pe.sect[isym->SectionNumber - 1].shdr.VirtualAddress +
377 isym->Value));
378 sym->u.var.offset = module->module.BaseOfImage +
379 fmap->u.pe.sect[isym->SectionNumber - 1].shdr.VirtualAddress + isym->Value;
380 break;
384 naux = isym->NumberOfAuxSymbols + 1;
386 pe_unmap_full(fmap);
387 return TRUE;
390 /******************************************************************
391 * pe_load_coff_symbol_table
393 * Load public symbols out of the COFF symbol table (if any).
395 static BOOL pe_load_coff_symbol_table(struct module* module)
397 struct image_file_map* fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
398 const IMAGE_SYMBOL* isym;
399 int i, numsym, naux;
400 const char* strtable;
401 char tmp[9];
402 const char* name;
403 const char* lastfilename = NULL;
404 struct symt_compiland* compiland = NULL;
405 const IMAGE_SECTION_HEADER* sect;
406 const char* mapping;
408 numsym = fmap->u.pe.ntheader.FileHeader.NumberOfSymbols;
409 if (!fmap->u.pe.ntheader.FileHeader.PointerToSymbolTable || !numsym)
410 return TRUE;
411 if (!(mapping = pe_map_full(fmap, NULL))) return FALSE;
412 isym = (const IMAGE_SYMBOL*)((const char*)mapping + fmap->u.pe.ntheader.FileHeader.PointerToSymbolTable);
413 /* FIXME: no way to get strtable size */
414 strtable = (const char*)&isym[numsym];
415 sect = IMAGE_FIRST_SECTION(RtlImageNtHeader((HMODULE)mapping));
417 for (i = 0; i < numsym; i+= naux, isym += naux)
419 if (isym->StorageClass == IMAGE_SYM_CLASS_FILE)
421 lastfilename = (const char*)(isym + 1);
422 compiland = NULL;
424 if (isym->StorageClass == IMAGE_SYM_CLASS_EXTERNAL &&
425 isym->SectionNumber > 0 && isym->SectionNumber <= fmap->u.pe.ntheader.FileHeader.NumberOfSections)
427 if (isym->N.Name.Short)
429 name = memcpy(tmp, isym->N.ShortName, 8);
430 tmp[8] = '\0';
432 else name = strtable + isym->N.Name.Long;
433 if (name[0] == '_') name++;
435 if (!compiland && lastfilename)
436 compiland = symt_new_compiland(module, 0,
437 source_new(module, NULL, lastfilename));
439 if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
440 symt_new_public(module, compiland, name,
441 module->module.BaseOfImage + sect[isym->SectionNumber - 1].VirtualAddress +
442 isym->Value,
445 naux = isym->NumberOfAuxSymbols + 1;
447 module->module.SymType = SymCoff;
448 module->module.LineNumbers = FALSE;
449 module->module.GlobalSymbols = FALSE;
450 module->module.TypeInfo = FALSE;
451 module->module.SourceIndexed = FALSE;
452 module->module.Publics = TRUE;
453 pe_unmap_full(fmap);
455 return TRUE;
458 static inline void* pe_get_sect(IMAGE_NT_HEADERS* nth, void* mapping,
459 IMAGE_SECTION_HEADER* sect)
461 return (sect) ? RtlImageRvaToVa(nth, mapping, sect->VirtualAddress, NULL) : NULL;
464 static inline DWORD pe_get_sect_size(IMAGE_SECTION_HEADER* sect)
466 return (sect) ? sect->SizeOfRawData : 0;
469 /******************************************************************
470 * pe_load_stabs
472 * look for stabs information in PE header (it's how the mingw compiler provides
473 * its debugging information)
475 static BOOL pe_load_stabs(const struct process* pcs, struct module* module)
477 struct image_file_map* fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
478 struct image_section_map sect_stabs, sect_stabstr;
479 BOOL ret = FALSE;
481 if (pe_find_section(fmap, ".stab", &sect_stabs) && pe_find_section(fmap, ".stabstr", &sect_stabstr))
483 const char* stab;
484 const char* stabstr;
486 stab = image_map_section(&sect_stabs);
487 stabstr = image_map_section(&sect_stabstr);
488 if (stab != IMAGE_NO_MAP && stabstr != IMAGE_NO_MAP)
490 ret = stabs_parse(module,
491 module->module.BaseOfImage - fmap->u.pe.ntheader.OptionalHeader.ImageBase,
492 stab, image_get_map_size(&sect_stabs),
493 stabstr, image_get_map_size(&sect_stabstr),
494 NULL, NULL);
496 image_unmap_section(&sect_stabs);
497 image_unmap_section(&sect_stabstr);
498 if (ret) pe_locate_with_coff_symbol_table(module);
500 TRACE("%s the STABS debug info\n", ret ? "successfully loaded" : "failed to load");
502 return ret;
505 /******************************************************************
506 * pe_load_dwarf
508 * look for dwarf information in PE header (it's also a way for the mingw compiler
509 * to provide its debugging information)
511 static BOOL pe_load_dwarf(struct module* module)
513 struct image_file_map* fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
514 BOOL ret = FALSE;
516 ret = dwarf2_parse(module,
517 module->module.BaseOfImage - fmap->u.pe.ntheader.OptionalHeader.ImageBase,
518 NULL, /* FIXME: some thunks to deal with ? */
519 fmap);
520 TRACE("%s the DWARF debug info\n", ret ? "successfully loaded" : "failed to load");
522 return ret;
525 /******************************************************************
526 * pe_load_dbg_file
528 * loads a .dbg file
530 static BOOL pe_load_dbg_file(const struct process* pcs, struct module* module,
531 const char* dbg_name, DWORD timestamp)
533 char tmp[MAX_PATH];
534 HANDLE hFile = INVALID_HANDLE_VALUE, hMap = 0;
535 const BYTE* dbg_mapping = NULL;
536 BOOL ret = FALSE;
538 TRACE("Processing DBG file %s\n", debugstr_a(dbg_name));
540 if (path_find_symbol_file(pcs, dbg_name, NULL, timestamp, 0, tmp, &module->module.DbgUnmatched) &&
541 (hFile = CreateFileA(tmp, GENERIC_READ, FILE_SHARE_READ, NULL,
542 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) != INVALID_HANDLE_VALUE &&
543 ((hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) != 0) &&
544 ((dbg_mapping = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) != NULL))
546 const IMAGE_SEPARATE_DEBUG_HEADER* hdr;
547 const IMAGE_SECTION_HEADER* sectp;
548 const IMAGE_DEBUG_DIRECTORY* dbg;
550 hdr = (const IMAGE_SEPARATE_DEBUG_HEADER*)dbg_mapping;
551 /* section headers come immediately after debug header */
552 sectp = (const IMAGE_SECTION_HEADER*)(hdr + 1);
553 /* and after that and the exported names comes the debug directory */
554 dbg = (const IMAGE_DEBUG_DIRECTORY*)
555 (dbg_mapping + sizeof(*hdr) +
556 hdr->NumberOfSections * sizeof(IMAGE_SECTION_HEADER) +
557 hdr->ExportedNamesSize);
559 ret = pe_load_debug_directory(pcs, module, dbg_mapping, sectp,
560 hdr->NumberOfSections, dbg,
561 hdr->DebugDirectorySize / sizeof(*dbg));
563 else
564 ERR("Couldn't find .DBG file %s (%s)\n", debugstr_a(dbg_name), debugstr_a(tmp));
566 if (dbg_mapping) UnmapViewOfFile(dbg_mapping);
567 if (hMap) CloseHandle(hMap);
568 if (hFile != INVALID_HANDLE_VALUE) CloseHandle(hFile);
569 return ret;
572 /******************************************************************
573 * pe_load_msc_debug_info
575 * Process MSC debug information in PE file.
577 static BOOL pe_load_msc_debug_info(const struct process* pcs, struct module* module)
579 struct image_file_map* fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
580 BOOL ret = FALSE;
581 const IMAGE_DATA_DIRECTORY* dir;
582 const IMAGE_DEBUG_DIRECTORY*dbg = NULL;
583 int nDbg;
584 void* mapping;
585 IMAGE_NT_HEADERS* nth;
587 if (!(mapping = pe_map_full(fmap, &nth))) return FALSE;
588 /* Read in debug directory */
589 dir = nth->OptionalHeader.DataDirectory + IMAGE_DIRECTORY_ENTRY_DEBUG;
590 nDbg = dir->Size / sizeof(IMAGE_DEBUG_DIRECTORY);
591 if (!nDbg) goto done;
593 dbg = RtlImageRvaToVa(nth, mapping, dir->VirtualAddress, NULL);
595 /* Parse debug directory */
596 if (nth->FileHeader.Characteristics & IMAGE_FILE_DEBUG_STRIPPED)
598 /* Debug info is stripped to .DBG file */
599 const IMAGE_DEBUG_MISC* misc = (const IMAGE_DEBUG_MISC*)
600 ((const char*)mapping + dbg->PointerToRawData);
602 if (nDbg != 1 || dbg->Type != IMAGE_DEBUG_TYPE_MISC ||
603 misc->DataType != IMAGE_DEBUG_MISC_EXENAME)
605 WINE_ERR("-Debug info stripped, but no .DBG file in module %s\n",
606 debugstr_w(module->module.ModuleName));
608 else
610 ret = pe_load_dbg_file(pcs, module, (const char*)misc->Data, nth->FileHeader.TimeDateStamp);
613 else
615 const IMAGE_SECTION_HEADER *sectp = (const IMAGE_SECTION_HEADER*)((const char*)&nth->OptionalHeader + nth->FileHeader.SizeOfOptionalHeader);
616 /* Debug info is embedded into PE module */
617 ret = pe_load_debug_directory(pcs, module, mapping, sectp,
618 nth->FileHeader.NumberOfSections, dbg, nDbg);
620 done:
621 pe_unmap_full(fmap);
622 return ret;
625 /***********************************************************************
626 * pe_load_export_debug_info
628 static BOOL pe_load_export_debug_info(const struct process* pcs, struct module* module)
630 struct image_file_map* fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
631 unsigned int i;
632 const IMAGE_EXPORT_DIRECTORY* exports;
633 DWORD base = module->module.BaseOfImage;
634 DWORD size;
635 IMAGE_NT_HEADERS* nth;
636 void* mapping;
638 if (dbghelp_options & SYMOPT_NO_PUBLICS) return TRUE;
640 if (!(mapping = pe_map_full(fmap, &nth))) return FALSE;
641 #if 0
642 /* Add start of DLL (better use the (yet unimplemented) Exe SymTag for this) */
643 /* FIXME: module.ModuleName isn't correctly set yet if it's passed in SymLoadModule */
644 symt_new_public(module, NULL, module->module.ModuleName, base, 1);
645 #endif
647 /* Add entry point */
648 symt_new_public(module, NULL, "EntryPoint",
649 base + nth->OptionalHeader.AddressOfEntryPoint, 1);
650 #if 0
651 /* FIXME: we'd better store addresses linked to sections rather than
652 absolute values */
653 IMAGE_SECTION_HEADER* section;
654 /* Add start of sections */
655 section = (IMAGE_SECTION_HEADER*)
656 ((char*)&nth->OptionalHeader + nth->FileHeader.SizeOfOptionalHeader);
657 for (i = 0; i < nth->FileHeader.NumberOfSections; i++, section++)
659 symt_new_public(module, NULL, section->Name,
660 RtlImageRvaToVa(nth, mapping, section->VirtualAddress, NULL), 1);
662 #endif
664 /* Add exported functions */
665 if ((exports = RtlImageDirectoryEntryToData(mapping, FALSE,
666 IMAGE_DIRECTORY_ENTRY_EXPORT, &size)))
668 const WORD* ordinals = NULL;
669 const DWORD_PTR* functions = NULL;
670 const DWORD* names = NULL;
671 unsigned int j;
672 char buffer[16];
674 functions = RtlImageRvaToVa(nth, mapping, exports->AddressOfFunctions, NULL);
675 ordinals = RtlImageRvaToVa(nth, mapping, exports->AddressOfNameOrdinals, NULL);
676 names = RtlImageRvaToVa(nth, mapping, exports->AddressOfNames, NULL);
678 if (functions && ordinals && names)
680 for (i = 0; i < exports->NumberOfNames; i++)
682 if (!names[i]) continue;
683 symt_new_public(module, NULL,
684 RtlImageRvaToVa(nth, mapping, names[i], NULL),
685 base + functions[ordinals[i]], 1);
688 for (i = 0; i < exports->NumberOfFunctions; i++)
690 if (!functions[i]) continue;
691 /* Check if we already added it with a name */
692 for (j = 0; j < exports->NumberOfNames; j++)
693 if ((ordinals[j] == i) && names[j]) break;
694 if (j < exports->NumberOfNames) continue;
695 snprintf(buffer, sizeof(buffer), "%d", i + exports->Base);
696 symt_new_public(module, NULL, buffer, base + (DWORD)functions[i], 1);
700 /* no real debug info, only entry points */
701 if (module->module.SymType == SymDeferred)
702 module->module.SymType = SymExport;
703 pe_unmap_full(fmap);
705 return TRUE;
708 /******************************************************************
709 * pe_load_debug_info
712 BOOL pe_load_debug_info(const struct process* pcs, struct module* module)
714 BOOL ret = FALSE;
716 if (!(dbghelp_options & SYMOPT_PUBLICS_ONLY))
718 ret = pe_load_stabs(pcs, module);
719 ret = pe_load_dwarf(module) || ret;
720 ret = pe_load_msc_debug_info(pcs, module) || ret;
721 ret = ret || pe_load_coff_symbol_table(module); /* FIXME */
722 /* if we still have no debug info (we could only get SymExport at this
723 * point), then do the SymExport except if we have an ELF container,
724 * in which case we'll rely on the export's on the ELF side
727 /* FIXME shouldn't we check that? if (!module_get_debug(pcs, module)) */
728 if (pe_load_export_debug_info(pcs, module) && !ret)
729 ret = TRUE;
731 return ret;
734 /******************************************************************
735 * pe_load_native_module
738 struct module* pe_load_native_module(struct process* pcs, const WCHAR* name,
739 HANDLE hFile, DWORD base, DWORD size)
741 struct module* module = NULL;
742 BOOL opened = FALSE;
743 struct module_format* modfmt;
744 WCHAR loaded_name[MAX_PATH];
746 loaded_name[0] = '\0';
747 if (!hFile)
749 assert(name);
751 if ((hFile = FindExecutableImageExW(name, pcs->search_path, loaded_name, NULL, NULL)) == NULL)
752 return NULL;
753 opened = TRUE;
755 else if (name) strcpyW(loaded_name, name);
756 else if (dbghelp_options & SYMOPT_DEFERRED_LOADS)
757 FIXME("Trouble ahead (no module name passed in deferred mode)\n");
758 if (!(modfmt = HeapAlloc(GetProcessHeap(), 0, sizeof(struct module_format) + sizeof(struct pe_module_info))))
759 return NULL;
760 modfmt->u.pe_info = (struct pe_module_info*)(modfmt + 1);
761 if (pe_map_file(hFile, &modfmt->u.pe_info->fmap, DMT_PE))
763 if (!base) base = modfmt->u.pe_info->fmap.u.pe.ntheader.OptionalHeader.ImageBase;
764 if (!size) size = modfmt->u.pe_info->fmap.u.pe.ntheader.OptionalHeader.SizeOfImage;
766 module = module_new(pcs, loaded_name, DMT_PE, FALSE, base, size,
767 modfmt->u.pe_info->fmap.u.pe.ntheader.FileHeader.TimeDateStamp,
768 modfmt->u.pe_info->fmap.u.pe.ntheader.OptionalHeader.CheckSum);
769 if (module)
771 modfmt->module = module;
772 modfmt->remove = pe_module_remove;
773 modfmt->loc_compute = NULL;
775 module->format_info[DFI_PE] = modfmt;
776 if (dbghelp_options & SYMOPT_DEFERRED_LOADS)
777 module->module.SymType = SymDeferred;
778 else
779 pe_load_debug_info(pcs, module);
781 else
783 ERR("could not load the module '%s'\n", debugstr_w(loaded_name));
784 pe_unmap_file(&modfmt->u.pe_info->fmap);
787 if (!module) HeapFree(GetProcessHeap(), 0, modfmt);
789 if (opened) CloseHandle(hFile);
791 return module;
794 /******************************************************************
795 * pe_load_nt_header
798 BOOL pe_load_nt_header(HANDLE hProc, DWORD64 base, IMAGE_NT_HEADERS* nth)
800 IMAGE_DOS_HEADER dos;
802 return ReadProcessMemory(hProc, (char*)(DWORD_PTR)base, &dos, sizeof(dos), NULL) &&
803 dos.e_magic == IMAGE_DOS_SIGNATURE &&
804 ReadProcessMemory(hProc, (char*)(DWORD_PTR)(base + dos.e_lfanew),
805 nth, sizeof(*nth), NULL) &&
806 nth->Signature == IMAGE_NT_SIGNATURE;
809 /******************************************************************
810 * pe_load_builtin_module
813 struct module* pe_load_builtin_module(struct process* pcs, const WCHAR* name,
814 DWORD64 base, DWORD64 size)
816 struct module* module = NULL;
818 if (base && pcs->dbg_hdr_addr)
820 IMAGE_NT_HEADERS nth;
822 if (pe_load_nt_header(pcs->handle, base, &nth))
824 if (!size) size = nth.OptionalHeader.SizeOfImage;
825 module = module_new(pcs, name, DMT_PE, FALSE, base, size,
826 nth.FileHeader.TimeDateStamp,
827 nth.OptionalHeader.CheckSum);
830 return module;
833 /***********************************************************************
834 * ImageDirectoryEntryToDataEx (DBGHELP.@)
836 * Search for specified directory in PE image
838 * PARAMS
840 * base [in] Image base address
841 * image [in] TRUE - image has been loaded by loader, FALSE - raw file image
842 * dir [in] Target directory index
843 * size [out] Receives directory size
844 * section [out] Receives pointer to section header of section containing directory data
846 * RETURNS
847 * Success: pointer to directory data
848 * Failure: NULL
851 PVOID WINAPI ImageDirectoryEntryToDataEx( PVOID base, BOOLEAN image, USHORT dir, PULONG size, PIMAGE_SECTION_HEADER *section )
853 const IMAGE_NT_HEADERS *nt;
854 DWORD addr;
856 *size = 0;
857 if (section) *section = NULL;
859 if (!(nt = RtlImageNtHeader( base ))) return NULL;
860 if (dir >= nt->OptionalHeader.NumberOfRvaAndSizes) return NULL;
861 if (!(addr = nt->OptionalHeader.DataDirectory[dir].VirtualAddress)) return NULL;
863 *size = nt->OptionalHeader.DataDirectory[dir].Size;
864 if (image || addr < nt->OptionalHeader.SizeOfHeaders) return (char *)base + addr;
866 return RtlImageRvaToVa( nt, base, addr, section );
869 /***********************************************************************
870 * ImageDirectoryEntryToData (DBGHELP.@)
872 * NOTES
873 * See ImageDirectoryEntryToDataEx
875 PVOID WINAPI ImageDirectoryEntryToData( PVOID base, BOOLEAN image, USHORT dir, PULONG size )
877 return ImageDirectoryEntryToDataEx( base, image, dir, size, NULL );