2004-04-06 00:21:27 +02:00
|
|
|
/*
|
|
|
|
* File pe_module.c - handle PE module information
|
|
|
|
*
|
|
|
|
* Copyright (C) 1996, Eric Youngdale.
|
|
|
|
* Copyright (C) 1999-2000, Ulrich Weigand.
|
2007-02-21 21:55:04 +01:00
|
|
|
* Copyright (C) 2004-2007, Eric Pouech.
|
2004-04-06 00:21:27 +02:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2004-04-06 00:21:27 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
2008-02-29 12:24:58 +01:00
|
|
|
#include "wine/port.h"
|
|
|
|
|
2004-04-06 00:21:27 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include "dbghelp_private.h"
|
|
|
|
#include "winternl.h"
|
|
|
|
#include "wine/debug.h"
|
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
|
|
|
|
|
2009-12-09 20:55:27 +01:00
|
|
|
/******************************************************************
|
|
|
|
* pe_load_symbol_table
|
|
|
|
*
|
|
|
|
* Use the COFF symbol table (if any) from the IMAGE_FILE_HEADER to set the absolute address
|
|
|
|
* of global symbols.
|
|
|
|
* Mingw32 requires this for stabs debug information as address for global variables isn't filled in
|
|
|
|
* (this is similar to what is done in elf_module.c when using the .symtab ELF section)
|
|
|
|
*/
|
|
|
|
static BOOL pe_load_symbol_table(struct module* module, IMAGE_NT_HEADERS* nth, void* mapping)
|
|
|
|
{
|
|
|
|
const IMAGE_SYMBOL* isym;
|
|
|
|
int i, numsym, naux;
|
|
|
|
const char* strtable;
|
|
|
|
char tmp[9];
|
|
|
|
const char* name;
|
|
|
|
struct hash_table_iter hti;
|
|
|
|
void* ptr;
|
|
|
|
struct symt_data* sym;
|
|
|
|
const IMAGE_SECTION_HEADER* sect;
|
|
|
|
|
|
|
|
numsym = nth->FileHeader.NumberOfSymbols;
|
|
|
|
if (!nth->FileHeader.PointerToSymbolTable || !numsym)
|
|
|
|
return TRUE;
|
|
|
|
isym = (const IMAGE_SYMBOL*)((char*)mapping + nth->FileHeader.PointerToSymbolTable);
|
|
|
|
/* FIXME: no way to get strtable size */
|
|
|
|
strtable = (const char*)&isym[numsym];
|
|
|
|
sect = IMAGE_FIRST_SECTION(nth);
|
|
|
|
|
|
|
|
for (i = 0; i < numsym; i+= naux, isym += naux)
|
|
|
|
{
|
|
|
|
if (isym->StorageClass == IMAGE_SYM_CLASS_EXTERNAL &&
|
|
|
|
isym->SectionNumber > 0 && isym->SectionNumber <= nth->FileHeader.NumberOfSections)
|
|
|
|
{
|
|
|
|
if (isym->N.Name.Short)
|
|
|
|
{
|
|
|
|
name = memcpy(tmp, isym->N.ShortName, 8);
|
|
|
|
tmp[8] = '\0';
|
|
|
|
}
|
|
|
|
else name = strtable + isym->N.Name.Long;
|
|
|
|
if (name[0] == '_') name++;
|
|
|
|
hash_table_iter_init(&module->ht_symbols, &hti, name);
|
|
|
|
while ((ptr = hash_table_iter_up(&hti)))
|
|
|
|
{
|
|
|
|
sym = GET_ENTRY(ptr, struct symt_data, hash_elt);
|
|
|
|
if (sym->symt.tag == SymTagData &&
|
|
|
|
(sym->kind == DataIsGlobal || sym->kind == DataIsFileStatic) &&
|
|
|
|
!strcmp(sym->hash_elt.name, name))
|
|
|
|
{
|
|
|
|
TRACE("Changing absolute address for %d.%s: %lx -> %s\n",
|
|
|
|
isym->SectionNumber, name, sym->u.var.offset,
|
|
|
|
wine_dbgstr_longlong(module->module.BaseOfImage +
|
|
|
|
sect[isym->SectionNumber - 1].VirtualAddress + isym->Value));
|
|
|
|
sym->u.var.offset = module->module.BaseOfImage +
|
|
|
|
sect[isym->SectionNumber - 1].VirtualAddress + isym->Value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
naux = isym->NumberOfAuxSymbols + 1;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2009-12-23 22:05:25 +01:00
|
|
|
static inline void* pe_get_sect(IMAGE_NT_HEADERS* nth, void* mapping,
|
|
|
|
IMAGE_SECTION_HEADER* sect)
|
|
|
|
{
|
|
|
|
return (sect) ? RtlImageRvaToVa(nth, mapping, sect->VirtualAddress, NULL) : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline DWORD pe_get_sect_size(IMAGE_SECTION_HEADER* sect)
|
|
|
|
{
|
|
|
|
return (sect) ? sect->SizeOfRawData : 0;
|
|
|
|
}
|
|
|
|
|
2004-04-06 00:21:27 +02:00
|
|
|
/******************************************************************
|
|
|
|
* pe_load_stabs
|
|
|
|
*
|
|
|
|
* look for stabs information in PE header (it's how the mingw compiler provides
|
|
|
|
* its debugging information)
|
|
|
|
*/
|
2004-08-30 21:31:13 +02:00
|
|
|
static BOOL pe_load_stabs(const struct process* pcs, struct module* module,
|
2008-01-17 21:57:12 +01:00
|
|
|
void* mapping, IMAGE_NT_HEADERS* nth)
|
2004-04-06 00:21:27 +02:00
|
|
|
{
|
|
|
|
IMAGE_SECTION_HEADER* section;
|
2009-12-23 22:05:25 +01:00
|
|
|
IMAGE_SECTION_HEADER* sect_stabs = NULL;
|
|
|
|
IMAGE_SECTION_HEADER* sect_stabstr = NULL;
|
|
|
|
int i;
|
|
|
|
BOOL ret;
|
2004-04-06 00:21:27 +02:00
|
|
|
|
|
|
|
section = (IMAGE_SECTION_HEADER*)
|
|
|
|
((char*)&nth->OptionalHeader + nth->FileHeader.SizeOfOptionalHeader);
|
|
|
|
for (i = 0; i < nth->FileHeader.NumberOfSections; i++, section++)
|
|
|
|
{
|
2009-12-23 22:05:25 +01:00
|
|
|
if (!strcasecmp((const char*)section->Name, ".stab")) sect_stabs = section;
|
|
|
|
else if (!strncasecmp((const char*)section->Name, ".stabstr", 8)) sect_stabstr = section;
|
2004-04-06 00:21:27 +02:00
|
|
|
}
|
2009-12-23 22:05:25 +01:00
|
|
|
if (sect_stabs && sect_stabstr)
|
2004-04-06 00:21:27 +02:00
|
|
|
{
|
2009-12-23 22:05:25 +01:00
|
|
|
ret = stabs_parse(module,
|
|
|
|
module->module.BaseOfImage - nth->OptionalHeader.ImageBase,
|
|
|
|
pe_get_sect(nth, mapping, sect_stabs), pe_get_sect_size(sect_stabs),
|
|
|
|
pe_get_sect(nth, mapping, sect_stabstr), pe_get_sect_size(sect_stabstr),
|
2009-03-11 15:05:49 +01:00
|
|
|
NULL, NULL);
|
2009-12-09 20:55:27 +01:00
|
|
|
if (ret) pe_load_symbol_table(module, nth, mapping);
|
2004-04-06 00:21:27 +02:00
|
|
|
}
|
2008-01-17 21:57:12 +01:00
|
|
|
TRACE("%s the STABS debug info\n", ret ? "successfully loaded" : "failed to load");
|
2009-12-23 22:05:25 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************
|
|
|
|
* pe_load_dwarf
|
|
|
|
*
|
|
|
|
* look for dwarf information in PE header (it's also a way for the mingw compiler
|
|
|
|
* to provide its debugging information)
|
|
|
|
*/
|
|
|
|
static BOOL pe_load_dwarf(const struct process* pcs, struct module* module,
|
|
|
|
void* mapping, IMAGE_NT_HEADERS* nth)
|
|
|
|
{
|
|
|
|
IMAGE_SECTION_HEADER* section;
|
|
|
|
IMAGE_SECTION_HEADER* sect_debuginfo = NULL;
|
|
|
|
IMAGE_SECTION_HEADER* sect_debugstr = NULL;
|
|
|
|
IMAGE_SECTION_HEADER* sect_debugabbrev = NULL;
|
|
|
|
IMAGE_SECTION_HEADER* sect_debugline = NULL;
|
|
|
|
IMAGE_SECTION_HEADER* sect_debugloc = NULL;
|
|
|
|
int i;
|
|
|
|
const char* strtable;
|
|
|
|
const char* sectname;
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
if (nth->FileHeader.PointerToSymbolTable && nth->FileHeader.NumberOfSymbols)
|
|
|
|
/* FIXME: no way to get strtable size */
|
|
|
|
strtable = (const char*)mapping + nth->FileHeader.PointerToSymbolTable +
|
|
|
|
nth->FileHeader.NumberOfSymbols * sizeof(IMAGE_SYMBOL);
|
|
|
|
else strtable = NULL;
|
|
|
|
|
|
|
|
section = (IMAGE_SECTION_HEADER*)
|
|
|
|
((char*)&nth->OptionalHeader + nth->FileHeader.SizeOfOptionalHeader);
|
|
|
|
for (i = 0; i < nth->FileHeader.NumberOfSections; i++, section++)
|
|
|
|
{
|
|
|
|
sectname = (const char*)section->Name;
|
|
|
|
/* long section names start with a '/' (at least on MinGW32) */
|
|
|
|
if (*sectname == '/' && strtable)
|
|
|
|
sectname = strtable + atoi(sectname + 1);
|
|
|
|
if (!strcasecmp(sectname, ".debug_info")) sect_debuginfo = section;
|
|
|
|
else if (!strcasecmp(sectname, ".debug_str")) sect_debugstr = section;
|
|
|
|
else if (!strcasecmp(sectname, ".debug_abbrev")) sect_debugabbrev = section;
|
|
|
|
else if (!strcasecmp(sectname, ".debug_line")) sect_debugline = section;
|
|
|
|
else if (!strcasecmp(sectname, ".debug_loc")) sect_debugloc = section;
|
|
|
|
}
|
|
|
|
if (sect_debuginfo)
|
|
|
|
{
|
|
|
|
ret = dwarf2_parse(module,
|
|
|
|
module->module.BaseOfImage - nth->OptionalHeader.ImageBase,
|
|
|
|
NULL, /* FIXME: some thunks to deal with ? */
|
|
|
|
pe_get_sect(nth, mapping, sect_debuginfo), pe_get_sect_size(sect_debuginfo),
|
|
|
|
pe_get_sect(nth, mapping, sect_debugabbrev), pe_get_sect_size(sect_debugabbrev),
|
|
|
|
pe_get_sect(nth, mapping, sect_debugstr), pe_get_sect_size(sect_debugstr),
|
|
|
|
pe_get_sect(nth, mapping, sect_debugline), pe_get_sect_size(sect_debugline),
|
|
|
|
pe_get_sect(nth, mapping, sect_debugloc), pe_get_sect_size(sect_debugloc));
|
|
|
|
}
|
|
|
|
TRACE("%s the DWARF debug info\n", ret ? "successfully loaded" : "failed to load");
|
|
|
|
|
2004-08-30 21:31:13 +02:00
|
|
|
return ret;
|
2004-04-06 00:21:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************
|
|
|
|
* pe_load_dbg_file
|
|
|
|
*
|
|
|
|
* loads a .dbg file
|
|
|
|
*/
|
2004-08-30 21:31:13 +02:00
|
|
|
static BOOL pe_load_dbg_file(const struct process* pcs, struct module* module,
|
|
|
|
const char* dbg_name, DWORD timestamp)
|
2004-04-06 00:21:27 +02:00
|
|
|
{
|
2004-05-18 23:29:09 +02:00
|
|
|
char tmp[MAX_PATH];
|
2004-06-14 19:58:31 +02:00
|
|
|
HANDLE hFile = INVALID_HANDLE_VALUE, hMap = 0;
|
2004-05-18 23:29:09 +02:00
|
|
|
const BYTE* dbg_mapping = NULL;
|
2004-08-30 21:31:13 +02:00
|
|
|
BOOL ret = FALSE;
|
2004-04-06 00:21:27 +02:00
|
|
|
|
2008-08-29 21:50:53 +02:00
|
|
|
TRACE("Processing DBG file %s\n", debugstr_a(dbg_name));
|
2004-04-06 00:21:27 +02:00
|
|
|
|
2008-10-18 09:15:03 +02:00
|
|
|
if (path_find_symbol_file(pcs, dbg_name, NULL, timestamp, 0, tmp, &module->module.DbgUnmatched) &&
|
2007-02-21 21:55:15 +01:00
|
|
|
(hFile = CreateFileA(tmp, GENERIC_READ, FILE_SHARE_READ, NULL,
|
2004-06-14 19:58:31 +02:00
|
|
|
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) != INVALID_HANDLE_VALUE &&
|
2007-02-21 21:55:15 +01:00
|
|
|
((hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) != 0) &&
|
2004-04-06 00:21:27 +02:00
|
|
|
((dbg_mapping = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) != NULL))
|
|
|
|
{
|
2008-08-29 21:50:53 +02:00
|
|
|
const IMAGE_SEPARATE_DEBUG_HEADER* hdr;
|
|
|
|
const IMAGE_SECTION_HEADER* sectp;
|
|
|
|
const IMAGE_DEBUG_DIRECTORY* dbg;
|
2004-10-11 22:08:07 +02:00
|
|
|
|
2008-08-29 21:50:53 +02:00
|
|
|
hdr = (const IMAGE_SEPARATE_DEBUG_HEADER*)dbg_mapping;
|
|
|
|
/* section headers come immediately after debug header */
|
|
|
|
sectp = (const IMAGE_SECTION_HEADER*)(hdr + 1);
|
|
|
|
/* and after that and the exported names comes the debug directory */
|
|
|
|
dbg = (const IMAGE_DEBUG_DIRECTORY*)
|
|
|
|
(dbg_mapping + sizeof(*hdr) +
|
|
|
|
hdr->NumberOfSections * sizeof(IMAGE_SECTION_HEADER) +
|
|
|
|
hdr->ExportedNamesSize);
|
|
|
|
|
|
|
|
ret = pe_load_debug_directory(pcs, module, dbg_mapping, sectp,
|
|
|
|
hdr->NumberOfSections, dbg,
|
|
|
|
hdr->DebugDirectorySize / sizeof(*dbg));
|
2004-04-06 00:21:27 +02:00
|
|
|
}
|
|
|
|
else
|
2008-08-29 21:50:53 +02:00
|
|
|
ERR("Couldn't find .DBG file %s (%s)\n", debugstr_a(dbg_name), debugstr_a(tmp));
|
2004-05-24 21:08:19 +02:00
|
|
|
|
2006-09-06 18:55:40 +02:00
|
|
|
if (dbg_mapping) UnmapViewOfFile(dbg_mapping);
|
2004-04-06 00:21:27 +02:00
|
|
|
if (hMap) CloseHandle(hMap);
|
2007-03-08 15:28:40 +01:00
|
|
|
if (hFile != INVALID_HANDLE_VALUE) CloseHandle(hFile);
|
2004-08-30 21:31:13 +02:00
|
|
|
return ret;
|
2004-04-06 00:21:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************
|
|
|
|
* pe_load_msc_debug_info
|
|
|
|
*
|
|
|
|
* Process MSC debug information in PE file.
|
|
|
|
*/
|
2004-08-30 21:31:13 +02:00
|
|
|
static BOOL pe_load_msc_debug_info(const struct process* pcs,
|
|
|
|
struct module* module,
|
2009-07-13 23:12:01 +02:00
|
|
|
void* mapping, const IMAGE_NT_HEADERS* nth)
|
2004-04-06 00:21:27 +02:00
|
|
|
{
|
2004-08-30 21:31:13 +02:00
|
|
|
BOOL ret = FALSE;
|
2004-05-18 23:29:09 +02:00
|
|
|
const IMAGE_DATA_DIRECTORY* dir;
|
|
|
|
const IMAGE_DEBUG_DIRECTORY*dbg = NULL;
|
|
|
|
int nDbg;
|
2004-04-06 00:21:27 +02:00
|
|
|
|
|
|
|
/* Read in debug directory */
|
|
|
|
dir = nth->OptionalHeader.DataDirectory + IMAGE_DIRECTORY_ENTRY_DEBUG;
|
|
|
|
nDbg = dir->Size / sizeof(IMAGE_DEBUG_DIRECTORY);
|
2004-08-30 21:31:13 +02:00
|
|
|
if (!nDbg) return FALSE;
|
2004-04-06 00:21:27 +02:00
|
|
|
|
2008-01-17 21:57:12 +01:00
|
|
|
dbg = RtlImageRvaToVa(nth, mapping, dir->VirtualAddress, NULL);
|
2004-04-06 00:21:27 +02:00
|
|
|
|
|
|
|
/* Parse debug directory */
|
|
|
|
if (nth->FileHeader.Characteristics & IMAGE_FILE_DEBUG_STRIPPED)
|
|
|
|
{
|
|
|
|
/* Debug info is stripped to .DBG file */
|
2004-05-18 23:29:09 +02:00
|
|
|
const IMAGE_DEBUG_MISC* misc = (const IMAGE_DEBUG_MISC*)
|
|
|
|
((const char*)mapping + dbg->PointerToRawData);
|
2004-04-06 00:21:27 +02:00
|
|
|
|
|
|
|
if (nDbg != 1 || dbg->Type != IMAGE_DEBUG_TYPE_MISC ||
|
|
|
|
misc->DataType != IMAGE_DEBUG_MISC_EXENAME)
|
|
|
|
{
|
|
|
|
WINE_ERR("-Debug info stripped, but no .DBG file in module %s\n",
|
2007-03-13 17:26:33 +01:00
|
|
|
debugstr_w(module->module.ModuleName));
|
2004-04-06 00:21:27 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-09-02 16:47:36 +02:00
|
|
|
ret = pe_load_dbg_file(pcs, module, (const char*)misc->Data, nth->FileHeader.TimeDateStamp);
|
2004-04-06 00:21:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-10-11 22:08:07 +02:00
|
|
|
const IMAGE_SECTION_HEADER *sectp = (const IMAGE_SECTION_HEADER*)((const char*)&nth->OptionalHeader + nth->FileHeader.SizeOfOptionalHeader);
|
2004-04-06 00:21:27 +02:00
|
|
|
/* Debug info is embedded into PE module */
|
2004-10-11 22:08:07 +02:00
|
|
|
ret = pe_load_debug_directory(pcs, module, mapping, sectp,
|
|
|
|
nth->FileHeader.NumberOfSections, dbg, nDbg);
|
2004-04-06 00:21:27 +02:00
|
|
|
}
|
|
|
|
|
2004-08-30 21:31:13 +02:00
|
|
|
return ret;
|
2004-04-06 00:21:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* pe_load_export_debug_info
|
|
|
|
*/
|
2004-08-30 21:31:13 +02:00
|
|
|
static BOOL pe_load_export_debug_info(const struct process* pcs,
|
|
|
|
struct module* module,
|
2009-07-13 23:12:01 +02:00
|
|
|
void* mapping, const IMAGE_NT_HEADERS* nth)
|
2004-04-06 00:21:27 +02:00
|
|
|
{
|
2004-08-30 21:31:13 +02:00
|
|
|
unsigned int i;
|
|
|
|
const IMAGE_EXPORT_DIRECTORY* exports;
|
|
|
|
DWORD base = module->module.BaseOfImage;
|
|
|
|
DWORD size;
|
2004-04-06 00:21:27 +02:00
|
|
|
|
2004-08-30 21:31:13 +02:00
|
|
|
if (dbghelp_options & SYMOPT_NO_PUBLICS) return TRUE;
|
2004-04-06 00:21:27 +02:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
/* Add start of DLL (better use the (yet unimplemented) Exe SymTag for this) */
|
|
|
|
/* FIXME: module.ModuleName isn't correctly set yet if it's passed in SymLoadModule */
|
2006-02-14 14:04:16 +01:00
|
|
|
symt_new_public(module, NULL, module->module.ModuleName, base, 1,
|
2004-04-06 00:21:27 +02:00
|
|
|
TRUE /* FIXME */, TRUE /* FIXME */);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Add entry point */
|
2004-05-18 23:29:09 +02:00
|
|
|
symt_new_public(module, NULL, "EntryPoint",
|
2006-02-14 14:04:16 +01:00
|
|
|
base + nth->OptionalHeader.AddressOfEntryPoint, 1,
|
2004-08-30 21:31:13 +02:00
|
|
|
TRUE, TRUE);
|
2004-04-06 00:21:27 +02:00
|
|
|
#if 0
|
2004-05-18 23:29:09 +02:00
|
|
|
/* FIXME: we'd better store addresses linked to sections rather than
|
|
|
|
absolute values */
|
2004-04-06 00:21:27 +02:00
|
|
|
IMAGE_SECTION_HEADER* section;
|
|
|
|
/* Add start of sections */
|
|
|
|
section = (IMAGE_SECTION_HEADER*)
|
|
|
|
((char*)&nth->OptionalHeader + nth->FileHeader.SizeOfOptionalHeader);
|
|
|
|
for (i = 0; i < nth->FileHeader.NumberOfSections; i++, section++)
|
|
|
|
{
|
2004-08-30 21:31:13 +02:00
|
|
|
symt_new_public(module, NULL, section->Name,
|
2008-01-17 21:57:12 +01:00
|
|
|
RtlImageRvaToVa(nth, mapping, section->VirtualAddress, NULL),
|
2006-02-14 14:04:16 +01:00
|
|
|
1, TRUE /* FIXME */, TRUE /* FIXME */);
|
2004-04-06 00:21:27 +02:00
|
|
|
}
|
|
|
|
#endif
|
2004-08-30 21:31:13 +02:00
|
|
|
|
2004-04-06 00:21:27 +02:00
|
|
|
/* Add exported functions */
|
2008-01-17 21:57:12 +01:00
|
|
|
if ((exports = RtlImageDirectoryEntryToData(mapping, FALSE,
|
2004-08-30 21:31:13 +02:00
|
|
|
IMAGE_DIRECTORY_ENTRY_EXPORT, &size)))
|
2004-04-06 00:21:27 +02:00
|
|
|
{
|
2004-08-30 21:31:13 +02:00
|
|
|
const WORD* ordinals = NULL;
|
|
|
|
const DWORD_PTR* functions = NULL;
|
|
|
|
const DWORD* names = NULL;
|
|
|
|
unsigned int j;
|
|
|
|
char buffer[16];
|
|
|
|
|
2008-01-17 21:57:12 +01:00
|
|
|
functions = RtlImageRvaToVa(nth, mapping, exports->AddressOfFunctions, NULL);
|
|
|
|
ordinals = RtlImageRvaToVa(nth, mapping, exports->AddressOfNameOrdinals, NULL);
|
|
|
|
names = RtlImageRvaToVa(nth, mapping, exports->AddressOfNames, NULL);
|
2004-08-30 21:31:13 +02:00
|
|
|
|
2007-10-08 19:41:37 +02:00
|
|
|
if (functions && ordinals && names)
|
2004-04-06 00:21:27 +02:00
|
|
|
{
|
2007-10-08 19:41:37 +02:00
|
|
|
for (i = 0; i < exports->NumberOfNames; i++)
|
|
|
|
{
|
|
|
|
if (!names[i]) continue;
|
|
|
|
symt_new_public(module, NULL,
|
2008-01-17 21:57:12 +01:00
|
|
|
RtlImageRvaToVa(nth, mapping, names[i], NULL),
|
2007-10-08 19:41:37 +02:00
|
|
|
base + functions[ordinals[i]],
|
|
|
|
1, TRUE /* FIXME */, TRUE /* FIXME */);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < exports->NumberOfFunctions; i++)
|
|
|
|
{
|
|
|
|
if (!functions[i]) continue;
|
|
|
|
/* Check if we already added it with a name */
|
|
|
|
for (j = 0; j < exports->NumberOfNames; j++)
|
|
|
|
if ((ordinals[j] == i) && names[j]) break;
|
|
|
|
if (j < exports->NumberOfNames) continue;
|
|
|
|
snprintf(buffer, sizeof(buffer), "%d", i + exports->Base);
|
|
|
|
symt_new_public(module, NULL, buffer, base + (DWORD)functions[i], 1,
|
|
|
|
TRUE /* FIXME */, TRUE /* FIXME */);
|
|
|
|
}
|
2004-04-06 00:21:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* no real debug info, only entry points */
|
2004-11-08 21:26:22 +01:00
|
|
|
if (module->module.SymType == SymDeferred)
|
|
|
|
module->module.SymType = SymExport;
|
2004-08-30 21:31:13 +02:00
|
|
|
return TRUE;
|
2004-04-06 00:21:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************
|
|
|
|
* pe_load_debug_info
|
|
|
|
*
|
|
|
|
*/
|
2004-08-30 21:31:13 +02:00
|
|
|
BOOL pe_load_debug_info(const struct process* pcs, struct module* module)
|
2004-04-06 00:21:27 +02:00
|
|
|
{
|
2004-08-30 21:31:13 +02:00
|
|
|
BOOL ret = FALSE;
|
2004-04-06 00:21:27 +02:00
|
|
|
HANDLE hFile;
|
|
|
|
HANDLE hMap;
|
|
|
|
void* mapping;
|
|
|
|
IMAGE_NT_HEADERS* nth;
|
|
|
|
|
2007-02-21 21:55:04 +01:00
|
|
|
hFile = CreateFileW(module->module.LoadedImageName, GENERIC_READ, FILE_SHARE_READ,
|
2004-04-06 00:21:27 +02:00
|
|
|
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
2004-08-30 21:31:13 +02:00
|
|
|
if (hFile == INVALID_HANDLE_VALUE) return ret;
|
2007-02-21 21:55:04 +01:00
|
|
|
if ((hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) != 0)
|
2004-04-06 00:21:27 +02:00
|
|
|
{
|
|
|
|
if ((mapping = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) != NULL)
|
|
|
|
{
|
|
|
|
nth = RtlImageNtHeader(mapping);
|
|
|
|
|
|
|
|
if (!(dbghelp_options & SYMOPT_PUBLICS_ONLY))
|
|
|
|
{
|
2004-08-30 21:31:13 +02:00
|
|
|
ret = pe_load_stabs(pcs, module, mapping, nth) ||
|
2009-12-23 22:05:25 +01:00
|
|
|
pe_load_dwarf(pcs, module, mapping, nth) ||
|
2004-08-30 21:31:13 +02:00
|
|
|
pe_load_msc_debug_info(pcs, module, mapping, nth);
|
2004-04-06 00:21:27 +02:00
|
|
|
/* if we still have no debug info (we could only get SymExport at this
|
|
|
|
* point), then do the SymExport except if we have an ELF container,
|
|
|
|
* in which case we'll rely on the export's on the ELF side
|
|
|
|
*/
|
|
|
|
}
|
2004-12-06 12:41:32 +01:00
|
|
|
/* FIXME shouldn't we check that? if (!module_get_debug(pcs, module))l */
|
2004-08-30 21:31:13 +02:00
|
|
|
if (pe_load_export_debug_info(pcs, module, mapping, nth) && !ret)
|
|
|
|
ret = TRUE;
|
2004-04-06 00:21:27 +02:00
|
|
|
UnmapViewOfFile(mapping);
|
|
|
|
}
|
|
|
|
CloseHandle(hMap);
|
|
|
|
}
|
|
|
|
CloseHandle(hFile);
|
|
|
|
|
2004-08-30 21:31:13 +02:00
|
|
|
return ret;
|
2004-04-06 00:21:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************
|
2007-03-13 17:33:08 +01:00
|
|
|
* pe_load_native_module
|
2004-04-06 00:21:27 +02:00
|
|
|
*
|
|
|
|
*/
|
2007-03-13 17:33:08 +01:00
|
|
|
struct module* pe_load_native_module(struct process* pcs, const WCHAR* name,
|
|
|
|
HANDLE hFile, DWORD base, DWORD size)
|
2004-04-06 00:21:27 +02:00
|
|
|
{
|
|
|
|
struct module* module = NULL;
|
|
|
|
BOOL opened = FALSE;
|
|
|
|
HANDLE hMap;
|
2007-02-21 21:55:15 +01:00
|
|
|
WCHAR loaded_name[MAX_PATH];
|
2004-04-06 00:21:27 +02:00
|
|
|
|
|
|
|
loaded_name[0] = '\0';
|
|
|
|
if (!hFile)
|
|
|
|
{
|
2007-03-13 17:33:02 +01:00
|
|
|
|
|
|
|
assert(name);
|
2006-05-10 21:35:07 +02:00
|
|
|
|
2007-02-21 21:55:15 +01:00
|
|
|
if ((hFile = FindExecutableImageExW(name, pcs->search_path, loaded_name, NULL, NULL)) == NULL)
|
2004-04-06 00:21:27 +02:00
|
|
|
return NULL;
|
|
|
|
opened = TRUE;
|
|
|
|
}
|
2007-02-21 21:55:15 +01:00
|
|
|
else if (name) strcpyW(loaded_name, name);
|
2004-04-06 00:21:27 +02:00
|
|
|
else if (dbghelp_options & SYMOPT_DEFERRED_LOADS)
|
|
|
|
FIXME("Trouble ahead (no module name passed in deferred mode)\n");
|
2007-03-13 17:33:02 +01:00
|
|
|
|
|
|
|
if ((hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) != NULL)
|
2004-04-06 00:21:27 +02:00
|
|
|
{
|
2007-03-13 17:33:02 +01:00
|
|
|
void* mapping;
|
|
|
|
|
2004-04-06 00:21:27 +02:00
|
|
|
if ((mapping = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) != NULL)
|
|
|
|
{
|
|
|
|
IMAGE_NT_HEADERS* nth = RtlImageNtHeader(mapping);
|
|
|
|
|
|
|
|
if (nth)
|
|
|
|
{
|
|
|
|
if (!base) base = nth->OptionalHeader.ImageBase;
|
|
|
|
if (!size) size = nth->OptionalHeader.SizeOfImage;
|
2007-02-21 21:55:04 +01:00
|
|
|
|
2007-02-21 21:55:15 +01:00
|
|
|
module = module_new(pcs, loaded_name, DMT_PE, FALSE, base, size,
|
|
|
|
nth->FileHeader.TimeDateStamp,
|
|
|
|
nth->OptionalHeader.CheckSum);
|
2004-04-06 00:21:27 +02:00
|
|
|
if (module)
|
|
|
|
{
|
2004-08-30 21:31:13 +02:00
|
|
|
if (dbghelp_options & SYMOPT_DEFERRED_LOADS)
|
|
|
|
module->module.SymType = SymDeferred;
|
|
|
|
else
|
|
|
|
pe_load_debug_info(pcs, module);
|
2004-04-06 00:21:27 +02:00
|
|
|
}
|
2008-01-17 21:57:12 +01:00
|
|
|
else
|
|
|
|
ERR("could not load the module '%s'\n", debugstr_w(loaded_name));
|
2004-04-06 00:21:27 +02:00
|
|
|
}
|
|
|
|
UnmapViewOfFile(mapping);
|
|
|
|
}
|
|
|
|
CloseHandle(hMap);
|
|
|
|
}
|
|
|
|
if (opened) CloseHandle(hFile);
|
|
|
|
|
|
|
|
return module;
|
|
|
|
}
|
|
|
|
|
2005-03-29 15:14:08 +02:00
|
|
|
/******************************************************************
|
|
|
|
* pe_load_nt_header
|
|
|
|
*
|
|
|
|
*/
|
2009-12-12 12:18:28 +01:00
|
|
|
BOOL pe_load_nt_header(HANDLE hProc, DWORD64 base, IMAGE_NT_HEADERS* nth)
|
2005-03-29 15:14:08 +02:00
|
|
|
{
|
|
|
|
IMAGE_DOS_HEADER dos;
|
|
|
|
|
2009-12-12 12:18:28 +01:00
|
|
|
return ReadProcessMemory(hProc, (char*)(DWORD_PTR)base, &dos, sizeof(dos), NULL) &&
|
2005-03-29 15:14:08 +02:00
|
|
|
dos.e_magic == IMAGE_DOS_SIGNATURE &&
|
2009-12-12 12:18:28 +01:00
|
|
|
ReadProcessMemory(hProc, (char*)(DWORD_PTR)(base + dos.e_lfanew),
|
2005-03-29 15:14:08 +02:00
|
|
|
nth, sizeof(*nth), NULL) &&
|
|
|
|
nth->Signature == IMAGE_NT_SIGNATURE;
|
|
|
|
}
|
|
|
|
|
2004-04-06 00:21:27 +02:00
|
|
|
/******************************************************************
|
2007-03-13 17:33:08 +01:00
|
|
|
* pe_load_builtin_module
|
2004-04-06 00:21:27 +02:00
|
|
|
*
|
|
|
|
*/
|
2007-03-13 17:33:08 +01:00
|
|
|
struct module* pe_load_builtin_module(struct process* pcs, const WCHAR* name,
|
2009-12-12 12:18:56 +01:00
|
|
|
DWORD64 base, DWORD64 size)
|
2004-04-06 00:21:27 +02:00
|
|
|
{
|
2007-03-13 17:33:02 +01:00
|
|
|
struct module* module = NULL;
|
2004-04-06 00:21:27 +02:00
|
|
|
|
2007-03-13 17:33:02 +01:00
|
|
|
if (base && pcs->dbg_hdr_addr)
|
2004-04-06 00:21:27 +02:00
|
|
|
{
|
2007-03-13 17:33:02 +01:00
|
|
|
IMAGE_NT_HEADERS nth;
|
2005-03-01 11:39:49 +01:00
|
|
|
|
2007-03-13 17:33:02 +01:00
|
|
|
if (pe_load_nt_header(pcs->handle, base, &nth))
|
|
|
|
{
|
|
|
|
if (!size) size = nth.OptionalHeader.SizeOfImage;
|
2007-02-21 21:55:15 +01:00
|
|
|
module = module_new(pcs, name, DMT_PE, FALSE, base, size,
|
2007-03-13 17:33:02 +01:00
|
|
|
nth.FileHeader.TimeDateStamp,
|
|
|
|
nth.OptionalHeader.CheckSum);
|
|
|
|
}
|
2004-04-06 00:21:27 +02:00
|
|
|
}
|
|
|
|
return module;
|
|
|
|
}
|
2008-01-11 13:42:17 +01:00
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* ImageDirectoryEntryToDataEx (DBGHELP.@)
|
|
|
|
*
|
|
|
|
* Search for specified directory in PE image
|
|
|
|
*
|
|
|
|
* PARAMS
|
|
|
|
*
|
|
|
|
* base [in] Image base address
|
|
|
|
* image [in] TRUE - image has been loaded by loader, FALSE - raw file image
|
|
|
|
* dir [in] Target directory index
|
|
|
|
* size [out] Receives directory size
|
|
|
|
* section [out] Receives pointer to section header of section containing directory data
|
|
|
|
*
|
|
|
|
* RETURNS
|
|
|
|
* Success: pointer to directory data
|
|
|
|
* Failure: NULL
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
PVOID WINAPI ImageDirectoryEntryToDataEx( PVOID base, BOOLEAN image, USHORT dir, PULONG size, PIMAGE_SECTION_HEADER *section )
|
|
|
|
{
|
|
|
|
const IMAGE_NT_HEADERS *nt;
|
|
|
|
DWORD addr;
|
|
|
|
|
|
|
|
*size = 0;
|
2008-07-18 11:16:02 +02:00
|
|
|
if (section) *section = NULL;
|
2008-01-11 13:42:17 +01:00
|
|
|
|
|
|
|
if (!(nt = RtlImageNtHeader( base ))) return NULL;
|
|
|
|
if (dir >= nt->OptionalHeader.NumberOfRvaAndSizes) return NULL;
|
|
|
|
if (!(addr = nt->OptionalHeader.DataDirectory[dir].VirtualAddress)) return NULL;
|
|
|
|
|
|
|
|
*size = nt->OptionalHeader.DataDirectory[dir].Size;
|
2008-07-18 11:16:02 +02:00
|
|
|
if (image || addr < nt->OptionalHeader.SizeOfHeaders) return (char *)base + addr;
|
2008-01-11 13:42:17 +01:00
|
|
|
|
2009-01-19 10:24:06 +01:00
|
|
|
return RtlImageRvaToVa( nt, base, addr, section );
|
2008-01-11 13:42:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* ImageDirectoryEntryToData (DBGHELP.@)
|
|
|
|
*
|
|
|
|
* NOTES
|
|
|
|
* See ImageDirectoryEntryToDataEx
|
|
|
|
*/
|
|
|
|
PVOID WINAPI ImageDirectoryEntryToData( PVOID base, BOOLEAN image, USHORT dir, PULONG size )
|
|
|
|
{
|
|
|
|
return ImageDirectoryEntryToDataEx( base, image, dir, size, NULL );
|
|
|
|
}
|