dbghelp: Added full support for DW_FORM_data8 attributes, as well as primary support...
[wine] / dlls / 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 "winternl.h"
34 #include "wine/debug.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
37
38 /******************************************************************
39  *              pe_locate_with_coff_symbol_table
40  *
41  * Use the COFF symbol table (if any) from the IMAGE_FILE_HEADER to set the absolute address
42  * of global symbols.
43  * Mingw32 requires this for stabs debug information as address for global variables isn't filled in
44  * (this is similar to what is done in elf_module.c when using the .symtab ELF section)
45  */
46 static BOOL pe_locate_with_coff_symbol_table(struct module* module, IMAGE_NT_HEADERS* nth, void* mapping)
47 {
48     const IMAGE_SYMBOL* isym;
49     int                 i, numsym, naux;
50     const char*         strtable;
51     char                tmp[9];
52     const char*         name;
53     struct hash_table_iter      hti;
54     void*               ptr;
55     struct symt_data*   sym;
56     const IMAGE_SECTION_HEADER* sect;
57
58     numsym = nth->FileHeader.NumberOfSymbols;
59     if (!nth->FileHeader.PointerToSymbolTable || !numsym)
60         return TRUE;
61     isym = (const IMAGE_SYMBOL*)((char*)mapping + nth->FileHeader.PointerToSymbolTable);
62     /* FIXME: no way to get strtable size */
63     strtable = (const char*)&isym[numsym];
64     sect = IMAGE_FIRST_SECTION(nth);
65
66     for (i = 0; i < numsym; i+= naux, isym += naux)
67     {
68         if (isym->StorageClass == IMAGE_SYM_CLASS_EXTERNAL &&
69             isym->SectionNumber > 0 && isym->SectionNumber <= nth->FileHeader.NumberOfSections)
70         {
71             if (isym->N.Name.Short)
72             {
73                 name = memcpy(tmp, isym->N.ShortName, 8);
74                 tmp[8] = '\0';
75             }
76             else name = strtable + isym->N.Name.Long;
77             if (name[0] == '_') name++;
78             hash_table_iter_init(&module->ht_symbols, &hti, name);
79             while ((ptr = hash_table_iter_up(&hti)))
80             {
81                 sym = GET_ENTRY(ptr, struct symt_data, hash_elt);
82                 if (sym->symt.tag == SymTagData &&
83                     (sym->kind == DataIsGlobal || sym->kind == DataIsFileStatic) &&
84                     !strcmp(sym->hash_elt.name, name))
85                 {
86                     TRACE("Changing absolute address for %d.%s: %lx -> %s\n",
87                           isym->SectionNumber, name, sym->u.var.offset,
88                           wine_dbgstr_longlong(module->module.BaseOfImage +
89                                                sect[isym->SectionNumber - 1].VirtualAddress + isym->Value));
90                     sym->u.var.offset = module->module.BaseOfImage +
91                         sect[isym->SectionNumber - 1].VirtualAddress + isym->Value;
92                     break;
93                 }
94             }
95         }
96         naux = isym->NumberOfAuxSymbols + 1;
97     }
98     return TRUE;
99 }
100
101 /******************************************************************
102  *              pe_load_coff_symbol_table
103  *
104  * Load public symbols out of the COFF symbol table (if any).
105  */
106 static BOOL pe_load_coff_symbol_table(struct module* module, IMAGE_NT_HEADERS* nth, void* mapping)
107 {
108     const IMAGE_SYMBOL* isym;
109     int                 i, numsym, naux;
110     const char*         strtable;
111     char                tmp[9];
112     const char*         name;
113     const char*         lastfilename = NULL;
114     struct symt_compiland*   compiland = NULL;
115     const IMAGE_SECTION_HEADER* sect;
116
117     numsym = nth->FileHeader.NumberOfSymbols;
118     if (!nth->FileHeader.PointerToSymbolTable || !numsym)
119         return TRUE;
120     isym = (const IMAGE_SYMBOL*)((char*)mapping + nth->FileHeader.PointerToSymbolTable);
121     /* FIXME: no way to get strtable size */
122     strtable = (const char*)&isym[numsym];
123     sect = IMAGE_FIRST_SECTION(nth);
124
125     for (i = 0; i < numsym; i+= naux, isym += naux)
126     {
127         if (isym->StorageClass == IMAGE_SYM_CLASS_FILE)
128         {
129             lastfilename = (const char*)(isym + 1);
130             compiland = NULL;
131         }
132         if (isym->StorageClass == IMAGE_SYM_CLASS_EXTERNAL &&
133             isym->SectionNumber > 0 && isym->SectionNumber <= nth->FileHeader.NumberOfSections)
134         {
135             if (isym->N.Name.Short)
136             {
137                 name = memcpy(tmp, isym->N.ShortName, 8);
138                 tmp[8] = '\0';
139             }
140             else name = strtable + isym->N.Name.Long;
141             if (name[0] == '_') name++;
142
143             if (!compiland && lastfilename)
144                 compiland = symt_new_compiland(module, 0,
145                                                source_new(module, NULL, lastfilename));
146             symt_new_public(module, compiland, name,
147                             module->module.BaseOfImage + sect[isym->SectionNumber - 1].VirtualAddress + isym->Value,
148                             1);
149         }
150         naux = isym->NumberOfAuxSymbols + 1;
151     }
152     module->module.SymType = SymCoff;
153     module->module.LineNumbers = FALSE;
154     module->module.GlobalSymbols = FALSE;
155     module->module.TypeInfo = FALSE;
156     module->module.SourceIndexed = FALSE;
157     module->module.Publics = TRUE;
158
159     return TRUE;
160 }
161
162 static inline void* pe_get_sect(IMAGE_NT_HEADERS* nth, void* mapping,
163                                 IMAGE_SECTION_HEADER* sect)
164 {
165     return (sect) ? RtlImageRvaToVa(nth, mapping, sect->VirtualAddress, NULL) : NULL;
166 }
167
168 static inline DWORD pe_get_sect_size(IMAGE_SECTION_HEADER* sect)
169 {
170     return (sect) ? sect->SizeOfRawData : 0;
171 }
172
173 /******************************************************************
174  *              pe_load_stabs
175  *
176  * look for stabs information in PE header (it's how the mingw compiler provides 
177  * its debugging information)
178  */
179 static BOOL pe_load_stabs(const struct process* pcs, struct module* module, 
180                           void* mapping, IMAGE_NT_HEADERS* nth)
181 {
182     IMAGE_SECTION_HEADER*       section;
183     IMAGE_SECTION_HEADER*       sect_stabs = NULL;
184     IMAGE_SECTION_HEADER*       sect_stabstr = NULL;
185     int                         i;
186     BOOL                        ret = FALSE;
187
188     section = (IMAGE_SECTION_HEADER*)
189         ((char*)&nth->OptionalHeader + nth->FileHeader.SizeOfOptionalHeader);
190     for (i = 0; i < nth->FileHeader.NumberOfSections; i++, section++)
191     {
192         if (!strcasecmp((const char*)section->Name, ".stab"))              sect_stabs = section;
193         else if (!strncasecmp((const char*)section->Name, ".stabstr", 8))  sect_stabstr = section;
194     }
195     if (sect_stabs && sect_stabstr)
196     {
197         ret = stabs_parse(module,
198                           module->module.BaseOfImage - nth->OptionalHeader.ImageBase,
199                           pe_get_sect(nth, mapping, sect_stabs),   pe_get_sect_size(sect_stabs),
200                           pe_get_sect(nth, mapping, sect_stabstr), pe_get_sect_size(sect_stabstr),
201                           NULL, NULL);
202         if (ret) pe_locate_with_coff_symbol_table(module, nth, mapping);
203     }
204     TRACE("%s the STABS debug info\n", ret ? "successfully loaded" : "failed to load");
205
206     return ret;
207 }
208
209 /******************************************************************
210  *              pe_load_dwarf
211  *
212  * look for dwarf information in PE header (it's also a way for the mingw compiler
213  * to provide its debugging information)
214  */
215 static BOOL pe_load_dwarf(const struct process* pcs, struct module* module,
216                           void* mapping, IMAGE_NT_HEADERS* nth)
217 {
218     IMAGE_SECTION_HEADER*       section;
219     IMAGE_SECTION_HEADER*       sect_debuginfo = NULL;
220     IMAGE_SECTION_HEADER*       sect_debugstr = NULL;
221     IMAGE_SECTION_HEADER*       sect_debugabbrev = NULL;
222     IMAGE_SECTION_HEADER*       sect_debugline = NULL;
223     IMAGE_SECTION_HEADER*       sect_debugloc = NULL;
224     int                         i;
225     const char*                 strtable;
226     const char*                 sectname;
227     BOOL                        ret = FALSE;
228
229     if (nth->FileHeader.PointerToSymbolTable && nth->FileHeader.NumberOfSymbols)
230         /* FIXME: no way to get strtable size */
231         strtable = (const char*)mapping + nth->FileHeader.PointerToSymbolTable +
232              nth->FileHeader.NumberOfSymbols * sizeof(IMAGE_SYMBOL);
233     else strtable = NULL;
234
235     section = (IMAGE_SECTION_HEADER*)
236         ((char*)&nth->OptionalHeader + nth->FileHeader.SizeOfOptionalHeader);
237     for (i = 0; i < nth->FileHeader.NumberOfSections; i++, section++)
238     {
239         sectname = (const char*)section->Name;
240         /* long section names start with a '/' (at least on MinGW32) */
241         if (*sectname == '/' && strtable)
242             sectname = strtable + atoi(sectname + 1);
243         if (!strcasecmp(sectname, ".debug_info"))        sect_debuginfo = section;
244         else if (!strcasecmp(sectname, ".debug_str"))    sect_debugstr = section;
245         else if (!strcasecmp(sectname, ".debug_abbrev")) sect_debugabbrev = section;
246         else if (!strcasecmp(sectname, ".debug_line"))   sect_debugline = section;
247         else if (!strcasecmp(sectname, ".debug_loc"))    sect_debugloc = section;
248     }
249     if (sect_debuginfo)
250     {
251         ret = dwarf2_parse(module,
252                            module->module.BaseOfImage - nth->OptionalHeader.ImageBase,
253                            NULL, /* FIXME: some thunks to deal with ? */
254                            pe_get_sect(nth, mapping, sect_debuginfo),   pe_get_sect_size(sect_debuginfo),
255                            pe_get_sect(nth, mapping, sect_debugabbrev), pe_get_sect_size(sect_debugabbrev),
256                            pe_get_sect(nth, mapping, sect_debugstr),    pe_get_sect_size(sect_debugstr),
257                            pe_get_sect(nth, mapping, sect_debugline),   pe_get_sect_size(sect_debugline),
258                            pe_get_sect(nth, mapping, sect_debugloc),    pe_get_sect_size(sect_debugloc));
259     }
260     TRACE("%s the DWARF debug info\n", ret ? "successfully loaded" : "failed to load");
261
262     return ret;
263 }
264
265 /******************************************************************
266  *              pe_load_dbg_file
267  *
268  * loads a .dbg file
269  */
270 static BOOL pe_load_dbg_file(const struct process* pcs, struct module* module,
271                              const char* dbg_name, DWORD timestamp)
272 {
273     char                                tmp[MAX_PATH];
274     HANDLE                              hFile = INVALID_HANDLE_VALUE, hMap = 0;
275     const BYTE*                         dbg_mapping = NULL;
276     BOOL                                ret = FALSE;
277
278     TRACE("Processing DBG file %s\n", debugstr_a(dbg_name));
279
280     if (path_find_symbol_file(pcs, dbg_name, NULL, timestamp, 0, tmp, &module->module.DbgUnmatched) &&
281         (hFile = CreateFileA(tmp, GENERIC_READ, FILE_SHARE_READ, NULL,
282                              OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) != INVALID_HANDLE_VALUE &&
283         ((hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) != 0) &&
284         ((dbg_mapping = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) != NULL))
285     {
286         const IMAGE_SEPARATE_DEBUG_HEADER*      hdr;
287         const IMAGE_SECTION_HEADER*             sectp;
288         const IMAGE_DEBUG_DIRECTORY*            dbg;
289
290         hdr = (const IMAGE_SEPARATE_DEBUG_HEADER*)dbg_mapping;
291         /* section headers come immediately after debug header */
292         sectp = (const IMAGE_SECTION_HEADER*)(hdr + 1);
293         /* and after that and the exported names comes the debug directory */
294         dbg = (const IMAGE_DEBUG_DIRECTORY*)
295             (dbg_mapping + sizeof(*hdr) +
296              hdr->NumberOfSections * sizeof(IMAGE_SECTION_HEADER) +
297              hdr->ExportedNamesSize);
298
299         ret = pe_load_debug_directory(pcs, module, dbg_mapping, sectp,
300                                       hdr->NumberOfSections, dbg,
301                                       hdr->DebugDirectorySize / sizeof(*dbg));
302     }
303     else
304         ERR("Couldn't find .DBG file %s (%s)\n", debugstr_a(dbg_name), debugstr_a(tmp));
305
306     if (dbg_mapping) UnmapViewOfFile(dbg_mapping);
307     if (hMap) CloseHandle(hMap);
308     if (hFile != INVALID_HANDLE_VALUE) CloseHandle(hFile);
309     return ret;
310 }
311
312 /******************************************************************
313  *              pe_load_msc_debug_info
314  *
315  * Process MSC debug information in PE file.
316  */
317 static BOOL pe_load_msc_debug_info(const struct process* pcs, 
318                                    struct module* module,
319                                    void* mapping, const IMAGE_NT_HEADERS* nth)
320 {
321     BOOL                        ret = FALSE;
322     const IMAGE_DATA_DIRECTORY* dir;
323     const IMAGE_DEBUG_DIRECTORY*dbg = NULL;
324     int                         nDbg;
325
326     /* Read in debug directory */
327     dir = nth->OptionalHeader.DataDirectory + IMAGE_DIRECTORY_ENTRY_DEBUG;
328     nDbg = dir->Size / sizeof(IMAGE_DEBUG_DIRECTORY);
329     if (!nDbg) return FALSE;
330
331     dbg = RtlImageRvaToVa(nth, mapping, dir->VirtualAddress, NULL);
332
333     /* Parse debug directory */
334     if (nth->FileHeader.Characteristics & IMAGE_FILE_DEBUG_STRIPPED)
335     {
336         /* Debug info is stripped to .DBG file */
337         const IMAGE_DEBUG_MISC* misc = (const IMAGE_DEBUG_MISC*)
338             ((const char*)mapping + dbg->PointerToRawData);
339
340         if (nDbg != 1 || dbg->Type != IMAGE_DEBUG_TYPE_MISC ||
341             misc->DataType != IMAGE_DEBUG_MISC_EXENAME)
342         {
343             WINE_ERR("-Debug info stripped, but no .DBG file in module %s\n",
344                      debugstr_w(module->module.ModuleName));
345         }
346         else
347         {
348             ret = pe_load_dbg_file(pcs, module, (const char*)misc->Data, nth->FileHeader.TimeDateStamp);
349         }
350     }
351     else
352     {
353         const IMAGE_SECTION_HEADER *sectp = (const IMAGE_SECTION_HEADER*)((const char*)&nth->OptionalHeader + nth->FileHeader.SizeOfOptionalHeader);
354         /* Debug info is embedded into PE module */
355         ret = pe_load_debug_directory(pcs, module, mapping, sectp,
356             nth->FileHeader.NumberOfSections, dbg, nDbg);
357     }
358
359     return ret;
360 }
361
362 /***********************************************************************
363  *                      pe_load_export_debug_info
364  */
365 static BOOL pe_load_export_debug_info(const struct process* pcs, 
366                                       struct module* module, 
367                                       void* mapping, const IMAGE_NT_HEADERS* nth)
368 {
369     unsigned int                        i;
370     const IMAGE_EXPORT_DIRECTORY*       exports;
371     DWORD                               base = module->module.BaseOfImage;
372     DWORD                               size;
373
374     if (dbghelp_options & SYMOPT_NO_PUBLICS) return TRUE;
375
376 #if 0
377     /* Add start of DLL (better use the (yet unimplemented) Exe SymTag for this) */
378     /* FIXME: module.ModuleName isn't correctly set yet if it's passed in SymLoadModule */
379     symt_new_public(module, NULL, module->module.ModuleName, base, 1);
380 #endif
381     
382     /* Add entry point */
383     symt_new_public(module, NULL, "EntryPoint", 
384                     base + nth->OptionalHeader.AddressOfEntryPoint, 1);
385 #if 0
386     /* FIXME: we'd better store addresses linked to sections rather than 
387        absolute values */
388     IMAGE_SECTION_HEADER*       section;
389     /* Add start of sections */
390     section = (IMAGE_SECTION_HEADER*)
391         ((char*)&nth->OptionalHeader + nth->FileHeader.SizeOfOptionalHeader);
392     for (i = 0; i < nth->FileHeader.NumberOfSections; i++, section++) 
393     {
394         symt_new_public(module, NULL, section->Name, 
395                         RtlImageRvaToVa(nth, mapping, section->VirtualAddress, NULL), 1);
396     }
397 #endif
398
399     /* Add exported functions */
400     if ((exports = RtlImageDirectoryEntryToData(mapping, FALSE,
401                                                 IMAGE_DIRECTORY_ENTRY_EXPORT, &size)))
402     {
403         const WORD*             ordinals = NULL;
404         const DWORD_PTR*        functions = NULL;
405         const DWORD*            names = NULL;
406         unsigned int            j;
407         char                    buffer[16];
408
409         functions = RtlImageRvaToVa(nth, mapping, exports->AddressOfFunctions, NULL);
410         ordinals  = RtlImageRvaToVa(nth, mapping, exports->AddressOfNameOrdinals, NULL);
411         names     = RtlImageRvaToVa(nth, mapping, exports->AddressOfNames, NULL);
412
413         if (functions && ordinals && names)
414         {
415             for (i = 0; i < exports->NumberOfNames; i++)
416             {
417                 if (!names[i]) continue;
418                 symt_new_public(module, NULL,
419                                 RtlImageRvaToVa(nth, mapping, names[i], NULL),
420                                 base + functions[ordinals[i]], 1);
421             }
422
423             for (i = 0; i < exports->NumberOfFunctions; i++)
424             {
425                 if (!functions[i]) continue;
426                 /* Check if we already added it with a name */
427                 for (j = 0; j < exports->NumberOfNames; j++)
428                     if ((ordinals[j] == i) && names[j]) break;
429                 if (j < exports->NumberOfNames) continue;
430                 snprintf(buffer, sizeof(buffer), "%d", i + exports->Base);
431                 symt_new_public(module, NULL, buffer, base + (DWORD)functions[i], 1);
432             }
433         }
434     }
435     /* no real debug info, only entry points */
436     if (module->module.SymType == SymDeferred)
437         module->module.SymType = SymExport;
438     return TRUE;
439 }
440
441 /******************************************************************
442  *              pe_load_debug_info
443  *
444  */
445 BOOL pe_load_debug_info(const struct process* pcs, struct module* module)
446 {
447     BOOL                ret = FALSE;
448     HANDLE              hFile;
449     HANDLE              hMap;
450     void*               mapping;
451     IMAGE_NT_HEADERS*   nth;
452
453     hFile = CreateFileW(module->module.LoadedImageName, GENERIC_READ, FILE_SHARE_READ,
454                         NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
455     if (hFile == INVALID_HANDLE_VALUE) return ret;
456     if ((hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) != 0)
457     {
458         if ((mapping = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) != NULL)
459         {
460             nth = RtlImageNtHeader(mapping);
461
462             if (!(dbghelp_options & SYMOPT_PUBLICS_ONLY))
463             {
464                 ret = pe_load_stabs(pcs, module, mapping, nth) ||
465                     pe_load_dwarf(pcs, module, mapping, nth) ||
466                     pe_load_msc_debug_info(pcs, module, mapping, nth) ||
467                     pe_load_coff_symbol_table(module, nth, mapping);
468                 /* if we still have no debug info (we could only get SymExport at this
469                  * point), then do the SymExport except if we have an ELF container, 
470                  * in which case we'll rely on the export's on the ELF side
471                  */
472             }
473 /* FIXME shouldn't we check that? if (!module_get_debug(pcs, module))l */
474             if (pe_load_export_debug_info(pcs, module, mapping, nth) && !ret)
475                 ret = TRUE;
476             UnmapViewOfFile(mapping);
477         }
478         CloseHandle(hMap);
479     }
480     CloseHandle(hFile);
481
482     return ret;
483 }
484
485 /******************************************************************
486  *              pe_load_native_module
487  *
488  */
489 struct module* pe_load_native_module(struct process* pcs, const WCHAR* name,
490                                      HANDLE hFile, DWORD base, DWORD size)
491 {
492     struct module*      module = NULL;
493     BOOL                opened = FALSE;
494     HANDLE              hMap;
495     WCHAR               loaded_name[MAX_PATH];
496
497     loaded_name[0] = '\0';
498     if (!hFile)
499     {
500
501         assert(name);
502
503         if ((hFile = FindExecutableImageExW(name, pcs->search_path, loaded_name, NULL, NULL)) == NULL)
504             return NULL;
505         opened = TRUE;
506     }
507     else if (name) strcpyW(loaded_name, name);
508     else if (dbghelp_options & SYMOPT_DEFERRED_LOADS)
509         FIXME("Trouble ahead (no module name passed in deferred mode)\n");
510
511     if ((hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) != NULL)
512     {
513         void*   mapping;
514
515         if ((mapping = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) != NULL)
516         {
517             IMAGE_NT_HEADERS*   nth = RtlImageNtHeader(mapping);
518
519             if (nth)
520             {
521                 if (!base) base = nth->OptionalHeader.ImageBase;
522                 if (!size) size = nth->OptionalHeader.SizeOfImage;
523
524                 module = module_new(pcs, loaded_name, DMT_PE, FALSE, base, size,
525                                     nth->FileHeader.TimeDateStamp,
526                                     nth->OptionalHeader.CheckSum);
527                 if (module)
528                 {
529                     if (dbghelp_options & SYMOPT_DEFERRED_LOADS)
530                         module->module.SymType = SymDeferred;
531                     else
532                         pe_load_debug_info(pcs, module);
533                 }
534                 else
535                     ERR("could not load the module '%s'\n", debugstr_w(loaded_name));
536             }
537             UnmapViewOfFile(mapping);
538         }
539         CloseHandle(hMap);
540     }
541     if (opened) CloseHandle(hFile);
542
543     return module;
544 }
545
546 /******************************************************************
547  *              pe_load_nt_header
548  *
549  */
550 BOOL pe_load_nt_header(HANDLE hProc, DWORD64 base, IMAGE_NT_HEADERS* nth)
551 {
552     IMAGE_DOS_HEADER    dos;
553
554     return ReadProcessMemory(hProc, (char*)(DWORD_PTR)base, &dos, sizeof(dos), NULL) &&
555         dos.e_magic == IMAGE_DOS_SIGNATURE &&
556         ReadProcessMemory(hProc, (char*)(DWORD_PTR)(base + dos.e_lfanew),
557                           nth, sizeof(*nth), NULL) &&
558         nth->Signature == IMAGE_NT_SIGNATURE;
559 }
560
561 /******************************************************************
562  *              pe_load_builtin_module
563  *
564  */
565 struct module* pe_load_builtin_module(struct process* pcs, const WCHAR* name,
566                                       DWORD64 base, DWORD64 size)
567 {
568     struct module*      module = NULL;
569
570     if (base && pcs->dbg_hdr_addr)
571     {
572         IMAGE_NT_HEADERS    nth;
573
574         if (pe_load_nt_header(pcs->handle, base, &nth))
575         {
576             if (!size) size = nth.OptionalHeader.SizeOfImage;
577             module = module_new(pcs, name, DMT_PE, FALSE, base, size,
578                                 nth.FileHeader.TimeDateStamp,
579                                 nth.OptionalHeader.CheckSum);
580         }
581     }
582     return module;
583 }
584
585 /***********************************************************************
586  *           ImageDirectoryEntryToDataEx (DBGHELP.@)
587  *
588  * Search for specified directory in PE image
589  *
590  * PARAMS
591  *
592  *   base    [in]  Image base address
593  *   image   [in]  TRUE - image has been loaded by loader, FALSE - raw file image
594  *   dir     [in]  Target directory index
595  *   size    [out] Receives directory size
596  *   section [out] Receives pointer to section header of section containing directory data
597  *
598  * RETURNS
599  *   Success: pointer to directory data
600  *   Failure: NULL
601  *
602  */
603 PVOID WINAPI ImageDirectoryEntryToDataEx( PVOID base, BOOLEAN image, USHORT dir, PULONG size, PIMAGE_SECTION_HEADER *section )
604 {
605     const IMAGE_NT_HEADERS *nt;
606     DWORD addr;
607
608     *size = 0;
609     if (section) *section = NULL;
610
611     if (!(nt = RtlImageNtHeader( base ))) return NULL;
612     if (dir >= nt->OptionalHeader.NumberOfRvaAndSizes) return NULL;
613     if (!(addr = nt->OptionalHeader.DataDirectory[dir].VirtualAddress)) return NULL;
614
615     *size = nt->OptionalHeader.DataDirectory[dir].Size;
616     if (image || addr < nt->OptionalHeader.SizeOfHeaders) return (char *)base + addr;
617
618     return RtlImageRvaToVa( nt, base, addr, section );
619 }
620
621 /***********************************************************************
622  *         ImageDirectoryEntryToData   (DBGHELP.@)
623  *
624  * NOTES
625  *   See ImageDirectoryEntryToDataEx
626  */
627 PVOID WINAPI ImageDirectoryEntryToData( PVOID base, BOOLEAN image, USHORT dir, PULONG size )
628 {
629     return ImageDirectoryEntryToDataEx( base, image, dir, size, NULL );
630 }