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
30 #define WIN32_NO_STATUS
31 #include "dbghelp_private.h"
32 #include "image_private.h"
34 #include "wine/debug.h"
35 #include "wine/heap.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp
);
41 struct image_file_map fmap
;
44 static const char builtin_signature
[] = "Wine builtin DLL";
46 static void* pe_map_full(struct image_file_map
* fmap
, IMAGE_NT_HEADERS
** nth
)
48 if (!fmap
->u
.pe
.full_map
)
50 fmap
->u
.pe
.full_map
= MapViewOfFile(fmap
->u
.pe
.hMap
, FILE_MAP_READ
, 0, 0, 0);
52 if (fmap
->u
.pe
.full_map
)
54 if (nth
) *nth
= RtlImageNtHeader(fmap
->u
.pe
.full_map
);
55 fmap
->u
.pe
.full_count
++;
56 return fmap
->u
.pe
.full_map
;
61 static void pe_unmap_full(struct image_file_map
* fmap
)
63 if (fmap
->u
.pe
.full_count
&& !--fmap
->u
.pe
.full_count
)
65 UnmapViewOfFile(fmap
->u
.pe
.full_map
);
66 fmap
->u
.pe
.full_map
= NULL
;
70 /* as we store either IMAGE_OPTIONAL_HEADER(32|64) inside pe_file_map,
71 * this helper will read to any field 'field' inside such an header
73 #define PE_FROM_OPTHDR(fmap, field) (((fmap)->addr_size == 32) ? ((fmap)->u.pe.opt.header32. field) : ((fmap)->u.pe.opt.header64. field))
75 /******************************************************************
78 * Maps a single section into memory from an PE file
80 static const char* pe_map_section(struct image_section_map
* ism
)
83 struct pe_file_map
* fmap
= &ism
->fmap
->u
.pe
;
85 if (ism
->sidx
>= 0 && ism
->sidx
< fmap
->file_header
.NumberOfSections
&&
86 fmap
->sect
[ism
->sidx
].mapped
== IMAGE_NO_MAP
)
88 IMAGE_NT_HEADERS
* nth
;
90 if (fmap
->sect
[ism
->sidx
].shdr
.Misc
.VirtualSize
> fmap
->sect
[ism
->sidx
].shdr
.SizeOfRawData
)
92 FIXME("Section %Id: virtual (0x%lx) > raw (0x%lx) size - not supported\n",
93 ism
->sidx
, fmap
->sect
[ism
->sidx
].shdr
.Misc
.VirtualSize
,
94 fmap
->sect
[ism
->sidx
].shdr
.SizeOfRawData
);
97 /* FIXME: that's rather drastic, but that will do for now
98 * that's ok if the full file map exists, but we could be less aggressive otherwise and
99 * only map the relevant section
101 if ((mapping
= pe_map_full(ism
->fmap
, &nth
)))
103 fmap
->sect
[ism
->sidx
].mapped
= RtlImageRvaToVa(nth
, mapping
,
104 fmap
->sect
[ism
->sidx
].shdr
.VirtualAddress
,
106 return fmap
->sect
[ism
->sidx
].mapped
;
112 /******************************************************************
115 * Finds a section by name (and type) into memory from an PE file
116 * or its alternate if any
118 static BOOL
pe_find_section(struct image_file_map
* fmap
, const char* name
,
119 struct image_section_map
* ism
)
121 const char* sectname
;
123 char tmp
[IMAGE_SIZEOF_SHORT_NAME
+ 1];
125 for (i
= 0; i
< fmap
->u
.pe
.file_header
.NumberOfSections
; i
++)
127 sectname
= (const char*)fmap
->u
.pe
.sect
[i
].shdr
.Name
;
128 /* long section names start with a '/' (at least on MinGW32) */
129 if (sectname
[0] == '/' && fmap
->u
.pe
.strtable
)
130 sectname
= fmap
->u
.pe
.strtable
+ atoi(sectname
+ 1);
133 /* the section name may not be null terminated */
134 sectname
= memcpy(tmp
, sectname
, IMAGE_SIZEOF_SHORT_NAME
);
135 tmp
[IMAGE_SIZEOF_SHORT_NAME
] = '\0';
137 if (!stricmp(sectname
, name
))
150 /******************************************************************
153 * Unmaps a single section from memory
155 static void pe_unmap_section(struct image_section_map
* ism
)
157 if (ism
->sidx
>= 0 && ism
->sidx
< ism
->fmap
->u
.pe
.file_header
.NumberOfSections
&&
158 ism
->fmap
->u
.pe
.sect
[ism
->sidx
].mapped
!= IMAGE_NO_MAP
)
160 pe_unmap_full(ism
->fmap
);
161 ism
->fmap
->u
.pe
.sect
[ism
->sidx
].mapped
= IMAGE_NO_MAP
;
165 /******************************************************************
168 * Get the RVA of an PE section
170 static DWORD_PTR
pe_get_map_rva(const struct image_section_map
* ism
)
172 if (ism
->sidx
< 0 || ism
->sidx
>= ism
->fmap
->u
.pe
.file_header
.NumberOfSections
)
174 return ism
->fmap
->u
.pe
.sect
[ism
->sidx
].shdr
.VirtualAddress
;
177 /******************************************************************
180 * Get the size of a PE section
182 static unsigned pe_get_map_size(const struct image_section_map
* ism
)
184 if (ism
->sidx
< 0 || ism
->sidx
>= ism
->fmap
->u
.pe
.file_header
.NumberOfSections
)
186 return ism
->fmap
->u
.pe
.sect
[ism
->sidx
].shdr
.Misc
.VirtualSize
;
189 /******************************************************************
192 * Unmaps an PE file from memory (previously mapped with pe_map_file)
194 static void pe_unmap_file(struct image_file_map
* fmap
)
196 if (fmap
->u
.pe
.hMap
!= 0)
198 struct image_section_map ism
;
200 for (ism
.sidx
= 0; ism
.sidx
< fmap
->u
.pe
.file_header
.NumberOfSections
; ism
.sidx
++)
202 pe_unmap_section(&ism
);
204 while (fmap
->u
.pe
.full_count
) pe_unmap_full(fmap
);
205 HeapFree(GetProcessHeap(), 0, fmap
->u
.pe
.sect
);
206 HeapFree(GetProcessHeap(), 0, (void*)fmap
->u
.pe
.strtable
); /* FIXME ugly (see pe_map_file) */
207 CloseHandle(fmap
->u
.pe
.hMap
);
208 fmap
->u
.pe
.hMap
= NULL
;
212 static const struct image_file_map_ops pe_file_map_ops
=
222 /******************************************************************
223 * pe_is_valid_pointer_table
225 * Checks whether the PointerToSymbolTable and NumberOfSymbols in file_header contain
228 static BOOL
pe_is_valid_pointer_table(const IMAGE_NT_HEADERS
* nthdr
, const void* mapping
, DWORD64 sz
)
232 /* is the iSym table inside file size ? (including first DWORD of string table, which is its size) */
233 offset
= (DWORD64
)nthdr
->FileHeader
.PointerToSymbolTable
;
234 offset
+= (DWORD64
)nthdr
->FileHeader
.NumberOfSymbols
* sizeof(IMAGE_SYMBOL
);
235 if (offset
+ sizeof(DWORD
) > sz
) return FALSE
;
236 /* is string table (following iSym table) inside file size ? */
237 offset
+= *(DWORD
*)((const char*)mapping
+ offset
);
241 /******************************************************************
244 * Maps an PE file into memory (and checks it's a real PE file)
246 BOOL
pe_map_file(HANDLE file
, struct image_file_map
* fmap
)
249 IMAGE_NT_HEADERS
* nthdr
;
250 IMAGE_SECTION_HEADER
* section
;
253 fmap
->modtype
= DMT_PE
;
254 fmap
->ops
= &pe_file_map_ops
;
255 fmap
->alternate
= NULL
;
256 fmap
->u
.pe
.hMap
= CreateFileMappingW(file
, NULL
, PAGE_READONLY
, 0, 0, NULL
);
257 if (fmap
->u
.pe
.hMap
== 0) return FALSE
;
258 fmap
->u
.pe
.full_count
= 0;
259 fmap
->u
.pe
.full_map
= NULL
;
260 if (!(mapping
= pe_map_full(fmap
, NULL
))) goto error
;
262 if (!(nthdr
= RtlImageNtHeader(mapping
))) goto error
;
263 memcpy(&fmap
->u
.pe
.file_header
, &nthdr
->FileHeader
, sizeof(fmap
->u
.pe
.file_header
));
264 switch (nthdr
->OptionalHeader
.Magic
)
266 case IMAGE_NT_OPTIONAL_HDR32_MAGIC
:
267 fmap
->addr_size
= 32;
268 memcpy(&fmap
->u
.pe
.opt
.header32
, &nthdr
->OptionalHeader
, sizeof(fmap
->u
.pe
.opt
.header32
));
270 case IMAGE_NT_OPTIONAL_HDR64_MAGIC
:
271 if (sizeof(void*) == 4) return FALSE
;
272 fmap
->addr_size
= 64;
273 memcpy(&fmap
->u
.pe
.opt
.header64
, &nthdr
->OptionalHeader
, sizeof(fmap
->u
.pe
.opt
.header64
));
279 fmap
->u
.pe
.builtin
= !memcmp((const IMAGE_DOS_HEADER
*)mapping
+ 1, builtin_signature
, sizeof(builtin_signature
));
280 section
= IMAGE_FIRST_SECTION( nthdr
);
281 fmap
->u
.pe
.sect
= HeapAlloc(GetProcessHeap(), 0,
282 nthdr
->FileHeader
.NumberOfSections
* sizeof(fmap
->u
.pe
.sect
[0]));
283 if (!fmap
->u
.pe
.sect
) goto error
;
284 for (i
= 0; i
< nthdr
->FileHeader
.NumberOfSections
; i
++)
286 memcpy(&fmap
->u
.pe
.sect
[i
].shdr
, section
+ i
, sizeof(IMAGE_SECTION_HEADER
));
287 fmap
->u
.pe
.sect
[i
].mapped
= IMAGE_NO_MAP
;
289 if (nthdr
->FileHeader
.PointerToSymbolTable
&& nthdr
->FileHeader
.NumberOfSymbols
)
293 if (GetFileSizeEx(file
, &li
) && pe_is_valid_pointer_table(nthdr
, mapping
, li
.QuadPart
))
295 /* FIXME ugly: should rather map the relevant content instead of copying it */
296 const char* src
= (const char*)mapping
+
297 nthdr
->FileHeader
.PointerToSymbolTable
+
298 nthdr
->FileHeader
.NumberOfSymbols
* sizeof(IMAGE_SYMBOL
);
300 DWORD sz
= *(DWORD
*)src
;
302 if ((dst
= HeapAlloc(GetProcessHeap(), 0, sz
)))
303 memcpy(dst
, src
, sz
);
304 fmap
->u
.pe
.strtable
= dst
;
308 WARN("Bad coff table... wipping out\n");
309 /* we have bad information here, wipe it out */
310 fmap
->u
.pe
.file_header
.PointerToSymbolTable
= 0;
311 fmap
->u
.pe
.file_header
.NumberOfSymbols
= 0;
312 fmap
->u
.pe
.strtable
= NULL
;
315 else fmap
->u
.pe
.strtable
= NULL
;
322 CloseHandle(fmap
->u
.pe
.hMap
);
326 /******************************************************************
329 * Maps a directory content out of a PE file
331 const char* pe_map_directory(struct module
* module
, int dirno
, DWORD
* size
)
333 IMAGE_NT_HEADERS
* nth
;
336 if (module
->type
!= DMT_PE
|| !module
->format_info
[DFI_PE
]) return NULL
;
337 if (dirno
>= IMAGE_NUMBEROF_DIRECTORY_ENTRIES
||
338 !(mapping
= pe_map_full(&module
->format_info
[DFI_PE
]->u
.pe_info
->fmap
, &nth
)))
340 if (size
) *size
= nth
->OptionalHeader
.DataDirectory
[dirno
].Size
;
341 return RtlImageRvaToVa(nth
, mapping
,
342 nth
->OptionalHeader
.DataDirectory
[dirno
].VirtualAddress
, NULL
);
345 static void pe_module_remove(struct process
* pcs
, struct module_format
* modfmt
)
347 image_unmap_file(&modfmt
->u
.pe_info
->fmap
);
348 HeapFree(GetProcessHeap(), 0, modfmt
);
351 /******************************************************************
352 * pe_locate_with_coff_symbol_table
354 * Use the COFF symbol table (if any) from the IMAGE_FILE_HEADER to set the absolute address
356 * Mingw32 requires this for stabs debug information as address for global variables isn't filled in
357 * (this is similar to what is done in elf_module.c when using the .symtab ELF section)
359 static BOOL
pe_locate_with_coff_symbol_table(struct module
* module
)
361 struct image_file_map
* fmap
= &module
->format_info
[DFI_PE
]->u
.pe_info
->fmap
;
362 const IMAGE_SYMBOL
* isym
;
366 struct hash_table_iter hti
;
368 struct symt_data
* sym
;
371 numsym
= fmap
->u
.pe
.file_header
.NumberOfSymbols
;
372 if (!fmap
->u
.pe
.file_header
.PointerToSymbolTable
|| !numsym
)
374 if (!(mapping
= pe_map_full(fmap
, NULL
))) return FALSE
;
375 isym
= (const IMAGE_SYMBOL
*)(mapping
+ fmap
->u
.pe
.file_header
.PointerToSymbolTable
);
377 for (i
= 0; i
< numsym
; i
+= naux
, isym
+= naux
)
379 if (isym
->StorageClass
== IMAGE_SYM_CLASS_EXTERNAL
&&
380 isym
->SectionNumber
> 0 && isym
->SectionNumber
<= fmap
->u
.pe
.file_header
.NumberOfSections
)
382 if (isym
->N
.Name
.Short
)
384 name
= memcpy(tmp
, isym
->N
.ShortName
, 8);
387 else name
= fmap
->u
.pe
.strtable
+ isym
->N
.Name
.Long
;
388 if (name
[0] == '_') name
++;
389 hash_table_iter_init(&module
->ht_symbols
, &hti
, name
);
390 while ((ptr
= hash_table_iter_up(&hti
)))
392 sym
= CONTAINING_RECORD(ptr
, struct symt_data
, hash_elt
);
393 if (sym
->symt
.tag
== SymTagData
&&
394 (sym
->kind
== DataIsGlobal
|| sym
->kind
== DataIsFileStatic
) &&
395 sym
->u
.var
.kind
== loc_absolute
&&
396 !strcmp(sym
->hash_elt
.name
, name
))
398 TRACE("Changing absolute address for %d.%s: %Ix -> %I64x\n",
399 isym
->SectionNumber
, name
, sym
->u
.var
.offset
,
400 module
->module
.BaseOfImage
+
401 fmap
->u
.pe
.sect
[isym
->SectionNumber
- 1].shdr
.VirtualAddress
+
403 sym
->u
.var
.offset
= module
->module
.BaseOfImage
+
404 fmap
->u
.pe
.sect
[isym
->SectionNumber
- 1].shdr
.VirtualAddress
+ isym
->Value
;
409 naux
= isym
->NumberOfAuxSymbols
+ 1;
415 /******************************************************************
416 * pe_load_coff_symbol_table
418 * Load public symbols out of the COFF symbol table (if any).
420 static BOOL
pe_load_coff_symbol_table(struct module
* module
)
422 struct image_file_map
* fmap
= &module
->format_info
[DFI_PE
]->u
.pe_info
->fmap
;
423 const IMAGE_SYMBOL
* isym
;
425 const char* strtable
;
428 const char* lastfilename
= NULL
;
429 struct symt_compiland
* compiland
= NULL
;
430 const IMAGE_SECTION_HEADER
* sect
;
433 numsym
= fmap
->u
.pe
.file_header
.NumberOfSymbols
;
434 if (!fmap
->u
.pe
.file_header
.PointerToSymbolTable
|| !numsym
)
436 if (!(mapping
= pe_map_full(fmap
, NULL
))) return FALSE
;
437 isym
= (const IMAGE_SYMBOL
*)(mapping
+ fmap
->u
.pe
.file_header
.PointerToSymbolTable
);
438 /* FIXME: no way to get strtable size */
439 strtable
= (const char*)&isym
[numsym
];
440 sect
= IMAGE_FIRST_SECTION(RtlImageNtHeader((HMODULE
)mapping
));
442 for (i
= 0; i
< numsym
; i
+= naux
, isym
+= naux
)
444 if (isym
->StorageClass
== IMAGE_SYM_CLASS_FILE
)
446 lastfilename
= (const char*)(isym
+ 1);
449 if (isym
->StorageClass
== IMAGE_SYM_CLASS_EXTERNAL
&&
450 isym
->SectionNumber
> 0 && isym
->SectionNumber
<= fmap
->u
.pe
.file_header
.NumberOfSections
)
452 if (isym
->N
.Name
.Short
)
454 name
= memcpy(tmp
, isym
->N
.ShortName
, 8);
457 else name
= strtable
+ isym
->N
.Name
.Long
;
458 if (name
[0] == '_') name
++;
460 if (!compiland
&& lastfilename
)
461 compiland
= symt_new_compiland(module
, source_new(module
, NULL
, lastfilename
));
463 if (!(dbghelp_options
& SYMOPT_NO_PUBLICS
))
464 symt_new_public(module
, compiland
, name
, FALSE
,
465 module
->module
.BaseOfImage
+ sect
[isym
->SectionNumber
- 1].VirtualAddress
+
469 naux
= isym
->NumberOfAuxSymbols
+ 1;
471 module
->module
.SymType
= SymCoff
;
472 module
->module
.LineNumbers
= FALSE
;
473 module
->module
.GlobalSymbols
= FALSE
;
474 module
->module
.TypeInfo
= FALSE
;
475 module
->module
.SourceIndexed
= FALSE
;
476 module
->module
.Publics
= TRUE
;
482 /******************************************************************
485 * look for stabs information in PE header (it's how the mingw compiler provides
486 * its debugging information)
488 static BOOL
pe_load_stabs(const struct process
* pcs
, struct module
* module
)
490 struct image_file_map
* fmap
= &module
->format_info
[DFI_PE
]->u
.pe_info
->fmap
;
491 struct image_section_map sect_stabs
, sect_stabstr
;
494 if (pe_find_section(fmap
, ".stab", §_stabs
) && pe_find_section(fmap
, ".stabstr", §_stabstr
))
499 stab
= image_map_section(§_stabs
);
500 stabstr
= image_map_section(§_stabstr
);
501 if (stab
!= IMAGE_NO_MAP
&& stabstr
!= IMAGE_NO_MAP
)
503 ret
= stabs_parse(module
,
504 module
->module
.BaseOfImage
- PE_FROM_OPTHDR(fmap
, ImageBase
),
505 stab
, image_get_map_size(§_stabs
) / sizeof(struct stab_nlist
), sizeof(struct stab_nlist
),
506 stabstr
, image_get_map_size(§_stabstr
),
509 image_unmap_section(§_stabs
);
510 image_unmap_section(§_stabstr
);
511 if (ret
) pe_locate_with_coff_symbol_table(module
);
513 TRACE("%s the STABS debug info\n", ret
? "successfully loaded" : "failed to load");
518 /******************************************************************
521 * look for dwarf information in PE header (it's also a way for the mingw compiler
522 * to provide its debugging information)
524 static BOOL
pe_load_dwarf(struct module
* module
)
526 struct image_file_map
* fmap
= &module
->format_info
[DFI_PE
]->u
.pe_info
->fmap
;
529 ret
= dwarf2_parse(module
,
530 module
->module
.BaseOfImage
- PE_FROM_OPTHDR(fmap
, ImageBase
),
531 NULL
, /* FIXME: some thunks to deal with ? */
533 TRACE("%s the DWARF debug info\n", ret
? "successfully loaded" : "failed to load");
538 /******************************************************************
543 static BOOL
pe_load_dbg_file(const struct process
* pcs
, struct module
* module
,
544 const char* dbg_name
, DWORD timestamp
)
547 HANDLE hFile
= INVALID_HANDLE_VALUE
, hMap
= 0;
548 const BYTE
* dbg_mapping
= NULL
;
551 TRACE("Processing DBG file %s\n", debugstr_a(dbg_name
));
553 if (path_find_symbol_file(pcs
, module
, dbg_name
, FALSE
, NULL
, timestamp
, 0, tmp
, &module
->module
.DbgUnmatched
) &&
554 (hFile
= CreateFileW(tmp
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
555 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
)) != INVALID_HANDLE_VALUE
&&
556 ((hMap
= CreateFileMappingW(hFile
, NULL
, PAGE_READONLY
, 0, 0, NULL
)) != 0) &&
557 ((dbg_mapping
= MapViewOfFile(hMap
, FILE_MAP_READ
, 0, 0, 0)) != NULL
))
559 const IMAGE_SEPARATE_DEBUG_HEADER
* hdr
;
560 const IMAGE_SECTION_HEADER
* sectp
;
561 const IMAGE_DEBUG_DIRECTORY
* dbg
;
563 hdr
= (const IMAGE_SEPARATE_DEBUG_HEADER
*)dbg_mapping
;
564 /* section headers come immediately after debug header */
565 sectp
= (const IMAGE_SECTION_HEADER
*)(hdr
+ 1);
566 /* and after that and the exported names comes the debug directory */
567 dbg
= (const IMAGE_DEBUG_DIRECTORY
*)
568 (dbg_mapping
+ sizeof(*hdr
) +
569 hdr
->NumberOfSections
* sizeof(IMAGE_SECTION_HEADER
) +
570 hdr
->ExportedNamesSize
);
572 ret
= pe_load_debug_directory(pcs
, module
, dbg_mapping
, sectp
,
573 hdr
->NumberOfSections
, dbg
,
574 hdr
->DebugDirectorySize
/ sizeof(*dbg
));
577 ERR("Couldn't find .DBG file %s (%s)\n", debugstr_a(dbg_name
), debugstr_w(tmp
));
579 if (dbg_mapping
) UnmapViewOfFile(dbg_mapping
);
580 if (hMap
) CloseHandle(hMap
);
581 if (hFile
!= INVALID_HANDLE_VALUE
) CloseHandle(hFile
);
585 /******************************************************************
586 * pe_load_msc_debug_info
588 * Process MSC debug information in PE file.
590 static BOOL
pe_load_msc_debug_info(const struct process
* pcs
, struct module
* module
)
592 struct image_file_map
* fmap
= &module
->format_info
[DFI_PE
]->u
.pe_info
->fmap
;
594 const IMAGE_DEBUG_DIRECTORY
*dbg
;
597 IMAGE_NT_HEADERS
* nth
;
599 if (!(mapping
= pe_map_full(fmap
, &nth
))) return FALSE
;
600 /* Read in debug directory */
601 dbg
= RtlImageDirectoryEntryToData( mapping
, FALSE
, IMAGE_DIRECTORY_ENTRY_DEBUG
, &nDbg
);
602 if (!dbg
|| !(nDbg
/= sizeof(IMAGE_DEBUG_DIRECTORY
))) goto done
;
604 /* Parse debug directory */
605 if (nth
->FileHeader
.Characteristics
& IMAGE_FILE_DEBUG_STRIPPED
)
607 /* Debug info is stripped to .DBG file */
608 const IMAGE_DEBUG_MISC
* misc
= (const IMAGE_DEBUG_MISC
*)
609 ((const char*)mapping
+ dbg
->PointerToRawData
);
611 if (nDbg
!= 1 || dbg
->Type
!= IMAGE_DEBUG_TYPE_MISC
||
612 misc
->DataType
!= IMAGE_DEBUG_MISC_EXENAME
)
614 ERR("-Debug info stripped, but no .DBG file in module %s\n",
615 debugstr_w(module
->modulename
));
619 ret
= pe_load_dbg_file(pcs
, module
, (const char*)misc
->Data
, nth
->FileHeader
.TimeDateStamp
);
624 /* Debug info is embedded into PE module */
625 ret
= pe_load_debug_directory(pcs
, module
, mapping
, IMAGE_FIRST_SECTION( nth
),
626 nth
->FileHeader
.NumberOfSections
, dbg
, nDbg
);
633 /***********************************************************************
634 * pe_load_export_debug_info
636 static BOOL
pe_load_export_debug_info(const struct process
* pcs
, struct module
* module
)
638 struct image_file_map
* fmap
= &module
->format_info
[DFI_PE
]->u
.pe_info
->fmap
;
640 const IMAGE_EXPORT_DIRECTORY
* exports
;
641 DWORD_PTR base
= module
->module
.BaseOfImage
;
643 IMAGE_NT_HEADERS
* nth
;
646 if (dbghelp_options
& SYMOPT_NO_PUBLICS
) return TRUE
;
648 if (!(mapping
= pe_map_full(fmap
, &nth
))) return FALSE
;
650 /* Add start of DLL (better use the (yet unimplemented) Exe SymTag for this) */
651 /* FIXME: module.ModuleName isn't correctly set yet if it's passed in SymLoadModule */
652 symt_new_public(module
, NULL
, module
->module
.ModuleName
, FALSE
, base
, 1);
655 /* Add entry point */
656 symt_new_public(module
, NULL
, "EntryPoint", FALSE
,
657 base
+ nth
->OptionalHeader
.AddressOfEntryPoint
, 1);
659 /* FIXME: we'd better store addresses linked to sections rather than
661 IMAGE_SECTION_HEADER
*section
= IMAGE_FIRST_SECTION( nth
);
662 /* Add start of sections */
663 for (i
= 0; i
< nth
->FileHeader
.NumberOfSections
; i
++, section
++)
665 symt_new_public(module
, NULL
, section
->Name
, FALSE
,
666 RtlImageRvaToVa(nth
, mapping
, section
->VirtualAddress
, NULL
), 1);
670 /* Add exported functions */
671 if ((exports
= RtlImageDirectoryEntryToData(mapping
, FALSE
,
672 IMAGE_DIRECTORY_ENTRY_EXPORT
, &size
)))
674 const WORD
* ordinals
= NULL
;
675 const DWORD
* functions
= NULL
;
676 const DWORD
* names
= NULL
;
680 functions
= RtlImageRvaToVa(nth
, mapping
, exports
->AddressOfFunctions
, NULL
);
681 ordinals
= RtlImageRvaToVa(nth
, mapping
, exports
->AddressOfNameOrdinals
, NULL
);
682 names
= RtlImageRvaToVa(nth
, mapping
, exports
->AddressOfNames
, NULL
);
684 if (functions
&& ordinals
&& names
)
686 for (i
= 0; i
< exports
->NumberOfNames
; i
++)
688 if (!names
[i
]) continue;
689 symt_new_public(module
, NULL
,
690 RtlImageRvaToVa(nth
, mapping
, names
[i
], NULL
),
692 base
+ functions
[ordinals
[i
]], 1);
695 for (i
= 0; i
< exports
->NumberOfFunctions
; i
++)
697 if (!functions
[i
]) continue;
698 /* Check if we already added it with a name */
699 for (j
= 0; j
< exports
->NumberOfNames
; j
++)
700 if ((ordinals
[j
] == i
) && names
[j
]) break;
701 if (j
< exports
->NumberOfNames
) continue;
702 snprintf(buffer
, sizeof(buffer
), "%ld", i
+ exports
->Base
);
703 symt_new_public(module
, NULL
, buffer
, FALSE
, base
+ functions
[i
], 1);
707 /* no real debug info, only entry points */
708 if (module
->module
.SymType
== SymDeferred
)
709 module
->module
.SymType
= SymExport
;
715 /******************************************************************
719 BOOL
pe_load_debug_info(const struct process
* pcs
, struct module
* module
)
723 if (!(dbghelp_options
& SYMOPT_PUBLICS_ONLY
))
725 ret
= image_check_alternate(&module
->format_info
[DFI_PE
]->u
.pe_info
->fmap
, module
);
726 ret
= pe_load_stabs(pcs
, module
) || ret
;
727 ret
= pe_load_dwarf(module
) || ret
;
728 ret
= pe_load_msc_debug_info(pcs
, module
) || ret
;
729 ret
= ret
|| pe_load_coff_symbol_table(module
); /* FIXME */
730 /* if we still have no debug info (we could only get SymExport at this
731 * point), then do the SymExport except if we have an ELF container,
732 * in which case we'll rely on the export's on the ELF side
735 /* FIXME shouldn't we check that? if (!module_get_debug(pcs, module)) */
736 if (pe_load_export_debug_info(pcs
, module
) && !ret
)
738 if (!ret
) module
->module
.SymType
= SymNone
;
742 struct builtin_search
745 struct image_file_map fmap
;
748 static BOOL
search_builtin_pe(void *param
, HANDLE handle
, const WCHAR
*path
)
750 struct builtin_search
*search
= param
;
752 if (!pe_map_file(handle
, &search
->fmap
)) return FALSE
;
754 search
->path
= wcsdup(path
);
758 /******************************************************************
759 * pe_load_native_module
762 struct module
* pe_load_native_module(struct process
* pcs
, const WCHAR
* name
,
763 HANDLE hFile
, DWORD64 base
, DWORD size
)
765 struct module
* module
= NULL
;
767 struct module_format
* modfmt
;
768 WCHAR loaded_name
[MAX_PATH
];
769 WCHAR
* real_path
= NULL
;
771 loaded_name
[0] = '\0';
776 if ((hFile
= FindExecutableImageExW(name
, pcs
->search_path
, loaded_name
, NULL
, NULL
)) == NULL
&&
777 (hFile
= FindExecutableImageExW(name
, L
".", loaded_name
, NULL
, NULL
)) == NULL
)
783 ULONG sz
= sizeof(OBJECT_NAME_INFORMATION
) + MAX_PATH
* sizeof(WCHAR
), needed
;
784 OBJECT_NAME_INFORMATION
*obj_name
;
787 obj_name
= RtlAllocateHeap(GetProcessHeap(), 0, sz
);
790 nts
= NtQueryObject(hFile
, ObjectNameInformation
, obj_name
, sz
, &needed
);
791 if (nts
== STATUS_BUFFER_OVERFLOW
)
794 obj_name
= RtlReAllocateHeap(GetProcessHeap(), 0, obj_name
, sz
);
795 nts
= NtQueryObject(hFile
, ObjectNameInformation
, obj_name
, sz
, &needed
);
799 obj_name
->Name
.Buffer
[obj_name
->Name
.Length
/ sizeof(WCHAR
)] = L
'\0';
800 real_path
= wcsdup(obj_name
->Name
.Buffer
);
802 RtlFreeHeap(GetProcessHeap(), 0, obj_name
);
804 if (name
) lstrcpyW(loaded_name
, name
);
807 if ((modfmt
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct module_format
) + sizeof(struct pe_module_info
))))
809 modfmt
->u
.pe_info
= (struct pe_module_info
*)(modfmt
+ 1);
810 if (pe_map_file(hFile
, &modfmt
->u
.pe_info
->fmap
))
812 struct builtin_search builtin
= { NULL
};
813 if (opened
&& modfmt
->u
.pe_info
->fmap
.u
.pe
.builtin
&&
814 search_dll_path(pcs
, loaded_name
, modfmt
->u
.pe_info
->fmap
.u
.pe
.file_header
.Machine
, search_builtin_pe
, &builtin
))
816 TRACE("reloaded %s from %s\n", debugstr_w(loaded_name
), debugstr_w(builtin
.path
));
817 image_unmap_file(&modfmt
->u
.pe_info
->fmap
);
818 modfmt
->u
.pe_info
->fmap
= builtin
.fmap
;
819 real_path
= builtin
.path
;
821 if (!base
) base
= PE_FROM_OPTHDR(&modfmt
->u
.pe_info
->fmap
, ImageBase
);
822 if (!size
) size
= PE_FROM_OPTHDR(&modfmt
->u
.pe_info
->fmap
, SizeOfImage
);
824 module
= module_new(pcs
, loaded_name
, DMT_PE
, modfmt
->u
.pe_info
->fmap
.u
.pe
.builtin
, FALSE
,
826 modfmt
->u
.pe_info
->fmap
.u
.pe
.file_header
.TimeDateStamp
,
827 PE_FROM_OPTHDR(&modfmt
->u
.pe_info
->fmap
, CheckSum
),
828 modfmt
->u
.pe_info
->fmap
.u
.pe
.file_header
.Machine
);
831 module
->real_path
= real_path
? pool_wcsdup(&module
->pool
, real_path
) : NULL
;
832 modfmt
->module
= module
;
833 modfmt
->remove
= pe_module_remove
;
834 modfmt
->loc_compute
= NULL
;
835 module
->format_info
[DFI_PE
] = modfmt
;
836 module
->reloc_delta
= base
- PE_FROM_OPTHDR(&modfmt
->u
.pe_info
->fmap
, ImageBase
);
840 ERR("could not load the module '%s'\n", debugstr_w(loaded_name
));
841 image_unmap_file(&modfmt
->u
.pe_info
->fmap
);
844 if (!module
) HeapFree(GetProcessHeap(), 0, modfmt
);
847 if (opened
) CloseHandle(hFile
);
852 /******************************************************************
856 BOOL
pe_load_nt_header(HANDLE hProc
, DWORD64 base
, IMAGE_NT_HEADERS
* nth
, BOOL
* is_builtin
)
858 IMAGE_DOS_HEADER dos
;
860 if (!ReadProcessMemory(hProc
, (char*)(DWORD_PTR
)base
, &dos
, sizeof(dos
), NULL
) ||
861 dos
.e_magic
!= IMAGE_DOS_SIGNATURE
||
862 !ReadProcessMemory(hProc
, (char*)(DWORD_PTR
)(base
+ dos
.e_lfanew
),
863 nth
, sizeof(*nth
), NULL
) ||
864 nth
->Signature
!= IMAGE_NT_SIGNATURE
)
868 if (dos
.e_lfanew
>= sizeof(dos
) + sizeof(builtin_signature
))
870 char sig
[sizeof(builtin_signature
)];
871 *is_builtin
= ReadProcessMemory(hProc
, (char*)(DWORD_PTR
)base
+ sizeof(dos
), sig
, sizeof(sig
), NULL
) &&
872 !memcmp(sig
, builtin_signature
, sizeof(builtin_signature
));
874 else *is_builtin
= FALSE
;
879 /******************************************************************
880 * pe_load_builtin_module
883 struct module
* pe_load_builtin_module(struct process
* pcs
, const WCHAR
* name
,
884 DWORD64 base
, DWORD64 size
)
886 struct module
* module
= NULL
;
888 if (base
&& pcs
->dbg_hdr_addr
)
890 IMAGE_NT_HEADERS nth
;
893 if (pe_load_nt_header(pcs
->handle
, base
, &nth
, &is_builtin
))
895 if (!size
) size
= nth
.OptionalHeader
.SizeOfImage
;
896 module
= module_new(pcs
, name
, DMT_PE
, is_builtin
, FALSE
, base
, size
,
897 nth
.FileHeader
.TimeDateStamp
,
898 nth
.OptionalHeader
.CheckSum
,
899 nth
.FileHeader
.Machine
);
905 /***********************************************************************
906 * ImageDirectoryEntryToDataEx (DBGHELP.@)
908 * Search for specified directory in PE image
912 * base [in] Image base address
913 * image [in] TRUE - image has been loaded by loader, FALSE - raw file image
914 * dir [in] Target directory index
915 * size [out] Receives directory size
916 * section [out] Receives pointer to section header of section containing directory data
919 * Success: pointer to directory data
923 PVOID WINAPI
ImageDirectoryEntryToDataEx( PVOID base
, BOOLEAN image
, USHORT dir
, PULONG size
, PIMAGE_SECTION_HEADER
*section
)
925 const IMAGE_NT_HEADERS
*nt
;
929 if (section
) *section
= NULL
;
931 if (!(nt
= RtlImageNtHeader( base
))) return NULL
;
932 if (nt
->OptionalHeader
.Magic
== IMAGE_NT_OPTIONAL_HDR64_MAGIC
)
934 const IMAGE_NT_HEADERS64
*nt64
= (const IMAGE_NT_HEADERS64
*)nt
;
936 if (dir
>= nt64
->OptionalHeader
.NumberOfRvaAndSizes
) return NULL
;
937 if (!(addr
= nt64
->OptionalHeader
.DataDirectory
[dir
].VirtualAddress
)) return NULL
;
938 *size
= nt64
->OptionalHeader
.DataDirectory
[dir
].Size
;
939 if (image
|| addr
< nt64
->OptionalHeader
.SizeOfHeaders
) return (char *)base
+ addr
;
941 else if (nt
->OptionalHeader
.Magic
== IMAGE_NT_OPTIONAL_HDR32_MAGIC
)
943 const IMAGE_NT_HEADERS32
*nt32
= (const IMAGE_NT_HEADERS32
*)nt
;
945 if (dir
>= nt32
->OptionalHeader
.NumberOfRvaAndSizes
) return NULL
;
946 if (!(addr
= nt32
->OptionalHeader
.DataDirectory
[dir
].VirtualAddress
)) return NULL
;
947 *size
= nt32
->OptionalHeader
.DataDirectory
[dir
].Size
;
948 if (image
|| addr
< nt32
->OptionalHeader
.SizeOfHeaders
) return (char *)base
+ addr
;
952 return RtlImageRvaToVa( nt
, base
, addr
, section
);
955 /***********************************************************************
956 * ImageDirectoryEntryToData (DBGHELP.@)
959 * See ImageDirectoryEntryToDataEx
961 PVOID WINAPI
ImageDirectoryEntryToData( PVOID base
, BOOLEAN image
, USHORT dir
, PULONG size
)
963 return ImageDirectoryEntryToDataEx( base
, image
, dir
, size
, NULL
);
966 DWORD
pe_get_file_indexinfo(void* image
, DWORD size
, SYMSRV_INDEX_INFOW
* info
)
968 const IMAGE_NT_HEADERS
* nthdr
;
969 const IMAGE_DEBUG_DIRECTORY
* dbg
;
972 if (!(nthdr
= RtlImageNtHeader(image
))) return ERROR_BAD_FORMAT
;
974 dbg
= RtlImageDirectoryEntryToData(image
, FALSE
, IMAGE_DIRECTORY_ENTRY_DEBUG
, &dirsize
);
975 if (!dbg
|| dirsize
< sizeof(dbg
)) return ERROR_BAD_EXE_FORMAT
;
977 /* fill in information from NT header */
978 info
->timestamp
= nthdr
->FileHeader
.TimeDateStamp
;
979 info
->stripped
= (nthdr
->FileHeader
.Characteristics
& IMAGE_FILE_DEBUG_STRIPPED
) != 0;
980 if (nthdr
->OptionalHeader
.Magic
== IMAGE_NT_OPTIONAL_HDR64_MAGIC
)
982 const IMAGE_NT_HEADERS64
* nthdr64
= (const IMAGE_NT_HEADERS64
*)nthdr
;
983 info
->size
= nthdr64
->OptionalHeader
.SizeOfImage
;
985 else if (nthdr
->OptionalHeader
.Magic
== IMAGE_NT_OPTIONAL_HDR32_MAGIC
)
987 const IMAGE_NT_HEADERS32
* nthdr32
= (const IMAGE_NT_HEADERS32
*)nthdr
;
988 info
->size
= nthdr32
->OptionalHeader
.SizeOfImage
;
990 return msc_get_file_indexinfo(image
, dbg
, dirsize
/ sizeof(*dbg
), info
);