2 * File pe_module.c - handle PE module information
4 * Copyright (C) 1996, Eric Youngdale.
5 * Copyright (C) 1999-2000, Ulrich Weigand.
6 * Copyright (C) 2004-2007, Eric Pouech.
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.
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.
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
25 #include "wine/port.h"
32 #include "dbghelp_private.h"
33 #include "image_private.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp
);
41 struct image_file_map fmap
;
44 static void* pe_map_full(struct image_file_map
* fmap
, IMAGE_NT_HEADERS
** nth
)
46 if (!fmap
->u
.pe
.full_map
)
48 fmap
->u
.pe
.full_map
= MapViewOfFile(fmap
->u
.pe
.hMap
, FILE_MAP_READ
, 0, 0, 0);
50 if (fmap
->u
.pe
.full_map
)
52 if (nth
) *nth
= RtlImageNtHeader(fmap
->u
.pe
.full_map
);
53 fmap
->u
.pe
.full_count
++;
54 return fmap
->u
.pe
.full_map
;
59 static void pe_unmap_full(struct image_file_map
* fmap
)
61 if (fmap
->u
.pe
.full_count
&& !--fmap
->u
.pe
.full_count
)
63 UnmapViewOfFile(fmap
->u
.pe
.full_map
);
64 fmap
->u
.pe
.full_map
= NULL
;
68 /******************************************************************
71 * Maps a single section into memory from an PE file
73 const char* pe_map_section(struct image_section_map
* ism
)
76 struct pe_file_map
* fmap
= &ism
->fmap
->u
.pe
;
78 if (ism
->sidx
>= 0 && ism
->sidx
< fmap
->ntheader
.FileHeader
.NumberOfSections
&&
79 fmap
->sect
[ism
->sidx
].mapped
== IMAGE_NO_MAP
)
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
86 if ((mapping
= pe_map_full(ism
->fmap
, &nth
)))
88 fmap
->sect
[ism
->sidx
].mapped
= RtlImageRvaToVa(nth
, mapping
,
89 fmap
->sect
[ism
->sidx
].shdr
.VirtualAddress
,
91 return fmap
->sect
[ism
->sidx
].mapped
;
97 /******************************************************************
100 * Finds a section by name (and type) into memory from an PE file
101 * or its alternate if any
103 BOOL
pe_find_section(struct image_file_map
* fmap
, const char* name
,
104 struct image_section_map
* ism
)
106 const char* sectname
;
108 char tmp
[IMAGE_SIZEOF_SHORT_NAME
+ 1];
110 for (i
= 0; i
< fmap
->u
.pe
.ntheader
.FileHeader
.NumberOfSections
; i
++)
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);
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';
122 if (!strcasecmp(sectname
, name
))
135 /******************************************************************
138 * Unmaps a single section from memory
140 void pe_unmap_section(struct image_section_map
* ism
)
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
)
145 pe_unmap_full(ism
->fmap
);
146 ism
->fmap
->u
.pe
.sect
[ism
->sidx
].mapped
= IMAGE_NO_MAP
;
150 /******************************************************************
153 * Get the RVA of an PE section
155 DWORD_PTR
pe_get_map_rva(const struct image_section_map
* ism
)
157 if (ism
->sidx
< 0 || ism
->sidx
>= ism
->fmap
->u
.pe
.ntheader
.FileHeader
.NumberOfSections
)
159 return ism
->fmap
->u
.pe
.sect
[ism
->sidx
].shdr
.VirtualAddress
;
162 /******************************************************************
165 * Get the size of an PE section
167 unsigned pe_get_map_size(const struct image_section_map
* ism
)
169 if (ism
->sidx
< 0 || ism
->sidx
>= ism
->fmap
->u
.pe
.ntheader
.FileHeader
.NumberOfSections
)
171 return ism
->fmap
->u
.pe
.sect
[ism
->sidx
].shdr
.SizeOfRawData
;
174 /******************************************************************
177 * Maps an PE file into memory (and checks it's a real PE file)
179 static BOOL
pe_map_file(HANDLE file
, struct image_file_map
* fmap
, enum module_type mt
)
184 fmap
->u
.pe
.hMap
= CreateFileMappingW(file
, NULL
, PAGE_READONLY
, 0, 0, NULL
);
185 if (fmap
->u
.pe
.hMap
== 0) return FALSE
;
186 fmap
->u
.pe
.full_count
= 0;
187 fmap
->u
.pe
.full_map
= NULL
;
188 if (!(mapping
= pe_map_full(fmap
, NULL
))) goto error
;
194 IMAGE_NT_HEADERS
* nthdr
;
195 IMAGE_SECTION_HEADER
* section
;
198 if (!(nthdr
= RtlImageNtHeader(mapping
))) goto error
;
199 memcpy(&fmap
->u
.pe
.ntheader
, nthdr
, sizeof(fmap
->u
.pe
.ntheader
));
200 section
= (IMAGE_SECTION_HEADER
*)
201 ((char*)&nthdr
->OptionalHeader
+ nthdr
->FileHeader
.SizeOfOptionalHeader
);
202 fmap
->u
.pe
.sect
= HeapAlloc(GetProcessHeap(), 0,
203 nthdr
->FileHeader
.NumberOfSections
* sizeof(fmap
->u
.pe
.sect
[0]));
204 if (!fmap
->u
.pe
.sect
) goto error
;
205 for (i
= 0; i
< nthdr
->FileHeader
.NumberOfSections
; i
++)
207 memcpy(&fmap
->u
.pe
.sect
[i
].shdr
, section
+ i
, sizeof(IMAGE_SECTION_HEADER
));
208 fmap
->u
.pe
.sect
[i
].mapped
= IMAGE_NO_MAP
;
210 if (nthdr
->FileHeader
.PointerToSymbolTable
&& nthdr
->FileHeader
.NumberOfSymbols
)
212 /* FIXME ugly: should rather map the relevant content instead of copying it */
213 const char* src
= (const char*)mapping
+
214 nthdr
->FileHeader
.PointerToSymbolTable
+
215 nthdr
->FileHeader
.NumberOfSymbols
* sizeof(IMAGE_SYMBOL
);
217 DWORD sz
= *(DWORD
*)src
;
219 if ((dst
= HeapAlloc(GetProcessHeap(), 0, sz
)))
220 memcpy(dst
, src
, sz
);
221 fmap
->u
.pe
.strtable
= dst
;
223 else fmap
->u
.pe
.strtable
= NULL
;
226 default: assert(0); goto error
;
233 CloseHandle(fmap
->u
.pe
.hMap
);
237 /******************************************************************
240 * Unmaps an PE file from memory (previously mapped with pe_map_file)
242 static void pe_unmap_file(struct image_file_map
* fmap
)
244 if (fmap
->u
.pe
.hMap
!= 0)
246 struct image_section_map ism
;
248 for (ism
.sidx
= 0; ism
.sidx
< fmap
->u
.pe
.ntheader
.FileHeader
.NumberOfSections
; ism
.sidx
++)
250 pe_unmap_section(&ism
);
252 while (fmap
->u
.pe
.full_count
) pe_unmap_full(fmap
);
253 HeapFree(GetProcessHeap(), 0, fmap
->u
.pe
.sect
);
254 HeapFree(GetProcessHeap(), 0, (void*)fmap
->u
.pe
.strtable
); /* FIXME ugly (see pe_map_file) */
255 CloseHandle(fmap
->u
.pe
.hMap
);
256 fmap
->u
.pe
.hMap
= NULL
;
260 static void pe_module_remove(struct process
* pcs
, struct module_format
* modfmt
)
262 pe_unmap_file(&modfmt
->u
.pe_info
->fmap
);
263 HeapFree(GetProcessHeap(), 0, modfmt
);
266 /******************************************************************
267 * pe_locate_with_coff_symbol_table
269 * Use the COFF symbol table (if any) from the IMAGE_FILE_HEADER to set the absolute address
271 * Mingw32 requires this for stabs debug information as address for global variables isn't filled in
272 * (this is similar to what is done in elf_module.c when using the .symtab ELF section)
274 static BOOL
pe_locate_with_coff_symbol_table(struct module
* module
)
276 struct image_file_map
* fmap
= &module
->format_info
[DFI_PE
]->u
.pe_info
->fmap
;
277 const IMAGE_SYMBOL
* isym
;
281 struct hash_table_iter hti
;
283 struct symt_data
* sym
;
286 numsym
= fmap
->u
.pe
.ntheader
.FileHeader
.NumberOfSymbols
;
287 if (!fmap
->u
.pe
.ntheader
.FileHeader
.PointerToSymbolTable
|| !numsym
)
289 if (!(mapping
= pe_map_full(fmap
, NULL
))) return FALSE
;
290 isym
= (const IMAGE_SYMBOL
*)(mapping
+ fmap
->u
.pe
.ntheader
.FileHeader
.PointerToSymbolTable
);
292 for (i
= 0; i
< numsym
; i
+= naux
, isym
+= naux
)
294 if (isym
->StorageClass
== IMAGE_SYM_CLASS_EXTERNAL
&&
295 isym
->SectionNumber
> 0 && isym
->SectionNumber
<= fmap
->u
.pe
.ntheader
.FileHeader
.NumberOfSections
)
297 if (isym
->N
.Name
.Short
)
299 name
= memcpy(tmp
, isym
->N
.ShortName
, 8);
302 else name
= fmap
->u
.pe
.strtable
+ isym
->N
.Name
.Long
;
303 if (name
[0] == '_') name
++;
304 hash_table_iter_init(&module
->ht_symbols
, &hti
, name
);
305 while ((ptr
= hash_table_iter_up(&hti
)))
307 sym
= GET_ENTRY(ptr
, struct symt_data
, hash_elt
);
308 if (sym
->symt
.tag
== SymTagData
&&
309 (sym
->kind
== DataIsGlobal
|| sym
->kind
== DataIsFileStatic
) &&
310 !strcmp(sym
->hash_elt
.name
, name
))
312 TRACE("Changing absolute address for %d.%s: %lx -> %s\n",
313 isym
->SectionNumber
, name
, sym
->u
.var
.offset
,
314 wine_dbgstr_longlong(module
->module
.BaseOfImage
+
315 fmap
->u
.pe
.sect
[isym
->SectionNumber
- 1].shdr
.VirtualAddress
+
317 sym
->u
.var
.offset
= module
->module
.BaseOfImage
+
318 fmap
->u
.pe
.sect
[isym
->SectionNumber
- 1].shdr
.VirtualAddress
+ isym
->Value
;
323 naux
= isym
->NumberOfAuxSymbols
+ 1;
329 /******************************************************************
330 * pe_load_coff_symbol_table
332 * Load public symbols out of the COFF symbol table (if any).
334 static BOOL
pe_load_coff_symbol_table(struct module
* module
)
336 struct image_file_map
* fmap
= &module
->format_info
[DFI_PE
]->u
.pe_info
->fmap
;
337 const IMAGE_SYMBOL
* isym
;
339 const char* strtable
;
342 const char* lastfilename
= NULL
;
343 struct symt_compiland
* compiland
= NULL
;
344 const IMAGE_SECTION_HEADER
* sect
;
347 numsym
= fmap
->u
.pe
.ntheader
.FileHeader
.NumberOfSymbols
;
348 if (!fmap
->u
.pe
.ntheader
.FileHeader
.PointerToSymbolTable
|| !numsym
)
350 if (!(mapping
= pe_map_full(fmap
, NULL
))) return FALSE
;
351 isym
= (const IMAGE_SYMBOL
*)((char*)mapping
+ fmap
->u
.pe
.ntheader
.FileHeader
.PointerToSymbolTable
);
352 /* FIXME: no way to get strtable size */
353 strtable
= (const char*)&isym
[numsym
];
354 sect
= IMAGE_FIRST_SECTION(&fmap
->u
.pe
.ntheader
);
356 for (i
= 0; i
< numsym
; i
+= naux
, isym
+= naux
)
358 if (isym
->StorageClass
== IMAGE_SYM_CLASS_FILE
)
360 lastfilename
= (const char*)(isym
+ 1);
363 if (isym
->StorageClass
== IMAGE_SYM_CLASS_EXTERNAL
&&
364 isym
->SectionNumber
> 0 && isym
->SectionNumber
<= fmap
->u
.pe
.ntheader
.FileHeader
.NumberOfSections
)
366 if (isym
->N
.Name
.Short
)
368 name
= memcpy(tmp
, isym
->N
.ShortName
, 8);
371 else name
= strtable
+ isym
->N
.Name
.Long
;
372 if (name
[0] == '_') name
++;
374 if (!compiland
&& lastfilename
)
375 compiland
= symt_new_compiland(module
, 0,
376 source_new(module
, NULL
, lastfilename
));
378 if (!(dbghelp_options
& SYMOPT_NO_PUBLICS
))
379 symt_new_public(module
, compiland
, name
,
380 module
->module
.BaseOfImage
+ sect
[isym
->SectionNumber
- 1].VirtualAddress
+
384 naux
= isym
->NumberOfAuxSymbols
+ 1;
386 module
->module
.SymType
= SymCoff
;
387 module
->module
.LineNumbers
= FALSE
;
388 module
->module
.GlobalSymbols
= FALSE
;
389 module
->module
.TypeInfo
= FALSE
;
390 module
->module
.SourceIndexed
= FALSE
;
391 module
->module
.Publics
= TRUE
;
397 static inline void* pe_get_sect(IMAGE_NT_HEADERS
* nth
, void* mapping
,
398 IMAGE_SECTION_HEADER
* sect
)
400 return (sect
) ? RtlImageRvaToVa(nth
, mapping
, sect
->VirtualAddress
, NULL
) : NULL
;
403 static inline DWORD
pe_get_sect_size(IMAGE_SECTION_HEADER
* sect
)
405 return (sect
) ? sect
->SizeOfRawData
: 0;
408 /******************************************************************
411 * look for stabs information in PE header (it's how the mingw compiler provides
412 * its debugging information)
414 static BOOL
pe_load_stabs(const struct process
* pcs
, struct module
* module
)
416 struct image_file_map
* fmap
= &module
->format_info
[DFI_PE
]->u
.pe_info
->fmap
;
417 struct image_section_map sect_stabs
, sect_stabstr
;
420 if (pe_find_section(fmap
, ".stab", §_stabs
) && pe_find_section(fmap
, ".stabstr", §_stabstr
))
425 stab
= image_map_section(§_stabs
);
426 stabstr
= image_map_section(§_stabstr
);
427 if (stab
!= IMAGE_NO_MAP
&& stabstr
!= IMAGE_NO_MAP
)
429 ret
= stabs_parse(module
,
430 module
->module
.BaseOfImage
- fmap
->u
.pe
.ntheader
.OptionalHeader
.ImageBase
,
431 stab
, image_get_map_size(§_stabs
),
432 stabstr
, image_get_map_size(§_stabstr
),
435 image_unmap_section(§_stabs
);
436 image_unmap_section(§_stabstr
);
437 if (ret
) pe_locate_with_coff_symbol_table(module
);
439 TRACE("%s the STABS debug info\n", ret
? "successfully loaded" : "failed to load");
444 /******************************************************************
447 * look for dwarf information in PE header (it's also a way for the mingw compiler
448 * to provide its debugging information)
450 static BOOL
pe_load_dwarf(struct module
* module
)
452 struct image_file_map
* fmap
= &module
->format_info
[DFI_PE
]->u
.pe_info
->fmap
;
455 ret
= dwarf2_parse(module
,
456 module
->module
.BaseOfImage
- fmap
->u
.pe
.ntheader
.OptionalHeader
.ImageBase
,
457 NULL
, /* FIXME: some thunks to deal with ? */
459 TRACE("%s the DWARF debug info\n", ret
? "successfully loaded" : "failed to load");
464 /******************************************************************
469 static BOOL
pe_load_dbg_file(const struct process
* pcs
, struct module
* module
,
470 const char* dbg_name
, DWORD timestamp
)
473 HANDLE hFile
= INVALID_HANDLE_VALUE
, hMap
= 0;
474 const BYTE
* dbg_mapping
= NULL
;
477 TRACE("Processing DBG file %s\n", debugstr_a(dbg_name
));
479 if (path_find_symbol_file(pcs
, dbg_name
, NULL
, timestamp
, 0, tmp
, &module
->module
.DbgUnmatched
) &&
480 (hFile
= CreateFileA(tmp
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
481 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
)) != INVALID_HANDLE_VALUE
&&
482 ((hMap
= CreateFileMappingW(hFile
, NULL
, PAGE_READONLY
, 0, 0, NULL
)) != 0) &&
483 ((dbg_mapping
= MapViewOfFile(hMap
, FILE_MAP_READ
, 0, 0, 0)) != NULL
))
485 const IMAGE_SEPARATE_DEBUG_HEADER
* hdr
;
486 const IMAGE_SECTION_HEADER
* sectp
;
487 const IMAGE_DEBUG_DIRECTORY
* dbg
;
489 hdr
= (const IMAGE_SEPARATE_DEBUG_HEADER
*)dbg_mapping
;
490 /* section headers come immediately after debug header */
491 sectp
= (const IMAGE_SECTION_HEADER
*)(hdr
+ 1);
492 /* and after that and the exported names comes the debug directory */
493 dbg
= (const IMAGE_DEBUG_DIRECTORY
*)
494 (dbg_mapping
+ sizeof(*hdr
) +
495 hdr
->NumberOfSections
* sizeof(IMAGE_SECTION_HEADER
) +
496 hdr
->ExportedNamesSize
);
498 ret
= pe_load_debug_directory(pcs
, module
, dbg_mapping
, sectp
,
499 hdr
->NumberOfSections
, dbg
,
500 hdr
->DebugDirectorySize
/ sizeof(*dbg
));
503 ERR("Couldn't find .DBG file %s (%s)\n", debugstr_a(dbg_name
), debugstr_a(tmp
));
505 if (dbg_mapping
) UnmapViewOfFile(dbg_mapping
);
506 if (hMap
) CloseHandle(hMap
);
507 if (hFile
!= INVALID_HANDLE_VALUE
) CloseHandle(hFile
);
511 /******************************************************************
512 * pe_load_msc_debug_info
514 * Process MSC debug information in PE file.
516 static BOOL
pe_load_msc_debug_info(const struct process
* pcs
, struct module
* module
)
518 struct image_file_map
* fmap
= &module
->format_info
[DFI_PE
]->u
.pe_info
->fmap
;
520 const IMAGE_DATA_DIRECTORY
* dir
;
521 const IMAGE_DEBUG_DIRECTORY
*dbg
= NULL
;
524 IMAGE_NT_HEADERS
* nth
;
526 if (!(mapping
= pe_map_full(fmap
, &nth
))) return FALSE
;
527 /* Read in debug directory */
528 dir
= nth
->OptionalHeader
.DataDirectory
+ IMAGE_DIRECTORY_ENTRY_DEBUG
;
529 nDbg
= dir
->Size
/ sizeof(IMAGE_DEBUG_DIRECTORY
);
530 if (!nDbg
) goto done
;
532 dbg
= RtlImageRvaToVa(nth
, mapping
, dir
->VirtualAddress
, NULL
);
534 /* Parse debug directory */
535 if (nth
->FileHeader
.Characteristics
& IMAGE_FILE_DEBUG_STRIPPED
)
537 /* Debug info is stripped to .DBG file */
538 const IMAGE_DEBUG_MISC
* misc
= (const IMAGE_DEBUG_MISC
*)
539 ((const char*)mapping
+ dbg
->PointerToRawData
);
541 if (nDbg
!= 1 || dbg
->Type
!= IMAGE_DEBUG_TYPE_MISC
||
542 misc
->DataType
!= IMAGE_DEBUG_MISC_EXENAME
)
544 WINE_ERR("-Debug info stripped, but no .DBG file in module %s\n",
545 debugstr_w(module
->module
.ModuleName
));
549 ret
= pe_load_dbg_file(pcs
, module
, (const char*)misc
->Data
, nth
->FileHeader
.TimeDateStamp
);
554 const IMAGE_SECTION_HEADER
*sectp
= (const IMAGE_SECTION_HEADER
*)((const char*)&nth
->OptionalHeader
+ nth
->FileHeader
.SizeOfOptionalHeader
);
555 /* Debug info is embedded into PE module */
556 ret
= pe_load_debug_directory(pcs
, module
, mapping
, sectp
,
557 nth
->FileHeader
.NumberOfSections
, dbg
, nDbg
);
564 /***********************************************************************
565 * pe_load_export_debug_info
567 static BOOL
pe_load_export_debug_info(const struct process
* pcs
, struct module
* module
)
569 struct image_file_map
* fmap
= &module
->format_info
[DFI_PE
]->u
.pe_info
->fmap
;
571 const IMAGE_EXPORT_DIRECTORY
* exports
;
572 DWORD base
= module
->module
.BaseOfImage
;
574 IMAGE_NT_HEADERS
* nth
;
577 if (dbghelp_options
& SYMOPT_NO_PUBLICS
) return TRUE
;
579 if (!(mapping
= pe_map_full(fmap
, &nth
))) return FALSE
;
581 /* Add start of DLL (better use the (yet unimplemented) Exe SymTag for this) */
582 /* FIXME: module.ModuleName isn't correctly set yet if it's passed in SymLoadModule */
583 symt_new_public(module
, NULL
, module
->module
.ModuleName
, base
, 1);
586 /* Add entry point */
587 symt_new_public(module
, NULL
, "EntryPoint",
588 base
+ nth
->OptionalHeader
.AddressOfEntryPoint
, 1);
590 /* FIXME: we'd better store addresses linked to sections rather than
592 IMAGE_SECTION_HEADER
* section
;
593 /* Add start of sections */
594 section
= (IMAGE_SECTION_HEADER
*)
595 ((char*)&nth
->OptionalHeader
+ nth
->FileHeader
.SizeOfOptionalHeader
);
596 for (i
= 0; i
< nth
->FileHeader
.NumberOfSections
; i
++, section
++)
598 symt_new_public(module
, NULL
, section
->Name
,
599 RtlImageRvaToVa(nth
, mapping
, section
->VirtualAddress
, NULL
), 1);
603 /* Add exported functions */
604 if ((exports
= RtlImageDirectoryEntryToData(mapping
, FALSE
,
605 IMAGE_DIRECTORY_ENTRY_EXPORT
, &size
)))
607 const WORD
* ordinals
= NULL
;
608 const DWORD_PTR
* functions
= NULL
;
609 const DWORD
* names
= NULL
;
613 functions
= RtlImageRvaToVa(nth
, mapping
, exports
->AddressOfFunctions
, NULL
);
614 ordinals
= RtlImageRvaToVa(nth
, mapping
, exports
->AddressOfNameOrdinals
, NULL
);
615 names
= RtlImageRvaToVa(nth
, mapping
, exports
->AddressOfNames
, NULL
);
617 if (functions
&& ordinals
&& names
)
619 for (i
= 0; i
< exports
->NumberOfNames
; i
++)
621 if (!names
[i
]) continue;
622 symt_new_public(module
, NULL
,
623 RtlImageRvaToVa(nth
, mapping
, names
[i
], NULL
),
624 base
+ functions
[ordinals
[i
]], 1);
627 for (i
= 0; i
< exports
->NumberOfFunctions
; i
++)
629 if (!functions
[i
]) continue;
630 /* Check if we already added it with a name */
631 for (j
= 0; j
< exports
->NumberOfNames
; j
++)
632 if ((ordinals
[j
] == i
) && names
[j
]) break;
633 if (j
< exports
->NumberOfNames
) continue;
634 snprintf(buffer
, sizeof(buffer
), "%d", i
+ exports
->Base
);
635 symt_new_public(module
, NULL
, buffer
, base
+ (DWORD
)functions
[i
], 1);
639 /* no real debug info, only entry points */
640 if (module
->module
.SymType
== SymDeferred
)
641 module
->module
.SymType
= SymExport
;
647 /******************************************************************
651 BOOL
pe_load_debug_info(const struct process
* pcs
, struct module
* module
)
655 if (!(dbghelp_options
& SYMOPT_PUBLICS_ONLY
))
657 ret
= pe_load_stabs(pcs
, module
);
658 ret
= pe_load_dwarf(module
) || ret
;
659 ret
= pe_load_msc_debug_info(pcs
, module
) || ret
;
660 ret
= ret
|| pe_load_coff_symbol_table(module
); /* FIXME */
661 /* if we still have no debug info (we could only get SymExport at this
662 * point), then do the SymExport except if we have an ELF container,
663 * in which case we'll rely on the export's on the ELF side
666 /* FIXME shouldn't we check that? if (!module_get_debug(pcs, module)) */
667 if (pe_load_export_debug_info(pcs
, module
) && !ret
)
673 /******************************************************************
674 * pe_load_native_module
677 struct module
* pe_load_native_module(struct process
* pcs
, const WCHAR
* name
,
678 HANDLE hFile
, DWORD base
, DWORD size
)
680 struct module
* module
= NULL
;
682 struct module_format
* modfmt
;
683 WCHAR loaded_name
[MAX_PATH
];
685 loaded_name
[0] = '\0';
690 if ((hFile
= FindExecutableImageExW(name
, pcs
->search_path
, loaded_name
, NULL
, NULL
)) == NULL
)
694 else if (name
) strcpyW(loaded_name
, name
);
695 else if (dbghelp_options
& SYMOPT_DEFERRED_LOADS
)
696 FIXME("Trouble ahead (no module name passed in deferred mode)\n");
697 if (!(modfmt
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct module_format
) + sizeof(struct pe_module_info
))))
699 modfmt
->u
.pe_info
= (struct pe_module_info
*)(modfmt
+ 1);
700 if (pe_map_file(hFile
, &modfmt
->u
.pe_info
->fmap
, DMT_PE
))
702 if (!base
) base
= modfmt
->u
.pe_info
->fmap
.u
.pe
.ntheader
.OptionalHeader
.ImageBase
;
703 if (!size
) size
= modfmt
->u
.pe_info
->fmap
.u
.pe
.ntheader
.OptionalHeader
.SizeOfImage
;
705 module
= module_new(pcs
, loaded_name
, DMT_PE
, FALSE
, base
, size
,
706 modfmt
->u
.pe_info
->fmap
.u
.pe
.ntheader
.FileHeader
.TimeDateStamp
,
707 modfmt
->u
.pe_info
->fmap
.u
.pe
.ntheader
.OptionalHeader
.CheckSum
);
710 modfmt
->module
= module
;
711 modfmt
->remove
= pe_module_remove
;
712 modfmt
->loc_compute
= NULL
;
714 module
->format_info
[DFI_PE
] = modfmt
;
715 if (dbghelp_options
& SYMOPT_DEFERRED_LOADS
)
716 module
->module
.SymType
= SymDeferred
;
718 pe_load_debug_info(pcs
, module
);
722 ERR("could not load the module '%s'\n", debugstr_w(loaded_name
));
723 pe_unmap_file(&modfmt
->u
.pe_info
->fmap
);
726 if (!module
) HeapFree(GetProcessHeap(), 0, modfmt
);
728 if (opened
) CloseHandle(hFile
);
733 /******************************************************************
737 BOOL
pe_load_nt_header(HANDLE hProc
, DWORD64 base
, IMAGE_NT_HEADERS
* nth
)
739 IMAGE_DOS_HEADER dos
;
741 return ReadProcessMemory(hProc
, (char*)(DWORD_PTR
)base
, &dos
, sizeof(dos
), NULL
) &&
742 dos
.e_magic
== IMAGE_DOS_SIGNATURE
&&
743 ReadProcessMemory(hProc
, (char*)(DWORD_PTR
)(base
+ dos
.e_lfanew
),
744 nth
, sizeof(*nth
), NULL
) &&
745 nth
->Signature
== IMAGE_NT_SIGNATURE
;
748 /******************************************************************
749 * pe_load_builtin_module
752 struct module
* pe_load_builtin_module(struct process
* pcs
, const WCHAR
* name
,
753 DWORD64 base
, DWORD64 size
)
755 struct module
* module
= NULL
;
757 if (base
&& pcs
->dbg_hdr_addr
)
759 IMAGE_NT_HEADERS nth
;
761 if (pe_load_nt_header(pcs
->handle
, base
, &nth
))
763 if (!size
) size
= nth
.OptionalHeader
.SizeOfImage
;
764 module
= module_new(pcs
, name
, DMT_PE
, FALSE
, base
, size
,
765 nth
.FileHeader
.TimeDateStamp
,
766 nth
.OptionalHeader
.CheckSum
);
772 /***********************************************************************
773 * ImageDirectoryEntryToDataEx (DBGHELP.@)
775 * Search for specified directory in PE image
779 * base [in] Image base address
780 * image [in] TRUE - image has been loaded by loader, FALSE - raw file image
781 * dir [in] Target directory index
782 * size [out] Receives directory size
783 * section [out] Receives pointer to section header of section containing directory data
786 * Success: pointer to directory data
790 PVOID WINAPI
ImageDirectoryEntryToDataEx( PVOID base
, BOOLEAN image
, USHORT dir
, PULONG size
, PIMAGE_SECTION_HEADER
*section
)
792 const IMAGE_NT_HEADERS
*nt
;
796 if (section
) *section
= NULL
;
798 if (!(nt
= RtlImageNtHeader( base
))) return NULL
;
799 if (dir
>= nt
->OptionalHeader
.NumberOfRvaAndSizes
) return NULL
;
800 if (!(addr
= nt
->OptionalHeader
.DataDirectory
[dir
].VirtualAddress
)) return NULL
;
802 *size
= nt
->OptionalHeader
.DataDirectory
[dir
].Size
;
803 if (image
|| addr
< nt
->OptionalHeader
.SizeOfHeaders
) return (char *)base
+ addr
;
805 return RtlImageRvaToVa( nt
, base
, addr
, section
);
808 /***********************************************************************
809 * ImageDirectoryEntryToData (DBGHELP.@)
812 * See ImageDirectoryEntryToDataEx
814 PVOID WINAPI
ImageDirectoryEntryToData( PVOID base
, BOOLEAN image
, USHORT dir
, PULONG size
)
816 return ImageDirectoryEntryToDataEx( base
, image
, dir
, size
, NULL
);