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"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp
);
38 /******************************************************************
39 * pe_load_symbol_table
41 * Use the COFF symbol table (if any) from the IMAGE_FILE_HEADER to set the absolute address
43 * Mingw32 requires this for stabs debug information as address for global variables isn't filled in
44 * (this is similar to what is done in elf_module.c when using the .symtab ELF section)
46 static BOOL
pe_load_symbol_table(struct module
* module
, IMAGE_NT_HEADERS
* nth
, void* mapping
)
48 const IMAGE_SYMBOL
* isym
;
53 struct hash_table_iter hti
;
55 struct symt_data
* sym
;
56 const IMAGE_SECTION_HEADER
* sect
;
58 numsym
= nth
->FileHeader
.NumberOfSymbols
;
59 if (!nth
->FileHeader
.PointerToSymbolTable
|| !numsym
)
61 isym
= (const IMAGE_SYMBOL
*)((char*)mapping
+ nth
->FileHeader
.PointerToSymbolTable
);
62 /* FIXME: no way to get strtable size */
63 strtable
= (const char*)&isym
[numsym
];
64 sect
= IMAGE_FIRST_SECTION(nth
);
66 for (i
= 0; i
< numsym
; i
+= naux
, isym
+= naux
)
68 if (isym
->StorageClass
== IMAGE_SYM_CLASS_EXTERNAL
&&
69 isym
->SectionNumber
> 0 && isym
->SectionNumber
<= nth
->FileHeader
.NumberOfSections
)
71 if (isym
->N
.Name
.Short
)
73 name
= memcpy(tmp
, isym
->N
.ShortName
, 8);
76 else name
= strtable
+ isym
->N
.Name
.Long
;
77 if (name
[0] == '_') name
++;
78 hash_table_iter_init(&module
->ht_symbols
, &hti
, name
);
79 while ((ptr
= hash_table_iter_up(&hti
)))
81 sym
= GET_ENTRY(ptr
, struct symt_data
, hash_elt
);
82 if (sym
->symt
.tag
== SymTagData
&&
83 (sym
->kind
== DataIsGlobal
|| sym
->kind
== DataIsFileStatic
) &&
84 !strcmp(sym
->hash_elt
.name
, name
))
86 TRACE("Changing absolute address for %d.%s: %lx -> %s\n",
87 isym
->SectionNumber
, name
, sym
->u
.var
.offset
,
88 wine_dbgstr_longlong(module
->module
.BaseOfImage
+
89 sect
[isym
->SectionNumber
- 1].VirtualAddress
+ isym
->Value
));
90 sym
->u
.var
.offset
= module
->module
.BaseOfImage
+
91 sect
[isym
->SectionNumber
- 1].VirtualAddress
+ isym
->Value
;
96 naux
= isym
->NumberOfAuxSymbols
+ 1;
101 static inline void* pe_get_sect(IMAGE_NT_HEADERS
* nth
, void* mapping
,
102 IMAGE_SECTION_HEADER
* sect
)
104 return (sect
) ? RtlImageRvaToVa(nth
, mapping
, sect
->VirtualAddress
, NULL
) : NULL
;
107 static inline DWORD
pe_get_sect_size(IMAGE_SECTION_HEADER
* sect
)
109 return (sect
) ? sect
->SizeOfRawData
: 0;
112 /******************************************************************
115 * look for stabs information in PE header (it's how the mingw compiler provides
116 * its debugging information)
118 static BOOL
pe_load_stabs(const struct process
* pcs
, struct module
* module
,
119 void* mapping
, IMAGE_NT_HEADERS
* nth
)
121 IMAGE_SECTION_HEADER
* section
;
122 IMAGE_SECTION_HEADER
* sect_stabs
= NULL
;
123 IMAGE_SECTION_HEADER
* sect_stabstr
= NULL
;
127 section
= (IMAGE_SECTION_HEADER
*)
128 ((char*)&nth
->OptionalHeader
+ nth
->FileHeader
.SizeOfOptionalHeader
);
129 for (i
= 0; i
< nth
->FileHeader
.NumberOfSections
; i
++, section
++)
131 if (!strcasecmp((const char*)section
->Name
, ".stab")) sect_stabs
= section
;
132 else if (!strncasecmp((const char*)section
->Name
, ".stabstr", 8)) sect_stabstr
= section
;
134 if (sect_stabs
&& sect_stabstr
)
136 ret
= stabs_parse(module
,
137 module
->module
.BaseOfImage
- nth
->OptionalHeader
.ImageBase
,
138 pe_get_sect(nth
, mapping
, sect_stabs
), pe_get_sect_size(sect_stabs
),
139 pe_get_sect(nth
, mapping
, sect_stabstr
), pe_get_sect_size(sect_stabstr
),
141 if (ret
) pe_load_symbol_table(module
, nth
, mapping
);
143 TRACE("%s the STABS debug info\n", ret
? "successfully loaded" : "failed to load");
148 /******************************************************************
151 * look for dwarf information in PE header (it's also a way for the mingw compiler
152 * to provide its debugging information)
154 static BOOL
pe_load_dwarf(const struct process
* pcs
, struct module
* module
,
155 void* mapping
, IMAGE_NT_HEADERS
* nth
)
157 IMAGE_SECTION_HEADER
* section
;
158 IMAGE_SECTION_HEADER
* sect_debuginfo
= NULL
;
159 IMAGE_SECTION_HEADER
* sect_debugstr
= NULL
;
160 IMAGE_SECTION_HEADER
* sect_debugabbrev
= NULL
;
161 IMAGE_SECTION_HEADER
* sect_debugline
= NULL
;
162 IMAGE_SECTION_HEADER
* sect_debugloc
= NULL
;
164 const char* strtable
;
165 const char* sectname
;
168 if (nth
->FileHeader
.PointerToSymbolTable
&& nth
->FileHeader
.NumberOfSymbols
)
169 /* FIXME: no way to get strtable size */
170 strtable
= (const char*)mapping
+ nth
->FileHeader
.PointerToSymbolTable
+
171 nth
->FileHeader
.NumberOfSymbols
* sizeof(IMAGE_SYMBOL
);
172 else strtable
= NULL
;
174 section
= (IMAGE_SECTION_HEADER
*)
175 ((char*)&nth
->OptionalHeader
+ nth
->FileHeader
.SizeOfOptionalHeader
);
176 for (i
= 0; i
< nth
->FileHeader
.NumberOfSections
; i
++, section
++)
178 sectname
= (const char*)section
->Name
;
179 /* long section names start with a '/' (at least on MinGW32) */
180 if (*sectname
== '/' && strtable
)
181 sectname
= strtable
+ atoi(sectname
+ 1);
182 if (!strcasecmp(sectname
, ".debug_info")) sect_debuginfo
= section
;
183 else if (!strcasecmp(sectname
, ".debug_str")) sect_debugstr
= section
;
184 else if (!strcasecmp(sectname
, ".debug_abbrev")) sect_debugabbrev
= section
;
185 else if (!strcasecmp(sectname
, ".debug_line")) sect_debugline
= section
;
186 else if (!strcasecmp(sectname
, ".debug_loc")) sect_debugloc
= section
;
190 ret
= dwarf2_parse(module
,
191 module
->module
.BaseOfImage
- nth
->OptionalHeader
.ImageBase
,
192 NULL
, /* FIXME: some thunks to deal with ? */
193 pe_get_sect(nth
, mapping
, sect_debuginfo
), pe_get_sect_size(sect_debuginfo
),
194 pe_get_sect(nth
, mapping
, sect_debugabbrev
), pe_get_sect_size(sect_debugabbrev
),
195 pe_get_sect(nth
, mapping
, sect_debugstr
), pe_get_sect_size(sect_debugstr
),
196 pe_get_sect(nth
, mapping
, sect_debugline
), pe_get_sect_size(sect_debugline
),
197 pe_get_sect(nth
, mapping
, sect_debugloc
), pe_get_sect_size(sect_debugloc
));
199 TRACE("%s the DWARF debug info\n", ret
? "successfully loaded" : "failed to load");
204 /******************************************************************
209 static BOOL
pe_load_dbg_file(const struct process
* pcs
, struct module
* module
,
210 const char* dbg_name
, DWORD timestamp
)
213 HANDLE hFile
= INVALID_HANDLE_VALUE
, hMap
= 0;
214 const BYTE
* dbg_mapping
= NULL
;
217 TRACE("Processing DBG file %s\n", debugstr_a(dbg_name
));
219 if (path_find_symbol_file(pcs
, dbg_name
, NULL
, timestamp
, 0, tmp
, &module
->module
.DbgUnmatched
) &&
220 (hFile
= CreateFileA(tmp
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
221 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
)) != INVALID_HANDLE_VALUE
&&
222 ((hMap
= CreateFileMappingW(hFile
, NULL
, PAGE_READONLY
, 0, 0, NULL
)) != 0) &&
223 ((dbg_mapping
= MapViewOfFile(hMap
, FILE_MAP_READ
, 0, 0, 0)) != NULL
))
225 const IMAGE_SEPARATE_DEBUG_HEADER
* hdr
;
226 const IMAGE_SECTION_HEADER
* sectp
;
227 const IMAGE_DEBUG_DIRECTORY
* dbg
;
229 hdr
= (const IMAGE_SEPARATE_DEBUG_HEADER
*)dbg_mapping
;
230 /* section headers come immediately after debug header */
231 sectp
= (const IMAGE_SECTION_HEADER
*)(hdr
+ 1);
232 /* and after that and the exported names comes the debug directory */
233 dbg
= (const IMAGE_DEBUG_DIRECTORY
*)
234 (dbg_mapping
+ sizeof(*hdr
) +
235 hdr
->NumberOfSections
* sizeof(IMAGE_SECTION_HEADER
) +
236 hdr
->ExportedNamesSize
);
238 ret
= pe_load_debug_directory(pcs
, module
, dbg_mapping
, sectp
,
239 hdr
->NumberOfSections
, dbg
,
240 hdr
->DebugDirectorySize
/ sizeof(*dbg
));
243 ERR("Couldn't find .DBG file %s (%s)\n", debugstr_a(dbg_name
), debugstr_a(tmp
));
245 if (dbg_mapping
) UnmapViewOfFile(dbg_mapping
);
246 if (hMap
) CloseHandle(hMap
);
247 if (hFile
!= INVALID_HANDLE_VALUE
) CloseHandle(hFile
);
251 /******************************************************************
252 * pe_load_msc_debug_info
254 * Process MSC debug information in PE file.
256 static BOOL
pe_load_msc_debug_info(const struct process
* pcs
,
257 struct module
* module
,
258 void* mapping
, const IMAGE_NT_HEADERS
* nth
)
261 const IMAGE_DATA_DIRECTORY
* dir
;
262 const IMAGE_DEBUG_DIRECTORY
*dbg
= NULL
;
265 /* Read in debug directory */
266 dir
= nth
->OptionalHeader
.DataDirectory
+ IMAGE_DIRECTORY_ENTRY_DEBUG
;
267 nDbg
= dir
->Size
/ sizeof(IMAGE_DEBUG_DIRECTORY
);
268 if (!nDbg
) return FALSE
;
270 dbg
= RtlImageRvaToVa(nth
, mapping
, dir
->VirtualAddress
, NULL
);
272 /* Parse debug directory */
273 if (nth
->FileHeader
.Characteristics
& IMAGE_FILE_DEBUG_STRIPPED
)
275 /* Debug info is stripped to .DBG file */
276 const IMAGE_DEBUG_MISC
* misc
= (const IMAGE_DEBUG_MISC
*)
277 ((const char*)mapping
+ dbg
->PointerToRawData
);
279 if (nDbg
!= 1 || dbg
->Type
!= IMAGE_DEBUG_TYPE_MISC
||
280 misc
->DataType
!= IMAGE_DEBUG_MISC_EXENAME
)
282 WINE_ERR("-Debug info stripped, but no .DBG file in module %s\n",
283 debugstr_w(module
->module
.ModuleName
));
287 ret
= pe_load_dbg_file(pcs
, module
, (const char*)misc
->Data
, nth
->FileHeader
.TimeDateStamp
);
292 const IMAGE_SECTION_HEADER
*sectp
= (const IMAGE_SECTION_HEADER
*)((const char*)&nth
->OptionalHeader
+ nth
->FileHeader
.SizeOfOptionalHeader
);
293 /* Debug info is embedded into PE module */
294 ret
= pe_load_debug_directory(pcs
, module
, mapping
, sectp
,
295 nth
->FileHeader
.NumberOfSections
, dbg
, nDbg
);
301 /***********************************************************************
302 * pe_load_export_debug_info
304 static BOOL
pe_load_export_debug_info(const struct process
* pcs
,
305 struct module
* module
,
306 void* mapping
, const IMAGE_NT_HEADERS
* nth
)
309 const IMAGE_EXPORT_DIRECTORY
* exports
;
310 DWORD base
= module
->module
.BaseOfImage
;
313 if (dbghelp_options
& SYMOPT_NO_PUBLICS
) return TRUE
;
316 /* Add start of DLL (better use the (yet unimplemented) Exe SymTag for this) */
317 /* FIXME: module.ModuleName isn't correctly set yet if it's passed in SymLoadModule */
318 symt_new_public(module
, NULL
, module
->module
.ModuleName
, base
, 1,
319 TRUE
/* FIXME */, TRUE
/* FIXME */);
322 /* Add entry point */
323 symt_new_public(module
, NULL
, "EntryPoint",
324 base
+ nth
->OptionalHeader
.AddressOfEntryPoint
, 1,
327 /* FIXME: we'd better store addresses linked to sections rather than
329 IMAGE_SECTION_HEADER
* section
;
330 /* Add start of sections */
331 section
= (IMAGE_SECTION_HEADER
*)
332 ((char*)&nth
->OptionalHeader
+ nth
->FileHeader
.SizeOfOptionalHeader
);
333 for (i
= 0; i
< nth
->FileHeader
.NumberOfSections
; i
++, section
++)
335 symt_new_public(module
, NULL
, section
->Name
,
336 RtlImageRvaToVa(nth
, mapping
, section
->VirtualAddress
, NULL
),
337 1, TRUE
/* FIXME */, TRUE
/* FIXME */);
341 /* Add exported functions */
342 if ((exports
= RtlImageDirectoryEntryToData(mapping
, FALSE
,
343 IMAGE_DIRECTORY_ENTRY_EXPORT
, &size
)))
345 const WORD
* ordinals
= NULL
;
346 const DWORD_PTR
* functions
= NULL
;
347 const DWORD
* names
= NULL
;
351 functions
= RtlImageRvaToVa(nth
, mapping
, exports
->AddressOfFunctions
, NULL
);
352 ordinals
= RtlImageRvaToVa(nth
, mapping
, exports
->AddressOfNameOrdinals
, NULL
);
353 names
= RtlImageRvaToVa(nth
, mapping
, exports
->AddressOfNames
, NULL
);
355 if (functions
&& ordinals
&& names
)
357 for (i
= 0; i
< exports
->NumberOfNames
; i
++)
359 if (!names
[i
]) continue;
360 symt_new_public(module
, NULL
,
361 RtlImageRvaToVa(nth
, mapping
, names
[i
], NULL
),
362 base
+ functions
[ordinals
[i
]],
363 1, TRUE
/* FIXME */, TRUE
/* FIXME */);
366 for (i
= 0; i
< exports
->NumberOfFunctions
; i
++)
368 if (!functions
[i
]) continue;
369 /* Check if we already added it with a name */
370 for (j
= 0; j
< exports
->NumberOfNames
; j
++)
371 if ((ordinals
[j
] == i
) && names
[j
]) break;
372 if (j
< exports
->NumberOfNames
) continue;
373 snprintf(buffer
, sizeof(buffer
), "%d", i
+ exports
->Base
);
374 symt_new_public(module
, NULL
, buffer
, base
+ (DWORD
)functions
[i
], 1,
375 TRUE
/* FIXME */, TRUE
/* FIXME */);
379 /* no real debug info, only entry points */
380 if (module
->module
.SymType
== SymDeferred
)
381 module
->module
.SymType
= SymExport
;
385 /******************************************************************
389 BOOL
pe_load_debug_info(const struct process
* pcs
, struct module
* module
)
395 IMAGE_NT_HEADERS
* nth
;
397 hFile
= CreateFileW(module
->module
.LoadedImageName
, GENERIC_READ
, FILE_SHARE_READ
,
398 NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
399 if (hFile
== INVALID_HANDLE_VALUE
) return ret
;
400 if ((hMap
= CreateFileMappingW(hFile
, NULL
, PAGE_READONLY
, 0, 0, NULL
)) != 0)
402 if ((mapping
= MapViewOfFile(hMap
, FILE_MAP_READ
, 0, 0, 0)) != NULL
)
404 nth
= RtlImageNtHeader(mapping
);
406 if (!(dbghelp_options
& SYMOPT_PUBLICS_ONLY
))
408 ret
= pe_load_stabs(pcs
, module
, mapping
, nth
) ||
409 pe_load_dwarf(pcs
, module
, mapping
, nth
) ||
410 pe_load_msc_debug_info(pcs
, module
, mapping
, nth
);
411 /* if we still have no debug info (we could only get SymExport at this
412 * point), then do the SymExport except if we have an ELF container,
413 * in which case we'll rely on the export's on the ELF side
416 /* FIXME shouldn't we check that? if (!module_get_debug(pcs, module))l */
417 if (pe_load_export_debug_info(pcs
, module
, mapping
, nth
) && !ret
)
419 UnmapViewOfFile(mapping
);
428 /******************************************************************
429 * pe_load_native_module
432 struct module
* pe_load_native_module(struct process
* pcs
, const WCHAR
* name
,
433 HANDLE hFile
, DWORD base
, DWORD size
)
435 struct module
* module
= NULL
;
438 WCHAR loaded_name
[MAX_PATH
];
440 loaded_name
[0] = '\0';
446 if ((hFile
= FindExecutableImageExW(name
, pcs
->search_path
, loaded_name
, NULL
, NULL
)) == NULL
)
450 else if (name
) strcpyW(loaded_name
, name
);
451 else if (dbghelp_options
& SYMOPT_DEFERRED_LOADS
)
452 FIXME("Trouble ahead (no module name passed in deferred mode)\n");
454 if ((hMap
= CreateFileMappingW(hFile
, NULL
, PAGE_READONLY
, 0, 0, NULL
)) != NULL
)
458 if ((mapping
= MapViewOfFile(hMap
, FILE_MAP_READ
, 0, 0, 0)) != NULL
)
460 IMAGE_NT_HEADERS
* nth
= RtlImageNtHeader(mapping
);
464 if (!base
) base
= nth
->OptionalHeader
.ImageBase
;
465 if (!size
) size
= nth
->OptionalHeader
.SizeOfImage
;
467 module
= module_new(pcs
, loaded_name
, DMT_PE
, FALSE
, base
, size
,
468 nth
->FileHeader
.TimeDateStamp
,
469 nth
->OptionalHeader
.CheckSum
);
472 if (dbghelp_options
& SYMOPT_DEFERRED_LOADS
)
473 module
->module
.SymType
= SymDeferred
;
475 pe_load_debug_info(pcs
, module
);
478 ERR("could not load the module '%s'\n", debugstr_w(loaded_name
));
480 UnmapViewOfFile(mapping
);
484 if (opened
) CloseHandle(hFile
);
489 /******************************************************************
493 BOOL
pe_load_nt_header(HANDLE hProc
, DWORD64 base
, IMAGE_NT_HEADERS
* nth
)
495 IMAGE_DOS_HEADER dos
;
497 return ReadProcessMemory(hProc
, (char*)(DWORD_PTR
)base
, &dos
, sizeof(dos
), NULL
) &&
498 dos
.e_magic
== IMAGE_DOS_SIGNATURE
&&
499 ReadProcessMemory(hProc
, (char*)(DWORD_PTR
)(base
+ dos
.e_lfanew
),
500 nth
, sizeof(*nth
), NULL
) &&
501 nth
->Signature
== IMAGE_NT_SIGNATURE
;
504 /******************************************************************
505 * pe_load_builtin_module
508 struct module
* pe_load_builtin_module(struct process
* pcs
, const WCHAR
* name
,
509 DWORD64 base
, DWORD64 size
)
511 struct module
* module
= NULL
;
513 if (base
&& pcs
->dbg_hdr_addr
)
515 IMAGE_NT_HEADERS nth
;
517 if (pe_load_nt_header(pcs
->handle
, base
, &nth
))
519 if (!size
) size
= nth
.OptionalHeader
.SizeOfImage
;
520 module
= module_new(pcs
, name
, DMT_PE
, FALSE
, base
, size
,
521 nth
.FileHeader
.TimeDateStamp
,
522 nth
.OptionalHeader
.CheckSum
);
528 /***********************************************************************
529 * ImageDirectoryEntryToDataEx (DBGHELP.@)
531 * Search for specified directory in PE image
535 * base [in] Image base address
536 * image [in] TRUE - image has been loaded by loader, FALSE - raw file image
537 * dir [in] Target directory index
538 * size [out] Receives directory size
539 * section [out] Receives pointer to section header of section containing directory data
542 * Success: pointer to directory data
546 PVOID WINAPI
ImageDirectoryEntryToDataEx( PVOID base
, BOOLEAN image
, USHORT dir
, PULONG size
, PIMAGE_SECTION_HEADER
*section
)
548 const IMAGE_NT_HEADERS
*nt
;
552 if (section
) *section
= NULL
;
554 if (!(nt
= RtlImageNtHeader( base
))) return NULL
;
555 if (dir
>= nt
->OptionalHeader
.NumberOfRvaAndSizes
) return NULL
;
556 if (!(addr
= nt
->OptionalHeader
.DataDirectory
[dir
].VirtualAddress
)) return NULL
;
558 *size
= nt
->OptionalHeader
.DataDirectory
[dir
].Size
;
559 if (image
|| addr
< nt
->OptionalHeader
.SizeOfHeaders
) return (char *)base
+ addr
;
561 return RtlImageRvaToVa( nt
, base
, addr
, section
);
564 /***********************************************************************
565 * ImageDirectoryEntryToData (DBGHELP.@)
568 * See ImageDirectoryEntryToDataEx
570 PVOID WINAPI
ImageDirectoryEntryToData( PVOID base
, BOOLEAN image
, USHORT dir
, PULONG size
)
572 return ImageDirectoryEntryToDataEx( base
, image
, dir
, size
, NULL
);