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
29 #include "dbghelp_private.h"
30 #include "image_private.h"
32 #include "wine/debug.h"
33 #include "wine/heap.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp
);
39 struct image_file_map fmap
;
42 static const char builtin_signature
[] = "Wine builtin DLL";
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 /* as we store either IMAGE_OPTIONAL_HEADER(32|64) inside pe_file_map,
69 * this helper will read to any field 'field' inside such an header
71 #define PE_FROM_OPTHDR(fmap, field) (((fmap)->addr_size == 32) ? ((fmap)->u.pe.opt.header32. field) : ((fmap)->u.pe.opt.header64. field))
73 /******************************************************************
76 * Maps a single section into memory from an PE file
78 static const char* pe_map_section(struct image_section_map
* ism
)
81 struct pe_file_map
* fmap
= &ism
->fmap
->u
.pe
;
83 if (ism
->sidx
>= 0 && ism
->sidx
< fmap
->file_header
.NumberOfSections
&&
84 fmap
->sect
[ism
->sidx
].mapped
== IMAGE_NO_MAP
)
86 IMAGE_NT_HEADERS
* nth
;
88 if (fmap
->sect
[ism
->sidx
].shdr
.Misc
.VirtualSize
> fmap
->sect
[ism
->sidx
].shdr
.SizeOfRawData
)
90 FIXME("Section %Id: virtual (0x%lx) > raw (0x%lx) size - not supported\n",
91 ism
->sidx
, fmap
->sect
[ism
->sidx
].shdr
.Misc
.VirtualSize
,
92 fmap
->sect
[ism
->sidx
].shdr
.SizeOfRawData
);
95 /* FIXME: that's rather drastic, but that will do for now
96 * that's ok if the full file map exists, but we could be less aggressive otherwise and
97 * only map the relevant section
99 if ((mapping
= pe_map_full(ism
->fmap
, &nth
)))
101 fmap
->sect
[ism
->sidx
].mapped
= RtlImageRvaToVa(nth
, mapping
,
102 fmap
->sect
[ism
->sidx
].shdr
.VirtualAddress
,
104 return fmap
->sect
[ism
->sidx
].mapped
;
110 /******************************************************************
113 * Finds a section by name (and type) into memory from an PE file
114 * or its alternate if any
116 static BOOL
pe_find_section(struct image_file_map
* fmap
, const char* name
,
117 struct image_section_map
* ism
)
119 const char* sectname
;
121 char tmp
[IMAGE_SIZEOF_SHORT_NAME
+ 1];
123 for (i
= 0; i
< fmap
->u
.pe
.file_header
.NumberOfSections
; i
++)
125 sectname
= (const char*)fmap
->u
.pe
.sect
[i
].shdr
.Name
;
126 /* long section names start with a '/' (at least on MinGW32) */
127 if (sectname
[0] == '/' && fmap
->u
.pe
.strtable
)
128 sectname
= fmap
->u
.pe
.strtable
+ atoi(sectname
+ 1);
131 /* the section name may not be null terminated */
132 sectname
= memcpy(tmp
, sectname
, IMAGE_SIZEOF_SHORT_NAME
);
133 tmp
[IMAGE_SIZEOF_SHORT_NAME
] = '\0';
135 if (!stricmp(sectname
, name
))
148 /******************************************************************
151 * Unmaps a single section from memory
153 static void pe_unmap_section(struct image_section_map
* ism
)
155 if (ism
->sidx
>= 0 && ism
->sidx
< ism
->fmap
->u
.pe
.file_header
.NumberOfSections
&&
156 ism
->fmap
->u
.pe
.sect
[ism
->sidx
].mapped
!= IMAGE_NO_MAP
)
158 pe_unmap_full(ism
->fmap
);
159 ism
->fmap
->u
.pe
.sect
[ism
->sidx
].mapped
= IMAGE_NO_MAP
;
163 /******************************************************************
166 * Get the RVA of an PE section
168 static DWORD_PTR
pe_get_map_rva(const struct image_section_map
* ism
)
170 if (ism
->sidx
< 0 || ism
->sidx
>= ism
->fmap
->u
.pe
.file_header
.NumberOfSections
)
172 return ism
->fmap
->u
.pe
.sect
[ism
->sidx
].shdr
.VirtualAddress
;
175 /******************************************************************
178 * Get the size of a PE section
180 static unsigned pe_get_map_size(const struct image_section_map
* ism
)
182 if (ism
->sidx
< 0 || ism
->sidx
>= ism
->fmap
->u
.pe
.file_header
.NumberOfSections
)
184 return ism
->fmap
->u
.pe
.sect
[ism
->sidx
].shdr
.Misc
.VirtualSize
;
187 /******************************************************************
190 * Unmaps an PE file from memory (previously mapped with pe_map_file)
192 static void pe_unmap_file(struct image_file_map
* fmap
)
194 if (fmap
->u
.pe
.hMap
!= 0)
196 struct image_section_map ism
;
198 for (ism
.sidx
= 0; ism
.sidx
< fmap
->u
.pe
.file_header
.NumberOfSections
; ism
.sidx
++)
200 pe_unmap_section(&ism
);
202 while (fmap
->u
.pe
.full_count
) pe_unmap_full(fmap
);
203 HeapFree(GetProcessHeap(), 0, fmap
->u
.pe
.sect
);
204 HeapFree(GetProcessHeap(), 0, (void*)fmap
->u
.pe
.strtable
); /* FIXME ugly (see pe_map_file) */
205 CloseHandle(fmap
->u
.pe
.hMap
);
206 fmap
->u
.pe
.hMap
= NULL
;
210 static const struct image_file_map_ops pe_file_map_ops
=
220 /******************************************************************
221 * pe_is_valid_pointer_table
223 * Checks whether the PointerToSymbolTable and NumberOfSymbols in file_header contain
226 static BOOL
pe_is_valid_pointer_table(const IMAGE_NT_HEADERS
* nthdr
, const void* mapping
, DWORD64 sz
)
230 /* is the iSym table inside file size ? (including first DWORD of string table, which is its size) */
231 offset
= (DWORD64
)nthdr
->FileHeader
.PointerToSymbolTable
;
232 offset
+= (DWORD64
)nthdr
->FileHeader
.NumberOfSymbols
* sizeof(IMAGE_SYMBOL
);
233 if (offset
+ sizeof(DWORD
) > sz
) return FALSE
;
234 /* is string table (following iSym table) inside file size ? */
235 offset
+= *(DWORD
*)((const char*)mapping
+ offset
);
239 /******************************************************************
242 * Maps an PE file into memory (and checks it's a real PE file)
244 BOOL
pe_map_file(HANDLE file
, struct image_file_map
* fmap
, enum module_type mt
)
249 fmap
->ops
= &pe_file_map_ops
;
250 fmap
->alternate
= NULL
;
251 fmap
->u
.pe
.hMap
= CreateFileMappingW(file
, NULL
, PAGE_READONLY
, 0, 0, NULL
);
252 if (fmap
->u
.pe
.hMap
== 0) return FALSE
;
253 fmap
->u
.pe
.full_count
= 0;
254 fmap
->u
.pe
.full_map
= NULL
;
255 if (!(mapping
= pe_map_full(fmap
, NULL
))) goto error
;
261 IMAGE_NT_HEADERS
* nthdr
;
262 IMAGE_SECTION_HEADER
* section
;
265 if (!(nthdr
= RtlImageNtHeader(mapping
))) goto error
;
266 memcpy(&fmap
->u
.pe
.file_header
, &nthdr
->FileHeader
, sizeof(fmap
->u
.pe
.file_header
));
267 switch (nthdr
->OptionalHeader
.Magic
)
269 case IMAGE_NT_OPTIONAL_HDR32_MAGIC
:
270 if (sizeof(void*) == 8 && !(SymGetOptions() & SYMOPT_INCLUDE_32BIT_MODULES
))
272 TRACE("Won't load 32bit module in 64bit dbghelp when options don't ask for it\n");
275 fmap
->addr_size
= 32;
276 memcpy(&fmap
->u
.pe
.opt
.header32
, &nthdr
->OptionalHeader
, sizeof(fmap
->u
.pe
.opt
.header32
));
278 case IMAGE_NT_OPTIONAL_HDR64_MAGIC
:
279 if (sizeof(void*) == 4) return FALSE
;
280 fmap
->addr_size
= 64;
281 memcpy(&fmap
->u
.pe
.opt
.header64
, &nthdr
->OptionalHeader
, sizeof(fmap
->u
.pe
.opt
.header64
));
287 fmap
->u
.pe
.builtin
= !memcmp((const IMAGE_DOS_HEADER
*)mapping
+ 1, builtin_signature
, sizeof(builtin_signature
));
288 section
= (IMAGE_SECTION_HEADER
*)
289 ((char*)&nthdr
->OptionalHeader
+ nthdr
->FileHeader
.SizeOfOptionalHeader
);
290 fmap
->u
.pe
.sect
= HeapAlloc(GetProcessHeap(), 0,
291 nthdr
->FileHeader
.NumberOfSections
* sizeof(fmap
->u
.pe
.sect
[0]));
292 if (!fmap
->u
.pe
.sect
) goto error
;
293 for (i
= 0; i
< nthdr
->FileHeader
.NumberOfSections
; i
++)
295 memcpy(&fmap
->u
.pe
.sect
[i
].shdr
, section
+ i
, sizeof(IMAGE_SECTION_HEADER
));
296 fmap
->u
.pe
.sect
[i
].mapped
= IMAGE_NO_MAP
;
298 if (nthdr
->FileHeader
.PointerToSymbolTable
&& nthdr
->FileHeader
.NumberOfSymbols
)
302 if (GetFileSizeEx(file
, &li
) && pe_is_valid_pointer_table(nthdr
, mapping
, li
.QuadPart
))
304 /* FIXME ugly: should rather map the relevant content instead of copying it */
305 const char* src
= (const char*)mapping
+
306 nthdr
->FileHeader
.PointerToSymbolTable
+
307 nthdr
->FileHeader
.NumberOfSymbols
* sizeof(IMAGE_SYMBOL
);
309 DWORD sz
= *(DWORD
*)src
;
311 if ((dst
= HeapAlloc(GetProcessHeap(), 0, sz
)))
312 memcpy(dst
, src
, sz
);
313 fmap
->u
.pe
.strtable
= dst
;
317 WARN("Bad coff table... wipping out\n");
318 /* we have bad information here, wipe it out */
319 fmap
->u
.pe
.file_header
.PointerToSymbolTable
= 0;
320 fmap
->u
.pe
.file_header
.NumberOfSymbols
= 0;
321 fmap
->u
.pe
.strtable
= NULL
;
324 else fmap
->u
.pe
.strtable
= NULL
;
327 default: assert(0); goto error
;
334 CloseHandle(fmap
->u
.pe
.hMap
);
338 /******************************************************************
341 * Maps a directory content out of a PE file
343 const char* pe_map_directory(struct module
* module
, int dirno
, DWORD
* size
)
345 IMAGE_NT_HEADERS
* nth
;
348 if (module
->type
!= DMT_PE
|| !module
->format_info
[DFI_PE
]) return NULL
;
349 if (dirno
>= IMAGE_NUMBEROF_DIRECTORY_ENTRIES
||
350 !(mapping
= pe_map_full(&module
->format_info
[DFI_PE
]->u
.pe_info
->fmap
, &nth
)))
352 if (size
) *size
= nth
->OptionalHeader
.DataDirectory
[dirno
].Size
;
353 return RtlImageRvaToVa(nth
, mapping
,
354 nth
->OptionalHeader
.DataDirectory
[dirno
].VirtualAddress
, NULL
);
357 static void pe_module_remove(struct process
* pcs
, struct module_format
* modfmt
)
359 image_unmap_file(&modfmt
->u
.pe_info
->fmap
);
360 HeapFree(GetProcessHeap(), 0, modfmt
);
363 /******************************************************************
364 * pe_locate_with_coff_symbol_table
366 * Use the COFF symbol table (if any) from the IMAGE_FILE_HEADER to set the absolute address
368 * Mingw32 requires this for stabs debug information as address for global variables isn't filled in
369 * (this is similar to what is done in elf_module.c when using the .symtab ELF section)
371 static BOOL
pe_locate_with_coff_symbol_table(struct module
* module
)
373 struct image_file_map
* fmap
= &module
->format_info
[DFI_PE
]->u
.pe_info
->fmap
;
374 const IMAGE_SYMBOL
* isym
;
378 struct hash_table_iter hti
;
380 struct symt_data
* sym
;
383 numsym
= fmap
->u
.pe
.file_header
.NumberOfSymbols
;
384 if (!fmap
->u
.pe
.file_header
.PointerToSymbolTable
|| !numsym
)
386 if (!(mapping
= pe_map_full(fmap
, NULL
))) return FALSE
;
387 isym
= (const IMAGE_SYMBOL
*)(mapping
+ fmap
->u
.pe
.file_header
.PointerToSymbolTable
);
389 for (i
= 0; i
< numsym
; i
+= naux
, isym
+= naux
)
391 if (isym
->StorageClass
== IMAGE_SYM_CLASS_EXTERNAL
&&
392 isym
->SectionNumber
> 0 && isym
->SectionNumber
<= fmap
->u
.pe
.file_header
.NumberOfSections
)
394 if (isym
->N
.Name
.Short
)
396 name
= memcpy(tmp
, isym
->N
.ShortName
, 8);
399 else name
= fmap
->u
.pe
.strtable
+ isym
->N
.Name
.Long
;
400 if (name
[0] == '_') name
++;
401 hash_table_iter_init(&module
->ht_symbols
, &hti
, name
);
402 while ((ptr
= hash_table_iter_up(&hti
)))
404 sym
= CONTAINING_RECORD(ptr
, struct symt_data
, hash_elt
);
405 if (sym
->symt
.tag
== SymTagData
&&
406 (sym
->kind
== DataIsGlobal
|| sym
->kind
== DataIsFileStatic
) &&
407 sym
->u
.var
.kind
== loc_absolute
&&
408 !strcmp(sym
->hash_elt
.name
, name
))
410 TRACE("Changing absolute address for %d.%s: %Ix -> %I64x\n",
411 isym
->SectionNumber
, name
, sym
->u
.var
.offset
,
412 module
->module
.BaseOfImage
+
413 fmap
->u
.pe
.sect
[isym
->SectionNumber
- 1].shdr
.VirtualAddress
+
415 sym
->u
.var
.offset
= module
->module
.BaseOfImage
+
416 fmap
->u
.pe
.sect
[isym
->SectionNumber
- 1].shdr
.VirtualAddress
+ isym
->Value
;
421 naux
= isym
->NumberOfAuxSymbols
+ 1;
427 /******************************************************************
428 * pe_load_coff_symbol_table
430 * Load public symbols out of the COFF symbol table (if any).
432 static BOOL
pe_load_coff_symbol_table(struct module
* module
)
434 struct image_file_map
* fmap
= &module
->format_info
[DFI_PE
]->u
.pe_info
->fmap
;
435 const IMAGE_SYMBOL
* isym
;
437 const char* strtable
;
440 const char* lastfilename
= NULL
;
441 struct symt_compiland
* compiland
= NULL
;
442 const IMAGE_SECTION_HEADER
* sect
;
445 numsym
= fmap
->u
.pe
.file_header
.NumberOfSymbols
;
446 if (!fmap
->u
.pe
.file_header
.PointerToSymbolTable
|| !numsym
)
448 if (!(mapping
= pe_map_full(fmap
, NULL
))) return FALSE
;
449 isym
= (const IMAGE_SYMBOL
*)(mapping
+ fmap
->u
.pe
.file_header
.PointerToSymbolTable
);
450 /* FIXME: no way to get strtable size */
451 strtable
= (const char*)&isym
[numsym
];
452 sect
= IMAGE_FIRST_SECTION(RtlImageNtHeader((HMODULE
)mapping
));
454 for (i
= 0; i
< numsym
; i
+= naux
, isym
+= naux
)
456 if (isym
->StorageClass
== IMAGE_SYM_CLASS_FILE
)
458 lastfilename
= (const char*)(isym
+ 1);
461 if (isym
->StorageClass
== IMAGE_SYM_CLASS_EXTERNAL
&&
462 isym
->SectionNumber
> 0 && isym
->SectionNumber
<= fmap
->u
.pe
.file_header
.NumberOfSections
)
464 if (isym
->N
.Name
.Short
)
466 name
= memcpy(tmp
, isym
->N
.ShortName
, 8);
469 else name
= strtable
+ isym
->N
.Name
.Long
;
470 if (name
[0] == '_') name
++;
472 if (!compiland
&& lastfilename
)
473 compiland
= symt_new_compiland(module
, source_new(module
, NULL
, lastfilename
));
475 if (!(dbghelp_options
& SYMOPT_NO_PUBLICS
))
476 symt_new_public(module
, compiland
, name
, FALSE
,
477 module
->module
.BaseOfImage
+ sect
[isym
->SectionNumber
- 1].VirtualAddress
+
481 naux
= isym
->NumberOfAuxSymbols
+ 1;
483 module
->module
.SymType
= SymCoff
;
484 module
->module
.LineNumbers
= FALSE
;
485 module
->module
.GlobalSymbols
= FALSE
;
486 module
->module
.TypeInfo
= FALSE
;
487 module
->module
.SourceIndexed
= FALSE
;
488 module
->module
.Publics
= TRUE
;
494 /******************************************************************
497 * look for stabs information in PE header (it's how the mingw compiler provides
498 * its debugging information)
500 static BOOL
pe_load_stabs(const struct process
* pcs
, struct module
* module
)
502 struct image_file_map
* fmap
= &module
->format_info
[DFI_PE
]->u
.pe_info
->fmap
;
503 struct image_section_map sect_stabs
, sect_stabstr
;
506 if (pe_find_section(fmap
, ".stab", §_stabs
) && pe_find_section(fmap
, ".stabstr", §_stabstr
))
511 stab
= image_map_section(§_stabs
);
512 stabstr
= image_map_section(§_stabstr
);
513 if (stab
!= IMAGE_NO_MAP
&& stabstr
!= IMAGE_NO_MAP
)
515 ret
= stabs_parse(module
,
516 module
->module
.BaseOfImage
- PE_FROM_OPTHDR(fmap
, ImageBase
),
517 stab
, image_get_map_size(§_stabs
) / sizeof(struct stab_nlist
), sizeof(struct stab_nlist
),
518 stabstr
, image_get_map_size(§_stabstr
),
521 image_unmap_section(§_stabs
);
522 image_unmap_section(§_stabstr
);
523 if (ret
) pe_locate_with_coff_symbol_table(module
);
525 TRACE("%s the STABS debug info\n", ret
? "successfully loaded" : "failed to load");
530 /******************************************************************
533 * look for dwarf information in PE header (it's also a way for the mingw compiler
534 * to provide its debugging information)
536 static BOOL
pe_load_dwarf(struct module
* module
)
538 struct image_file_map
* fmap
= &module
->format_info
[DFI_PE
]->u
.pe_info
->fmap
;
541 ret
= dwarf2_parse(module
,
542 module
->module
.BaseOfImage
- PE_FROM_OPTHDR(fmap
, ImageBase
),
543 NULL
, /* FIXME: some thunks to deal with ? */
545 TRACE("%s the DWARF debug info\n", ret
? "successfully loaded" : "failed to load");
550 /******************************************************************
555 static BOOL
pe_load_dbg_file(const struct process
* pcs
, struct module
* module
,
556 const char* dbg_name
, DWORD timestamp
)
559 HANDLE hFile
= INVALID_HANDLE_VALUE
, hMap
= 0;
560 const BYTE
* dbg_mapping
= NULL
;
563 TRACE("Processing DBG file %s\n", debugstr_a(dbg_name
));
565 if (path_find_symbol_file(pcs
, module
, dbg_name
, DMT_DBG
, NULL
, timestamp
, 0, tmp
, &module
->module
.DbgUnmatched
) &&
566 (hFile
= CreateFileW(tmp
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
567 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
)) != INVALID_HANDLE_VALUE
&&
568 ((hMap
= CreateFileMappingW(hFile
, NULL
, PAGE_READONLY
, 0, 0, NULL
)) != 0) &&
569 ((dbg_mapping
= MapViewOfFile(hMap
, FILE_MAP_READ
, 0, 0, 0)) != NULL
))
571 const IMAGE_SEPARATE_DEBUG_HEADER
* hdr
;
572 const IMAGE_SECTION_HEADER
* sectp
;
573 const IMAGE_DEBUG_DIRECTORY
* dbg
;
575 hdr
= (const IMAGE_SEPARATE_DEBUG_HEADER
*)dbg_mapping
;
576 /* section headers come immediately after debug header */
577 sectp
= (const IMAGE_SECTION_HEADER
*)(hdr
+ 1);
578 /* and after that and the exported names comes the debug directory */
579 dbg
= (const IMAGE_DEBUG_DIRECTORY
*)
580 (dbg_mapping
+ sizeof(*hdr
) +
581 hdr
->NumberOfSections
* sizeof(IMAGE_SECTION_HEADER
) +
582 hdr
->ExportedNamesSize
);
584 ret
= pe_load_debug_directory(pcs
, module
, dbg_mapping
, sectp
,
585 hdr
->NumberOfSections
, dbg
,
586 hdr
->DebugDirectorySize
/ sizeof(*dbg
));
589 ERR("Couldn't find .DBG file %s (%s)\n", debugstr_a(dbg_name
), debugstr_w(tmp
));
591 if (dbg_mapping
) UnmapViewOfFile(dbg_mapping
);
592 if (hMap
) CloseHandle(hMap
);
593 if (hFile
!= INVALID_HANDLE_VALUE
) CloseHandle(hFile
);
597 /******************************************************************
598 * pe_load_msc_debug_info
600 * Process MSC debug information in PE file.
602 static BOOL
pe_load_msc_debug_info(const struct process
* pcs
, struct module
* module
)
604 struct image_file_map
* fmap
= &module
->format_info
[DFI_PE
]->u
.pe_info
->fmap
;
606 const IMAGE_DEBUG_DIRECTORY
*dbg
;
609 IMAGE_NT_HEADERS
* nth
;
611 if (!(mapping
= pe_map_full(fmap
, &nth
))) return FALSE
;
612 /* Read in debug directory */
613 dbg
= RtlImageDirectoryEntryToData( mapping
, FALSE
, IMAGE_DIRECTORY_ENTRY_DEBUG
, &nDbg
);
614 if (!dbg
|| !(nDbg
/= sizeof(IMAGE_DEBUG_DIRECTORY
))) goto done
;
616 /* Parse debug directory */
617 if (nth
->FileHeader
.Characteristics
& IMAGE_FILE_DEBUG_STRIPPED
)
619 /* Debug info is stripped to .DBG file */
620 const IMAGE_DEBUG_MISC
* misc
= (const IMAGE_DEBUG_MISC
*)
621 ((const char*)mapping
+ dbg
->PointerToRawData
);
623 if (nDbg
!= 1 || dbg
->Type
!= IMAGE_DEBUG_TYPE_MISC
||
624 misc
->DataType
!= IMAGE_DEBUG_MISC_EXENAME
)
626 ERR("-Debug info stripped, but no .DBG file in module %s\n",
627 debugstr_w(module
->modulename
));
631 ret
= pe_load_dbg_file(pcs
, module
, (const char*)misc
->Data
, nth
->FileHeader
.TimeDateStamp
);
636 const IMAGE_SECTION_HEADER
*sectp
= (const IMAGE_SECTION_HEADER
*)((const char*)&nth
->OptionalHeader
+ nth
->FileHeader
.SizeOfOptionalHeader
);
637 /* Debug info is embedded into PE module */
638 ret
= pe_load_debug_directory(pcs
, module
, mapping
, sectp
,
639 nth
->FileHeader
.NumberOfSections
, dbg
, nDbg
);
646 /***********************************************************************
647 * pe_load_export_debug_info
649 static BOOL
pe_load_export_debug_info(const struct process
* pcs
, struct module
* module
)
651 struct image_file_map
* fmap
= &module
->format_info
[DFI_PE
]->u
.pe_info
->fmap
;
653 const IMAGE_EXPORT_DIRECTORY
* exports
;
654 DWORD_PTR base
= module
->module
.BaseOfImage
;
656 IMAGE_NT_HEADERS
* nth
;
659 if (dbghelp_options
& SYMOPT_NO_PUBLICS
) return TRUE
;
661 if (!(mapping
= pe_map_full(fmap
, &nth
))) return FALSE
;
663 /* Add start of DLL (better use the (yet unimplemented) Exe SymTag for this) */
664 /* FIXME: module.ModuleName isn't correctly set yet if it's passed in SymLoadModule */
665 symt_new_public(module
, NULL
, module
->module
.ModuleName
, FALSE
, base
, 1);
668 /* Add entry point */
669 symt_new_public(module
, NULL
, "EntryPoint", FALSE
,
670 base
+ nth
->OptionalHeader
.AddressOfEntryPoint
, 1);
672 /* FIXME: we'd better store addresses linked to sections rather than
674 IMAGE_SECTION_HEADER
* section
;
675 /* Add start of sections */
676 section
= (IMAGE_SECTION_HEADER
*)
677 ((char*)&nth
->OptionalHeader
+ nth
->FileHeader
.SizeOfOptionalHeader
);
678 for (i
= 0; i
< nth
->FileHeader
.NumberOfSections
; i
++, section
++)
680 symt_new_public(module
, NULL
, section
->Name
, FALSE
,
681 RtlImageRvaToVa(nth
, mapping
, section
->VirtualAddress
, NULL
), 1);
685 /* Add exported functions */
686 if ((exports
= RtlImageDirectoryEntryToData(mapping
, FALSE
,
687 IMAGE_DIRECTORY_ENTRY_EXPORT
, &size
)))
689 const WORD
* ordinals
= NULL
;
690 const DWORD
* functions
= NULL
;
691 const DWORD
* names
= NULL
;
695 functions
= RtlImageRvaToVa(nth
, mapping
, exports
->AddressOfFunctions
, NULL
);
696 ordinals
= RtlImageRvaToVa(nth
, mapping
, exports
->AddressOfNameOrdinals
, NULL
);
697 names
= RtlImageRvaToVa(nth
, mapping
, exports
->AddressOfNames
, NULL
);
699 if (functions
&& ordinals
&& names
)
701 for (i
= 0; i
< exports
->NumberOfNames
; i
++)
703 if (!names
[i
]) continue;
704 symt_new_public(module
, NULL
,
705 RtlImageRvaToVa(nth
, mapping
, names
[i
], NULL
),
707 base
+ functions
[ordinals
[i
]], 1);
710 for (i
= 0; i
< exports
->NumberOfFunctions
; i
++)
712 if (!functions
[i
]) continue;
713 /* Check if we already added it with a name */
714 for (j
= 0; j
< exports
->NumberOfNames
; j
++)
715 if ((ordinals
[j
] == i
) && names
[j
]) break;
716 if (j
< exports
->NumberOfNames
) continue;
717 snprintf(buffer
, sizeof(buffer
), "%ld", i
+ exports
->Base
);
718 symt_new_public(module
, NULL
, buffer
, FALSE
, base
+ functions
[i
], 1);
722 /* no real debug info, only entry points */
723 if (module
->module
.SymType
== SymDeferred
)
724 module
->module
.SymType
= SymExport
;
730 /******************************************************************
734 BOOL
pe_load_debug_info(const struct process
* pcs
, struct module
* module
)
738 if (!(dbghelp_options
& SYMOPT_PUBLICS_ONLY
))
740 ret
= image_check_alternate(&module
->format_info
[DFI_PE
]->u
.pe_info
->fmap
, module
);
741 ret
= pe_load_stabs(pcs
, module
) || ret
;
742 ret
= pe_load_dwarf(module
) || ret
;
743 ret
= pe_load_msc_debug_info(pcs
, module
) || ret
;
744 ret
= ret
|| pe_load_coff_symbol_table(module
); /* FIXME */
745 /* if we still have no debug info (we could only get SymExport at this
746 * point), then do the SymExport except if we have an ELF container,
747 * in which case we'll rely on the export's on the ELF side
750 /* FIXME shouldn't we check that? if (!module_get_debug(pcs, module)) */
751 if (pe_load_export_debug_info(pcs
, module
) && !ret
)
753 if (!ret
) module
->module
.SymType
= SymNone
;
757 struct builtin_search
760 struct image_file_map fmap
;
763 static BOOL
search_builtin_pe(void *param
, HANDLE handle
, const WCHAR
*path
)
765 struct builtin_search
*search
= param
;
768 if (!pe_map_file(handle
, &search
->fmap
, DMT_PE
)) return FALSE
;
770 size
= (lstrlenW(path
) + 1) * sizeof(WCHAR
);
771 if ((search
->path
= heap_alloc(size
)))
772 memcpy(search
->path
, path
, size
);
776 /******************************************************************
777 * pe_load_native_module
780 struct module
* pe_load_native_module(struct process
* pcs
, const WCHAR
* name
,
781 HANDLE hFile
, DWORD64 base
, DWORD size
)
783 struct module
* module
= NULL
;
785 struct module_format
* modfmt
;
786 WCHAR loaded_name
[MAX_PATH
];
788 loaded_name
[0] = '\0';
793 if ((hFile
= FindExecutableImageExW(name
, pcs
->search_path
, loaded_name
, NULL
, NULL
)) == NULL
&&
794 (hFile
= FindExecutableImageExW(name
, L
".", loaded_name
, NULL
, NULL
)) == NULL
)
798 else if (name
) lstrcpyW(loaded_name
, name
);
799 if (!(modfmt
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct module_format
) + sizeof(struct pe_module_info
))))
801 modfmt
->u
.pe_info
= (struct pe_module_info
*)(modfmt
+ 1);
802 if (pe_map_file(hFile
, &modfmt
->u
.pe_info
->fmap
, DMT_PE
))
804 struct builtin_search builtin
= { NULL
};
805 if (modfmt
->u
.pe_info
->fmap
.u
.pe
.builtin
&& search_dll_path(pcs
, loaded_name
, search_builtin_pe
, &builtin
))
807 TRACE("reloaded %s from %s\n", debugstr_w(loaded_name
), debugstr_w(builtin
.path
));
808 image_unmap_file(&modfmt
->u
.pe_info
->fmap
);
809 modfmt
->u
.pe_info
->fmap
= builtin
.fmap
;
811 if (!base
) base
= PE_FROM_OPTHDR(&modfmt
->u
.pe_info
->fmap
, ImageBase
);
812 if (!size
) size
= PE_FROM_OPTHDR(&modfmt
->u
.pe_info
->fmap
, SizeOfImage
);
814 module
= module_new(pcs
, loaded_name
, DMT_PE
, FALSE
, base
, size
,
815 modfmt
->u
.pe_info
->fmap
.u
.pe
.file_header
.TimeDateStamp
,
816 PE_FROM_OPTHDR(&modfmt
->u
.pe_info
->fmap
, CheckSum
),
817 modfmt
->u
.pe_info
->fmap
.u
.pe
.file_header
.Machine
);
820 module
->real_path
= builtin
.path
;
821 modfmt
->module
= module
;
822 modfmt
->remove
= pe_module_remove
;
823 modfmt
->loc_compute
= NULL
;
824 module
->format_info
[DFI_PE
] = modfmt
;
825 module
->reloc_delta
= base
- PE_FROM_OPTHDR(&modfmt
->u
.pe_info
->fmap
, ImageBase
);
829 ERR("could not load the module '%s'\n", debugstr_w(loaded_name
));
830 heap_free(builtin
.path
);
831 image_unmap_file(&modfmt
->u
.pe_info
->fmap
);
834 if (!module
) HeapFree(GetProcessHeap(), 0, modfmt
);
836 if (opened
) CloseHandle(hFile
);
841 /******************************************************************
845 BOOL
pe_load_nt_header(HANDLE hProc
, DWORD64 base
, IMAGE_NT_HEADERS
* nth
)
847 IMAGE_DOS_HEADER dos
;
849 return ReadProcessMemory(hProc
, (char*)(DWORD_PTR
)base
, &dos
, sizeof(dos
), NULL
) &&
850 dos
.e_magic
== IMAGE_DOS_SIGNATURE
&&
851 ReadProcessMemory(hProc
, (char*)(DWORD_PTR
)(base
+ dos
.e_lfanew
),
852 nth
, sizeof(*nth
), NULL
) &&
853 nth
->Signature
== IMAGE_NT_SIGNATURE
;
856 /******************************************************************
857 * pe_load_builtin_module
860 struct module
* pe_load_builtin_module(struct process
* pcs
, const WCHAR
* name
,
861 DWORD64 base
, DWORD64 size
)
863 struct module
* module
= NULL
;
865 if (base
&& pcs
->dbg_hdr_addr
)
867 IMAGE_NT_HEADERS nth
;
869 if (pe_load_nt_header(pcs
->handle
, base
, &nth
))
871 if (!size
) size
= nth
.OptionalHeader
.SizeOfImage
;
872 module
= module_new(pcs
, name
, DMT_PE
, FALSE
, base
, size
,
873 nth
.FileHeader
.TimeDateStamp
,
874 nth
.OptionalHeader
.CheckSum
,
875 nth
.FileHeader
.Machine
);
881 /***********************************************************************
882 * ImageDirectoryEntryToDataEx (DBGHELP.@)
884 * Search for specified directory in PE image
888 * base [in] Image base address
889 * image [in] TRUE - image has been loaded by loader, FALSE - raw file image
890 * dir [in] Target directory index
891 * size [out] Receives directory size
892 * section [out] Receives pointer to section header of section containing directory data
895 * Success: pointer to directory data
899 PVOID WINAPI
ImageDirectoryEntryToDataEx( PVOID base
, BOOLEAN image
, USHORT dir
, PULONG size
, PIMAGE_SECTION_HEADER
*section
)
901 const IMAGE_NT_HEADERS
*nt
;
905 if (section
) *section
= NULL
;
907 if (!(nt
= RtlImageNtHeader( base
))) return NULL
;
908 if (nt
->OptionalHeader
.Magic
== IMAGE_NT_OPTIONAL_HDR64_MAGIC
)
910 const IMAGE_NT_HEADERS64
*nt64
= (const IMAGE_NT_HEADERS64
*)nt
;
912 if (dir
>= nt64
->OptionalHeader
.NumberOfRvaAndSizes
) return NULL
;
913 if (!(addr
= nt64
->OptionalHeader
.DataDirectory
[dir
].VirtualAddress
)) return NULL
;
914 *size
= nt64
->OptionalHeader
.DataDirectory
[dir
].Size
;
915 if (image
|| addr
< nt64
->OptionalHeader
.SizeOfHeaders
) return (char *)base
+ addr
;
917 else if (nt
->OptionalHeader
.Magic
== IMAGE_NT_OPTIONAL_HDR32_MAGIC
)
919 const IMAGE_NT_HEADERS32
*nt32
= (const IMAGE_NT_HEADERS32
*)nt
;
921 if (dir
>= nt32
->OptionalHeader
.NumberOfRvaAndSizes
) return NULL
;
922 if (!(addr
= nt32
->OptionalHeader
.DataDirectory
[dir
].VirtualAddress
)) return NULL
;
923 *size
= nt32
->OptionalHeader
.DataDirectory
[dir
].Size
;
924 if (image
|| addr
< nt32
->OptionalHeader
.SizeOfHeaders
) return (char *)base
+ addr
;
928 return RtlImageRvaToVa( nt
, base
, addr
, section
);
931 /***********************************************************************
932 * ImageDirectoryEntryToData (DBGHELP.@)
935 * See ImageDirectoryEntryToDataEx
937 PVOID WINAPI
ImageDirectoryEntryToData( PVOID base
, BOOLEAN image
, USHORT dir
, PULONG size
)
939 return ImageDirectoryEntryToDataEx( base
, image
, dir
, size
, NULL
);