mshtml: Respond to SID_SContainerDispatch service id.
[wine/multimedia.git] / dlls / dbghelp / pe_module.c
blob79a4d90a29c817f9fc32e1a0d31ca73b9da51a21
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;
83 if (fmap->sect[ism->sidx].shdr.Misc.VirtualSize > fmap->sect[ism->sidx].shdr.SizeOfRawData)
85 FIXME("Section %ld: virtual (0x%x) > raw (0x%x) size - not supported\n",
86 ism->sidx, fmap->sect[ism->sidx].shdr.Misc.VirtualSize,
87 fmap->sect[ism->sidx].shdr.SizeOfRawData);
88 return IMAGE_NO_MAP;
90 /* FIXME: that's rather drastic, but that will do for now
91 * that's ok if the full file map exists, but we could be less aggressive otherwise and
92 * only map the relevant section
94 if ((mapping = pe_map_full(ism->fmap, &nth)))
96 fmap->sect[ism->sidx].mapped = RtlImageRvaToVa(nth, mapping,
97 fmap->sect[ism->sidx].shdr.VirtualAddress,
98 NULL);
99 return fmap->sect[ism->sidx].mapped;
102 return IMAGE_NO_MAP;
105 /******************************************************************
106 * pe_find_section
108 * Finds a section by name (and type) into memory from an PE file
109 * or its alternate if any
111 BOOL pe_find_section(struct image_file_map* fmap, const char* name,
112 struct image_section_map* ism)
114 const char* sectname;
115 unsigned i;
116 char tmp[IMAGE_SIZEOF_SHORT_NAME + 1];
118 for (i = 0; i < fmap->u.pe.ntheader.FileHeader.NumberOfSections; i++)
120 sectname = (const char*)fmap->u.pe.sect[i].shdr.Name;
121 /* long section names start with a '/' (at least on MinGW32) */
122 if (sectname[0] == '/' && fmap->u.pe.strtable)
123 sectname = fmap->u.pe.strtable + atoi(sectname + 1);
124 else
126 /* the section name may not be null terminated */
127 sectname = memcpy(tmp, sectname, IMAGE_SIZEOF_SHORT_NAME);
128 tmp[IMAGE_SIZEOF_SHORT_NAME] = '\0';
130 if (!strcasecmp(sectname, name))
132 ism->fmap = fmap;
133 ism->sidx = i;
134 return TRUE;
137 ism->fmap = NULL;
138 ism->sidx = -1;
140 return FALSE;
143 /******************************************************************
144 * pe_unmap_section
146 * Unmaps a single section from memory
148 void pe_unmap_section(struct image_section_map* ism)
150 if (ism->sidx >= 0 && ism->sidx < ism->fmap->u.pe.ntheader.FileHeader.NumberOfSections &&
151 ism->fmap->u.pe.sect[ism->sidx].mapped != IMAGE_NO_MAP)
153 pe_unmap_full(ism->fmap);
154 ism->fmap->u.pe.sect[ism->sidx].mapped = IMAGE_NO_MAP;
158 /******************************************************************
159 * pe_get_map_rva
161 * Get the RVA of an PE section
163 DWORD_PTR pe_get_map_rva(const struct image_section_map* ism)
165 if (ism->sidx < 0 || ism->sidx >= ism->fmap->u.pe.ntheader.FileHeader.NumberOfSections)
166 return 0;
167 return ism->fmap->u.pe.sect[ism->sidx].shdr.VirtualAddress;
170 /******************************************************************
171 * pe_get_map_size
173 * Get the size of a PE section
175 unsigned pe_get_map_size(const struct image_section_map* ism)
177 if (ism->sidx < 0 || ism->sidx >= ism->fmap->u.pe.ntheader.FileHeader.NumberOfSections)
178 return 0;
179 return ism->fmap->u.pe.sect[ism->sidx].shdr.Misc.VirtualSize;
182 /******************************************************************
183 * pe_is_valid_pointer_table
185 * Checks whether the PointerToSymbolTable and NumberOfSymbols in file_header contain
186 * valid information.
188 static BOOL pe_is_valid_pointer_table(const IMAGE_NT_HEADERS* nthdr, const void* mapping, DWORD64 sz)
190 DWORD64 offset;
192 /* is the iSym table inside file size ? (including first DWORD of string table, which is its size) */
193 offset = (DWORD64)nthdr->FileHeader.PointerToSymbolTable;
194 offset += (DWORD64)nthdr->FileHeader.NumberOfSymbols * sizeof(IMAGE_SYMBOL);
195 if (offset + sizeof(DWORD) > sz) return FALSE;
196 /* is string table (following iSym table) inside file size ? */
197 offset += *(DWORD*)((const char*)mapping + offset);
198 return offset <= sz;
201 /******************************************************************
202 * pe_map_file
204 * Maps an PE file into memory (and checks it's a real PE file)
206 static BOOL pe_map_file(HANDLE file, struct image_file_map* fmap, enum module_type mt)
208 void* mapping;
210 fmap->modtype = mt;
211 fmap->u.pe.hMap = CreateFileMappingW(file, NULL, PAGE_READONLY, 0, 0, NULL);
212 if (fmap->u.pe.hMap == 0) return FALSE;
213 fmap->u.pe.full_count = 0;
214 fmap->u.pe.full_map = NULL;
215 if (!(mapping = pe_map_full(fmap, NULL))) goto error;
217 switch (mt)
219 case DMT_PE:
221 IMAGE_NT_HEADERS* nthdr;
222 IMAGE_SECTION_HEADER* section;
223 unsigned i;
225 if (!(nthdr = RtlImageNtHeader(mapping))) goto error;
226 memcpy(&fmap->u.pe.ntheader, nthdr, sizeof(fmap->u.pe.ntheader));
227 section = (IMAGE_SECTION_HEADER*)
228 ((char*)&nthdr->OptionalHeader + nthdr->FileHeader.SizeOfOptionalHeader);
229 fmap->u.pe.sect = HeapAlloc(GetProcessHeap(), 0,
230 nthdr->FileHeader.NumberOfSections * sizeof(fmap->u.pe.sect[0]));
231 if (!fmap->u.pe.sect) goto error;
232 for (i = 0; i < nthdr->FileHeader.NumberOfSections; i++)
234 memcpy(&fmap->u.pe.sect[i].shdr, section + i, sizeof(IMAGE_SECTION_HEADER));
235 fmap->u.pe.sect[i].mapped = IMAGE_NO_MAP;
237 if (nthdr->FileHeader.PointerToSymbolTable && nthdr->FileHeader.NumberOfSymbols)
239 LARGE_INTEGER li;
241 if (GetFileSizeEx(file, &li) && pe_is_valid_pointer_table(nthdr, mapping, li.QuadPart))
243 /* FIXME ugly: should rather map the relevant content instead of copying it */
244 const char* src = (const char*)mapping +
245 nthdr->FileHeader.PointerToSymbolTable +
246 nthdr->FileHeader.NumberOfSymbols * sizeof(IMAGE_SYMBOL);
247 char* dst;
248 DWORD sz = *(DWORD*)src;
250 if ((dst = HeapAlloc(GetProcessHeap(), 0, sz)))
251 memcpy(dst, src, sz);
252 fmap->u.pe.strtable = dst;
254 else
256 WARN("Bad coff table... wipping out\n");
257 /* we have bad information here, wipe it out */
258 fmap->u.pe.ntheader.FileHeader.PointerToSymbolTable = 0;
259 fmap->u.pe.ntheader.FileHeader.NumberOfSymbols = 0;
260 fmap->u.pe.strtable = NULL;
263 else fmap->u.pe.strtable = NULL;
265 break;
266 default: assert(0); goto error;
268 pe_unmap_full(fmap);
270 return TRUE;
271 error:
272 pe_unmap_full(fmap);
273 CloseHandle(fmap->u.pe.hMap);
274 return FALSE;
277 /******************************************************************
278 * pe_unmap_file
280 * Unmaps an PE file from memory (previously mapped with pe_map_file)
282 static void pe_unmap_file(struct image_file_map* fmap)
284 if (fmap->u.pe.hMap != 0)
286 struct image_section_map ism;
287 ism.fmap = fmap;
288 for (ism.sidx = 0; ism.sidx < fmap->u.pe.ntheader.FileHeader.NumberOfSections; ism.sidx++)
290 pe_unmap_section(&ism);
292 while (fmap->u.pe.full_count) pe_unmap_full(fmap);
293 HeapFree(GetProcessHeap(), 0, fmap->u.pe.sect);
294 HeapFree(GetProcessHeap(), 0, (void*)fmap->u.pe.strtable); /* FIXME ugly (see pe_map_file) */
295 CloseHandle(fmap->u.pe.hMap);
296 fmap->u.pe.hMap = NULL;
300 /******************************************************************
301 * pe_map_directory
303 * Maps a directory content out of a PE file
305 const char* pe_map_directory(struct module* module, int dirno, DWORD* size)
307 IMAGE_NT_HEADERS* nth;
308 void* mapping;
310 if (module->type != DMT_PE || !module->format_info[DFI_PE]) return NULL;
311 if (dirno >= IMAGE_NUMBEROF_DIRECTORY_ENTRIES ||
312 !(mapping = pe_map_full(&module->format_info[DFI_PE]->u.pe_info->fmap, &nth)))
313 return NULL;
314 if (size) *size = nth->OptionalHeader.DataDirectory[dirno].Size;
315 return RtlImageRvaToVa(nth, mapping,
316 nth->OptionalHeader.DataDirectory[dirno].VirtualAddress, NULL);
319 /******************************************************************
320 * pe_unmap_directory
322 * Unmaps a directory content
324 void pe_unmap_directory(struct image_file_map* fmap, int dirno)
326 pe_unmap_full(fmap);
329 static void pe_module_remove(struct process* pcs, struct module_format* modfmt)
331 pe_unmap_file(&modfmt->u.pe_info->fmap);
332 HeapFree(GetProcessHeap(), 0, modfmt);
335 /******************************************************************
336 * pe_locate_with_coff_symbol_table
338 * Use the COFF symbol table (if any) from the IMAGE_FILE_HEADER to set the absolute address
339 * of global symbols.
340 * Mingw32 requires this for stabs debug information as address for global variables isn't filled in
341 * (this is similar to what is done in elf_module.c when using the .symtab ELF section)
343 static BOOL pe_locate_with_coff_symbol_table(struct module* module)
345 struct image_file_map* fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
346 const IMAGE_SYMBOL* isym;
347 int i, numsym, naux;
348 char tmp[9];
349 const char* name;
350 struct hash_table_iter hti;
351 void* ptr;
352 struct symt_data* sym;
353 const char* mapping;
355 numsym = fmap->u.pe.ntheader.FileHeader.NumberOfSymbols;
356 if (!fmap->u.pe.ntheader.FileHeader.PointerToSymbolTable || !numsym)
357 return TRUE;
358 if (!(mapping = pe_map_full(fmap, NULL))) return FALSE;
359 isym = (const IMAGE_SYMBOL*)(mapping + fmap->u.pe.ntheader.FileHeader.PointerToSymbolTable);
361 for (i = 0; i < numsym; i+= naux, isym += naux)
363 if (isym->StorageClass == IMAGE_SYM_CLASS_EXTERNAL &&
364 isym->SectionNumber > 0 && isym->SectionNumber <= fmap->u.pe.ntheader.FileHeader.NumberOfSections)
366 if (isym->N.Name.Short)
368 name = memcpy(tmp, isym->N.ShortName, 8);
369 tmp[8] = '\0';
371 else name = fmap->u.pe.strtable + isym->N.Name.Long;
372 if (name[0] == '_') name++;
373 hash_table_iter_init(&module->ht_symbols, &hti, name);
374 while ((ptr = hash_table_iter_up(&hti)))
376 sym = GET_ENTRY(ptr, struct symt_data, hash_elt);
377 if (sym->symt.tag == SymTagData &&
378 (sym->kind == DataIsGlobal || sym->kind == DataIsFileStatic) &&
379 sym->u.var.kind == loc_absolute &&
380 !strcmp(sym->hash_elt.name, name))
382 TRACE("Changing absolute address for %d.%s: %lx -> %s\n",
383 isym->SectionNumber, name, sym->u.var.offset,
384 wine_dbgstr_longlong(module->module.BaseOfImage +
385 fmap->u.pe.sect[isym->SectionNumber - 1].shdr.VirtualAddress +
386 isym->Value));
387 sym->u.var.offset = module->module.BaseOfImage +
388 fmap->u.pe.sect[isym->SectionNumber - 1].shdr.VirtualAddress + isym->Value;
389 break;
393 naux = isym->NumberOfAuxSymbols + 1;
395 pe_unmap_full(fmap);
396 return TRUE;
399 /******************************************************************
400 * pe_load_coff_symbol_table
402 * Load public symbols out of the COFF symbol table (if any).
404 static BOOL pe_load_coff_symbol_table(struct module* module)
406 struct image_file_map* fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
407 const IMAGE_SYMBOL* isym;
408 int i, numsym, naux;
409 const char* strtable;
410 char tmp[9];
411 const char* name;
412 const char* lastfilename = NULL;
413 struct symt_compiland* compiland = NULL;
414 const IMAGE_SECTION_HEADER* sect;
415 const char* mapping;
417 numsym = fmap->u.pe.ntheader.FileHeader.NumberOfSymbols;
418 if (!fmap->u.pe.ntheader.FileHeader.PointerToSymbolTable || !numsym)
419 return TRUE;
420 if (!(mapping = pe_map_full(fmap, NULL))) return FALSE;
421 isym = (const IMAGE_SYMBOL*)((const char*)mapping + fmap->u.pe.ntheader.FileHeader.PointerToSymbolTable);
422 /* FIXME: no way to get strtable size */
423 strtable = (const char*)&isym[numsym];
424 sect = IMAGE_FIRST_SECTION(RtlImageNtHeader((HMODULE)mapping));
426 for (i = 0; i < numsym; i+= naux, isym += naux)
428 if (isym->StorageClass == IMAGE_SYM_CLASS_FILE)
430 lastfilename = (const char*)(isym + 1);
431 compiland = NULL;
433 if (isym->StorageClass == IMAGE_SYM_CLASS_EXTERNAL &&
434 isym->SectionNumber > 0 && isym->SectionNumber <= fmap->u.pe.ntheader.FileHeader.NumberOfSections)
436 if (isym->N.Name.Short)
438 name = memcpy(tmp, isym->N.ShortName, 8);
439 tmp[8] = '\0';
441 else name = strtable + isym->N.Name.Long;
442 if (name[0] == '_') name++;
444 if (!compiland && lastfilename)
445 compiland = symt_new_compiland(module, 0,
446 source_new(module, NULL, lastfilename));
448 if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
449 symt_new_public(module, compiland, name,
450 module->module.BaseOfImage + sect[isym->SectionNumber - 1].VirtualAddress +
451 isym->Value,
454 naux = isym->NumberOfAuxSymbols + 1;
456 module->module.SymType = SymCoff;
457 module->module.LineNumbers = FALSE;
458 module->module.GlobalSymbols = FALSE;
459 module->module.TypeInfo = FALSE;
460 module->module.SourceIndexed = FALSE;
461 module->module.Publics = TRUE;
462 pe_unmap_full(fmap);
464 return TRUE;
467 /******************************************************************
468 * pe_load_stabs
470 * look for stabs information in PE header (it's how the mingw compiler provides
471 * its debugging information)
473 static BOOL pe_load_stabs(const struct process* pcs, struct module* module)
475 struct image_file_map* fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
476 struct image_section_map sect_stabs, sect_stabstr;
477 BOOL ret = FALSE;
479 if (pe_find_section(fmap, ".stab", &sect_stabs) && pe_find_section(fmap, ".stabstr", &sect_stabstr))
481 const char* stab;
482 const char* stabstr;
484 stab = image_map_section(&sect_stabs);
485 stabstr = image_map_section(&sect_stabstr);
486 if (stab != IMAGE_NO_MAP && stabstr != IMAGE_NO_MAP)
488 ret = stabs_parse(module,
489 module->module.BaseOfImage - fmap->u.pe.ntheader.OptionalHeader.ImageBase,
490 stab, image_get_map_size(&sect_stabs),
491 stabstr, image_get_map_size(&sect_stabstr),
492 NULL, NULL);
494 image_unmap_section(&sect_stabs);
495 image_unmap_section(&sect_stabstr);
496 if (ret) pe_locate_with_coff_symbol_table(module);
498 TRACE("%s the STABS debug info\n", ret ? "successfully loaded" : "failed to load");
500 return ret;
503 /******************************************************************
504 * pe_load_dwarf
506 * look for dwarf information in PE header (it's also a way for the mingw compiler
507 * to provide its debugging information)
509 static BOOL pe_load_dwarf(struct module* module)
511 struct image_file_map* fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
512 BOOL ret = FALSE;
514 ret = dwarf2_parse(module,
515 module->module.BaseOfImage - fmap->u.pe.ntheader.OptionalHeader.ImageBase,
516 NULL, /* FIXME: some thunks to deal with ? */
517 fmap);
518 TRACE("%s the DWARF debug info\n", ret ? "successfully loaded" : "failed to load");
520 return ret;
523 /******************************************************************
524 * pe_load_dbg_file
526 * loads a .dbg file
528 static BOOL pe_load_dbg_file(const struct process* pcs, struct module* module,
529 const char* dbg_name, DWORD timestamp)
531 char tmp[MAX_PATH];
532 HANDLE hFile = INVALID_HANDLE_VALUE, hMap = 0;
533 const BYTE* dbg_mapping = NULL;
534 BOOL ret = FALSE;
536 TRACE("Processing DBG file %s\n", debugstr_a(dbg_name));
538 if (path_find_symbol_file(pcs, dbg_name, NULL, timestamp, 0, tmp, &module->module.DbgUnmatched) &&
539 (hFile = CreateFileA(tmp, GENERIC_READ, FILE_SHARE_READ, NULL,
540 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) != INVALID_HANDLE_VALUE &&
541 ((hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) != 0) &&
542 ((dbg_mapping = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) != NULL))
544 const IMAGE_SEPARATE_DEBUG_HEADER* hdr;
545 const IMAGE_SECTION_HEADER* sectp;
546 const IMAGE_DEBUG_DIRECTORY* dbg;
548 hdr = (const IMAGE_SEPARATE_DEBUG_HEADER*)dbg_mapping;
549 /* section headers come immediately after debug header */
550 sectp = (const IMAGE_SECTION_HEADER*)(hdr + 1);
551 /* and after that and the exported names comes the debug directory */
552 dbg = (const IMAGE_DEBUG_DIRECTORY*)
553 (dbg_mapping + sizeof(*hdr) +
554 hdr->NumberOfSections * sizeof(IMAGE_SECTION_HEADER) +
555 hdr->ExportedNamesSize);
557 ret = pe_load_debug_directory(pcs, module, dbg_mapping, sectp,
558 hdr->NumberOfSections, dbg,
559 hdr->DebugDirectorySize / sizeof(*dbg));
561 else
562 ERR("Couldn't find .DBG file %s (%s)\n", debugstr_a(dbg_name), debugstr_a(tmp));
564 if (dbg_mapping) UnmapViewOfFile(dbg_mapping);
565 if (hMap) CloseHandle(hMap);
566 if (hFile != INVALID_HANDLE_VALUE) CloseHandle(hFile);
567 return ret;
570 /******************************************************************
571 * pe_load_msc_debug_info
573 * Process MSC debug information in PE file.
575 static BOOL pe_load_msc_debug_info(const struct process* pcs, struct module* module)
577 struct image_file_map* fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
578 BOOL ret = FALSE;
579 const IMAGE_DATA_DIRECTORY* dir;
580 const IMAGE_DEBUG_DIRECTORY*dbg = NULL;
581 int nDbg;
582 void* mapping;
583 IMAGE_NT_HEADERS* nth;
585 if (!(mapping = pe_map_full(fmap, &nth))) return FALSE;
586 /* Read in debug directory */
587 dir = nth->OptionalHeader.DataDirectory + IMAGE_DIRECTORY_ENTRY_DEBUG;
588 nDbg = dir->Size / sizeof(IMAGE_DEBUG_DIRECTORY);
589 if (!nDbg) goto done;
591 dbg = RtlImageRvaToVa(nth, mapping, dir->VirtualAddress, NULL);
593 /* Parse debug directory */
594 if (nth->FileHeader.Characteristics & IMAGE_FILE_DEBUG_STRIPPED)
596 /* Debug info is stripped to .DBG file */
597 const IMAGE_DEBUG_MISC* misc = (const IMAGE_DEBUG_MISC*)
598 ((const char*)mapping + dbg->PointerToRawData);
600 if (nDbg != 1 || dbg->Type != IMAGE_DEBUG_TYPE_MISC ||
601 misc->DataType != IMAGE_DEBUG_MISC_EXENAME)
603 ERR("-Debug info stripped, but no .DBG file in module %s\n",
604 debugstr_w(module->module.ModuleName));
606 else
608 ret = pe_load_dbg_file(pcs, module, (const char*)misc->Data, nth->FileHeader.TimeDateStamp);
611 else
613 const IMAGE_SECTION_HEADER *sectp = (const IMAGE_SECTION_HEADER*)((const char*)&nth->OptionalHeader + nth->FileHeader.SizeOfOptionalHeader);
614 /* Debug info is embedded into PE module */
615 ret = pe_load_debug_directory(pcs, module, mapping, sectp,
616 nth->FileHeader.NumberOfSections, dbg, nDbg);
618 done:
619 pe_unmap_full(fmap);
620 return ret;
623 /***********************************************************************
624 * pe_load_export_debug_info
626 static BOOL pe_load_export_debug_info(const struct process* pcs, struct module* module)
628 struct image_file_map* fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
629 unsigned int i;
630 const IMAGE_EXPORT_DIRECTORY* exports;
631 DWORD base = module->module.BaseOfImage;
632 DWORD size;
633 IMAGE_NT_HEADERS* nth;
634 void* mapping;
636 if (dbghelp_options & SYMOPT_NO_PUBLICS) return TRUE;
638 if (!(mapping = pe_map_full(fmap, &nth))) return FALSE;
639 #if 0
640 /* Add start of DLL (better use the (yet unimplemented) Exe SymTag for this) */
641 /* FIXME: module.ModuleName isn't correctly set yet if it's passed in SymLoadModule */
642 symt_new_public(module, NULL, module->module.ModuleName, base, 1);
643 #endif
645 /* Add entry point */
646 symt_new_public(module, NULL, "EntryPoint",
647 base + nth->OptionalHeader.AddressOfEntryPoint, 1);
648 #if 0
649 /* FIXME: we'd better store addresses linked to sections rather than
650 absolute values */
651 IMAGE_SECTION_HEADER* section;
652 /* Add start of sections */
653 section = (IMAGE_SECTION_HEADER*)
654 ((char*)&nth->OptionalHeader + nth->FileHeader.SizeOfOptionalHeader);
655 for (i = 0; i < nth->FileHeader.NumberOfSections; i++, section++)
657 symt_new_public(module, NULL, section->Name,
658 RtlImageRvaToVa(nth, mapping, section->VirtualAddress, NULL), 1);
660 #endif
662 /* Add exported functions */
663 if ((exports = RtlImageDirectoryEntryToData(mapping, FALSE,
664 IMAGE_DIRECTORY_ENTRY_EXPORT, &size)))
666 const WORD* ordinals = NULL;
667 const DWORD_PTR* functions = NULL;
668 const DWORD* names = NULL;
669 unsigned int j;
670 char buffer[16];
672 functions = RtlImageRvaToVa(nth, mapping, exports->AddressOfFunctions, NULL);
673 ordinals = RtlImageRvaToVa(nth, mapping, exports->AddressOfNameOrdinals, NULL);
674 names = RtlImageRvaToVa(nth, mapping, exports->AddressOfNames, NULL);
676 if (functions && ordinals && names)
678 for (i = 0; i < exports->NumberOfNames; i++)
680 if (!names[i]) continue;
681 symt_new_public(module, NULL,
682 RtlImageRvaToVa(nth, mapping, names[i], NULL),
683 base + functions[ordinals[i]], 1);
686 for (i = 0; i < exports->NumberOfFunctions; i++)
688 if (!functions[i]) continue;
689 /* Check if we already added it with a name */
690 for (j = 0; j < exports->NumberOfNames; j++)
691 if ((ordinals[j] == i) && names[j]) break;
692 if (j < exports->NumberOfNames) continue;
693 snprintf(buffer, sizeof(buffer), "%d", i + exports->Base);
694 symt_new_public(module, NULL, buffer, base + (DWORD)functions[i], 1);
698 /* no real debug info, only entry points */
699 if (module->module.SymType == SymDeferred)
700 module->module.SymType = SymExport;
701 pe_unmap_full(fmap);
703 return TRUE;
706 /******************************************************************
707 * pe_load_debug_info
710 BOOL pe_load_debug_info(const struct process* pcs, struct module* module)
712 BOOL ret = FALSE;
714 if (!(dbghelp_options & SYMOPT_PUBLICS_ONLY))
716 ret = pe_load_stabs(pcs, module);
717 ret = pe_load_dwarf(module) || ret;
718 ret = pe_load_msc_debug_info(pcs, module) || ret;
719 ret = ret || pe_load_coff_symbol_table(module); /* FIXME */
720 /* if we still have no debug info (we could only get SymExport at this
721 * point), then do the SymExport except if we have an ELF container,
722 * in which case we'll rely on the export's on the ELF side
725 /* FIXME shouldn't we check that? if (!module_get_debug(pcs, module)) */
726 if (pe_load_export_debug_info(pcs, module) && !ret)
727 ret = TRUE;
729 return ret;
732 /******************************************************************
733 * pe_load_native_module
736 struct module* pe_load_native_module(struct process* pcs, const WCHAR* name,
737 HANDLE hFile, DWORD64 base, DWORD size)
739 struct module* module = NULL;
740 BOOL opened = FALSE;
741 struct module_format* modfmt;
742 WCHAR loaded_name[MAX_PATH];
744 loaded_name[0] = '\0';
745 if (!hFile)
747 assert(name);
749 if ((hFile = FindExecutableImageExW(name, pcs->search_path, loaded_name, NULL, NULL)) == NULL)
750 return NULL;
751 opened = TRUE;
753 else if (name) strcpyW(loaded_name, name);
754 else if (dbghelp_options & SYMOPT_DEFERRED_LOADS)
755 FIXME("Trouble ahead (no module name passed in deferred mode)\n");
756 if (!(modfmt = HeapAlloc(GetProcessHeap(), 0, sizeof(struct module_format) + sizeof(struct pe_module_info))))
757 return NULL;
758 modfmt->u.pe_info = (struct pe_module_info*)(modfmt + 1);
759 if (pe_map_file(hFile, &modfmt->u.pe_info->fmap, DMT_PE))
761 if (!base) base = modfmt->u.pe_info->fmap.u.pe.ntheader.OptionalHeader.ImageBase;
762 if (!size) size = modfmt->u.pe_info->fmap.u.pe.ntheader.OptionalHeader.SizeOfImage;
764 module = module_new(pcs, loaded_name, DMT_PE, FALSE, base, size,
765 modfmt->u.pe_info->fmap.u.pe.ntheader.FileHeader.TimeDateStamp,
766 modfmt->u.pe_info->fmap.u.pe.ntheader.OptionalHeader.CheckSum);
767 if (module)
769 modfmt->module = module;
770 modfmt->remove = pe_module_remove;
771 modfmt->loc_compute = NULL;
773 module->format_info[DFI_PE] = modfmt;
774 if (dbghelp_options & SYMOPT_DEFERRED_LOADS)
775 module->module.SymType = SymDeferred;
776 else
777 pe_load_debug_info(pcs, module);
778 module->reloc_delta = base - modfmt->u.pe_info->fmap.u.pe.ntheader.OptionalHeader.ImageBase;
780 else
782 ERR("could not load the module '%s'\n", debugstr_w(loaded_name));
783 pe_unmap_file(&modfmt->u.pe_info->fmap);
786 if (!module) HeapFree(GetProcessHeap(), 0, modfmt);
788 if (opened) CloseHandle(hFile);
790 return module;
793 /******************************************************************
794 * pe_load_nt_header
797 BOOL pe_load_nt_header(HANDLE hProc, DWORD64 base, IMAGE_NT_HEADERS* nth)
799 IMAGE_DOS_HEADER dos;
801 return ReadProcessMemory(hProc, (char*)(DWORD_PTR)base, &dos, sizeof(dos), NULL) &&
802 dos.e_magic == IMAGE_DOS_SIGNATURE &&
803 ReadProcessMemory(hProc, (char*)(DWORD_PTR)(base + dos.e_lfanew),
804 nth, sizeof(*nth), NULL) &&
805 nth->Signature == IMAGE_NT_SIGNATURE;
808 /******************************************************************
809 * pe_load_builtin_module
812 struct module* pe_load_builtin_module(struct process* pcs, const WCHAR* name,
813 DWORD64 base, DWORD64 size)
815 struct module* module = NULL;
817 if (base && pcs->dbg_hdr_addr)
819 IMAGE_NT_HEADERS nth;
821 if (pe_load_nt_header(pcs->handle, base, &nth))
823 if (!size) size = nth.OptionalHeader.SizeOfImage;
824 module = module_new(pcs, name, DMT_PE, FALSE, base, size,
825 nth.FileHeader.TimeDateStamp,
826 nth.OptionalHeader.CheckSum);
829 return module;
832 /***********************************************************************
833 * ImageDirectoryEntryToDataEx (DBGHELP.@)
835 * Search for specified directory in PE image
837 * PARAMS
839 * base [in] Image base address
840 * image [in] TRUE - image has been loaded by loader, FALSE - raw file image
841 * dir [in] Target directory index
842 * size [out] Receives directory size
843 * section [out] Receives pointer to section header of section containing directory data
845 * RETURNS
846 * Success: pointer to directory data
847 * Failure: NULL
850 PVOID WINAPI ImageDirectoryEntryToDataEx( PVOID base, BOOLEAN image, USHORT dir, PULONG size, PIMAGE_SECTION_HEADER *section )
852 const IMAGE_NT_HEADERS *nt;
853 DWORD addr;
855 *size = 0;
856 if (section) *section = NULL;
858 if (!(nt = RtlImageNtHeader( base ))) return NULL;
859 if (dir >= nt->OptionalHeader.NumberOfRvaAndSizes) return NULL;
860 if (!(addr = nt->OptionalHeader.DataDirectory[dir].VirtualAddress)) return NULL;
862 *size = nt->OptionalHeader.DataDirectory[dir].Size;
863 if (image || addr < nt->OptionalHeader.SizeOfHeaders) return (char *)base + addr;
865 return RtlImageRvaToVa( nt, base, addr, section );
868 /***********************************************************************
869 * ImageDirectoryEntryToData (DBGHELP.@)
871 * NOTES
872 * See ImageDirectoryEntryToDataEx
874 PVOID WINAPI ImageDirectoryEntryToData( PVOID base, BOOLEAN image, USHORT dir, PULONG size )
876 return ImageDirectoryEntryToDataEx( base, image, dir, size, NULL );