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