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
;
83 if (fmap
->sect
[ism
->sidx
].shdr
.Misc
.VirtualSize
> fmap
->sect
[ism
->sidx
].shdr
.SizeOfRawData
)
85 FIXME("Section %ld: virtual (0x%x) > raw (0x%x) size - not supported\n",
86 ism
->sidx
, fmap
->sect
[ism
->sidx
].shdr
.Misc
.VirtualSize
,
87 fmap
->sect
[ism
->sidx
].shdr
.SizeOfRawData
);
90 /* FIXME: that's rather drastic, but that will do for now
91 * that's ok if the full file map exists, but we could be less aggressive otherwise and
92 * only map the relevant section
94 if ((mapping
= pe_map_full(ism
->fmap
, &nth
)))
96 fmap
->sect
[ism
->sidx
].mapped
= RtlImageRvaToVa(nth
, mapping
,
97 fmap
->sect
[ism
->sidx
].shdr
.VirtualAddress
,
99 return fmap
->sect
[ism
->sidx
].mapped
;
105 /******************************************************************
108 * Finds a section by name (and type) into memory from an PE file
109 * or its alternate if any
111 BOOL
pe_find_section(struct image_file_map
* fmap
, const char* name
,
112 struct image_section_map
* ism
)
114 const char* sectname
;
116 char tmp
[IMAGE_SIZEOF_SHORT_NAME
+ 1];
118 for (i
= 0; i
< fmap
->u
.pe
.ntheader
.FileHeader
.NumberOfSections
; i
++)
120 sectname
= (const char*)fmap
->u
.pe
.sect
[i
].shdr
.Name
;
121 /* long section names start with a '/' (at least on MinGW32) */
122 if (sectname
[0] == '/' && fmap
->u
.pe
.strtable
)
123 sectname
= fmap
->u
.pe
.strtable
+ atoi(sectname
+ 1);
126 /* the section name may not be null terminated */
127 sectname
= memcpy(tmp
, sectname
, IMAGE_SIZEOF_SHORT_NAME
);
128 tmp
[IMAGE_SIZEOF_SHORT_NAME
] = '\0';
130 if (!strcasecmp(sectname
, name
))
143 /******************************************************************
146 * Unmaps a single section from memory
148 void pe_unmap_section(struct image_section_map
* ism
)
150 if (ism
->sidx
>= 0 && ism
->sidx
< ism
->fmap
->u
.pe
.ntheader
.FileHeader
.NumberOfSections
&&
151 ism
->fmap
->u
.pe
.sect
[ism
->sidx
].mapped
!= IMAGE_NO_MAP
)
153 pe_unmap_full(ism
->fmap
);
154 ism
->fmap
->u
.pe
.sect
[ism
->sidx
].mapped
= IMAGE_NO_MAP
;
158 /******************************************************************
161 * Get the RVA of an PE section
163 DWORD_PTR
pe_get_map_rva(const struct image_section_map
* ism
)
165 if (ism
->sidx
< 0 || ism
->sidx
>= ism
->fmap
->u
.pe
.ntheader
.FileHeader
.NumberOfSections
)
167 return ism
->fmap
->u
.pe
.sect
[ism
->sidx
].shdr
.VirtualAddress
;
170 /******************************************************************
173 * Get the size of a PE section
175 unsigned pe_get_map_size(const struct image_section_map
* ism
)
177 if (ism
->sidx
< 0 || ism
->sidx
>= ism
->fmap
->u
.pe
.ntheader
.FileHeader
.NumberOfSections
)
179 return ism
->fmap
->u
.pe
.sect
[ism
->sidx
].shdr
.Misc
.VirtualSize
;
182 /******************************************************************
183 * pe_is_valid_pointer_table
185 * Checks whether the PointerToSymbolTable and NumberOfSymbols in file_header contain
188 static BOOL
pe_is_valid_pointer_table(const IMAGE_NT_HEADERS
* nthdr
, const void* mapping
, DWORD64 sz
)
192 /* is the iSym table inside file size ? (including first DWORD of string table, which is its size) */
193 offset
= (DWORD64
)nthdr
->FileHeader
.PointerToSymbolTable
;
194 offset
+= (DWORD64
)nthdr
->FileHeader
.NumberOfSymbols
* sizeof(IMAGE_SYMBOL
);
195 if (offset
+ sizeof(DWORD
) > sz
) return FALSE
;
196 /* is string table (following iSym table) inside file size ? */
197 offset
+= *(DWORD
*)((const char*)mapping
+ offset
);
201 /******************************************************************
204 * Maps an PE file into memory (and checks it's a real PE file)
206 static BOOL
pe_map_file(HANDLE file
, struct image_file_map
* fmap
, enum module_type mt
)
211 fmap
->u
.pe
.hMap
= CreateFileMappingW(file
, NULL
, PAGE_READONLY
, 0, 0, NULL
);
212 if (fmap
->u
.pe
.hMap
== 0) return FALSE
;
213 fmap
->u
.pe
.full_count
= 0;
214 fmap
->u
.pe
.full_map
= NULL
;
215 if (!(mapping
= pe_map_full(fmap
, NULL
))) goto error
;
221 IMAGE_NT_HEADERS
* nthdr
;
222 IMAGE_SECTION_HEADER
* section
;
225 if (!(nthdr
= RtlImageNtHeader(mapping
))) goto error
;
226 memcpy(&fmap
->u
.pe
.ntheader
, nthdr
, sizeof(fmap
->u
.pe
.ntheader
));
227 switch (nthdr
->OptionalHeader
.Magic
)
229 case 0x10b: fmap
->addr_size
= 32; break;
230 case 0x20b: fmap
->addr_size
= 64; break;
231 default: return FALSE
;
233 section
= (IMAGE_SECTION_HEADER
*)
234 ((char*)&nthdr
->OptionalHeader
+ nthdr
->FileHeader
.SizeOfOptionalHeader
);
235 fmap
->u
.pe
.sect
= HeapAlloc(GetProcessHeap(), 0,
236 nthdr
->FileHeader
.NumberOfSections
* sizeof(fmap
->u
.pe
.sect
[0]));
237 if (!fmap
->u
.pe
.sect
) goto error
;
238 for (i
= 0; i
< nthdr
->FileHeader
.NumberOfSections
; i
++)
240 memcpy(&fmap
->u
.pe
.sect
[i
].shdr
, section
+ i
, sizeof(IMAGE_SECTION_HEADER
));
241 fmap
->u
.pe
.sect
[i
].mapped
= IMAGE_NO_MAP
;
243 if (nthdr
->FileHeader
.PointerToSymbolTable
&& nthdr
->FileHeader
.NumberOfSymbols
)
247 if (GetFileSizeEx(file
, &li
) && pe_is_valid_pointer_table(nthdr
, mapping
, li
.QuadPart
))
249 /* FIXME ugly: should rather map the relevant content instead of copying it */
250 const char* src
= (const char*)mapping
+
251 nthdr
->FileHeader
.PointerToSymbolTable
+
252 nthdr
->FileHeader
.NumberOfSymbols
* sizeof(IMAGE_SYMBOL
);
254 DWORD sz
= *(DWORD
*)src
;
256 if ((dst
= HeapAlloc(GetProcessHeap(), 0, sz
)))
257 memcpy(dst
, src
, sz
);
258 fmap
->u
.pe
.strtable
= dst
;
262 WARN("Bad coff table... wipping out\n");
263 /* we have bad information here, wipe it out */
264 fmap
->u
.pe
.ntheader
.FileHeader
.PointerToSymbolTable
= 0;
265 fmap
->u
.pe
.ntheader
.FileHeader
.NumberOfSymbols
= 0;
266 fmap
->u
.pe
.strtable
= NULL
;
269 else fmap
->u
.pe
.strtable
= NULL
;
272 default: assert(0); goto error
;
279 CloseHandle(fmap
->u
.pe
.hMap
);
283 /******************************************************************
286 * Unmaps an PE file from memory (previously mapped with pe_map_file)
288 static void pe_unmap_file(struct image_file_map
* fmap
)
290 if (fmap
->u
.pe
.hMap
!= 0)
292 struct image_section_map ism
;
294 for (ism
.sidx
= 0; ism
.sidx
< fmap
->u
.pe
.ntheader
.FileHeader
.NumberOfSections
; ism
.sidx
++)
296 pe_unmap_section(&ism
);
298 while (fmap
->u
.pe
.full_count
) pe_unmap_full(fmap
);
299 HeapFree(GetProcessHeap(), 0, fmap
->u
.pe
.sect
);
300 HeapFree(GetProcessHeap(), 0, (void*)fmap
->u
.pe
.strtable
); /* FIXME ugly (see pe_map_file) */
301 CloseHandle(fmap
->u
.pe
.hMap
);
302 fmap
->u
.pe
.hMap
= NULL
;
306 /******************************************************************
309 * Maps a directory content out of a PE file
311 const char* pe_map_directory(struct module
* module
, int dirno
, DWORD
* size
)
313 IMAGE_NT_HEADERS
* nth
;
316 if (module
->type
!= DMT_PE
|| !module
->format_info
[DFI_PE
]) return NULL
;
317 if (dirno
>= IMAGE_NUMBEROF_DIRECTORY_ENTRIES
||
318 !(mapping
= pe_map_full(&module
->format_info
[DFI_PE
]->u
.pe_info
->fmap
, &nth
)))
320 if (size
) *size
= nth
->OptionalHeader
.DataDirectory
[dirno
].Size
;
321 return RtlImageRvaToVa(nth
, mapping
,
322 nth
->OptionalHeader
.DataDirectory
[dirno
].VirtualAddress
, NULL
);
325 static void pe_module_remove(struct process
* pcs
, struct module_format
* modfmt
)
327 pe_unmap_file(&modfmt
->u
.pe_info
->fmap
);
328 HeapFree(GetProcessHeap(), 0, modfmt
);
331 /******************************************************************
332 * pe_locate_with_coff_symbol_table
334 * Use the COFF symbol table (if any) from the IMAGE_FILE_HEADER to set the absolute address
336 * Mingw32 requires this for stabs debug information as address for global variables isn't filled in
337 * (this is similar to what is done in elf_module.c when using the .symtab ELF section)
339 static BOOL
pe_locate_with_coff_symbol_table(struct module
* module
)
341 struct image_file_map
* fmap
= &module
->format_info
[DFI_PE
]->u
.pe_info
->fmap
;
342 const IMAGE_SYMBOL
* isym
;
346 struct hash_table_iter hti
;
348 struct symt_data
* sym
;
351 numsym
= fmap
->u
.pe
.ntheader
.FileHeader
.NumberOfSymbols
;
352 if (!fmap
->u
.pe
.ntheader
.FileHeader
.PointerToSymbolTable
|| !numsym
)
354 if (!(mapping
= pe_map_full(fmap
, NULL
))) return FALSE
;
355 isym
= (const IMAGE_SYMBOL
*)(mapping
+ fmap
->u
.pe
.ntheader
.FileHeader
.PointerToSymbolTable
);
357 for (i
= 0; i
< numsym
; i
+= naux
, isym
+= naux
)
359 if (isym
->StorageClass
== IMAGE_SYM_CLASS_EXTERNAL
&&
360 isym
->SectionNumber
> 0 && isym
->SectionNumber
<= fmap
->u
.pe
.ntheader
.FileHeader
.NumberOfSections
)
362 if (isym
->N
.Name
.Short
)
364 name
= memcpy(tmp
, isym
->N
.ShortName
, 8);
367 else name
= fmap
->u
.pe
.strtable
+ isym
->N
.Name
.Long
;
368 if (name
[0] == '_') name
++;
369 hash_table_iter_init(&module
->ht_symbols
, &hti
, name
);
370 while ((ptr
= hash_table_iter_up(&hti
)))
372 sym
= CONTAINING_RECORD(ptr
, struct symt_data
, hash_elt
);
373 if (sym
->symt
.tag
== SymTagData
&&
374 (sym
->kind
== DataIsGlobal
|| sym
->kind
== DataIsFileStatic
) &&
375 sym
->u
.var
.kind
== loc_absolute
&&
376 !strcmp(sym
->hash_elt
.name
, name
))
378 TRACE("Changing absolute address for %d.%s: %lx -> %s\n",
379 isym
->SectionNumber
, name
, sym
->u
.var
.offset
,
380 wine_dbgstr_longlong(module
->module
.BaseOfImage
+
381 fmap
->u
.pe
.sect
[isym
->SectionNumber
- 1].shdr
.VirtualAddress
+
383 sym
->u
.var
.offset
= module
->module
.BaseOfImage
+
384 fmap
->u
.pe
.sect
[isym
->SectionNumber
- 1].shdr
.VirtualAddress
+ isym
->Value
;
389 naux
= isym
->NumberOfAuxSymbols
+ 1;
395 /******************************************************************
396 * pe_load_coff_symbol_table
398 * Load public symbols out of the COFF symbol table (if any).
400 static BOOL
pe_load_coff_symbol_table(struct module
* module
)
402 struct image_file_map
* fmap
= &module
->format_info
[DFI_PE
]->u
.pe_info
->fmap
;
403 const IMAGE_SYMBOL
* isym
;
405 const char* strtable
;
408 const char* lastfilename
= NULL
;
409 struct symt_compiland
* compiland
= NULL
;
410 const IMAGE_SECTION_HEADER
* sect
;
413 numsym
= fmap
->u
.pe
.ntheader
.FileHeader
.NumberOfSymbols
;
414 if (!fmap
->u
.pe
.ntheader
.FileHeader
.PointerToSymbolTable
|| !numsym
)
416 if (!(mapping
= pe_map_full(fmap
, NULL
))) return FALSE
;
417 isym
= (const IMAGE_SYMBOL
*)((const char*)mapping
+ fmap
->u
.pe
.ntheader
.FileHeader
.PointerToSymbolTable
);
418 /* FIXME: no way to get strtable size */
419 strtable
= (const char*)&isym
[numsym
];
420 sect
= IMAGE_FIRST_SECTION(RtlImageNtHeader((HMODULE
)mapping
));
422 for (i
= 0; i
< numsym
; i
+= naux
, isym
+= naux
)
424 if (isym
->StorageClass
== IMAGE_SYM_CLASS_FILE
)
426 lastfilename
= (const char*)(isym
+ 1);
429 if (isym
->StorageClass
== IMAGE_SYM_CLASS_EXTERNAL
&&
430 isym
->SectionNumber
> 0 && isym
->SectionNumber
<= fmap
->u
.pe
.ntheader
.FileHeader
.NumberOfSections
)
432 if (isym
->N
.Name
.Short
)
434 name
= memcpy(tmp
, isym
->N
.ShortName
, 8);
437 else name
= strtable
+ isym
->N
.Name
.Long
;
438 if (name
[0] == '_') name
++;
440 if (!compiland
&& lastfilename
)
441 compiland
= symt_new_compiland(module
, 0,
442 source_new(module
, NULL
, lastfilename
));
444 if (!(dbghelp_options
& SYMOPT_NO_PUBLICS
))
445 symt_new_public(module
, compiland
, name
,
446 module
->module
.BaseOfImage
+ sect
[isym
->SectionNumber
- 1].VirtualAddress
+
450 naux
= isym
->NumberOfAuxSymbols
+ 1;
452 module
->module
.SymType
= SymCoff
;
453 module
->module
.LineNumbers
= FALSE
;
454 module
->module
.GlobalSymbols
= FALSE
;
455 module
->module
.TypeInfo
= FALSE
;
456 module
->module
.SourceIndexed
= FALSE
;
457 module
->module
.Publics
= TRUE
;
463 /******************************************************************
466 * look for stabs information in PE header (it's how the mingw compiler provides
467 * its debugging information)
469 static BOOL
pe_load_stabs(const struct process
* pcs
, struct module
* module
)
471 struct image_file_map
* fmap
= &module
->format_info
[DFI_PE
]->u
.pe_info
->fmap
;
472 struct image_section_map sect_stabs
, sect_stabstr
;
475 if (pe_find_section(fmap
, ".stab", §_stabs
) && pe_find_section(fmap
, ".stabstr", §_stabstr
))
480 stab
= image_map_section(§_stabs
);
481 stabstr
= image_map_section(§_stabstr
);
482 if (stab
!= IMAGE_NO_MAP
&& stabstr
!= IMAGE_NO_MAP
)
484 ret
= stabs_parse(module
,
485 module
->module
.BaseOfImage
- fmap
->u
.pe
.ntheader
.OptionalHeader
.ImageBase
,
486 stab
, image_get_map_size(§_stabs
),
487 stabstr
, image_get_map_size(§_stabstr
),
490 image_unmap_section(§_stabs
);
491 image_unmap_section(§_stabstr
);
492 if (ret
) pe_locate_with_coff_symbol_table(module
);
494 TRACE("%s the STABS debug info\n", ret
? "successfully loaded" : "failed to load");
499 /******************************************************************
502 * look for dwarf information in PE header (it's also a way for the mingw compiler
503 * to provide its debugging information)
505 static BOOL
pe_load_dwarf(struct module
* module
)
507 struct image_file_map
* fmap
= &module
->format_info
[DFI_PE
]->u
.pe_info
->fmap
;
510 ret
= dwarf2_parse(module
,
511 module
->module
.BaseOfImage
- fmap
->u
.pe
.ntheader
.OptionalHeader
.ImageBase
,
512 NULL
, /* FIXME: some thunks to deal with ? */
514 TRACE("%s the DWARF debug info\n", ret
? "successfully loaded" : "failed to load");
519 /******************************************************************
524 static BOOL
pe_load_dbg_file(const struct process
* pcs
, struct module
* module
,
525 const char* dbg_name
, DWORD timestamp
)
528 HANDLE hFile
= INVALID_HANDLE_VALUE
, hMap
= 0;
529 const BYTE
* dbg_mapping
= NULL
;
532 TRACE("Processing DBG file %s\n", debugstr_a(dbg_name
));
534 if (path_find_symbol_file(pcs
, dbg_name
, NULL
, timestamp
, 0, tmp
, &module
->module
.DbgUnmatched
) &&
535 (hFile
= CreateFileA(tmp
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
536 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
)) != INVALID_HANDLE_VALUE
&&
537 ((hMap
= CreateFileMappingW(hFile
, NULL
, PAGE_READONLY
, 0, 0, NULL
)) != 0) &&
538 ((dbg_mapping
= MapViewOfFile(hMap
, FILE_MAP_READ
, 0, 0, 0)) != NULL
))
540 const IMAGE_SEPARATE_DEBUG_HEADER
* hdr
;
541 const IMAGE_SECTION_HEADER
* sectp
;
542 const IMAGE_DEBUG_DIRECTORY
* dbg
;
544 hdr
= (const IMAGE_SEPARATE_DEBUG_HEADER
*)dbg_mapping
;
545 /* section headers come immediately after debug header */
546 sectp
= (const IMAGE_SECTION_HEADER
*)(hdr
+ 1);
547 /* and after that and the exported names comes the debug directory */
548 dbg
= (const IMAGE_DEBUG_DIRECTORY
*)
549 (dbg_mapping
+ sizeof(*hdr
) +
550 hdr
->NumberOfSections
* sizeof(IMAGE_SECTION_HEADER
) +
551 hdr
->ExportedNamesSize
);
553 ret
= pe_load_debug_directory(pcs
, module
, dbg_mapping
, sectp
,
554 hdr
->NumberOfSections
, dbg
,
555 hdr
->DebugDirectorySize
/ sizeof(*dbg
));
558 ERR("Couldn't find .DBG file %s (%s)\n", debugstr_a(dbg_name
), debugstr_a(tmp
));
560 if (dbg_mapping
) UnmapViewOfFile(dbg_mapping
);
561 if (hMap
) CloseHandle(hMap
);
562 if (hFile
!= INVALID_HANDLE_VALUE
) CloseHandle(hFile
);
566 /******************************************************************
567 * pe_load_msc_debug_info
569 * Process MSC debug information in PE file.
571 static BOOL
pe_load_msc_debug_info(const struct process
* pcs
, struct module
* module
)
573 struct image_file_map
* fmap
= &module
->format_info
[DFI_PE
]->u
.pe_info
->fmap
;
575 const IMAGE_DATA_DIRECTORY
* dir
;
576 const IMAGE_DEBUG_DIRECTORY
*dbg
= NULL
;
579 IMAGE_NT_HEADERS
* nth
;
581 if (!(mapping
= pe_map_full(fmap
, &nth
))) return FALSE
;
582 /* Read in debug directory */
583 dir
= nth
->OptionalHeader
.DataDirectory
+ IMAGE_DIRECTORY_ENTRY_DEBUG
;
584 nDbg
= dir
->Size
/ sizeof(IMAGE_DEBUG_DIRECTORY
);
585 if (!nDbg
) goto done
;
587 dbg
= RtlImageRvaToVa(nth
, mapping
, dir
->VirtualAddress
, NULL
);
589 /* Parse debug directory */
590 if (nth
->FileHeader
.Characteristics
& IMAGE_FILE_DEBUG_STRIPPED
)
592 /* Debug info is stripped to .DBG file */
593 const IMAGE_DEBUG_MISC
* misc
= (const IMAGE_DEBUG_MISC
*)
594 ((const char*)mapping
+ dbg
->PointerToRawData
);
596 if (nDbg
!= 1 || dbg
->Type
!= IMAGE_DEBUG_TYPE_MISC
||
597 misc
->DataType
!= IMAGE_DEBUG_MISC_EXENAME
)
599 ERR("-Debug info stripped, but no .DBG file in module %s\n",
600 debugstr_w(module
->module
.ModuleName
));
604 ret
= pe_load_dbg_file(pcs
, module
, (const char*)misc
->Data
, nth
->FileHeader
.TimeDateStamp
);
609 const IMAGE_SECTION_HEADER
*sectp
= (const IMAGE_SECTION_HEADER
*)((const char*)&nth
->OptionalHeader
+ nth
->FileHeader
.SizeOfOptionalHeader
);
610 /* Debug info is embedded into PE module */
611 ret
= pe_load_debug_directory(pcs
, module
, mapping
, sectp
,
612 nth
->FileHeader
.NumberOfSections
, dbg
, nDbg
);
619 /***********************************************************************
620 * pe_load_export_debug_info
622 static BOOL
pe_load_export_debug_info(const struct process
* pcs
, struct module
* module
)
624 struct image_file_map
* fmap
= &module
->format_info
[DFI_PE
]->u
.pe_info
->fmap
;
626 const IMAGE_EXPORT_DIRECTORY
* exports
;
627 DWORD base
= module
->module
.BaseOfImage
;
629 IMAGE_NT_HEADERS
* nth
;
632 if (dbghelp_options
& SYMOPT_NO_PUBLICS
) return TRUE
;
634 if (!(mapping
= pe_map_full(fmap
, &nth
))) return FALSE
;
636 /* Add start of DLL (better use the (yet unimplemented) Exe SymTag for this) */
637 /* FIXME: module.ModuleName isn't correctly set yet if it's passed in SymLoadModule */
638 symt_new_public(module
, NULL
, module
->module
.ModuleName
, base
, 1);
641 /* Add entry point */
642 symt_new_public(module
, NULL
, "EntryPoint",
643 base
+ nth
->OptionalHeader
.AddressOfEntryPoint
, 1);
645 /* FIXME: we'd better store addresses linked to sections rather than
647 IMAGE_SECTION_HEADER
* section
;
648 /* Add start of sections */
649 section
= (IMAGE_SECTION_HEADER
*)
650 ((char*)&nth
->OptionalHeader
+ nth
->FileHeader
.SizeOfOptionalHeader
);
651 for (i
= 0; i
< nth
->FileHeader
.NumberOfSections
; i
++, section
++)
653 symt_new_public(module
, NULL
, section
->Name
,
654 RtlImageRvaToVa(nth
, mapping
, section
->VirtualAddress
, NULL
), 1);
658 /* Add exported functions */
659 if ((exports
= RtlImageDirectoryEntryToData(mapping
, FALSE
,
660 IMAGE_DIRECTORY_ENTRY_EXPORT
, &size
)))
662 const WORD
* ordinals
= NULL
;
663 const DWORD_PTR
* functions
= NULL
;
664 const DWORD
* names
= NULL
;
668 functions
= RtlImageRvaToVa(nth
, mapping
, exports
->AddressOfFunctions
, NULL
);
669 ordinals
= RtlImageRvaToVa(nth
, mapping
, exports
->AddressOfNameOrdinals
, NULL
);
670 names
= RtlImageRvaToVa(nth
, mapping
, exports
->AddressOfNames
, NULL
);
672 if (functions
&& ordinals
&& names
)
674 for (i
= 0; i
< exports
->NumberOfNames
; i
++)
676 if (!names
[i
]) continue;
677 symt_new_public(module
, NULL
,
678 RtlImageRvaToVa(nth
, mapping
, names
[i
], NULL
),
679 base
+ functions
[ordinals
[i
]], 1);
682 for (i
= 0; i
< exports
->NumberOfFunctions
; i
++)
684 if (!functions
[i
]) continue;
685 /* Check if we already added it with a name */
686 for (j
= 0; j
< exports
->NumberOfNames
; j
++)
687 if ((ordinals
[j
] == i
) && names
[j
]) break;
688 if (j
< exports
->NumberOfNames
) continue;
689 snprintf(buffer
, sizeof(buffer
), "%d", i
+ exports
->Base
);
690 symt_new_public(module
, NULL
, buffer
, base
+ (DWORD
)functions
[i
], 1);
694 /* no real debug info, only entry points */
695 if (module
->module
.SymType
== SymDeferred
)
696 module
->module
.SymType
= SymExport
;
702 /******************************************************************
706 BOOL
pe_load_debug_info(const struct process
* pcs
, struct module
* module
)
710 if (!(dbghelp_options
& SYMOPT_PUBLICS_ONLY
))
712 ret
= pe_load_stabs(pcs
, module
);
713 ret
= pe_load_dwarf(module
) || ret
;
714 ret
= pe_load_msc_debug_info(pcs
, module
) || ret
;
715 ret
= ret
|| pe_load_coff_symbol_table(module
); /* FIXME */
716 /* if we still have no debug info (we could only get SymExport at this
717 * point), then do the SymExport except if we have an ELF container,
718 * in which case we'll rely on the export's on the ELF side
721 /* FIXME shouldn't we check that? if (!module_get_debug(pcs, module)) */
722 if (pe_load_export_debug_info(pcs
, module
) && !ret
)
728 /******************************************************************
729 * pe_load_native_module
732 struct module
* pe_load_native_module(struct process
* pcs
, const WCHAR
* name
,
733 HANDLE hFile
, DWORD64 base
, DWORD size
)
735 struct module
* module
= NULL
;
737 struct module_format
* modfmt
;
738 WCHAR loaded_name
[MAX_PATH
];
740 loaded_name
[0] = '\0';
745 if ((hFile
= FindExecutableImageExW(name
, pcs
->search_path
, loaded_name
, NULL
, NULL
)) == NULL
)
749 else if (name
) strcpyW(loaded_name
, name
);
750 else if (dbghelp_options
& SYMOPT_DEFERRED_LOADS
)
751 FIXME("Trouble ahead (no module name passed in deferred mode)\n");
752 if (!(modfmt
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct module_format
) + sizeof(struct pe_module_info
))))
754 modfmt
->u
.pe_info
= (struct pe_module_info
*)(modfmt
+ 1);
755 if (pe_map_file(hFile
, &modfmt
->u
.pe_info
->fmap
, DMT_PE
))
757 if (!base
) base
= modfmt
->u
.pe_info
->fmap
.u
.pe
.ntheader
.OptionalHeader
.ImageBase
;
758 if (!size
) size
= modfmt
->u
.pe_info
->fmap
.u
.pe
.ntheader
.OptionalHeader
.SizeOfImage
;
760 module
= module_new(pcs
, loaded_name
, DMT_PE
, FALSE
, base
, size
,
761 modfmt
->u
.pe_info
->fmap
.u
.pe
.ntheader
.FileHeader
.TimeDateStamp
,
762 modfmt
->u
.pe_info
->fmap
.u
.pe
.ntheader
.OptionalHeader
.CheckSum
);
765 modfmt
->module
= module
;
766 modfmt
->remove
= pe_module_remove
;
767 modfmt
->loc_compute
= NULL
;
769 module
->format_info
[DFI_PE
] = modfmt
;
770 if (dbghelp_options
& SYMOPT_DEFERRED_LOADS
)
771 module
->module
.SymType
= SymDeferred
;
773 pe_load_debug_info(pcs
, module
);
774 module
->reloc_delta
= base
- modfmt
->u
.pe_info
->fmap
.u
.pe
.ntheader
.OptionalHeader
.ImageBase
;
778 ERR("could not load the module '%s'\n", debugstr_w(loaded_name
));
779 pe_unmap_file(&modfmt
->u
.pe_info
->fmap
);
782 if (!module
) HeapFree(GetProcessHeap(), 0, modfmt
);
784 if (opened
) CloseHandle(hFile
);
789 /******************************************************************
793 BOOL
pe_load_nt_header(HANDLE hProc
, DWORD64 base
, IMAGE_NT_HEADERS
* nth
)
795 IMAGE_DOS_HEADER dos
;
797 return ReadProcessMemory(hProc
, (char*)(DWORD_PTR
)base
, &dos
, sizeof(dos
), NULL
) &&
798 dos
.e_magic
== IMAGE_DOS_SIGNATURE
&&
799 ReadProcessMemory(hProc
, (char*)(DWORD_PTR
)(base
+ dos
.e_lfanew
),
800 nth
, sizeof(*nth
), NULL
) &&
801 nth
->Signature
== IMAGE_NT_SIGNATURE
;
804 /******************************************************************
805 * pe_load_builtin_module
808 struct module
* pe_load_builtin_module(struct process
* pcs
, const WCHAR
* name
,
809 DWORD64 base
, DWORD64 size
)
811 struct module
* module
= NULL
;
813 if (base
&& pcs
->dbg_hdr_addr
)
815 IMAGE_NT_HEADERS nth
;
817 if (pe_load_nt_header(pcs
->handle
, base
, &nth
))
819 if (!size
) size
= nth
.OptionalHeader
.SizeOfImage
;
820 module
= module_new(pcs
, name
, DMT_PE
, FALSE
, base
, size
,
821 nth
.FileHeader
.TimeDateStamp
,
822 nth
.OptionalHeader
.CheckSum
);
828 /***********************************************************************
829 * ImageDirectoryEntryToDataEx (DBGHELP.@)
831 * Search for specified directory in PE image
835 * base [in] Image base address
836 * image [in] TRUE - image has been loaded by loader, FALSE - raw file image
837 * dir [in] Target directory index
838 * size [out] Receives directory size
839 * section [out] Receives pointer to section header of section containing directory data
842 * Success: pointer to directory data
846 PVOID WINAPI
ImageDirectoryEntryToDataEx( PVOID base
, BOOLEAN image
, USHORT dir
, PULONG size
, PIMAGE_SECTION_HEADER
*section
)
848 const IMAGE_NT_HEADERS
*nt
;
852 if (section
) *section
= NULL
;
854 if (!(nt
= RtlImageNtHeader( base
))) return NULL
;
855 if (dir
>= nt
->OptionalHeader
.NumberOfRvaAndSizes
) return NULL
;
856 if (!(addr
= nt
->OptionalHeader
.DataDirectory
[dir
].VirtualAddress
)) return NULL
;
858 *size
= nt
->OptionalHeader
.DataDirectory
[dir
].Size
;
859 if (image
|| addr
< nt
->OptionalHeader
.SizeOfHeaders
) return (char *)base
+ addr
;
861 return RtlImageRvaToVa( nt
, base
, addr
, section
);
864 /***********************************************************************
865 * ImageDirectoryEntryToData (DBGHELP.@)
868 * See ImageDirectoryEntryToDataEx
870 PVOID WINAPI
ImageDirectoryEntryToData( PVOID base
, BOOLEAN image
, USHORT dir
, PULONG size
)
872 return ImageDirectoryEntryToDataEx( base
, image
, dir
, size
, NULL
);