Synchronize with trunk revision 59636 (just before Alex's CreateProcess revamp).
[reactos.git] / dll / win32 / dbghelp / pe_module.c
1 /*
2 * File pe_module.c - handle PE module information
3 *
4 * Copyright (C) 1996, Eric Youngdale.
5 * Copyright (C) 1999-2000, Ulrich Weigand.
6 * Copyright (C) 2004-2007, Eric Pouech.
7 *
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.
12 *
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.
17 *
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
21 *
22 */
23
24 #include "config.h"
25 #include "wine/port.h"
26
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <assert.h>
31
32 #include "dbghelp_private.h"
33 #include "image_private.h"
34 #ifndef DBGHELP_STATIC_LIB
35 #include "winternl.h"
36 #include "wine/debug.h"
37 #endif
38
39 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
40
41 struct pe_module_info
42 {
43 struct image_file_map fmap;
44 };
45
46 static void* pe_map_full(struct image_file_map* fmap, IMAGE_NT_HEADERS** nth)
47 {
48 if (!fmap->u.pe.full_map)
49 {
50 fmap->u.pe.full_map = MapViewOfFile(fmap->u.pe.hMap, FILE_MAP_READ, 0, 0, 0);
51 }
52 if (fmap->u.pe.full_map)
53 {
54 if (nth) *nth = RtlImageNtHeader(fmap->u.pe.full_map);
55 fmap->u.pe.full_count++;
56 return fmap->u.pe.full_map;
57 }
58 return IMAGE_NO_MAP;
59 }
60
61 static void pe_unmap_full(struct image_file_map* fmap)
62 {
63 if (fmap->u.pe.full_count && !--fmap->u.pe.full_count)
64 {
65 UnmapViewOfFile(fmap->u.pe.full_map);
66 fmap->u.pe.full_map = NULL;
67 }
68 }
69
70 /******************************************************************
71 * pe_map_section
72 *
73 * Maps a single section into memory from an PE file
74 */
75 const char* pe_map_section(struct image_section_map* ism)
76 {
77 void* mapping;
78 struct pe_file_map* fmap = &ism->fmap->u.pe;
79
80 if (ism->sidx >= 0 && ism->sidx < fmap->ntheader.FileHeader.NumberOfSections &&
81 fmap->sect[ism->sidx].mapped == IMAGE_NO_MAP)
82 {
83 IMAGE_NT_HEADERS* nth;
84
85 if (fmap->sect[ism->sidx].shdr.Misc.VirtualSize > fmap->sect[ism->sidx].shdr.SizeOfRawData)
86 {
87 FIXME("Section %ld: virtual (0x%x) > raw (0x%x) size - not supported\n",
88 ism->sidx, fmap->sect[ism->sidx].shdr.Misc.VirtualSize,
89 fmap->sect[ism->sidx].shdr.SizeOfRawData);
90 return IMAGE_NO_MAP;
91 }
92 /* FIXME: that's rather drastic, but that will do for now
93 * that's ok if the full file map exists, but we could be less aggressive otherwise and
94 * only map the relevant section
95 */
96 if ((mapping = pe_map_full(ism->fmap, &nth)))
97 {
98 fmap->sect[ism->sidx].mapped = RtlImageRvaToVa(nth, mapping,
99 fmap->sect[ism->sidx].shdr.VirtualAddress,
100 NULL);
101 return fmap->sect[ism->sidx].mapped;
102 }
103 }
104 return IMAGE_NO_MAP;
105 }
106
107 /******************************************************************
108 * pe_find_section
109 *
110 * Finds a section by name (and type) into memory from an PE file
111 * or its alternate if any
112 */
113 BOOL pe_find_section(struct image_file_map* fmap, const char* name,
114 struct image_section_map* ism)
115 {
116 const char* sectname;
117 unsigned i;
118 char tmp[IMAGE_SIZEOF_SHORT_NAME + 1];
119
120 for (i = 0; i < fmap->u.pe.ntheader.FileHeader.NumberOfSections; i++)
121 {
122 sectname = (const char*)fmap->u.pe.sect[i].shdr.Name;
123 /* long section names start with a '/' (at least on MinGW32) */
124 if (sectname[0] == '/' && fmap->u.pe.strtable)
125 sectname = fmap->u.pe.strtable + atoi(sectname + 1);
126 else
127 {
128 /* the section name may not be null terminated */
129 sectname = memcpy(tmp, sectname, IMAGE_SIZEOF_SHORT_NAME);
130 tmp[IMAGE_SIZEOF_SHORT_NAME] = '\0';
131 }
132 if (!strcasecmp(sectname, name))
133 {
134 ism->fmap = fmap;
135 ism->sidx = i;
136 return TRUE;
137 }
138 }
139 ism->fmap = NULL;
140 ism->sidx = -1;
141
142 return FALSE;
143 }
144
145 /******************************************************************
146 * pe_unmap_section
147 *
148 * Unmaps a single section from memory
149 */
150 void pe_unmap_section(struct image_section_map* ism)
151 {
152 if (ism->sidx >= 0 && ism->sidx < ism->fmap->u.pe.ntheader.FileHeader.NumberOfSections &&
153 ism->fmap->u.pe.sect[ism->sidx].mapped != IMAGE_NO_MAP)
154 {
155 pe_unmap_full(ism->fmap);
156 ism->fmap->u.pe.sect[ism->sidx].mapped = IMAGE_NO_MAP;
157 }
158 }
159
160 /******************************************************************
161 * pe_get_map_rva
162 *
163 * Get the RVA of an PE section
164 */
165 DWORD_PTR pe_get_map_rva(const struct image_section_map* ism)
166 {
167 if (ism->sidx < 0 || ism->sidx >= ism->fmap->u.pe.ntheader.FileHeader.NumberOfSections)
168 return 0;
169 return ism->fmap->u.pe.sect[ism->sidx].shdr.VirtualAddress;
170 }
171
172 /******************************************************************
173 * pe_get_map_size
174 *
175 * Get the size of a PE section
176 */
177 unsigned pe_get_map_size(const struct image_section_map* ism)
178 {
179 if (ism->sidx < 0 || ism->sidx >= ism->fmap->u.pe.ntheader.FileHeader.NumberOfSections)
180 return 0;
181 return ism->fmap->u.pe.sect[ism->sidx].shdr.Misc.VirtualSize;
182 }
183
184 /******************************************************************
185 * pe_is_valid_pointer_table
186 *
187 * Checks whether the PointerToSymbolTable and NumberOfSymbols in file_header contain
188 * valid information.
189 */
190 static BOOL pe_is_valid_pointer_table(const IMAGE_NT_HEADERS* nthdr, const void* mapping, DWORD64 sz)
191 {
192 DWORD64 offset;
193
194 /* is the iSym table inside file size ? (including first DWORD of string table, which is its size) */
195 offset = (DWORD64)nthdr->FileHeader.PointerToSymbolTable;
196 offset += (DWORD64)nthdr->FileHeader.NumberOfSymbols * sizeof(IMAGE_SYMBOL);
197 if (offset + sizeof(DWORD) > sz) return FALSE;
198 /* is string table (following iSym table) inside file size ? */
199 offset += *(DWORD*)((const char*)mapping + offset);
200 return offset <= sz;
201 }
202
203 /******************************************************************
204 * pe_map_file
205 *
206 * Maps an PE file into memory (and checks it's a real PE file)
207 */
208 static BOOL pe_map_file(HANDLE file, struct image_file_map* fmap, enum module_type mt)
209 {
210 void* mapping;
211
212 fmap->modtype = mt;
213 fmap->u.pe.hMap = CreateFileMappingW(file, NULL, PAGE_READONLY, 0, 0, NULL);
214 if (fmap->u.pe.hMap == 0) return FALSE;
215 fmap->u.pe.full_count = 0;
216 fmap->u.pe.full_map = NULL;
217 if (!(mapping = pe_map_full(fmap, NULL))) goto error;
218
219 switch (mt)
220 {
221 case DMT_PE:
222 {
223 IMAGE_NT_HEADERS* nthdr;
224 IMAGE_SECTION_HEADER* section;
225 unsigned i;
226
227 if (!(nthdr = RtlImageNtHeader(mapping))) goto error;
228 memcpy(&fmap->u.pe.ntheader, nthdr, sizeof(fmap->u.pe.ntheader));
229 switch (nthdr->OptionalHeader.Magic)
230 {
231 case 0x10b: fmap->addr_size = 32; break;
232 case 0x20b: fmap->addr_size = 64; break;
233 default: return FALSE;
234 }
235 section = (IMAGE_SECTION_HEADER*)
236 ((char*)&nthdr->OptionalHeader + nthdr->FileHeader.SizeOfOptionalHeader);
237 fmap->u.pe.sect = HeapAlloc(GetProcessHeap(), 0,
238 nthdr->FileHeader.NumberOfSections * sizeof(fmap->u.pe.sect[0]));
239 if (!fmap->u.pe.sect) goto error;
240 for (i = 0; i < nthdr->FileHeader.NumberOfSections; i++)
241 {
242 memcpy(&fmap->u.pe.sect[i].shdr, section + i, sizeof(IMAGE_SECTION_HEADER));
243 fmap->u.pe.sect[i].mapped = IMAGE_NO_MAP;
244 }
245 if (nthdr->FileHeader.PointerToSymbolTable && nthdr->FileHeader.NumberOfSymbols)
246 {
247 LARGE_INTEGER li;
248
249 if (GetFileSizeEx(file, &li) && pe_is_valid_pointer_table(nthdr, mapping, li.QuadPart))
250 {
251 /* FIXME ugly: should rather map the relevant content instead of copying it */
252 const char* src = (const char*)mapping +
253 nthdr->FileHeader.PointerToSymbolTable +
254 nthdr->FileHeader.NumberOfSymbols * sizeof(IMAGE_SYMBOL);
255 char* dst;
256 DWORD sz = *(DWORD*)src;
257
258 if ((dst = HeapAlloc(GetProcessHeap(), 0, sz)))
259 memcpy(dst, src, sz);
260 fmap->u.pe.strtable = dst;
261 }
262 else
263 {
264 WARN("Bad coff table... wipping out\n");
265 /* we have bad information here, wipe it out */
266 fmap->u.pe.ntheader.FileHeader.PointerToSymbolTable = 0;
267 fmap->u.pe.ntheader.FileHeader.NumberOfSymbols = 0;
268 fmap->u.pe.strtable = NULL;
269 }
270 }
271 else fmap->u.pe.strtable = NULL;
272 }
273 break;
274 default: assert(0); goto error;
275 }
276 pe_unmap_full(fmap);
277
278 return TRUE;
279 error:
280 pe_unmap_full(fmap);
281 CloseHandle(fmap->u.pe.hMap);
282 return FALSE;
283 }
284
285 /******************************************************************
286 * pe_unmap_file
287 *
288 * Unmaps an PE file from memory (previously mapped with pe_map_file)
289 */
290 static void pe_unmap_file(struct image_file_map* fmap)
291 {
292 if (fmap->u.pe.hMap != 0)
293 {
294 struct image_section_map ism;
295 ism.fmap = fmap;
296 for (ism.sidx = 0; ism.sidx < fmap->u.pe.ntheader.FileHeader.NumberOfSections; ism.sidx++)
297 {
298 pe_unmap_section(&ism);
299 }
300 while (fmap->u.pe.full_count) pe_unmap_full(fmap);
301 HeapFree(GetProcessHeap(), 0, fmap->u.pe.sect);
302 HeapFree(GetProcessHeap(), 0, (void*)fmap->u.pe.strtable); /* FIXME ugly (see pe_map_file) */
303 CloseHandle(fmap->u.pe.hMap);
304 fmap->u.pe.hMap = NULL;
305 }
306 }
307
308 /******************************************************************
309 * pe_map_directory
310 *
311 * Maps a directory content out of a PE file
312 */
313 const char* pe_map_directory(struct module* module, int dirno, DWORD* size)
314 {
315 IMAGE_NT_HEADERS* nth;
316 void* mapping;
317
318 if (module->type != DMT_PE || !module->format_info[DFI_PE]) return NULL;
319 if (dirno >= IMAGE_NUMBEROF_DIRECTORY_ENTRIES ||
320 !(mapping = pe_map_full(&module->format_info[DFI_PE]->u.pe_info->fmap, &nth)))
321 return NULL;
322 if (size) *size = nth->OptionalHeader.DataDirectory[dirno].Size;
323 return RtlImageRvaToVa(nth, mapping,
324 nth->OptionalHeader.DataDirectory[dirno].VirtualAddress, NULL);
325 }
326
327 /******************************************************************
328 * pe_unmap_directory
329 *
330 * Unmaps a directory content
331 */
332 void pe_unmap_directory(struct image_file_map* fmap, int dirno)
333 {
334 pe_unmap_full(fmap);
335 }
336
337 static void pe_module_remove(struct process* pcs, struct module_format* modfmt)
338 {
339 pe_unmap_file(&modfmt->u.pe_info->fmap);
340 HeapFree(GetProcessHeap(), 0, modfmt);
341 }
342
343 /******************************************************************
344 * pe_locate_with_coff_symbol_table
345 *
346 * Use the COFF symbol table (if any) from the IMAGE_FILE_HEADER to set the absolute address
347 * of global symbols.
348 * Mingw32 requires this for stabs debug information as address for global variables isn't filled in
349 * (this is similar to what is done in elf_module.c when using the .symtab ELF section)
350 */
351 static BOOL pe_locate_with_coff_symbol_table(struct module* module)
352 {
353 struct image_file_map* fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
354 const IMAGE_SYMBOL* isym;
355 int i, numsym, naux;
356 char tmp[9];
357 const char* name;
358 struct hash_table_iter hti;
359 void* ptr;
360 struct symt_data* sym;
361 const char* mapping;
362
363 numsym = fmap->u.pe.ntheader.FileHeader.NumberOfSymbols;
364 if (!fmap->u.pe.ntheader.FileHeader.PointerToSymbolTable || !numsym)
365 return TRUE;
366 if (!(mapping = pe_map_full(fmap, NULL))) return FALSE;
367 isym = (const IMAGE_SYMBOL*)(mapping + fmap->u.pe.ntheader.FileHeader.PointerToSymbolTable);
368
369 for (i = 0; i < numsym; i+= naux, isym += naux)
370 {
371 if (isym->StorageClass == IMAGE_SYM_CLASS_EXTERNAL &&
372 isym->SectionNumber > 0 && isym->SectionNumber <= fmap->u.pe.ntheader.FileHeader.NumberOfSections)
373 {
374 if (isym->N.Name.Short)
375 {
376 name = memcpy(tmp, isym->N.ShortName, 8);
377 tmp[8] = '\0';
378 }
379 else name = fmap->u.pe.strtable + isym->N.Name.Long;
380 if (name[0] == '_') name++;
381 hash_table_iter_init(&module->ht_symbols, &hti, name);
382 while ((ptr = hash_table_iter_up(&hti)))
383 {
384 sym = GET_ENTRY(ptr, struct symt_data, hash_elt);
385 if (sym->symt.tag == SymTagData &&
386 (sym->kind == DataIsGlobal || sym->kind == DataIsFileStatic) &&
387 sym->u.var.kind == loc_absolute &&
388 !strcmp(sym->hash_elt.name, name))
389 {
390 TRACE("Changing absolute address for %d.%s: %lx -> %s\n",
391 isym->SectionNumber, name, sym->u.var.offset,
392 wine_dbgstr_longlong(module->module.BaseOfImage +
393 fmap->u.pe.sect[isym->SectionNumber - 1].shdr.VirtualAddress +
394 isym->Value));
395 sym->u.var.offset = module->module.BaseOfImage +
396 fmap->u.pe.sect[isym->SectionNumber - 1].shdr.VirtualAddress + isym->Value;
397 break;
398 }
399 }
400 }
401 naux = isym->NumberOfAuxSymbols + 1;
402 }
403 pe_unmap_full(fmap);
404 return TRUE;
405 }
406
407 /******************************************************************
408 * pe_load_coff_symbol_table
409 *
410 * Load public symbols out of the COFF symbol table (if any).
411 */
412 static BOOL pe_load_coff_symbol_table(struct module* module)
413 {
414 struct image_file_map* fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
415 const IMAGE_SYMBOL* isym;
416 int i, numsym, naux;
417 const char* strtable;
418 char tmp[9];
419 const char* name;
420 const char* lastfilename = NULL;
421 struct symt_compiland* compiland = NULL;
422 const IMAGE_SECTION_HEADER* sect;
423 const char* mapping;
424
425 numsym = fmap->u.pe.ntheader.FileHeader.NumberOfSymbols;
426 if (!fmap->u.pe.ntheader.FileHeader.PointerToSymbolTable || !numsym)
427 return TRUE;
428 if (!(mapping = pe_map_full(fmap, NULL))) return FALSE;
429 isym = (const IMAGE_SYMBOL*)((const char*)mapping + fmap->u.pe.ntheader.FileHeader.PointerToSymbolTable);
430 /* FIXME: no way to get strtable size */
431 strtable = (const char*)&isym[numsym];
432 sect = IMAGE_FIRST_SECTION(RtlImageNtHeader((HMODULE)mapping));
433
434 for (i = 0; i < numsym; i+= naux, isym += naux)
435 {
436 if (isym->StorageClass == IMAGE_SYM_CLASS_FILE)
437 {
438 lastfilename = (const char*)(isym + 1);
439 compiland = NULL;
440 }
441 if (isym->StorageClass == IMAGE_SYM_CLASS_EXTERNAL &&
442 isym->SectionNumber > 0 && isym->SectionNumber <= fmap->u.pe.ntheader.FileHeader.NumberOfSections)
443 {
444 if (isym->N.Name.Short)
445 {
446 name = memcpy(tmp, isym->N.ShortName, 8);
447 tmp[8] = '\0';
448 }
449 else name = strtable + isym->N.Name.Long;
450 if (name[0] == '_') name++;
451
452 if (!compiland && lastfilename)
453 compiland = symt_new_compiland(module, 0,
454 source_new(module, NULL, lastfilename));
455
456 if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
457 symt_new_public(module, compiland, name,
458 module->module.BaseOfImage + sect[isym->SectionNumber - 1].VirtualAddress +
459 isym->Value,
460 1);
461 }
462 naux = isym->NumberOfAuxSymbols + 1;
463 }
464 module->module.SymType = SymCoff;
465 module->module.LineNumbers = FALSE;
466 module->module.GlobalSymbols = FALSE;
467 module->module.TypeInfo = FALSE;
468 module->module.SourceIndexed = FALSE;
469 module->module.Publics = TRUE;
470 pe_unmap_full(fmap);
471
472 return TRUE;
473 }
474
475 /******************************************************************
476 * pe_load_stabs
477 *
478 * look for stabs information in PE header (it's how the mingw compiler provides
479 * its debugging information)
480 */
481 static BOOL pe_load_stabs(const struct process* pcs, struct module* module)
482 {
483 struct image_file_map* fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
484 struct image_section_map sect_stabs, sect_stabstr;
485 BOOL ret = FALSE;
486
487 if (pe_find_section(fmap, ".stab", &sect_stabs) && pe_find_section(fmap, ".stabstr", &sect_stabstr))
488 {
489 const char* stab;
490 const char* stabstr;
491
492 stab = image_map_section(&sect_stabs);
493 stabstr = image_map_section(&sect_stabstr);
494 if (stab != IMAGE_NO_MAP && stabstr != IMAGE_NO_MAP)
495 {
496 ret = stabs_parse(module,
497 module->module.BaseOfImage - fmap->u.pe.ntheader.OptionalHeader.ImageBase,
498 stab, image_get_map_size(&sect_stabs),
499 stabstr, image_get_map_size(&sect_stabstr),
500 NULL, NULL);
501 }
502 image_unmap_section(&sect_stabs);
503 image_unmap_section(&sect_stabstr);
504 if (ret) pe_locate_with_coff_symbol_table(module);
505 }
506 TRACE("%s the STABS debug info\n", ret ? "successfully loaded" : "failed to load");
507
508 return ret;
509 }
510
511 /******************************************************************
512 * pe_load_dwarf
513 *
514 * look for dwarf information in PE header (it's also a way for the mingw compiler
515 * to provide its debugging information)
516 */
517 static BOOL pe_load_dwarf(struct module* module)
518 {
519 struct image_file_map* fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
520 BOOL ret = FALSE;
521
522 ret = dwarf2_parse(module,
523 module->module.BaseOfImage - fmap->u.pe.ntheader.OptionalHeader.ImageBase,
524 NULL, /* FIXME: some thunks to deal with ? */
525 fmap);
526 TRACE("%s the DWARF debug info\n", ret ? "successfully loaded" : "failed to load");
527
528 return ret;
529 }
530
531 #ifndef DBGHELP_STATIC_LIB
532 /******************************************************************
533 * pe_load_dbg_file
534 *
535 * loads a .dbg file
536 */
537 static BOOL pe_load_dbg_file(const struct process* pcs, struct module* module,
538 const char* dbg_name, DWORD timestamp)
539 {
540 char tmp[MAX_PATH];
541 HANDLE hFile = INVALID_HANDLE_VALUE, hMap = 0;
542 const BYTE* dbg_mapping = NULL;
543 BOOL ret = FALSE;
544
545 TRACE("Processing DBG file %s\n", debugstr_a(dbg_name));
546
547 if (path_find_symbol_file(pcs, dbg_name, NULL, timestamp, 0, tmp, &module->module.DbgUnmatched) &&
548 (hFile = CreateFileA(tmp, GENERIC_READ, FILE_SHARE_READ, NULL,
549 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) != INVALID_HANDLE_VALUE &&
550 ((hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) != 0) &&
551 ((dbg_mapping = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) != NULL))
552 {
553 const IMAGE_SEPARATE_DEBUG_HEADER* hdr;
554 const IMAGE_SECTION_HEADER* sectp;
555 const IMAGE_DEBUG_DIRECTORY* dbg;
556
557 hdr = (const IMAGE_SEPARATE_DEBUG_HEADER*)dbg_mapping;
558 /* section headers come immediately after debug header */
559 sectp = (const IMAGE_SECTION_HEADER*)(hdr + 1);
560 /* and after that and the exported names comes the debug directory */
561 dbg = (const IMAGE_DEBUG_DIRECTORY*)
562 (dbg_mapping + sizeof(*hdr) +
563 hdr->NumberOfSections * sizeof(IMAGE_SECTION_HEADER) +
564 hdr->ExportedNamesSize);
565
566 ret = pe_load_debug_directory(pcs, module, dbg_mapping, sectp,
567 hdr->NumberOfSections, dbg,
568 hdr->DebugDirectorySize / sizeof(*dbg));
569 }
570 else
571 ERR("Couldn't find .DBG file %s (%s)\n", debugstr_a(dbg_name), debugstr_a(tmp));
572
573 if (dbg_mapping) UnmapViewOfFile(dbg_mapping);
574 if (hMap) CloseHandle(hMap);
575 if (hFile != INVALID_HANDLE_VALUE) CloseHandle(hFile);
576 return ret;
577 }
578
579 /******************************************************************
580 * pe_load_msc_debug_info
581 *
582 * Process MSC debug information in PE file.
583 */
584 static BOOL pe_load_msc_debug_info(const struct process* pcs, struct module* module)
585 {
586 struct image_file_map* fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
587 BOOL ret = FALSE;
588 const IMAGE_DATA_DIRECTORY* dir;
589 const IMAGE_DEBUG_DIRECTORY*dbg = NULL;
590 int nDbg;
591 void* mapping;
592 IMAGE_NT_HEADERS* nth;
593
594 if (!(mapping = pe_map_full(fmap, &nth))) return FALSE;
595 /* Read in debug directory */
596 dir = nth->OptionalHeader.DataDirectory + IMAGE_DIRECTORY_ENTRY_DEBUG;
597 nDbg = dir->Size / sizeof(IMAGE_DEBUG_DIRECTORY);
598 if (!nDbg) goto done;
599
600 dbg = RtlImageRvaToVa(nth, mapping, dir->VirtualAddress, NULL);
601
602 /* Parse debug directory */
603 if (nth->FileHeader.Characteristics & IMAGE_FILE_DEBUG_STRIPPED)
604 {
605 /* Debug info is stripped to .DBG file */
606 const IMAGE_DEBUG_MISC* misc = (const IMAGE_DEBUG_MISC*)
607 ((const char*)mapping + dbg->PointerToRawData);
608
609 if (nDbg != 1 || dbg->Type != IMAGE_DEBUG_TYPE_MISC ||
610 misc->DataType != IMAGE_DEBUG_MISC_EXENAME)
611 {
612 ERR("-Debug info stripped, but no .DBG file in module %s\n",
613 debugstr_w(module->module.ModuleName));
614 }
615 else
616 {
617 ret = pe_load_dbg_file(pcs, module, (const char*)misc->Data, nth->FileHeader.TimeDateStamp);
618 }
619 }
620 else
621 {
622 const IMAGE_SECTION_HEADER *sectp = (const IMAGE_SECTION_HEADER*)((const char*)&nth->OptionalHeader + nth->FileHeader.SizeOfOptionalHeader);
623 /* Debug info is embedded into PE module */
624 ret = pe_load_debug_directory(pcs, module, mapping, sectp,
625 nth->FileHeader.NumberOfSections, dbg, nDbg);
626 }
627 done:
628 pe_unmap_full(fmap);
629 return ret;
630 }
631 #endif /* DBGHELP_STATIC_LIB */
632
633 /***********************************************************************
634 * pe_load_export_debug_info
635 */
636 static BOOL pe_load_export_debug_info(const struct process* pcs, struct module* module)
637 {
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 base = module->module.BaseOfImage;
642 DWORD size;
643 IMAGE_NT_HEADERS* nth;
644 void* mapping;
645
646 if (dbghelp_options & SYMOPT_NO_PUBLICS) return TRUE;
647
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, base, 1);
653 #endif
654
655 /* Add entry point */
656 symt_new_public(module, NULL, "EntryPoint",
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;
662 /* Add start of sections */
663 section = (IMAGE_SECTION_HEADER*)
664 ((char*)&nth->OptionalHeader + nth->FileHeader.SizeOfOptionalHeader);
665 for (i = 0; i < nth->FileHeader.NumberOfSections; i++, section++)
666 {
667 symt_new_public(module, NULL, section->Name,
668 RtlImageRvaToVa(nth, mapping, section->VirtualAddress, NULL), 1);
669 }
670 #endif
671
672 /* Add exported functions */
673 if ((exports = RtlImageDirectoryEntryToData(mapping, FALSE,
674 IMAGE_DIRECTORY_ENTRY_EXPORT, &size)))
675 {
676 const WORD* ordinals = NULL;
677 const DWORD_PTR* functions = NULL;
678 const DWORD* names = NULL;
679 unsigned int j;
680 char buffer[16];
681
682 functions = RtlImageRvaToVa(nth, mapping, exports->AddressOfFunctions, NULL);
683 ordinals = RtlImageRvaToVa(nth, mapping, exports->AddressOfNameOrdinals, NULL);
684 names = RtlImageRvaToVa(nth, mapping, exports->AddressOfNames, NULL);
685
686 if (functions && ordinals && names)
687 {
688 for (i = 0; i < exports->NumberOfNames; i++)
689 {
690 if (!names[i]) continue;
691 symt_new_public(module, NULL,
692 RtlImageRvaToVa(nth, mapping, names[i], NULL),
693 base + functions[ordinals[i]], 1);
694 }
695
696 for (i = 0; i < exports->NumberOfFunctions; i++)
697 {
698 if (!functions[i]) continue;
699 /* Check if we already added it with a name */
700 for (j = 0; j < exports->NumberOfNames; j++)
701 if ((ordinals[j] == i) && names[j]) break;
702 if (j < exports->NumberOfNames) continue;
703 snprintf(buffer, sizeof(buffer), "%d", i + exports->Base);
704 symt_new_public(module, NULL, buffer, base + (DWORD)functions[i], 1);
705 }
706 }
707 }
708 /* no real debug info, only entry points */
709 if (module->module.SymType == SymDeferred)
710 module->module.SymType = SymExport;
711 pe_unmap_full(fmap);
712
713 return TRUE;
714 }
715
716 /******************************************************************
717 * pe_load_debug_info
718 *
719 */
720 BOOL pe_load_debug_info(const struct process* pcs, struct module* module)
721 {
722 BOOL ret = FALSE;
723
724 if (!(dbghelp_options & SYMOPT_PUBLICS_ONLY))
725 {
726 ret = pe_load_stabs(pcs, module);
727 ret = pe_load_dwarf(module) || ret;
728 #ifndef DBGHELP_STATIC_LIB
729 ret = pe_load_msc_debug_info(pcs, module) || ret;
730 #endif
731 ret = ret || pe_load_coff_symbol_table(module); /* FIXME */
732 /* if we still have no debug info (we could only get SymExport at this
733 * point), then do the SymExport except if we have an ELF container,
734 * in which case we'll rely on the export's on the ELF side
735 */
736 }
737 /* FIXME shouldn't we check that? if (!module_get_debug(pcs, module)) */
738 if (pe_load_export_debug_info(pcs, module) && !ret)
739 ret = TRUE;
740
741 return ret;
742 }
743
744 /******************************************************************
745 * pe_load_native_module
746 *
747 */
748 struct module* pe_load_native_module(struct process* pcs, const WCHAR* name,
749 HANDLE hFile, DWORD64 base, DWORD size)
750 {
751 struct module* module = NULL;
752 BOOL opened = FALSE;
753 struct module_format* modfmt;
754 WCHAR loaded_name[MAX_PATH];
755
756 loaded_name[0] = '\0';
757 if (!hFile)
758 {
759 assert(name);
760
761 if ((hFile = FindExecutableImageExW(name, pcs->search_path, loaded_name, NULL, NULL)) == NULL)
762 return NULL;
763 opened = TRUE;
764 }
765 else if (name) strcpyW(loaded_name, name);
766 else if (dbghelp_options & SYMOPT_DEFERRED_LOADS)
767 FIXME("Trouble ahead (no module name passed in deferred mode)\n");
768 if (!(modfmt = HeapAlloc(GetProcessHeap(), 0, sizeof(struct module_format) + sizeof(struct pe_module_info))))
769 return NULL;
770 modfmt->u.pe_info = (struct pe_module_info*)(modfmt + 1);
771 if (pe_map_file(hFile, &modfmt->u.pe_info->fmap, DMT_PE))
772 {
773 if (!base) base = modfmt->u.pe_info->fmap.u.pe.ntheader.OptionalHeader.ImageBase;
774 if (!size) size = modfmt->u.pe_info->fmap.u.pe.ntheader.OptionalHeader.SizeOfImage;
775
776 module = module_new(pcs, loaded_name, DMT_PE, FALSE, base, size,
777 modfmt->u.pe_info->fmap.u.pe.ntheader.FileHeader.TimeDateStamp,
778 modfmt->u.pe_info->fmap.u.pe.ntheader.OptionalHeader.CheckSum);
779 if (module)
780 {
781 modfmt->module = module;
782 modfmt->remove = pe_module_remove;
783 modfmt->loc_compute = NULL;
784
785 module->format_info[DFI_PE] = modfmt;
786 if (dbghelp_options & SYMOPT_DEFERRED_LOADS)
787 module->module.SymType = SymDeferred;
788 else
789 pe_load_debug_info(pcs, module);
790 module->reloc_delta = base - modfmt->u.pe_info->fmap.u.pe.ntheader.OptionalHeader.ImageBase;
791 }
792 else
793 {
794 ERR("could not load the module '%s'\n", debugstr_w(loaded_name));
795 pe_unmap_file(&modfmt->u.pe_info->fmap);
796 }
797 }
798 if (!module) HeapFree(GetProcessHeap(), 0, modfmt);
799
800 if (opened) CloseHandle(hFile);
801
802 return module;
803 }
804
805 /******************************************************************
806 * pe_load_nt_header
807 *
808 */
809 BOOL pe_load_nt_header(HANDLE hProc, DWORD64 base, IMAGE_NT_HEADERS* nth)
810 {
811 IMAGE_DOS_HEADER dos;
812
813 return ReadProcessMemory(hProc, (char*)(DWORD_PTR)base, &dos, sizeof(dos), NULL) &&
814 dos.e_magic == IMAGE_DOS_SIGNATURE &&
815 ReadProcessMemory(hProc, (char*)(DWORD_PTR)(base + dos.e_lfanew),
816 nth, sizeof(*nth), NULL) &&
817 nth->Signature == IMAGE_NT_SIGNATURE;
818 }
819
820 /******************************************************************
821 * pe_load_builtin_module
822 *
823 */
824 struct module* pe_load_builtin_module(struct process* pcs, const WCHAR* name,
825 DWORD64 base, DWORD64 size)
826 {
827 struct module* module = NULL;
828
829 if (base && pcs->dbg_hdr_addr)
830 {
831 IMAGE_NT_HEADERS nth;
832
833 if (pe_load_nt_header(pcs->handle, base, &nth))
834 {
835 if (!size) size = nth.OptionalHeader.SizeOfImage;
836 module = module_new(pcs, name, DMT_PE, FALSE, base, size,
837 nth.FileHeader.TimeDateStamp,
838 nth.OptionalHeader.CheckSum);
839 }
840 }
841 return module;
842 }
843
844 /***********************************************************************
845 * ImageDirectoryEntryToDataEx (DBGHELP.@)
846 *
847 * Search for specified directory in PE image
848 *
849 * PARAMS
850 *
851 * base [in] Image base address
852 * image [in] TRUE - image has been loaded by loader, FALSE - raw file image
853 * dir [in] Target directory index
854 * size [out] Receives directory size
855 * section [out] Receives pointer to section header of section containing directory data
856 *
857 * RETURNS
858 * Success: pointer to directory data
859 * Failure: NULL
860 *
861 */
862 PVOID WINAPI ImageDirectoryEntryToDataEx( PVOID base, BOOLEAN image, USHORT dir, PULONG size, PIMAGE_SECTION_HEADER *section )
863 {
864 const IMAGE_NT_HEADERS *nt;
865 DWORD addr;
866
867 *size = 0;
868 if (section) *section = NULL;
869
870 if (!(nt = RtlImageNtHeader( base ))) return NULL;
871 if (dir >= nt->OptionalHeader.NumberOfRvaAndSizes) return NULL;
872 if (!(addr = nt->OptionalHeader.DataDirectory[dir].VirtualAddress)) return NULL;
873
874 *size = nt->OptionalHeader.DataDirectory[dir].Size;
875 if (image || addr < nt->OptionalHeader.SizeOfHeaders) return (char *)base + addr;
876
877 return RtlImageRvaToVa( nt, base, addr, section );
878 }
879
880 /***********************************************************************
881 * ImageDirectoryEntryToData (DBGHELP.@)
882 *
883 * NOTES
884 * See ImageDirectoryEntryToDataEx
885 */
886 PVOID WINAPI ImageDirectoryEntryToData( PVOID base, BOOLEAN image, USHORT dir, PULONG size )
887 {
888 return ImageDirectoryEntryToDataEx( base, image, dir, size, NULL );
889 }