http.sys: Keep connection sockets open after sending a 400 response.
[wine.git] / dlls / dbghelp / pe_module.c
blob909ac794c38131109d3e4600c7de35ec932cf7c4
1 /*
2 * File pe_module.c - handle PE module information
4 * Copyright (C) 1996, Eric Youngdale.
5 * Copyright (C) 1999-2000, Ulrich Weigand.
6 * Copyright (C) 2004-2007, Eric Pouech.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <assert.h>
29 #include "ntstatus.h"
30 #define WIN32_NO_STATUS
31 #include "dbghelp_private.h"
32 #include "image_private.h"
33 #include "winternl.h"
34 #include "wine/debug.h"
35 #include "wine/heap.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
39 struct pe_module_info
41 struct image_file_map fmap;
44 static const char builtin_signature[] = "Wine builtin DLL";
46 static void* pe_map_full(struct image_file_map* fmap, IMAGE_NT_HEADERS** nth)
48 if (!fmap->u.pe.full_map)
50 fmap->u.pe.full_map = MapViewOfFile(fmap->u.pe.hMap, FILE_MAP_READ, 0, 0, 0);
52 if (fmap->u.pe.full_map)
54 if (nth) *nth = RtlImageNtHeader(fmap->u.pe.full_map);
55 fmap->u.pe.full_count++;
56 return fmap->u.pe.full_map;
58 return NULL;
61 static void pe_unmap_full(struct image_file_map* fmap)
63 if (fmap->u.pe.full_count && !--fmap->u.pe.full_count)
65 UnmapViewOfFile(fmap->u.pe.full_map);
66 fmap->u.pe.full_map = NULL;
70 /* as we store either IMAGE_OPTIONAL_HEADER(32|64) inside pe_file_map,
71 * this helper will read to any field 'field' inside such an header
73 #define PE_FROM_OPTHDR(fmap, field) (((fmap)->addr_size == 32) ? ((fmap)->u.pe.opt.header32. field) : ((fmap)->u.pe.opt.header64. field))
75 /******************************************************************
76 * pe_map_section
78 * Maps a single section into memory from an PE file
80 static const char* pe_map_section(struct image_section_map* ism)
82 void* mapping;
83 struct pe_file_map* fmap = &ism->fmap->u.pe;
85 if (ism->sidx >= 0 && ism->sidx < fmap->file_header.NumberOfSections &&
86 fmap->sect[ism->sidx].mapped == IMAGE_NO_MAP)
88 IMAGE_NT_HEADERS* nth;
90 if (fmap->sect[ism->sidx].shdr.Misc.VirtualSize > fmap->sect[ism->sidx].shdr.SizeOfRawData)
92 FIXME("Section %Id: virtual (0x%lx) > raw (0x%lx) size - not supported\n",
93 ism->sidx, fmap->sect[ism->sidx].shdr.Misc.VirtualSize,
94 fmap->sect[ism->sidx].shdr.SizeOfRawData);
95 return IMAGE_NO_MAP;
97 /* FIXME: that's rather drastic, but that will do for now
98 * that's ok if the full file map exists, but we could be less aggressive otherwise and
99 * only map the relevant section
101 if ((mapping = pe_map_full(ism->fmap, &nth)))
103 fmap->sect[ism->sidx].mapped = RtlImageRvaToVa(nth, mapping,
104 fmap->sect[ism->sidx].shdr.VirtualAddress,
105 NULL);
106 return fmap->sect[ism->sidx].mapped;
109 return IMAGE_NO_MAP;
112 /******************************************************************
113 * pe_find_section
115 * Finds a section by name (and type) into memory from an PE file
116 * or its alternate if any
118 static BOOL pe_find_section(struct image_file_map* fmap, const char* name,
119 struct image_section_map* ism)
121 const char* sectname;
122 unsigned i;
123 char tmp[IMAGE_SIZEOF_SHORT_NAME + 1];
125 for (i = 0; i < fmap->u.pe.file_header.NumberOfSections; i++)
127 sectname = (const char*)fmap->u.pe.sect[i].shdr.Name;
128 /* long section names start with a '/' (at least on MinGW32) */
129 if (sectname[0] == '/' && fmap->u.pe.strtable)
130 sectname = fmap->u.pe.strtable + atoi(sectname + 1);
131 else
133 /* the section name may not be null terminated */
134 sectname = memcpy(tmp, sectname, IMAGE_SIZEOF_SHORT_NAME);
135 tmp[IMAGE_SIZEOF_SHORT_NAME] = '\0';
137 if (!stricmp(sectname, name))
139 ism->fmap = fmap;
140 ism->sidx = i;
141 return TRUE;
144 ism->fmap = NULL;
145 ism->sidx = -1;
147 return FALSE;
150 /******************************************************************
151 * pe_unmap_section
153 * Unmaps a single section from memory
155 static void pe_unmap_section(struct image_section_map* ism)
157 if (ism->sidx >= 0 && ism->sidx < ism->fmap->u.pe.file_header.NumberOfSections &&
158 ism->fmap->u.pe.sect[ism->sidx].mapped != IMAGE_NO_MAP)
160 pe_unmap_full(ism->fmap);
161 ism->fmap->u.pe.sect[ism->sidx].mapped = IMAGE_NO_MAP;
165 /******************************************************************
166 * pe_get_map_rva
168 * Get the RVA of an PE section
170 static DWORD_PTR pe_get_map_rva(const struct image_section_map* ism)
172 if (ism->sidx < 0 || ism->sidx >= ism->fmap->u.pe.file_header.NumberOfSections)
173 return 0;
174 return ism->fmap->u.pe.sect[ism->sidx].shdr.VirtualAddress;
177 /******************************************************************
178 * pe_get_map_size
180 * Get the size of a PE section
182 static unsigned pe_get_map_size(const struct image_section_map* ism)
184 if (ism->sidx < 0 || ism->sidx >= ism->fmap->u.pe.file_header.NumberOfSections)
185 return 0;
186 return ism->fmap->u.pe.sect[ism->sidx].shdr.Misc.VirtualSize;
189 /******************************************************************
190 * pe_unmap_file
192 * Unmaps an PE file from memory (previously mapped with pe_map_file)
194 static void pe_unmap_file(struct image_file_map* fmap)
196 if (fmap->u.pe.hMap != 0)
198 struct image_section_map ism;
199 ism.fmap = fmap;
200 for (ism.sidx = 0; ism.sidx < fmap->u.pe.file_header.NumberOfSections; ism.sidx++)
202 pe_unmap_section(&ism);
204 while (fmap->u.pe.full_count) pe_unmap_full(fmap);
205 HeapFree(GetProcessHeap(), 0, fmap->u.pe.sect);
206 HeapFree(GetProcessHeap(), 0, (void*)fmap->u.pe.strtable); /* FIXME ugly (see pe_map_file) */
207 CloseHandle(fmap->u.pe.hMap);
208 fmap->u.pe.hMap = NULL;
212 static const struct image_file_map_ops pe_file_map_ops =
214 pe_map_section,
215 pe_unmap_section,
216 pe_find_section,
217 pe_get_map_rva,
218 pe_get_map_size,
219 pe_unmap_file,
222 /******************************************************************
223 * pe_is_valid_pointer_table
225 * Checks whether the PointerToSymbolTable and NumberOfSymbols in file_header contain
226 * valid information.
228 static BOOL pe_is_valid_pointer_table(const IMAGE_NT_HEADERS* nthdr, const void* mapping, DWORD64 sz)
230 DWORD64 offset;
232 /* is the iSym table inside file size ? (including first DWORD of string table, which is its size) */
233 offset = (DWORD64)nthdr->FileHeader.PointerToSymbolTable;
234 offset += (DWORD64)nthdr->FileHeader.NumberOfSymbols * sizeof(IMAGE_SYMBOL);
235 if (offset + sizeof(DWORD) > sz) return FALSE;
236 /* is string table (following iSym table) inside file size ? */
237 offset += *(DWORD*)((const char*)mapping + offset);
238 return offset <= sz;
241 /******************************************************************
242 * pe_map_file
244 * Maps an PE file into memory (and checks it's a real PE file)
246 BOOL pe_map_file(HANDLE file, struct image_file_map* fmap)
248 void* mapping;
249 IMAGE_NT_HEADERS* nthdr;
250 IMAGE_SECTION_HEADER* section;
251 unsigned i;
253 fmap->modtype = DMT_PE;
254 fmap->ops = &pe_file_map_ops;
255 fmap->alternate = NULL;
256 fmap->u.pe.hMap = CreateFileMappingW(file, NULL, PAGE_READONLY, 0, 0, NULL);
257 if (fmap->u.pe.hMap == 0) return FALSE;
258 fmap->u.pe.full_count = 0;
259 fmap->u.pe.full_map = NULL;
260 if (!(mapping = pe_map_full(fmap, NULL))) goto error;
262 if (!(nthdr = RtlImageNtHeader(mapping))) goto error;
263 memcpy(&fmap->u.pe.file_header, &nthdr->FileHeader, sizeof(fmap->u.pe.file_header));
264 switch (nthdr->OptionalHeader.Magic)
266 case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
267 fmap->addr_size = 32;
268 memcpy(&fmap->u.pe.opt.header32, &nthdr->OptionalHeader, sizeof(fmap->u.pe.opt.header32));
269 break;
270 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
271 if (sizeof(void*) == 4) return FALSE;
272 fmap->addr_size = 64;
273 memcpy(&fmap->u.pe.opt.header64, &nthdr->OptionalHeader, sizeof(fmap->u.pe.opt.header64));
274 break;
275 default:
276 return FALSE;
279 fmap->u.pe.builtin = !memcmp((const IMAGE_DOS_HEADER*)mapping + 1, builtin_signature, sizeof(builtin_signature));
280 section = IMAGE_FIRST_SECTION( nthdr );
281 fmap->u.pe.sect = HeapAlloc(GetProcessHeap(), 0,
282 nthdr->FileHeader.NumberOfSections * sizeof(fmap->u.pe.sect[0]));
283 if (!fmap->u.pe.sect) goto error;
284 for (i = 0; i < nthdr->FileHeader.NumberOfSections; i++)
286 memcpy(&fmap->u.pe.sect[i].shdr, section + i, sizeof(IMAGE_SECTION_HEADER));
287 fmap->u.pe.sect[i].mapped = IMAGE_NO_MAP;
289 if (nthdr->FileHeader.PointerToSymbolTable && nthdr->FileHeader.NumberOfSymbols)
291 LARGE_INTEGER li;
293 if (GetFileSizeEx(file, &li) && pe_is_valid_pointer_table(nthdr, mapping, li.QuadPart))
295 /* FIXME ugly: should rather map the relevant content instead of copying it */
296 const char* src = (const char*)mapping +
297 nthdr->FileHeader.PointerToSymbolTable +
298 nthdr->FileHeader.NumberOfSymbols * sizeof(IMAGE_SYMBOL);
299 char* dst;
300 DWORD sz = *(DWORD*)src;
302 if ((dst = HeapAlloc(GetProcessHeap(), 0, sz)))
303 memcpy(dst, src, sz);
304 fmap->u.pe.strtable = dst;
306 else
308 WARN("Bad coff table... wipping out\n");
309 /* we have bad information here, wipe it out */
310 fmap->u.pe.file_header.PointerToSymbolTable = 0;
311 fmap->u.pe.file_header.NumberOfSymbols = 0;
312 fmap->u.pe.strtable = NULL;
315 else fmap->u.pe.strtable = NULL;
317 pe_unmap_full(fmap);
319 return TRUE;
320 error:
321 pe_unmap_full(fmap);
322 CloseHandle(fmap->u.pe.hMap);
323 return FALSE;
326 /******************************************************************
327 * pe_map_directory
329 * Maps a directory content out of a PE file
331 const char* pe_map_directory(struct module* module, int dirno, DWORD* size)
333 IMAGE_NT_HEADERS* nth;
334 void* mapping;
336 if (module->type != DMT_PE || !module->format_info[DFI_PE]) return NULL;
337 if (dirno >= IMAGE_NUMBEROF_DIRECTORY_ENTRIES ||
338 !(mapping = pe_map_full(&module->format_info[DFI_PE]->u.pe_info->fmap, &nth)))
339 return NULL;
340 if (size) *size = nth->OptionalHeader.DataDirectory[dirno].Size;
341 return RtlImageRvaToVa(nth, mapping,
342 nth->OptionalHeader.DataDirectory[dirno].VirtualAddress, NULL);
345 static void pe_module_remove(struct process* pcs, struct module_format* modfmt)
347 image_unmap_file(&modfmt->u.pe_info->fmap);
348 HeapFree(GetProcessHeap(), 0, modfmt);
351 /******************************************************************
352 * pe_locate_with_coff_symbol_table
354 * Use the COFF symbol table (if any) from the IMAGE_FILE_HEADER to set the absolute address
355 * of global symbols.
356 * Mingw32 requires this for stabs debug information as address for global variables isn't filled in
357 * (this is similar to what is done in elf_module.c when using the .symtab ELF section)
359 static BOOL pe_locate_with_coff_symbol_table(struct module* module)
361 struct image_file_map* fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
362 const IMAGE_SYMBOL* isym;
363 int i, numsym, naux;
364 char tmp[9];
365 const char* name;
366 struct hash_table_iter hti;
367 void* ptr;
368 struct symt_data* sym;
369 const char* mapping;
371 numsym = fmap->u.pe.file_header.NumberOfSymbols;
372 if (!fmap->u.pe.file_header.PointerToSymbolTable || !numsym)
373 return TRUE;
374 if (!(mapping = pe_map_full(fmap, NULL))) return FALSE;
375 isym = (const IMAGE_SYMBOL*)(mapping + fmap->u.pe.file_header.PointerToSymbolTable);
377 for (i = 0; i < numsym; i+= naux, isym += naux)
379 if (isym->StorageClass == IMAGE_SYM_CLASS_EXTERNAL &&
380 isym->SectionNumber > 0 && isym->SectionNumber <= fmap->u.pe.file_header.NumberOfSections)
382 if (isym->N.Name.Short)
384 name = memcpy(tmp, isym->N.ShortName, 8);
385 tmp[8] = '\0';
387 else name = fmap->u.pe.strtable + isym->N.Name.Long;
388 if (name[0] == '_') name++;
389 hash_table_iter_init(&module->ht_symbols, &hti, name);
390 while ((ptr = hash_table_iter_up(&hti)))
392 sym = CONTAINING_RECORD(ptr, struct symt_data, hash_elt);
393 if (sym->symt.tag == SymTagData &&
394 (sym->kind == DataIsGlobal || sym->kind == DataIsFileStatic) &&
395 sym->u.var.kind == loc_absolute &&
396 !strcmp(sym->hash_elt.name, name))
398 TRACE("Changing absolute address for %d.%s: %Ix -> %I64x\n",
399 isym->SectionNumber, name, sym->u.var.offset,
400 module->module.BaseOfImage +
401 fmap->u.pe.sect[isym->SectionNumber - 1].shdr.VirtualAddress +
402 isym->Value);
403 sym->u.var.offset = module->module.BaseOfImage +
404 fmap->u.pe.sect[isym->SectionNumber - 1].shdr.VirtualAddress + isym->Value;
405 break;
409 naux = isym->NumberOfAuxSymbols + 1;
411 pe_unmap_full(fmap);
412 return TRUE;
415 /******************************************************************
416 * pe_load_coff_symbol_table
418 * Load public symbols out of the COFF symbol table (if any).
420 static BOOL pe_load_coff_symbol_table(struct module* module)
422 struct image_file_map* fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
423 const IMAGE_SYMBOL* isym;
424 int i, numsym, naux;
425 const char* strtable;
426 char tmp[9];
427 const char* name;
428 const char* lastfilename = NULL;
429 struct symt_compiland* compiland = NULL;
430 const IMAGE_SECTION_HEADER* sect;
431 const char* mapping;
433 numsym = fmap->u.pe.file_header.NumberOfSymbols;
434 if (!fmap->u.pe.file_header.PointerToSymbolTable || !numsym)
435 return TRUE;
436 if (!(mapping = pe_map_full(fmap, NULL))) return FALSE;
437 isym = (const IMAGE_SYMBOL*)(mapping + fmap->u.pe.file_header.PointerToSymbolTable);
438 /* FIXME: no way to get strtable size */
439 strtable = (const char*)&isym[numsym];
440 sect = IMAGE_FIRST_SECTION(RtlImageNtHeader((HMODULE)mapping));
442 for (i = 0; i < numsym; i+= naux, isym += naux)
444 if (isym->StorageClass == IMAGE_SYM_CLASS_FILE)
446 lastfilename = (const char*)(isym + 1);
447 compiland = NULL;
449 if (isym->StorageClass == IMAGE_SYM_CLASS_EXTERNAL &&
450 isym->SectionNumber > 0 && isym->SectionNumber <= fmap->u.pe.file_header.NumberOfSections)
452 if (isym->N.Name.Short)
454 name = memcpy(tmp, isym->N.ShortName, 8);
455 tmp[8] = '\0';
457 else name = strtable + isym->N.Name.Long;
458 if (name[0] == '_') name++;
460 if (!compiland && lastfilename)
461 compiland = symt_new_compiland(module, source_new(module, NULL, lastfilename));
463 if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
464 symt_new_public(module, compiland, name, FALSE,
465 module->module.BaseOfImage + sect[isym->SectionNumber - 1].VirtualAddress +
466 isym->Value,
469 naux = isym->NumberOfAuxSymbols + 1;
471 module->module.SymType = SymCoff;
472 module->module.LineNumbers = FALSE;
473 module->module.GlobalSymbols = FALSE;
474 module->module.TypeInfo = FALSE;
475 module->module.SourceIndexed = FALSE;
476 module->module.Publics = TRUE;
477 pe_unmap_full(fmap);
479 return TRUE;
482 /******************************************************************
483 * pe_load_stabs
485 * look for stabs information in PE header (it's how the mingw compiler provides
486 * its debugging information)
488 static BOOL pe_load_stabs(const struct process* pcs, struct module* module)
490 struct image_file_map* fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
491 struct image_section_map sect_stabs, sect_stabstr;
492 BOOL ret = FALSE;
494 if (pe_find_section(fmap, ".stab", &sect_stabs) && pe_find_section(fmap, ".stabstr", &sect_stabstr))
496 const char* stab;
497 const char* stabstr;
499 stab = image_map_section(&sect_stabs);
500 stabstr = image_map_section(&sect_stabstr);
501 if (stab != IMAGE_NO_MAP && stabstr != IMAGE_NO_MAP)
503 ret = stabs_parse(module,
504 module->module.BaseOfImage - PE_FROM_OPTHDR(fmap, ImageBase),
505 stab, image_get_map_size(&sect_stabs) / sizeof(struct stab_nlist), sizeof(struct stab_nlist),
506 stabstr, image_get_map_size(&sect_stabstr),
507 NULL, NULL);
509 image_unmap_section(&sect_stabs);
510 image_unmap_section(&sect_stabstr);
511 if (ret) pe_locate_with_coff_symbol_table(module);
513 TRACE("%s the STABS debug info\n", ret ? "successfully loaded" : "failed to load");
515 return ret;
518 /******************************************************************
519 * pe_load_dwarf
521 * look for dwarf information in PE header (it's also a way for the mingw compiler
522 * to provide its debugging information)
524 static BOOL pe_load_dwarf(struct module* module)
526 struct image_file_map* fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
527 BOOL ret;
529 ret = dwarf2_parse(module,
530 module->module.BaseOfImage - PE_FROM_OPTHDR(fmap, ImageBase),
531 NULL, /* FIXME: some thunks to deal with ? */
532 fmap);
533 TRACE("%s the DWARF debug info\n", ret ? "successfully loaded" : "failed to load");
535 return ret;
538 /******************************************************************
539 * pe_load_dbg_file
541 * loads a .dbg file
543 static BOOL pe_load_dbg_file(const struct process* pcs, struct module* module,
544 const char* dbg_name, DWORD timestamp)
546 WCHAR tmp[MAX_PATH];
547 HANDLE hFile = INVALID_HANDLE_VALUE, hMap = 0;
548 const BYTE* dbg_mapping = NULL;
549 BOOL ret = FALSE;
551 TRACE("Processing DBG file %s\n", debugstr_a(dbg_name));
553 if (path_find_symbol_file(pcs, module, dbg_name, FALSE, NULL, timestamp, 0, tmp, &module->module.DbgUnmatched) &&
554 (hFile = CreateFileW(tmp, GENERIC_READ, FILE_SHARE_READ, NULL,
555 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) != INVALID_HANDLE_VALUE &&
556 ((hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) != 0) &&
557 ((dbg_mapping = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) != NULL))
559 const IMAGE_SEPARATE_DEBUG_HEADER* hdr;
560 const IMAGE_SECTION_HEADER* sectp;
561 const IMAGE_DEBUG_DIRECTORY* dbg;
563 hdr = (const IMAGE_SEPARATE_DEBUG_HEADER*)dbg_mapping;
564 /* section headers come immediately after debug header */
565 sectp = (const IMAGE_SECTION_HEADER*)(hdr + 1);
566 /* and after that and the exported names comes the debug directory */
567 dbg = (const IMAGE_DEBUG_DIRECTORY*)
568 (dbg_mapping + sizeof(*hdr) +
569 hdr->NumberOfSections * sizeof(IMAGE_SECTION_HEADER) +
570 hdr->ExportedNamesSize);
572 ret = pe_load_debug_directory(pcs, module, dbg_mapping, sectp,
573 hdr->NumberOfSections, dbg,
574 hdr->DebugDirectorySize / sizeof(*dbg));
576 else
577 ERR("Couldn't find .DBG file %s (%s)\n", debugstr_a(dbg_name), debugstr_w(tmp));
579 if (dbg_mapping) UnmapViewOfFile(dbg_mapping);
580 if (hMap) CloseHandle(hMap);
581 if (hFile != INVALID_HANDLE_VALUE) CloseHandle(hFile);
582 return ret;
585 /******************************************************************
586 * pe_load_msc_debug_info
588 * Process MSC debug information in PE file.
590 static BOOL pe_load_msc_debug_info(const struct process* pcs, struct module* module)
592 struct image_file_map* fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
593 BOOL ret = FALSE;
594 const IMAGE_DEBUG_DIRECTORY*dbg;
595 ULONG nDbg;
596 void* mapping;
597 IMAGE_NT_HEADERS* nth;
599 if (!(mapping = pe_map_full(fmap, &nth))) return FALSE;
600 /* Read in debug directory */
601 dbg = RtlImageDirectoryEntryToData( mapping, FALSE, IMAGE_DIRECTORY_ENTRY_DEBUG, &nDbg );
602 if (!dbg || !(nDbg /= sizeof(IMAGE_DEBUG_DIRECTORY))) goto done;
604 /* Parse debug directory */
605 if (nth->FileHeader.Characteristics & IMAGE_FILE_DEBUG_STRIPPED)
607 /* Debug info is stripped to .DBG file */
608 const IMAGE_DEBUG_MISC* misc = (const IMAGE_DEBUG_MISC*)
609 ((const char*)mapping + dbg->PointerToRawData);
611 if (nDbg != 1 || dbg->Type != IMAGE_DEBUG_TYPE_MISC ||
612 misc->DataType != IMAGE_DEBUG_MISC_EXENAME)
614 ERR("-Debug info stripped, but no .DBG file in module %s\n",
615 debugstr_w(module->modulename));
617 else
619 ret = pe_load_dbg_file(pcs, module, (const char*)misc->Data, nth->FileHeader.TimeDateStamp);
622 else
624 /* Debug info is embedded into PE module */
625 ret = pe_load_debug_directory(pcs, module, mapping, IMAGE_FIRST_SECTION( nth ),
626 nth->FileHeader.NumberOfSections, dbg, nDbg);
628 done:
629 pe_unmap_full(fmap);
630 return ret;
633 /***********************************************************************
634 * pe_load_export_debug_info
636 static BOOL pe_load_export_debug_info(const struct process* pcs, struct module* module)
638 struct image_file_map* fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
639 unsigned int i;
640 const IMAGE_EXPORT_DIRECTORY* exports;
641 DWORD_PTR base = module->module.BaseOfImage;
642 DWORD size;
643 IMAGE_NT_HEADERS* nth;
644 void* mapping;
646 if (dbghelp_options & SYMOPT_NO_PUBLICS) return TRUE;
648 if (!(mapping = pe_map_full(fmap, &nth))) return FALSE;
649 #if 0
650 /* Add start of DLL (better use the (yet unimplemented) Exe SymTag for this) */
651 /* FIXME: module.ModuleName isn't correctly set yet if it's passed in SymLoadModule */
652 symt_new_public(module, NULL, module->module.ModuleName, FALSE, base, 1);
653 #endif
655 /* Add entry point */
656 symt_new_public(module, NULL, "EntryPoint", FALSE,
657 base + nth->OptionalHeader.AddressOfEntryPoint, 1);
658 #if 0
659 /* FIXME: we'd better store addresses linked to sections rather than
660 absolute values */
661 IMAGE_SECTION_HEADER *section = IMAGE_FIRST_SECTION( nth );
662 /* Add start of sections */
663 for (i = 0; i < nth->FileHeader.NumberOfSections; i++, section++)
665 symt_new_public(module, NULL, section->Name, FALSE,
666 RtlImageRvaToVa(nth, mapping, section->VirtualAddress, NULL), 1);
668 #endif
670 /* Add exported functions */
671 if ((exports = RtlImageDirectoryEntryToData(mapping, FALSE,
672 IMAGE_DIRECTORY_ENTRY_EXPORT, &size)))
674 const WORD* ordinals = NULL;
675 const DWORD* functions = NULL;
676 const DWORD* names = NULL;
677 unsigned int j;
678 char buffer[16];
680 functions = RtlImageRvaToVa(nth, mapping, exports->AddressOfFunctions, NULL);
681 ordinals = RtlImageRvaToVa(nth, mapping, exports->AddressOfNameOrdinals, NULL);
682 names = RtlImageRvaToVa(nth, mapping, exports->AddressOfNames, NULL);
684 if (functions && ordinals && names)
686 for (i = 0; i < exports->NumberOfNames; i++)
688 if (!names[i]) continue;
689 symt_new_public(module, NULL,
690 RtlImageRvaToVa(nth, mapping, names[i], NULL),
691 FALSE,
692 base + functions[ordinals[i]], 1);
695 for (i = 0; i < exports->NumberOfFunctions; i++)
697 if (!functions[i]) continue;
698 /* Check if we already added it with a name */
699 for (j = 0; j < exports->NumberOfNames; j++)
700 if ((ordinals[j] == i) && names[j]) break;
701 if (j < exports->NumberOfNames) continue;
702 snprintf(buffer, sizeof(buffer), "%ld", i + exports->Base);
703 symt_new_public(module, NULL, buffer, FALSE, base + functions[i], 1);
707 /* no real debug info, only entry points */
708 if (module->module.SymType == SymDeferred)
709 module->module.SymType = SymExport;
710 pe_unmap_full(fmap);
712 return TRUE;
715 /******************************************************************
716 * pe_load_debug_info
719 BOOL pe_load_debug_info(const struct process* pcs, struct module* module)
721 BOOL ret = FALSE;
723 if (!(dbghelp_options & SYMOPT_PUBLICS_ONLY))
725 ret = image_check_alternate(&module->format_info[DFI_PE]->u.pe_info->fmap, module);
726 ret = pe_load_stabs(pcs, module) || ret;
727 ret = pe_load_dwarf(module) || ret;
728 ret = pe_load_msc_debug_info(pcs, module) || ret;
729 ret = ret || pe_load_coff_symbol_table(module); /* FIXME */
730 /* if we still have no debug info (we could only get SymExport at this
731 * point), then do the SymExport except if we have an ELF container,
732 * in which case we'll rely on the export's on the ELF side
735 /* FIXME shouldn't we check that? if (!module_get_debug(pcs, module)) */
736 if (pe_load_export_debug_info(pcs, module) && !ret)
737 ret = TRUE;
738 if (!ret) module->module.SymType = SymNone;
739 return ret;
742 struct builtin_search
744 WCHAR *path;
745 struct image_file_map fmap;
748 static BOOL search_builtin_pe(void *param, HANDLE handle, const WCHAR *path)
750 struct builtin_search *search = param;
752 if (!pe_map_file(handle, &search->fmap)) return FALSE;
754 search->path = wcsdup(path);
755 return TRUE;
758 /******************************************************************
759 * pe_load_native_module
762 struct module* pe_load_native_module(struct process* pcs, const WCHAR* name,
763 HANDLE hFile, DWORD64 base, DWORD size)
765 struct module* module = NULL;
766 BOOL opened = FALSE;
767 struct module_format* modfmt;
768 WCHAR loaded_name[MAX_PATH];
769 WCHAR* real_path = NULL;
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 (hFile = FindExecutableImageExW(name, L".", loaded_name, NULL, NULL)) == NULL)
778 return NULL;
779 opened = TRUE;
781 else
783 ULONG sz = sizeof(OBJECT_NAME_INFORMATION) + MAX_PATH * sizeof(WCHAR), needed;
784 OBJECT_NAME_INFORMATION *obj_name;
785 NTSTATUS nts;
787 obj_name = RtlAllocateHeap(GetProcessHeap(), 0, sz);
788 if (obj_name)
790 nts = NtQueryObject(hFile, ObjectNameInformation, obj_name, sz, &needed);
791 if (nts == STATUS_BUFFER_OVERFLOW)
793 sz = needed;
794 obj_name = RtlReAllocateHeap(GetProcessHeap(), 0, obj_name, sz);
795 nts = NtQueryObject(hFile, ObjectNameInformation, obj_name, sz, &needed);
797 if (!nts)
799 obj_name->Name.Buffer[obj_name->Name.Length / sizeof(WCHAR)] = L'\0';
800 real_path = wcsdup(obj_name->Name.Buffer);
802 RtlFreeHeap(GetProcessHeap(), 0, obj_name);
804 if (name) lstrcpyW(loaded_name, name);
807 if ((modfmt = HeapAlloc(GetProcessHeap(), 0, sizeof(struct module_format) + sizeof(struct pe_module_info))))
809 modfmt->u.pe_info = (struct pe_module_info*)(modfmt + 1);
810 if (pe_map_file(hFile, &modfmt->u.pe_info->fmap))
812 struct builtin_search builtin = { NULL };
813 if (opened && modfmt->u.pe_info->fmap.u.pe.builtin &&
814 search_dll_path(pcs, loaded_name, modfmt->u.pe_info->fmap.u.pe.file_header.Machine, search_builtin_pe, &builtin))
816 TRACE("reloaded %s from %s\n", debugstr_w(loaded_name), debugstr_w(builtin.path));
817 image_unmap_file(&modfmt->u.pe_info->fmap);
818 modfmt->u.pe_info->fmap = builtin.fmap;
819 real_path = builtin.path;
821 if (!base) base = PE_FROM_OPTHDR(&modfmt->u.pe_info->fmap, ImageBase);
822 if (!size) size = PE_FROM_OPTHDR(&modfmt->u.pe_info->fmap, SizeOfImage);
824 module = module_new(pcs, loaded_name, DMT_PE, modfmt->u.pe_info->fmap.u.pe.builtin, FALSE,
825 base, size,
826 modfmt->u.pe_info->fmap.u.pe.file_header.TimeDateStamp,
827 PE_FROM_OPTHDR(&modfmt->u.pe_info->fmap, CheckSum),
828 modfmt->u.pe_info->fmap.u.pe.file_header.Machine);
829 if (module)
831 module->real_path = real_path ? pool_wcsdup(&module->pool, real_path) : NULL;
832 modfmt->module = module;
833 modfmt->remove = pe_module_remove;
834 modfmt->loc_compute = NULL;
835 module->format_info[DFI_PE] = modfmt;
836 module->reloc_delta = base - PE_FROM_OPTHDR(&modfmt->u.pe_info->fmap, ImageBase);
838 else
840 ERR("could not load the module '%s'\n", debugstr_w(loaded_name));
841 image_unmap_file(&modfmt->u.pe_info->fmap);
844 if (!module) HeapFree(GetProcessHeap(), 0, modfmt);
847 if (opened) CloseHandle(hFile);
848 free(real_path);
849 return module;
852 /******************************************************************
853 * pe_load_nt_header
856 BOOL pe_load_nt_header(HANDLE hProc, DWORD64 base, IMAGE_NT_HEADERS* nth, BOOL* is_builtin)
858 IMAGE_DOS_HEADER dos;
860 if (!ReadProcessMemory(hProc, (char*)(DWORD_PTR)base, &dos, sizeof(dos), NULL) ||
861 dos.e_magic != IMAGE_DOS_SIGNATURE ||
862 !ReadProcessMemory(hProc, (char*)(DWORD_PTR)(base + dos.e_lfanew),
863 nth, sizeof(*nth), NULL) ||
864 nth->Signature != IMAGE_NT_SIGNATURE)
865 return FALSE;
866 if (is_builtin)
868 if (dos.e_lfanew >= sizeof(dos) + sizeof(builtin_signature))
870 char sig[sizeof(builtin_signature)];
871 *is_builtin = ReadProcessMemory(hProc, (char*)(DWORD_PTR)base + sizeof(dos), sig, sizeof(sig), NULL) &&
872 !memcmp(sig, builtin_signature, sizeof(builtin_signature));
874 else *is_builtin = FALSE;
876 return TRUE;
879 /******************************************************************
880 * pe_load_builtin_module
883 struct module* pe_load_builtin_module(struct process* pcs, const WCHAR* name,
884 DWORD64 base, DWORD64 size)
886 struct module* module = NULL;
888 if (base && pcs->dbg_hdr_addr)
890 IMAGE_NT_HEADERS nth;
891 BOOL is_builtin;
893 if (pe_load_nt_header(pcs->handle, base, &nth, &is_builtin))
895 if (!size) size = nth.OptionalHeader.SizeOfImage;
896 module = module_new(pcs, name, DMT_PE, is_builtin, FALSE, base, size,
897 nth.FileHeader.TimeDateStamp,
898 nth.OptionalHeader.CheckSum,
899 nth.FileHeader.Machine);
902 return module;
905 /***********************************************************************
906 * ImageDirectoryEntryToDataEx (DBGHELP.@)
908 * Search for specified directory in PE image
910 * PARAMS
912 * base [in] Image base address
913 * image [in] TRUE - image has been loaded by loader, FALSE - raw file image
914 * dir [in] Target directory index
915 * size [out] Receives directory size
916 * section [out] Receives pointer to section header of section containing directory data
918 * RETURNS
919 * Success: pointer to directory data
920 * Failure: NULL
923 PVOID WINAPI ImageDirectoryEntryToDataEx( PVOID base, BOOLEAN image, USHORT dir, PULONG size, PIMAGE_SECTION_HEADER *section )
925 const IMAGE_NT_HEADERS *nt;
926 DWORD_PTR addr;
928 *size = 0;
929 if (section) *section = NULL;
931 if (!(nt = RtlImageNtHeader( base ))) return NULL;
932 if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
934 const IMAGE_NT_HEADERS64 *nt64 = (const IMAGE_NT_HEADERS64 *)nt;
936 if (dir >= nt64->OptionalHeader.NumberOfRvaAndSizes) return NULL;
937 if (!(addr = nt64->OptionalHeader.DataDirectory[dir].VirtualAddress)) return NULL;
938 *size = nt64->OptionalHeader.DataDirectory[dir].Size;
939 if (image || addr < nt64->OptionalHeader.SizeOfHeaders) return (char *)base + addr;
941 else if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC)
943 const IMAGE_NT_HEADERS32 *nt32 = (const IMAGE_NT_HEADERS32 *)nt;
945 if (dir >= nt32->OptionalHeader.NumberOfRvaAndSizes) return NULL;
946 if (!(addr = nt32->OptionalHeader.DataDirectory[dir].VirtualAddress)) return NULL;
947 *size = nt32->OptionalHeader.DataDirectory[dir].Size;
948 if (image || addr < nt32->OptionalHeader.SizeOfHeaders) return (char *)base + addr;
950 else return NULL;
952 return RtlImageRvaToVa( nt, base, addr, section );
955 /***********************************************************************
956 * ImageDirectoryEntryToData (DBGHELP.@)
958 * NOTES
959 * See ImageDirectoryEntryToDataEx
961 PVOID WINAPI ImageDirectoryEntryToData( PVOID base, BOOLEAN image, USHORT dir, PULONG size )
963 return ImageDirectoryEntryToDataEx( base, image, dir, size, NULL );
966 DWORD pe_get_file_indexinfo(void* image, DWORD size, SYMSRV_INDEX_INFOW* info)
968 const IMAGE_NT_HEADERS* nthdr;
969 const IMAGE_DEBUG_DIRECTORY* dbg;
970 ULONG dirsize;
972 if (!(nthdr = RtlImageNtHeader(image))) return ERROR_BAD_FORMAT;
974 dbg = RtlImageDirectoryEntryToData(image, FALSE, IMAGE_DIRECTORY_ENTRY_DEBUG, &dirsize);
975 if (!dbg || dirsize < sizeof(dbg)) return ERROR_BAD_EXE_FORMAT;
977 /* fill in information from NT header */
978 info->timestamp = nthdr->FileHeader.TimeDateStamp;
979 info->stripped = (nthdr->FileHeader.Characteristics & IMAGE_FILE_DEBUG_STRIPPED) != 0;
980 if (nthdr->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
982 const IMAGE_NT_HEADERS64* nthdr64 = (const IMAGE_NT_HEADERS64*)nthdr;
983 info->size = nthdr64->OptionalHeader.SizeOfImage;
985 else if (nthdr->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC)
987 const IMAGE_NT_HEADERS32* nthdr32 = (const IMAGE_NT_HEADERS32*)nthdr;
988 info->size = nthdr32->OptionalHeader.SizeOfImage;
990 return msc_get_file_indexinfo(image, dbg, dirsize / sizeof(*dbg), info);