xref: /reactos/dll/win32/dbghelp/pe_module.c (revision c2c66aff)
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 static void pe_module_remove(struct process* pcs, struct module_format* modfmt)
315 {
316     pe_unmap_file(&modfmt->u.pe_info->fmap);
317     HeapFree(GetProcessHeap(), 0, modfmt);
318 }
319 
320 /******************************************************************
321  *		pe_locate_with_coff_symbol_table
322  *
323  * Use the COFF symbol table (if any) from the IMAGE_FILE_HEADER to set the absolute address
324  * of global symbols.
325  * Mingw32 requires this for stabs debug information as address for global variables isn't filled in
326  * (this is similar to what is done in elf_module.c when using the .symtab ELF section)
327  */
328 static BOOL pe_locate_with_coff_symbol_table(struct module* module)
329 {
330     struct image_file_map* fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
331     const IMAGE_SYMBOL* isym;
332     int                 i, numsym, naux;
333     char                tmp[9];
334     const char*         name;
335     struct hash_table_iter      hti;
336     void*               ptr;
337     struct symt_data*   sym;
338     const char*         mapping;
339 
340     numsym = fmap->u.pe.ntheader.FileHeader.NumberOfSymbols;
341     if (!fmap->u.pe.ntheader.FileHeader.PointerToSymbolTable || !numsym)
342         return TRUE;
343     if (!(mapping = pe_map_full(fmap, NULL))) return FALSE;
344     isym = (const IMAGE_SYMBOL*)(mapping + fmap->u.pe.ntheader.FileHeader.PointerToSymbolTable);
345 
346     for (i = 0; i < numsym; i+= naux, isym += naux)
347     {
348         if (isym->StorageClass == IMAGE_SYM_CLASS_EXTERNAL &&
349             isym->SectionNumber > 0 && isym->SectionNumber <= fmap->u.pe.ntheader.FileHeader.NumberOfSections)
350         {
351             if (isym->N.Name.Short)
352             {
353                 name = memcpy(tmp, isym->N.ShortName, 8);
354                 tmp[8] = '\0';
355             }
356             else name = fmap->u.pe.strtable + isym->N.Name.Long;
357             if (name[0] == '_') name++;
358             hash_table_iter_init(&module->ht_symbols, &hti, name);
359             while ((ptr = hash_table_iter_up(&hti)))
360             {
361                 sym = CONTAINING_RECORD(ptr, struct symt_data, hash_elt);
362                 if (sym->symt.tag == SymTagData &&
363                     (sym->kind == DataIsGlobal || sym->kind == DataIsFileStatic) &&
364                     sym->u.var.kind == loc_absolute &&
365                     !strcmp(sym->hash_elt.name, name))
366                 {
367                     TRACE("Changing absolute address for %d.%s: %lx -> %s\n",
368                           isym->SectionNumber, name, sym->u.var.offset,
369                           wine_dbgstr_longlong(module->module.BaseOfImage +
370                                                fmap->u.pe.sect[isym->SectionNumber - 1].shdr.VirtualAddress +
371                                                isym->Value));
372                     sym->u.var.offset = module->module.BaseOfImage +
373                         fmap->u.pe.sect[isym->SectionNumber - 1].shdr.VirtualAddress + isym->Value;
374                     break;
375                 }
376             }
377         }
378         naux = isym->NumberOfAuxSymbols + 1;
379     }
380     pe_unmap_full(fmap);
381     return TRUE;
382 }
383 
384 /******************************************************************
385  *		pe_load_coff_symbol_table
386  *
387  * Load public symbols out of the COFF symbol table (if any).
388  */
389 static BOOL pe_load_coff_symbol_table(struct module* module)
390 {
391     struct image_file_map* fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
392     const IMAGE_SYMBOL* isym;
393     int                 i, numsym, naux;
394     const char*         strtable;
395     char                tmp[9];
396     const char*         name;
397     const char*         lastfilename = NULL;
398     struct symt_compiland*   compiland = NULL;
399     const IMAGE_SECTION_HEADER* sect;
400     const char*         mapping;
401 
402     numsym = fmap->u.pe.ntheader.FileHeader.NumberOfSymbols;
403     if (!fmap->u.pe.ntheader.FileHeader.PointerToSymbolTable || !numsym)
404         return TRUE;
405     if (!(mapping = pe_map_full(fmap, NULL))) return FALSE;
406     isym = (const IMAGE_SYMBOL*)((const char*)mapping + fmap->u.pe.ntheader.FileHeader.PointerToSymbolTable);
407     /* FIXME: no way to get strtable size */
408     strtable = (const char*)&isym[numsym];
409     sect = IMAGE_FIRST_SECTION(RtlImageNtHeader((HMODULE)mapping));
410 
411     for (i = 0; i < numsym; i+= naux, isym += naux)
412     {
413         if (isym->StorageClass == IMAGE_SYM_CLASS_FILE)
414         {
415             lastfilename = (const char*)(isym + 1);
416             compiland = NULL;
417         }
418         if (isym->StorageClass == IMAGE_SYM_CLASS_EXTERNAL &&
419             isym->SectionNumber > 0 && isym->SectionNumber <= fmap->u.pe.ntheader.FileHeader.NumberOfSections)
420         {
421             if (isym->N.Name.Short)
422             {
423                 name = memcpy(tmp, isym->N.ShortName, 8);
424                 tmp[8] = '\0';
425             }
426             else name = strtable + isym->N.Name.Long;
427             if (name[0] == '_') name++;
428 
429             if (!compiland && lastfilename)
430                 compiland = symt_new_compiland(module, 0,
431                                                source_new(module, NULL, lastfilename));
432 
433             if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
434                 symt_new_public(module, compiland, name,
435                                 module->module.BaseOfImage + sect[isym->SectionNumber - 1].VirtualAddress +
436                                      isym->Value,
437                                 1);
438         }
439         naux = isym->NumberOfAuxSymbols + 1;
440     }
441     module->module.SymType = SymCoff;
442     module->module.LineNumbers = FALSE;
443     module->module.GlobalSymbols = FALSE;
444     module->module.TypeInfo = FALSE;
445     module->module.SourceIndexed = FALSE;
446     module->module.Publics = TRUE;
447     pe_unmap_full(fmap);
448 
449     return TRUE;
450 }
451 
452 /******************************************************************
453  *		pe_load_stabs
454  *
455  * look for stabs information in PE header (it's how the mingw compiler provides
456  * its debugging information)
457  */
458 static BOOL pe_load_stabs(const struct process* pcs, struct module* module)
459 {
460     struct image_file_map*      fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
461     struct image_section_map    sect_stabs, sect_stabstr;
462     BOOL                        ret = FALSE;
463 
464     if (pe_find_section(fmap, ".stab", &sect_stabs) && pe_find_section(fmap, ".stabstr", &sect_stabstr))
465     {
466         const char* stab;
467         const char* stabstr;
468 
469         stab = image_map_section(&sect_stabs);
470         stabstr = image_map_section(&sect_stabstr);
471         if (stab != IMAGE_NO_MAP && stabstr != IMAGE_NO_MAP)
472         {
473             ret = stabs_parse(module,
474                               module->module.BaseOfImage - fmap->u.pe.ntheader.OptionalHeader.ImageBase,
475                               stab, image_get_map_size(&sect_stabs),
476                               stabstr, image_get_map_size(&sect_stabstr),
477                               NULL, NULL);
478         }
479         image_unmap_section(&sect_stabs);
480         image_unmap_section(&sect_stabstr);
481         if (ret) pe_locate_with_coff_symbol_table(module);
482     }
483     TRACE("%s the STABS debug info\n", ret ? "successfully loaded" : "failed to load");
484 
485     return ret;
486 }
487 
488 /******************************************************************
489  *		pe_load_dwarf
490  *
491  * look for dwarf information in PE header (it's also a way for the mingw compiler
492  * to provide its debugging information)
493  */
494 static BOOL pe_load_dwarf(struct module* module)
495 {
496     struct image_file_map*      fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
497     BOOL                        ret;
498 
499     ret = dwarf2_parse(module,
500                        module->module.BaseOfImage - fmap->u.pe.ntheader.OptionalHeader.ImageBase,
501                        NULL, /* FIXME: some thunks to deal with ? */
502                        fmap);
503     TRACE("%s the DWARF debug info\n", ret ? "successfully loaded" : "failed to load");
504 
505     return ret;
506 }
507 
508 #ifndef DBGHELP_STATIC_LIB
509 /******************************************************************
510  *		pe_load_rsym
511  *
512  * look for ReactOS's own rsym format
513  */
514 static BOOL pe_load_rsym(struct module* module)
515 {
516     struct image_file_map*      fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
517     struct image_section_map    sect_rsym;
518     BOOL                        ret = FALSE;
519 
520     if (pe_find_section(fmap, ".rossym", &sect_rsym))
521     {
522         const char* rsym = image_map_section(&sect_rsym);
523         if (rsym != IMAGE_NO_MAP)
524         {
525             ret = rsym_parse(module, module->module.BaseOfImage,
526                              rsym, image_get_map_size(&sect_rsym));
527         }
528         image_unmap_section(&sect_rsym);
529     }
530     TRACE("%s the RSYM debug info\n", ret ? "successfully loaded" : "failed to load");
531 
532     return ret;
533 }
534 
535 /******************************************************************
536  *		pe_load_dbg_file
537  *
538  * loads a .dbg file
539  */
540 static BOOL pe_load_dbg_file(const struct process* pcs, struct module* module,
541                              const char* dbg_name, DWORD timestamp)
542 {
543     char                                tmp[MAX_PATH];
544     HANDLE                              hFile = INVALID_HANDLE_VALUE, hMap = 0;
545     const BYTE*                         dbg_mapping = NULL;
546     BOOL                                ret = FALSE;
547 
548     TRACE("Processing DBG file %s\n", debugstr_a(dbg_name));
549 
550     if (path_find_symbol_file(pcs, dbg_name, NULL, timestamp, 0, tmp, &module->module.DbgUnmatched) &&
551         (hFile = CreateFileA(tmp, GENERIC_READ, FILE_SHARE_READ, NULL,
552                              OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) != INVALID_HANDLE_VALUE &&
553         ((hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) != 0) &&
554         ((dbg_mapping = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) != NULL))
555     {
556         const IMAGE_SEPARATE_DEBUG_HEADER*      hdr;
557         const IMAGE_SECTION_HEADER*             sectp;
558         const IMAGE_DEBUG_DIRECTORY*            dbg;
559 
560         hdr = (const IMAGE_SEPARATE_DEBUG_HEADER*)dbg_mapping;
561         /* section headers come immediately after debug header */
562         sectp = (const IMAGE_SECTION_HEADER*)(hdr + 1);
563         /* and after that and the exported names comes the debug directory */
564         dbg = (const IMAGE_DEBUG_DIRECTORY*)
565             (dbg_mapping + sizeof(*hdr) +
566              hdr->NumberOfSections * sizeof(IMAGE_SECTION_HEADER) +
567              hdr->ExportedNamesSize);
568 
569         ret = pe_load_debug_directory(pcs, module, dbg_mapping, sectp,
570                                       hdr->NumberOfSections, dbg,
571                                       hdr->DebugDirectorySize / sizeof(*dbg));
572     }
573     else
574         ERR("Couldn't find .DBG file %s (%s)\n", debugstr_a(dbg_name), debugstr_a(tmp));
575 
576     if (dbg_mapping) UnmapViewOfFile(dbg_mapping);
577     if (hMap) CloseHandle(hMap);
578     if (hFile != INVALID_HANDLE_VALUE) CloseHandle(hFile);
579     return ret;
580 }
581 
582 /******************************************************************
583  *		pe_load_msc_debug_info
584  *
585  * Process MSC debug information in PE file.
586  */
587 static BOOL pe_load_msc_debug_info(const struct process* pcs, struct module* module)
588 {
589     struct image_file_map*      fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
590     BOOL                        ret = FALSE;
591     const IMAGE_DATA_DIRECTORY* dir;
592     const IMAGE_DEBUG_DIRECTORY*dbg = NULL;
593     int                         nDbg;
594     void*                       mapping;
595     IMAGE_NT_HEADERS*           nth;
596 
597     if (!(mapping = pe_map_full(fmap, &nth))) return FALSE;
598     /* Read in debug directory */
599     dir = nth->OptionalHeader.DataDirectory + IMAGE_DIRECTORY_ENTRY_DEBUG;
600     nDbg = dir->Size / sizeof(IMAGE_DEBUG_DIRECTORY);
601     if (!nDbg) goto done;
602 
603     dbg = RtlImageRvaToVa(nth, mapping, dir->VirtualAddress, NULL);
604 
605     /* Parse debug directory */
606     if (nth->FileHeader.Characteristics & IMAGE_FILE_DEBUG_STRIPPED)
607     {
608         /* Debug info is stripped to .DBG file */
609         const IMAGE_DEBUG_MISC* misc = (const IMAGE_DEBUG_MISC*)
610             ((const char*)mapping + dbg->PointerToRawData);
611 
612         if (nDbg != 1 || dbg->Type != IMAGE_DEBUG_TYPE_MISC ||
613             misc->DataType != IMAGE_DEBUG_MISC_EXENAME)
614         {
615             ERR("-Debug info stripped, but no .DBG file in module %s\n",
616                 debugstr_w(module->module.ModuleName));
617         }
618         else
619         {
620             ret = pe_load_dbg_file(pcs, module, (const char*)misc->Data, nth->FileHeader.TimeDateStamp);
621         }
622     }
623     else
624     {
625         const IMAGE_SECTION_HEADER *sectp = (const IMAGE_SECTION_HEADER*)((const char*)&nth->OptionalHeader + nth->FileHeader.SizeOfOptionalHeader);
626         /* Debug info is embedded into PE module */
627         ret = pe_load_debug_directory(pcs, module, mapping, sectp,
628                                       nth->FileHeader.NumberOfSections, dbg, nDbg);
629     }
630 done:
631     pe_unmap_full(fmap);
632     return ret;
633 }
634 #endif /* DBGHELP_STATIC_LIB */
635 
636 /***********************************************************************
637  *			pe_load_export_debug_info
638  */
639 static BOOL pe_load_export_debug_info(const struct process* pcs, struct module* module)
640 {
641     struct image_file_map*              fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
642     unsigned int 		        i;
643     const IMAGE_EXPORT_DIRECTORY* 	exports;
644     DWORD			        base = module->module.BaseOfImage;
645     DWORD                               size;
646     IMAGE_NT_HEADERS*                   nth;
647     void*                               mapping;
648 
649     if (dbghelp_options & SYMOPT_NO_PUBLICS) return TRUE;
650 
651     if (!(mapping = pe_map_full(fmap, &nth))) return FALSE;
652 #if 0
653     /* Add start of DLL (better use the (yet unimplemented) Exe SymTag for this) */
654     /* FIXME: module.ModuleName isn't correctly set yet if it's passed in SymLoadModule */
655     symt_new_public(module, NULL, module->module.ModuleName, base, 1);
656 #endif
657 
658     /* Add entry point */
659     symt_new_public(module, NULL, "EntryPoint",
660                     base + nth->OptionalHeader.AddressOfEntryPoint, 1);
661 #if 0
662     /* FIXME: we'd better store addresses linked to sections rather than
663        absolute values */
664     IMAGE_SECTION_HEADER*       section;
665     /* Add start of sections */
666     section = (IMAGE_SECTION_HEADER*)
667         ((char*)&nth->OptionalHeader + nth->FileHeader.SizeOfOptionalHeader);
668     for (i = 0; i < nth->FileHeader.NumberOfSections; i++, section++)
669     {
670 	symt_new_public(module, NULL, section->Name,
671                         RtlImageRvaToVa(nth, mapping, section->VirtualAddress, NULL), 1);
672     }
673 #endif
674 
675     /* Add exported functions */
676     if ((exports = RtlImageDirectoryEntryToData(mapping, FALSE,
677                                                 IMAGE_DIRECTORY_ENTRY_EXPORT, &size)))
678     {
679         const WORD*             ordinals = NULL;
680         const DWORD_PTR*	functions = NULL;
681         const DWORD*		names = NULL;
682         unsigned int		j;
683         char			buffer[16];
684 
685         functions = RtlImageRvaToVa(nth, mapping, exports->AddressOfFunctions, NULL);
686         ordinals  = RtlImageRvaToVa(nth, mapping, exports->AddressOfNameOrdinals, NULL);
687         names     = RtlImageRvaToVa(nth, mapping, exports->AddressOfNames, NULL);
688 
689         if (functions && ordinals && names)
690         {
691             for (i = 0; i < exports->NumberOfNames; i++)
692             {
693                 if (!names[i]) continue;
694                 symt_new_public(module, NULL,
695                                 RtlImageRvaToVa(nth, mapping, names[i], NULL),
696                                 base + functions[ordinals[i]], 1);
697             }
698 
699             for (i = 0; i < exports->NumberOfFunctions; i++)
700             {
701                 if (!functions[i]) continue;
702                 /* Check if we already added it with a name */
703                 for (j = 0; j < exports->NumberOfNames; j++)
704                     if ((ordinals[j] == i) && names[j]) break;
705                 if (j < exports->NumberOfNames) continue;
706                 snprintf(buffer, sizeof(buffer), "%d", i + exports->Base);
707                 symt_new_public(module, NULL, buffer, base + (DWORD)functions[i], 1);
708             }
709         }
710     }
711     /* no real debug info, only entry points */
712     if (module->module.SymType == SymDeferred)
713         module->module.SymType = SymExport;
714     pe_unmap_full(fmap);
715 
716     return TRUE;
717 }
718 
719 /******************************************************************
720  *		pe_load_debug_info
721  *
722  */
723 BOOL pe_load_debug_info(const struct process* pcs, struct module* module)
724 {
725     BOOL                ret = FALSE;
726 
727     if (!(dbghelp_options & SYMOPT_PUBLICS_ONLY))
728     {
729         ret = pe_load_stabs(pcs, module);
730         ret = pe_load_dwarf(module) || ret;
731         #ifndef DBGHELP_STATIC_LIB
732         ret = pe_load_msc_debug_info(pcs, module) || ret;
733         ret = pe_load_rsym(module) || ret;
734         #endif
735 
736         ret = ret || pe_load_coff_symbol_table(module); /* FIXME */
737         /* if we still have no debug info (we could only get SymExport at this
738          * point), then do the SymExport except if we have an ELF container,
739          * in which case we'll rely on the export's on the ELF side
740          */
741     }
742     /* FIXME shouldn't we check that? if (!module_get_debug(pcs, module)) */
743     if (pe_load_export_debug_info(pcs, module) && !ret)
744         ret = TRUE;
745 
746     return ret;
747 }
748 
749 /******************************************************************
750  *		pe_load_native_module
751  *
752  */
753 struct module* pe_load_native_module(struct process* pcs, const WCHAR* name,
754                                      HANDLE hFile, DWORD64 base, DWORD size)
755 {
756     struct module*              module = NULL;
757     BOOL                        opened = FALSE;
758     struct module_format*       modfmt;
759     WCHAR                       loaded_name[MAX_PATH];
760 
761     loaded_name[0] = '\0';
762     if (!hFile)
763     {
764         assert(name);
765 
766         if ((hFile = FindExecutableImageExW(name, pcs->search_path, loaded_name, NULL, NULL)) == NULL)
767             return NULL;
768         opened = TRUE;
769     }
770     else if (name) strcpyW(loaded_name, name);
771     else if (dbghelp_options & SYMOPT_DEFERRED_LOADS)
772         FIXME("Trouble ahead (no module name passed in deferred mode)\n");
773     if (!(modfmt = HeapAlloc(GetProcessHeap(), 0, sizeof(struct module_format) + sizeof(struct pe_module_info))))
774         return NULL;
775     modfmt->u.pe_info = (struct pe_module_info*)(modfmt + 1);
776     if (pe_map_file(hFile, &modfmt->u.pe_info->fmap, DMT_PE))
777     {
778         if (!base) base = modfmt->u.pe_info->fmap.u.pe.ntheader.OptionalHeader.ImageBase;
779         if (!size) size = modfmt->u.pe_info->fmap.u.pe.ntheader.OptionalHeader.SizeOfImage;
780 
781         module = module_new(pcs, loaded_name, DMT_PE, FALSE, base, size,
782                             modfmt->u.pe_info->fmap.u.pe.ntheader.FileHeader.TimeDateStamp,
783                             modfmt->u.pe_info->fmap.u.pe.ntheader.OptionalHeader.CheckSum);
784         if (module)
785         {
786             modfmt->module = module;
787             modfmt->remove = pe_module_remove;
788             modfmt->loc_compute = NULL;
789 
790             module->format_info[DFI_PE] = modfmt;
791             if (dbghelp_options & SYMOPT_DEFERRED_LOADS)
792                 module->module.SymType = SymDeferred;
793             else
794                 pe_load_debug_info(pcs, module);
795             module->reloc_delta = base - modfmt->u.pe_info->fmap.u.pe.ntheader.OptionalHeader.ImageBase;
796         }
797         else
798         {
799             ERR("could not load the module '%s'\n", debugstr_w(loaded_name));
800             pe_unmap_file(&modfmt->u.pe_info->fmap);
801         }
802     }
803     if (!module) HeapFree(GetProcessHeap(), 0, modfmt);
804 
805     if (opened) CloseHandle(hFile);
806 
807     return module;
808 }
809 
810 /******************************************************************
811  *		pe_load_nt_header
812  *
813  */
814 BOOL pe_load_nt_header(HANDLE hProc, DWORD64 base, IMAGE_NT_HEADERS* nth)
815 {
816     IMAGE_DOS_HEADER    dos;
817 
818     return ReadProcessMemory(hProc, (char*)(DWORD_PTR)base, &dos, sizeof(dos), NULL) &&
819         dos.e_magic == IMAGE_DOS_SIGNATURE &&
820         ReadProcessMemory(hProc, (char*)(DWORD_PTR)(base + dos.e_lfanew),
821                           nth, sizeof(*nth), NULL) &&
822         nth->Signature == IMAGE_NT_SIGNATURE;
823 }
824 
825 /******************************************************************
826  *		pe_load_builtin_module
827  *
828  */
829 struct module* pe_load_builtin_module(struct process* pcs, const WCHAR* name,
830                                       DWORD64 base, DWORD64 size)
831 {
832     struct module*      module = NULL;
833 
834     if (base && pcs->dbg_hdr_addr)
835     {
836         IMAGE_NT_HEADERS    nth;
837 
838         if (pe_load_nt_header(pcs->handle, base, &nth))
839         {
840             if (!size) size = nth.OptionalHeader.SizeOfImage;
841             module = module_new(pcs, name, DMT_PE, FALSE, base, size,
842                                 nth.FileHeader.TimeDateStamp,
843                                 nth.OptionalHeader.CheckSum);
844         }
845     }
846     return module;
847 }
848 
849 /***********************************************************************
850  *           ImageDirectoryEntryToDataEx (DBGHELP.@)
851  *
852  * Search for specified directory in PE image
853  *
854  * PARAMS
855  *
856  *   base    [in]  Image base address
857  *   image   [in]  TRUE - image has been loaded by loader, FALSE - raw file image
858  *   dir     [in]  Target directory index
859  *   size    [out] Receives directory size
860  *   section [out] Receives pointer to section header of section containing directory data
861  *
862  * RETURNS
863  *   Success: pointer to directory data
864  *   Failure: NULL
865  *
866  */
867 PVOID WINAPI ImageDirectoryEntryToDataEx( PVOID base, BOOLEAN image, USHORT dir, PULONG size, PIMAGE_SECTION_HEADER *section )
868 {
869     const IMAGE_NT_HEADERS *nt;
870     DWORD addr;
871 
872     *size = 0;
873     if (section) *section = NULL;
874 
875     if (!(nt = RtlImageNtHeader( base ))) return NULL;
876     if (dir >= nt->OptionalHeader.NumberOfRvaAndSizes) return NULL;
877     if (!(addr = nt->OptionalHeader.DataDirectory[dir].VirtualAddress)) return NULL;
878 
879     *size = nt->OptionalHeader.DataDirectory[dir].Size;
880     if (image || addr < nt->OptionalHeader.SizeOfHeaders) return (char *)base + addr;
881 
882     return RtlImageRvaToVa( nt, base, addr, section );
883 }
884 
885 /***********************************************************************
886  *         ImageDirectoryEntryToData   (DBGHELP.@)
887  *
888  * NOTES
889  *   See ImageDirectoryEntryToDataEx
890  */
891 PVOID WINAPI ImageDirectoryEntryToData( PVOID base, BOOLEAN image, USHORT dir, PULONG size )
892 {
893     return ImageDirectoryEntryToDataEx( base, image, dir, size, NULL );
894 }
895