makefiles: Don't use standard libs for programs that specify -nodefaultlibs.
[wine/zf.git] / dlls / dbghelp / pe_module.c
blobd6f297a213d63248da0d84ab7c726e7967837456
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 /******************************************************************
69 * pe_map_section
71 * Maps a single section into memory from an PE file
73 static 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 static 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 (!stricmp(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 static 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 static 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 static 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_unmap_file
185 * Unmaps an PE file from memory (previously mapped with pe_map_file)
187 static void pe_unmap_file(struct image_file_map* fmap)
189 if (fmap->u.pe.hMap != 0)
191 struct image_section_map ism;
192 ism.fmap = fmap;
193 for (ism.sidx = 0; ism.sidx < fmap->u.pe.ntheader.FileHeader.NumberOfSections; ism.sidx++)
195 pe_unmap_section(&ism);
197 while (fmap->u.pe.full_count) pe_unmap_full(fmap);
198 HeapFree(GetProcessHeap(), 0, fmap->u.pe.sect);
199 HeapFree(GetProcessHeap(), 0, (void*)fmap->u.pe.strtable); /* FIXME ugly (see pe_map_file) */
200 CloseHandle(fmap->u.pe.hMap);
201 fmap->u.pe.hMap = NULL;
205 static const struct image_file_map_ops pe_file_map_ops =
207 pe_map_section,
208 pe_unmap_section,
209 pe_find_section,
210 pe_get_map_rva,
211 pe_get_map_size,
212 pe_unmap_file,
215 /******************************************************************
216 * pe_is_valid_pointer_table
218 * Checks whether the PointerToSymbolTable and NumberOfSymbols in file_header contain
219 * valid information.
221 static BOOL pe_is_valid_pointer_table(const IMAGE_NT_HEADERS* nthdr, const void* mapping, DWORD64 sz)
223 DWORD64 offset;
225 /* is the iSym table inside file size ? (including first DWORD of string table, which is its size) */
226 offset = (DWORD64)nthdr->FileHeader.PointerToSymbolTable;
227 offset += (DWORD64)nthdr->FileHeader.NumberOfSymbols * sizeof(IMAGE_SYMBOL);
228 if (offset + sizeof(DWORD) > sz) return FALSE;
229 /* is string table (following iSym table) inside file size ? */
230 offset += *(DWORD*)((const char*)mapping + offset);
231 return offset <= sz;
234 /******************************************************************
235 * pe_map_file
237 * Maps an PE file into memory (and checks it's a real PE file)
239 BOOL pe_map_file(HANDLE file, struct image_file_map* fmap, enum module_type mt)
241 void* mapping;
243 fmap->modtype = mt;
244 fmap->ops = &pe_file_map_ops;
245 fmap->alternate = NULL;
246 fmap->u.pe.hMap = CreateFileMappingW(file, NULL, PAGE_READONLY, 0, 0, NULL);
247 if (fmap->u.pe.hMap == 0) return FALSE;
248 fmap->u.pe.full_count = 0;
249 fmap->u.pe.full_map = NULL;
250 if (!(mapping = pe_map_full(fmap, NULL))) goto error;
252 switch (mt)
254 case DMT_PE:
256 IMAGE_NT_HEADERS* nthdr;
257 IMAGE_SECTION_HEADER* section;
258 unsigned i;
260 if (!(nthdr = RtlImageNtHeader(mapping))) goto error;
261 memcpy(&fmap->u.pe.ntheader, nthdr, sizeof(fmap->u.pe.ntheader));
262 switch (nthdr->OptionalHeader.Magic)
264 case 0x10b: fmap->addr_size = 32; break;
265 case 0x20b: fmap->addr_size = 64; break;
266 default: return FALSE;
269 fmap->u.pe.builtin = !memcmp((const IMAGE_DOS_HEADER*)mapping + 1, builtin_signature, sizeof(builtin_signature));
270 section = (IMAGE_SECTION_HEADER*)
271 ((char*)&nthdr->OptionalHeader + nthdr->FileHeader.SizeOfOptionalHeader);
272 fmap->u.pe.sect = HeapAlloc(GetProcessHeap(), 0,
273 nthdr->FileHeader.NumberOfSections * sizeof(fmap->u.pe.sect[0]));
274 if (!fmap->u.pe.sect) goto error;
275 for (i = 0; i < nthdr->FileHeader.NumberOfSections; i++)
277 memcpy(&fmap->u.pe.sect[i].shdr, section + i, sizeof(IMAGE_SECTION_HEADER));
278 fmap->u.pe.sect[i].mapped = IMAGE_NO_MAP;
280 if (nthdr->FileHeader.PointerToSymbolTable && nthdr->FileHeader.NumberOfSymbols)
282 LARGE_INTEGER li;
284 if (GetFileSizeEx(file, &li) && pe_is_valid_pointer_table(nthdr, mapping, li.QuadPart))
286 /* FIXME ugly: should rather map the relevant content instead of copying it */
287 const char* src = (const char*)mapping +
288 nthdr->FileHeader.PointerToSymbolTable +
289 nthdr->FileHeader.NumberOfSymbols * sizeof(IMAGE_SYMBOL);
290 char* dst;
291 DWORD sz = *(DWORD*)src;
293 if ((dst = HeapAlloc(GetProcessHeap(), 0, sz)))
294 memcpy(dst, src, sz);
295 fmap->u.pe.strtable = dst;
297 else
299 WARN("Bad coff table... wipping out\n");
300 /* we have bad information here, wipe it out */
301 fmap->u.pe.ntheader.FileHeader.PointerToSymbolTable = 0;
302 fmap->u.pe.ntheader.FileHeader.NumberOfSymbols = 0;
303 fmap->u.pe.strtable = NULL;
306 else fmap->u.pe.strtable = NULL;
308 break;
309 default: assert(0); goto error;
311 pe_unmap_full(fmap);
313 return TRUE;
314 error:
315 pe_unmap_full(fmap);
316 CloseHandle(fmap->u.pe.hMap);
317 return FALSE;
320 /******************************************************************
321 * pe_map_directory
323 * Maps a directory content out of a PE file
325 const char* pe_map_directory(struct module* module, int dirno, DWORD* size)
327 IMAGE_NT_HEADERS* nth;
328 void* mapping;
330 if (module->type != DMT_PE || !module->format_info[DFI_PE]) return NULL;
331 if (dirno >= IMAGE_NUMBEROF_DIRECTORY_ENTRIES ||
332 !(mapping = pe_map_full(&module->format_info[DFI_PE]->u.pe_info->fmap, &nth)))
333 return NULL;
334 if (size) *size = nth->OptionalHeader.DataDirectory[dirno].Size;
335 return RtlImageRvaToVa(nth, mapping,
336 nth->OptionalHeader.DataDirectory[dirno].VirtualAddress, NULL);
339 static void pe_module_remove(struct process* pcs, struct module_format* modfmt)
341 image_unmap_file(&modfmt->u.pe_info->fmap);
342 HeapFree(GetProcessHeap(), 0, modfmt);
345 /******************************************************************
346 * pe_locate_with_coff_symbol_table
348 * Use the COFF symbol table (if any) from the IMAGE_FILE_HEADER to set the absolute address
349 * of global symbols.
350 * Mingw32 requires this for stabs debug information as address for global variables isn't filled in
351 * (this is similar to what is done in elf_module.c when using the .symtab ELF section)
353 static BOOL pe_locate_with_coff_symbol_table(struct module* module)
355 struct image_file_map* fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
356 const IMAGE_SYMBOL* isym;
357 int i, numsym, naux;
358 char tmp[9];
359 const char* name;
360 struct hash_table_iter hti;
361 void* ptr;
362 struct symt_data* sym;
363 const char* mapping;
365 numsym = fmap->u.pe.ntheader.FileHeader.NumberOfSymbols;
366 if (!fmap->u.pe.ntheader.FileHeader.PointerToSymbolTable || !numsym)
367 return TRUE;
368 if (!(mapping = pe_map_full(fmap, NULL))) return FALSE;
369 isym = (const IMAGE_SYMBOL*)(mapping + fmap->u.pe.ntheader.FileHeader.PointerToSymbolTable);
371 for (i = 0; i < numsym; i+= naux, isym += naux)
373 if (isym->StorageClass == IMAGE_SYM_CLASS_EXTERNAL &&
374 isym->SectionNumber > 0 && isym->SectionNumber <= fmap->u.pe.ntheader.FileHeader.NumberOfSections)
376 if (isym->N.Name.Short)
378 name = memcpy(tmp, isym->N.ShortName, 8);
379 tmp[8] = '\0';
381 else name = fmap->u.pe.strtable + isym->N.Name.Long;
382 if (name[0] == '_') name++;
383 hash_table_iter_init(&module->ht_symbols, &hti, name);
384 while ((ptr = hash_table_iter_up(&hti)))
386 sym = CONTAINING_RECORD(ptr, struct symt_data, hash_elt);
387 if (sym->symt.tag == SymTagData &&
388 (sym->kind == DataIsGlobal || sym->kind == DataIsFileStatic) &&
389 sym->u.var.kind == loc_absolute &&
390 !strcmp(sym->hash_elt.name, name))
392 TRACE("Changing absolute address for %d.%s: %lx -> %s\n",
393 isym->SectionNumber, name, sym->u.var.offset,
394 wine_dbgstr_longlong(module->module.BaseOfImage +
395 fmap->u.pe.sect[isym->SectionNumber - 1].shdr.VirtualAddress +
396 isym->Value));
397 sym->u.var.offset = module->module.BaseOfImage +
398 fmap->u.pe.sect[isym->SectionNumber - 1].shdr.VirtualAddress + isym->Value;
399 break;
403 naux = isym->NumberOfAuxSymbols + 1;
405 pe_unmap_full(fmap);
406 return TRUE;
409 /******************************************************************
410 * pe_load_coff_symbol_table
412 * Load public symbols out of the COFF symbol table (if any).
414 static BOOL pe_load_coff_symbol_table(struct module* module)
416 struct image_file_map* fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
417 const IMAGE_SYMBOL* isym;
418 int i, numsym, naux;
419 const char* strtable;
420 char tmp[9];
421 const char* name;
422 const char* lastfilename = NULL;
423 struct symt_compiland* compiland = NULL;
424 const IMAGE_SECTION_HEADER* sect;
425 const char* mapping;
427 numsym = fmap->u.pe.ntheader.FileHeader.NumberOfSymbols;
428 if (!fmap->u.pe.ntheader.FileHeader.PointerToSymbolTable || !numsym)
429 return TRUE;
430 if (!(mapping = pe_map_full(fmap, NULL))) return FALSE;
431 isym = (const IMAGE_SYMBOL*)((const char*)mapping + fmap->u.pe.ntheader.FileHeader.PointerToSymbolTable);
432 /* FIXME: no way to get strtable size */
433 strtable = (const char*)&isym[numsym];
434 sect = IMAGE_FIRST_SECTION(RtlImageNtHeader((HMODULE)mapping));
436 for (i = 0; i < numsym; i+= naux, isym += naux)
438 if (isym->StorageClass == IMAGE_SYM_CLASS_FILE)
440 lastfilename = (const char*)(isym + 1);
441 compiland = NULL;
443 if (isym->StorageClass == IMAGE_SYM_CLASS_EXTERNAL &&
444 isym->SectionNumber > 0 && isym->SectionNumber <= fmap->u.pe.ntheader.FileHeader.NumberOfSections)
446 if (isym->N.Name.Short)
448 name = memcpy(tmp, isym->N.ShortName, 8);
449 tmp[8] = '\0';
451 else name = strtable + isym->N.Name.Long;
452 if (name[0] == '_') name++;
454 if (!compiland && lastfilename)
455 compiland = symt_new_compiland(module, 0,
456 source_new(module, NULL, lastfilename));
458 if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
459 symt_new_public(module, compiland, name, FALSE,
460 module->module.BaseOfImage + sect[isym->SectionNumber - 1].VirtualAddress +
461 isym->Value,
464 naux = isym->NumberOfAuxSymbols + 1;
466 module->module.SymType = SymCoff;
467 module->module.LineNumbers = FALSE;
468 module->module.GlobalSymbols = FALSE;
469 module->module.TypeInfo = FALSE;
470 module->module.SourceIndexed = FALSE;
471 module->module.Publics = TRUE;
472 pe_unmap_full(fmap);
474 return TRUE;
477 /******************************************************************
478 * pe_load_stabs
480 * look for stabs information in PE header (it's how the mingw compiler provides
481 * its debugging information)
483 static BOOL pe_load_stabs(const struct process* pcs, struct module* module)
485 struct image_file_map* fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
486 struct image_section_map sect_stabs, sect_stabstr;
487 BOOL ret = FALSE;
489 if (pe_find_section(fmap, ".stab", &sect_stabs) && pe_find_section(fmap, ".stabstr", &sect_stabstr))
491 const char* stab;
492 const char* stabstr;
494 stab = image_map_section(&sect_stabs);
495 stabstr = image_map_section(&sect_stabstr);
496 if (stab != IMAGE_NO_MAP && stabstr != IMAGE_NO_MAP)
498 ret = stabs_parse(module,
499 module->module.BaseOfImage - fmap->u.pe.ntheader.OptionalHeader.ImageBase,
500 stab, image_get_map_size(&sect_stabs) / sizeof(struct stab_nlist), sizeof(struct stab_nlist),
501 stabstr, image_get_map_size(&sect_stabstr),
502 NULL, NULL);
504 image_unmap_section(&sect_stabs);
505 image_unmap_section(&sect_stabstr);
506 if (ret) pe_locate_with_coff_symbol_table(module);
508 TRACE("%s the STABS debug info\n", ret ? "successfully loaded" : "failed to load");
510 return ret;
513 /******************************************************************
514 * pe_load_dwarf
516 * look for dwarf information in PE header (it's also a way for the mingw compiler
517 * to provide its debugging information)
519 static BOOL pe_load_dwarf(struct module* module)
521 struct image_file_map* fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
522 BOOL ret;
524 ret = dwarf2_parse(module,
525 module->module.BaseOfImage - fmap->u.pe.ntheader.OptionalHeader.ImageBase,
526 NULL, /* FIXME: some thunks to deal with ? */
527 fmap);
528 TRACE("%s the DWARF debug info\n", ret ? "successfully loaded" : "failed to load");
530 return ret;
533 /******************************************************************
534 * pe_load_dbg_file
536 * loads a .dbg file
538 static BOOL pe_load_dbg_file(const struct process* pcs, struct module* module,
539 const char* dbg_name, DWORD timestamp)
541 WCHAR tmp[MAX_PATH];
542 HANDLE hFile = INVALID_HANDLE_VALUE, hMap = 0;
543 const BYTE* dbg_mapping = NULL;
544 BOOL ret = FALSE;
546 TRACE("Processing DBG file %s\n", debugstr_a(dbg_name));
548 if (path_find_symbol_file(pcs, module, dbg_name, DMT_DBG, NULL, timestamp, 0, tmp, &module->module.DbgUnmatched) &&
549 (hFile = CreateFileW(tmp, GENERIC_READ, FILE_SHARE_READ, NULL,
550 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) != INVALID_HANDLE_VALUE &&
551 ((hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) != 0) &&
552 ((dbg_mapping = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) != NULL))
554 const IMAGE_SEPARATE_DEBUG_HEADER* hdr;
555 const IMAGE_SECTION_HEADER* sectp;
556 const IMAGE_DEBUG_DIRECTORY* dbg;
558 hdr = (const IMAGE_SEPARATE_DEBUG_HEADER*)dbg_mapping;
559 /* section headers come immediately after debug header */
560 sectp = (const IMAGE_SECTION_HEADER*)(hdr + 1);
561 /* and after that and the exported names comes the debug directory */
562 dbg = (const IMAGE_DEBUG_DIRECTORY*)
563 (dbg_mapping + sizeof(*hdr) +
564 hdr->NumberOfSections * sizeof(IMAGE_SECTION_HEADER) +
565 hdr->ExportedNamesSize);
567 ret = pe_load_debug_directory(pcs, module, dbg_mapping, sectp,
568 hdr->NumberOfSections, dbg,
569 hdr->DebugDirectorySize / sizeof(*dbg));
571 else
572 ERR("Couldn't find .DBG file %s (%s)\n", debugstr_a(dbg_name), debugstr_w(tmp));
574 if (dbg_mapping) UnmapViewOfFile(dbg_mapping);
575 if (hMap) CloseHandle(hMap);
576 if (hFile != INVALID_HANDLE_VALUE) CloseHandle(hFile);
577 return ret;
580 /******************************************************************
581 * pe_load_msc_debug_info
583 * Process MSC debug information in PE file.
585 static BOOL pe_load_msc_debug_info(const struct process* pcs, struct module* module)
587 struct image_file_map* fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
588 BOOL ret = FALSE;
589 const IMAGE_DEBUG_DIRECTORY*dbg;
590 ULONG nDbg;
591 void* mapping;
592 IMAGE_NT_HEADERS* nth;
594 if (!(mapping = pe_map_full(fmap, &nth))) return FALSE;
595 /* Read in debug directory */
596 dbg = RtlImageDirectoryEntryToData( mapping, FALSE, IMAGE_DIRECTORY_ENTRY_DEBUG, &nDbg );
597 if (!dbg || !(nDbg /= sizeof(IMAGE_DEBUG_DIRECTORY))) goto done;
599 /* Parse debug directory */
600 if (nth->FileHeader.Characteristics & IMAGE_FILE_DEBUG_STRIPPED)
602 /* Debug info is stripped to .DBG file */
603 const IMAGE_DEBUG_MISC* misc = (const IMAGE_DEBUG_MISC*)
604 ((const char*)mapping + dbg->PointerToRawData);
606 if (nDbg != 1 || dbg->Type != IMAGE_DEBUG_TYPE_MISC ||
607 misc->DataType != IMAGE_DEBUG_MISC_EXENAME)
609 ERR("-Debug info stripped, but no .DBG file in module %s\n",
610 debugstr_w(module->module.ModuleName));
612 else
614 ret = pe_load_dbg_file(pcs, module, (const char*)misc->Data, nth->FileHeader.TimeDateStamp);
617 else
619 const IMAGE_SECTION_HEADER *sectp = (const IMAGE_SECTION_HEADER*)((const char*)&nth->OptionalHeader + nth->FileHeader.SizeOfOptionalHeader);
620 /* Debug info is embedded into PE module */
621 ret = pe_load_debug_directory(pcs, module, mapping, sectp,
622 nth->FileHeader.NumberOfSections, dbg, nDbg);
624 done:
625 pe_unmap_full(fmap);
626 return ret;
629 /***********************************************************************
630 * pe_load_export_debug_info
632 static BOOL pe_load_export_debug_info(const struct process* pcs, struct module* module)
634 struct image_file_map* fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
635 unsigned int i;
636 const IMAGE_EXPORT_DIRECTORY* exports;
637 DWORD base = module->module.BaseOfImage;
638 DWORD size;
639 IMAGE_NT_HEADERS* nth;
640 void* mapping;
642 if (dbghelp_options & SYMOPT_NO_PUBLICS) return TRUE;
644 if (!(mapping = pe_map_full(fmap, &nth))) return FALSE;
645 #if 0
646 /* Add start of DLL (better use the (yet unimplemented) Exe SymTag for this) */
647 /* FIXME: module.ModuleName isn't correctly set yet if it's passed in SymLoadModule */
648 symt_new_public(module, NULL, module->module.ModuleName, FALSE, base, 1);
649 #endif
651 /* Add entry point */
652 symt_new_public(module, NULL, "EntryPoint", FALSE,
653 base + nth->OptionalHeader.AddressOfEntryPoint, 1);
654 #if 0
655 /* FIXME: we'd better store addresses linked to sections rather than
656 absolute values */
657 IMAGE_SECTION_HEADER* section;
658 /* Add start of sections */
659 section = (IMAGE_SECTION_HEADER*)
660 ((char*)&nth->OptionalHeader + nth->FileHeader.SizeOfOptionalHeader);
661 for (i = 0; i < nth->FileHeader.NumberOfSections; i++, section++)
663 symt_new_public(module, NULL, section->Name, FALSE,
664 RtlImageRvaToVa(nth, mapping, section->VirtualAddress, NULL), 1);
666 #endif
668 /* Add exported functions */
669 if ((exports = RtlImageDirectoryEntryToData(mapping, FALSE,
670 IMAGE_DIRECTORY_ENTRY_EXPORT, &size)))
672 const WORD* ordinals = NULL;
673 const DWORD_PTR* functions = NULL;
674 const DWORD* names = NULL;
675 unsigned int j;
676 char buffer[16];
678 functions = RtlImageRvaToVa(nth, mapping, exports->AddressOfFunctions, NULL);
679 ordinals = RtlImageRvaToVa(nth, mapping, exports->AddressOfNameOrdinals, NULL);
680 names = RtlImageRvaToVa(nth, mapping, exports->AddressOfNames, NULL);
682 if (functions && ordinals && names)
684 for (i = 0; i < exports->NumberOfNames; i++)
686 if (!names[i]) continue;
687 symt_new_public(module, NULL,
688 RtlImageRvaToVa(nth, mapping, names[i], NULL),
689 FALSE,
690 base + functions[ordinals[i]], 1);
693 for (i = 0; i < exports->NumberOfFunctions; i++)
695 if (!functions[i]) continue;
696 /* Check if we already added it with a name */
697 for (j = 0; j < exports->NumberOfNames; j++)
698 if ((ordinals[j] == i) && names[j]) break;
699 if (j < exports->NumberOfNames) continue;
700 snprintf(buffer, sizeof(buffer), "%d", i + exports->Base);
701 symt_new_public(module, NULL, buffer, FALSE, base + (DWORD)functions[i], 1);
705 /* no real debug info, only entry points */
706 if (module->module.SymType == SymDeferred)
707 module->module.SymType = SymExport;
708 pe_unmap_full(fmap);
710 return TRUE;
713 /******************************************************************
714 * pe_load_debug_info
717 BOOL pe_load_debug_info(const struct process* pcs, struct module* module)
719 BOOL ret = FALSE;
721 if (!(dbghelp_options & SYMOPT_PUBLICS_ONLY))
723 ret = image_check_alternate(&module->format_info[DFI_PE]->u.pe_info->fmap, module);
724 ret = pe_load_stabs(pcs, module) || ret;
725 ret = pe_load_dwarf(module) || ret;
726 ret = pe_load_msc_debug_info(pcs, module) || ret;
727 ret = ret || pe_load_coff_symbol_table(module); /* FIXME */
728 /* if we still have no debug info (we could only get SymExport at this
729 * point), then do the SymExport except if we have an ELF container,
730 * in which case we'll rely on the export's on the ELF side
733 /* FIXME shouldn't we check that? if (!module_get_debug(pcs, module)) */
734 if (pe_load_export_debug_info(pcs, module) && !ret)
735 ret = TRUE;
737 return ret;
740 struct builtin_search
742 WCHAR *path;
743 struct image_file_map fmap;
746 static BOOL search_builtin_pe(void *param, HANDLE handle, const WCHAR *path)
748 struct builtin_search *search = param;
749 size_t size;
751 if (!pe_map_file(handle, &search->fmap, DMT_PE)) return FALSE;
753 size = (lstrlenW(path) + 1) * sizeof(WCHAR);
754 if ((search->path = heap_alloc(size)))
755 memcpy(search->path, path, size);
756 return TRUE;
759 /******************************************************************
760 * pe_load_native_module
763 struct module* pe_load_native_module(struct process* pcs, const WCHAR* name,
764 HANDLE hFile, DWORD64 base, DWORD size)
766 struct module* module = NULL;
767 BOOL opened = FALSE;
768 struct module_format* modfmt;
769 WCHAR loaded_name[MAX_PATH];
771 loaded_name[0] = '\0';
772 if (!hFile)
774 assert(name);
776 if ((hFile = FindExecutableImageExW(name, pcs->search_path, loaded_name, NULL, NULL)) == NULL)
777 return NULL;
778 opened = TRUE;
780 else if (name) lstrcpyW(loaded_name, name);
781 else if (dbghelp_options & SYMOPT_DEFERRED_LOADS)
782 FIXME("Trouble ahead (no module name passed in deferred mode)\n");
783 if (!(modfmt = HeapAlloc(GetProcessHeap(), 0, sizeof(struct module_format) + sizeof(struct pe_module_info))))
784 return NULL;
785 modfmt->u.pe_info = (struct pe_module_info*)(modfmt + 1);
786 if (pe_map_file(hFile, &modfmt->u.pe_info->fmap, DMT_PE))
788 struct builtin_search builtin = { NULL };
789 if (modfmt->u.pe_info->fmap.u.pe.builtin && search_dll_path(loaded_name, search_builtin_pe, &builtin))
791 TRACE("reloaded %s from %s\n", debugstr_w(loaded_name), debugstr_w(builtin.path));
792 image_unmap_file(&modfmt->u.pe_info->fmap);
793 modfmt->u.pe_info->fmap = builtin.fmap;
795 if (!base) base = modfmt->u.pe_info->fmap.u.pe.ntheader.OptionalHeader.ImageBase;
796 if (!size) size = modfmt->u.pe_info->fmap.u.pe.ntheader.OptionalHeader.SizeOfImage;
798 module = module_new(pcs, loaded_name, DMT_PE, FALSE, base, size,
799 modfmt->u.pe_info->fmap.u.pe.ntheader.FileHeader.TimeDateStamp,
800 modfmt->u.pe_info->fmap.u.pe.ntheader.OptionalHeader.CheckSum);
801 if (module)
803 module->real_path = builtin.path;
804 modfmt->module = module;
805 modfmt->remove = pe_module_remove;
806 modfmt->loc_compute = NULL;
808 module->format_info[DFI_PE] = modfmt;
809 if (dbghelp_options & SYMOPT_DEFERRED_LOADS)
810 module->module.SymType = SymDeferred;
811 else
812 pe_load_debug_info(pcs, module);
813 module->reloc_delta = base - modfmt->u.pe_info->fmap.u.pe.ntheader.OptionalHeader.ImageBase;
815 else
817 ERR("could not load the module '%s'\n", debugstr_w(loaded_name));
818 heap_free(builtin.path);
819 image_unmap_file(&modfmt->u.pe_info->fmap);
822 if (!module) HeapFree(GetProcessHeap(), 0, modfmt);
824 if (opened) CloseHandle(hFile);
826 return module;
829 /******************************************************************
830 * pe_load_nt_header
833 BOOL pe_load_nt_header(HANDLE hProc, DWORD64 base, IMAGE_NT_HEADERS* nth)
835 IMAGE_DOS_HEADER dos;
837 return ReadProcessMemory(hProc, (char*)(DWORD_PTR)base, &dos, sizeof(dos), NULL) &&
838 dos.e_magic == IMAGE_DOS_SIGNATURE &&
839 ReadProcessMemory(hProc, (char*)(DWORD_PTR)(base + dos.e_lfanew),
840 nth, sizeof(*nth), NULL) &&
841 nth->Signature == IMAGE_NT_SIGNATURE;
844 /******************************************************************
845 * pe_load_builtin_module
848 struct module* pe_load_builtin_module(struct process* pcs, const WCHAR* name,
849 DWORD64 base, DWORD64 size)
851 struct module* module = NULL;
853 if (base && pcs->dbg_hdr_addr)
855 IMAGE_NT_HEADERS nth;
857 if (pe_load_nt_header(pcs->handle, base, &nth))
859 if (!size) size = nth.OptionalHeader.SizeOfImage;
860 module = module_new(pcs, name, DMT_PE, FALSE, base, size,
861 nth.FileHeader.TimeDateStamp,
862 nth.OptionalHeader.CheckSum);
865 return module;
868 /***********************************************************************
869 * ImageDirectoryEntryToDataEx (DBGHELP.@)
871 * Search for specified directory in PE image
873 * PARAMS
875 * base [in] Image base address
876 * image [in] TRUE - image has been loaded by loader, FALSE - raw file image
877 * dir [in] Target directory index
878 * size [out] Receives directory size
879 * section [out] Receives pointer to section header of section containing directory data
881 * RETURNS
882 * Success: pointer to directory data
883 * Failure: NULL
886 PVOID WINAPI ImageDirectoryEntryToDataEx( PVOID base, BOOLEAN image, USHORT dir, PULONG size, PIMAGE_SECTION_HEADER *section )
888 const IMAGE_NT_HEADERS *nt;
889 DWORD addr;
891 *size = 0;
892 if (section) *section = NULL;
894 if (!(nt = RtlImageNtHeader( base ))) return NULL;
895 if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
897 const IMAGE_NT_HEADERS64 *nt64 = (const IMAGE_NT_HEADERS64 *)nt;
899 if (dir >= nt64->OptionalHeader.NumberOfRvaAndSizes) return NULL;
900 if (!(addr = nt64->OptionalHeader.DataDirectory[dir].VirtualAddress)) return NULL;
901 *size = nt64->OptionalHeader.DataDirectory[dir].Size;
902 if (image || addr < nt64->OptionalHeader.SizeOfHeaders) return (char *)base + addr;
904 else if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC)
906 const IMAGE_NT_HEADERS32 *nt32 = (const IMAGE_NT_HEADERS32 *)nt;
908 if (dir >= nt32->OptionalHeader.NumberOfRvaAndSizes) return NULL;
909 if (!(addr = nt32->OptionalHeader.DataDirectory[dir].VirtualAddress)) return NULL;
910 *size = nt32->OptionalHeader.DataDirectory[dir].Size;
911 if (image || addr < nt32->OptionalHeader.SizeOfHeaders) return (char *)base + addr;
913 else return NULL;
915 return RtlImageRvaToVa( nt, base, addr, section );
918 /***********************************************************************
919 * ImageDirectoryEntryToData (DBGHELP.@)
921 * NOTES
922 * See ImageDirectoryEntryToDataEx
924 PVOID WINAPI ImageDirectoryEntryToData( PVOID base, BOOLEAN image, USHORT dir, PULONG size )
926 return ImageDirectoryEntryToDataEx( base, image, dir, size, NULL );