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