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 <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include "dbghelp_private.h"
|
2010-03-18 21:30:55 +01:00
|
|
|
#include "image_private.h"
|
2004-04-06 00:21:27 +02:00
|
|
|
#include "winternl.h"
|
|
|
|
#include "wine/debug.h"
|
2020-03-10 14:54:31 +01:00
|
|
|
#include "wine/heap.h"
|
2004-04-06 00:21:27 +02:00
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
|
|
|
|
|
2010-03-18 21:31:13 +01:00
|
|
|
struct pe_module_info
|
|
|
|
{
|
|
|
|
struct image_file_map fmap;
|
|
|
|
};
|
|
|
|
|
2020-03-10 14:54:31 +01:00
|
|
|
static const char builtin_signature[] = "Wine builtin DLL";
|
|
|
|
|
2010-03-18 21:31:03 +01:00
|
|
|
static void* pe_map_full(struct image_file_map* fmap, IMAGE_NT_HEADERS** nth)
|
2010-03-18 21:30:55 +01:00
|
|
|
{
|
2010-03-18 21:31:03 +01:00
|
|
|
if (!fmap->u.pe.full_map)
|
2010-03-18 21:30:55 +01:00
|
|
|
{
|
2010-03-18 21:31:03 +01:00
|
|
|
fmap->u.pe.full_map = MapViewOfFile(fmap->u.pe.hMap, FILE_MAP_READ, 0, 0, 0);
|
2010-03-18 21:30:55 +01:00
|
|
|
}
|
2010-03-18 21:31:03 +01:00
|
|
|
if (fmap->u.pe.full_map)
|
2010-03-18 21:30:55 +01:00
|
|
|
{
|
2010-03-18 21:31:03 +01:00
|
|
|
if (nth) *nth = RtlImageNtHeader(fmap->u.pe.full_map);
|
|
|
|
fmap->u.pe.full_count++;
|
|
|
|
return fmap->u.pe.full_map;
|
2010-03-18 21:30:55 +01:00
|
|
|
}
|
2019-07-16 05:41:08 +02:00
|
|
|
return NULL;
|
2010-03-18 21:30:55 +01:00
|
|
|
}
|
|
|
|
|
2010-03-18 21:31:03 +01:00
|
|
|
static void pe_unmap_full(struct image_file_map* fmap)
|
2010-03-18 21:30:55 +01:00
|
|
|
{
|
2010-03-18 21:31:03 +01:00
|
|
|
if (fmap->u.pe.full_count && !--fmap->u.pe.full_count)
|
2010-03-18 21:30:55 +01:00
|
|
|
{
|
2010-03-18 21:31:03 +01:00
|
|
|
UnmapViewOfFile(fmap->u.pe.full_map);
|
|
|
|
fmap->u.pe.full_map = NULL;
|
2010-03-18 21:30:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-31 13:20:33 +02:00
|
|
|
/* as we store either IMAGE_OPTIONAL_HEADER(32|64) inside pe_file_map,
|
|
|
|
* this helper will read to any field 'field' inside such an header
|
|
|
|
*/
|
|
|
|
#define PE_FROM_OPTHDR(fmap, field) (((fmap)->addr_size == 32) ? ((fmap)->u.pe.opt.header32. field) : ((fmap)->u.pe.opt.header64. field))
|
|
|
|
|
2010-03-18 21:30:55 +01:00
|
|
|
/******************************************************************
|
|
|
|
* pe_map_section
|
|
|
|
*
|
|
|
|
* Maps a single section into memory from an PE file
|
|
|
|
*/
|
2020-03-16 16:05:39 +01:00
|
|
|
static const char* pe_map_section(struct image_section_map* ism)
|
2010-03-18 21:30:55 +01:00
|
|
|
{
|
|
|
|
void* mapping;
|
2010-03-18 21:31:03 +01:00
|
|
|
struct pe_file_map* fmap = &ism->fmap->u.pe;
|
2010-03-18 21:30:55 +01:00
|
|
|
|
2021-08-31 13:20:26 +02:00
|
|
|
if (ism->sidx >= 0 && ism->sidx < fmap->file_header.NumberOfSections &&
|
2010-03-18 21:31:03 +01:00
|
|
|
fmap->sect[ism->sidx].mapped == IMAGE_NO_MAP)
|
2010-03-18 21:30:55 +01:00
|
|
|
{
|
|
|
|
IMAGE_NT_HEADERS* nth;
|
2010-08-28 09:09:24 +02:00
|
|
|
|
|
|
|
if (fmap->sect[ism->sidx].shdr.Misc.VirtualSize > fmap->sect[ism->sidx].shdr.SizeOfRawData)
|
|
|
|
{
|
|
|
|
FIXME("Section %ld: virtual (0x%x) > raw (0x%x) size - not supported\n",
|
|
|
|
ism->sidx, fmap->sect[ism->sidx].shdr.Misc.VirtualSize,
|
|
|
|
fmap->sect[ism->sidx].shdr.SizeOfRawData);
|
|
|
|
return IMAGE_NO_MAP;
|
|
|
|
}
|
2010-03-18 21:30:55 +01:00
|
|
|
/* FIXME: that's rather drastic, but that will do for now
|
2011-07-30 12:03:08 +02:00
|
|
|
* that's ok if the full file map exists, but we could be less aggressive otherwise and
|
2010-03-18 21:30:55 +01:00
|
|
|
* only map the relevant section
|
|
|
|
*/
|
2010-03-18 21:31:03 +01:00
|
|
|
if ((mapping = pe_map_full(ism->fmap, &nth)))
|
2010-03-18 21:30:55 +01:00
|
|
|
{
|
2010-03-18 21:31:03 +01:00
|
|
|
fmap->sect[ism->sidx].mapped = RtlImageRvaToVa(nth, mapping,
|
|
|
|
fmap->sect[ism->sidx].shdr.VirtualAddress,
|
|
|
|
NULL);
|
|
|
|
return fmap->sect[ism->sidx].mapped;
|
2010-03-18 21:30:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return IMAGE_NO_MAP;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************
|
|
|
|
* pe_find_section
|
|
|
|
*
|
|
|
|
* Finds a section by name (and type) into memory from an PE file
|
|
|
|
* or its alternate if any
|
|
|
|
*/
|
2020-03-16 16:05:39 +01:00
|
|
|
static BOOL pe_find_section(struct image_file_map* fmap, const char* name,
|
|
|
|
struct image_section_map* ism)
|
2010-03-18 21:30:55 +01:00
|
|
|
{
|
|
|
|
const char* sectname;
|
|
|
|
unsigned i;
|
|
|
|
char tmp[IMAGE_SIZEOF_SHORT_NAME + 1];
|
|
|
|
|
2021-08-31 13:20:26 +02:00
|
|
|
for (i = 0; i < fmap->u.pe.file_header.NumberOfSections; i++)
|
2010-03-18 21:30:55 +01:00
|
|
|
{
|
2010-03-18 21:31:03 +01:00
|
|
|
sectname = (const char*)fmap->u.pe.sect[i].shdr.Name;
|
2010-03-18 21:30:55 +01:00
|
|
|
/* long section names start with a '/' (at least on MinGW32) */
|
2010-03-18 21:31:03 +01:00
|
|
|
if (sectname[0] == '/' && fmap->u.pe.strtable)
|
|
|
|
sectname = fmap->u.pe.strtable + atoi(sectname + 1);
|
2010-03-18 21:30:55 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* the section name may not be null terminated */
|
|
|
|
sectname = memcpy(tmp, sectname, IMAGE_SIZEOF_SHORT_NAME);
|
|
|
|
tmp[IMAGE_SIZEOF_SHORT_NAME] = '\0';
|
|
|
|
}
|
2020-04-07 14:25:07 +02:00
|
|
|
if (!stricmp(sectname, name))
|
2010-03-18 21:30:55 +01:00
|
|
|
{
|
2010-03-18 21:31:03 +01:00
|
|
|
ism->fmap = fmap;
|
|
|
|
ism->sidx = i;
|
2010-03-18 21:30:55 +01:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
2010-03-18 21:31:03 +01:00
|
|
|
ism->fmap = NULL;
|
|
|
|
ism->sidx = -1;
|
|
|
|
|
2010-03-18 21:30:55 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************
|
|
|
|
* pe_unmap_section
|
|
|
|
*
|
|
|
|
* Unmaps a single section from memory
|
|
|
|
*/
|
2020-03-16 16:05:39 +01:00
|
|
|
static void pe_unmap_section(struct image_section_map* ism)
|
2010-03-18 21:30:55 +01:00
|
|
|
{
|
2021-08-31 13:20:26 +02:00
|
|
|
if (ism->sidx >= 0 && ism->sidx < ism->fmap->u.pe.file_header.NumberOfSections &&
|
2010-03-18 21:31:03 +01:00
|
|
|
ism->fmap->u.pe.sect[ism->sidx].mapped != IMAGE_NO_MAP)
|
2010-03-18 21:30:55 +01:00
|
|
|
{
|
2010-03-18 21:31:03 +01:00
|
|
|
pe_unmap_full(ism->fmap);
|
|
|
|
ism->fmap->u.pe.sect[ism->sidx].mapped = IMAGE_NO_MAP;
|
2010-03-18 21:30:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-18 21:31:19 +01:00
|
|
|
/******************************************************************
|
|
|
|
* pe_get_map_rva
|
|
|
|
*
|
|
|
|
* Get the RVA of an PE section
|
|
|
|
*/
|
2020-03-16 16:05:39 +01:00
|
|
|
static DWORD_PTR pe_get_map_rva(const struct image_section_map* ism)
|
2010-03-18 21:31:19 +01:00
|
|
|
{
|
2021-08-31 13:20:26 +02:00
|
|
|
if (ism->sidx < 0 || ism->sidx >= ism->fmap->u.pe.file_header.NumberOfSections)
|
2010-03-18 21:31:19 +01:00
|
|
|
return 0;
|
|
|
|
return ism->fmap->u.pe.sect[ism->sidx].shdr.VirtualAddress;
|
|
|
|
}
|
|
|
|
|
2010-03-18 21:30:55 +01:00
|
|
|
/******************************************************************
|
|
|
|
* pe_get_map_size
|
|
|
|
*
|
2010-08-28 09:09:24 +02:00
|
|
|
* Get the size of a PE section
|
2010-03-18 21:30:55 +01:00
|
|
|
*/
|
2020-03-16 16:05:39 +01:00
|
|
|
static unsigned pe_get_map_size(const struct image_section_map* ism)
|
2010-03-18 21:30:55 +01:00
|
|
|
{
|
2021-08-31 13:20:26 +02:00
|
|
|
if (ism->sidx < 0 || ism->sidx >= ism->fmap->u.pe.file_header.NumberOfSections)
|
2010-03-18 21:30:55 +01:00
|
|
|
return 0;
|
2010-08-28 09:09:24 +02:00
|
|
|
return ism->fmap->u.pe.sect[ism->sidx].shdr.Misc.VirtualSize;
|
2010-03-18 21:30:55 +01:00
|
|
|
}
|
|
|
|
|
2020-03-16 16:05:54 +01:00
|
|
|
/******************************************************************
|
|
|
|
* pe_unmap_file
|
|
|
|
*
|
|
|
|
* Unmaps an PE file from memory (previously mapped with pe_map_file)
|
|
|
|
*/
|
|
|
|
static void pe_unmap_file(struct image_file_map* fmap)
|
|
|
|
{
|
|
|
|
if (fmap->u.pe.hMap != 0)
|
|
|
|
{
|
|
|
|
struct image_section_map ism;
|
|
|
|
ism.fmap = fmap;
|
2021-08-31 13:20:26 +02:00
|
|
|
for (ism.sidx = 0; ism.sidx < fmap->u.pe.file_header.NumberOfSections; ism.sidx++)
|
2020-03-16 16:05:54 +01:00
|
|
|
{
|
|
|
|
pe_unmap_section(&ism);
|
|
|
|
}
|
|
|
|
while (fmap->u.pe.full_count) pe_unmap_full(fmap);
|
|
|
|
HeapFree(GetProcessHeap(), 0, fmap->u.pe.sect);
|
|
|
|
HeapFree(GetProcessHeap(), 0, (void*)fmap->u.pe.strtable); /* FIXME ugly (see pe_map_file) */
|
|
|
|
CloseHandle(fmap->u.pe.hMap);
|
|
|
|
fmap->u.pe.hMap = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-16 16:05:39 +01:00
|
|
|
static const struct image_file_map_ops pe_file_map_ops =
|
|
|
|
{
|
|
|
|
pe_map_section,
|
|
|
|
pe_unmap_section,
|
|
|
|
pe_find_section,
|
|
|
|
pe_get_map_rva,
|
|
|
|
pe_get_map_size,
|
2020-03-16 16:05:54 +01:00
|
|
|
pe_unmap_file,
|
2020-03-16 16:05:39 +01:00
|
|
|
};
|
|
|
|
|
2010-05-08 21:47:16 +02:00
|
|
|
/******************************************************************
|
|
|
|
* pe_is_valid_pointer_table
|
|
|
|
*
|
|
|
|
* Checks whether the PointerToSymbolTable and NumberOfSymbols in file_header contain
|
|
|
|
* valid information.
|
|
|
|
*/
|
2010-06-26 22:54:01 +02:00
|
|
|
static BOOL pe_is_valid_pointer_table(const IMAGE_NT_HEADERS* nthdr, const void* mapping, DWORD64 sz)
|
2010-05-08 21:47:16 +02:00
|
|
|
{
|
|
|
|
DWORD64 offset;
|
|
|
|
|
2010-06-26 22:54:01 +02:00
|
|
|
/* is the iSym table inside file size ? (including first DWORD of string table, which is its size) */
|
2010-05-08 21:47:16 +02:00
|
|
|
offset = (DWORD64)nthdr->FileHeader.PointerToSymbolTable;
|
2010-06-26 22:54:07 +02:00
|
|
|
offset += (DWORD64)nthdr->FileHeader.NumberOfSymbols * sizeof(IMAGE_SYMBOL);
|
|
|
|
if (offset + sizeof(DWORD) > sz) return FALSE;
|
2010-06-26 22:54:01 +02:00
|
|
|
/* is string table (following iSym table) inside file size ? */
|
2010-05-08 21:47:16 +02:00
|
|
|
offset += *(DWORD*)((const char*)mapping + offset);
|
2010-06-26 22:54:01 +02:00
|
|
|
return offset <= sz;
|
2010-05-08 21:47:16 +02:00
|
|
|
}
|
|
|
|
|
2010-03-18 21:30:55 +01:00
|
|
|
/******************************************************************
|
|
|
|
* pe_map_file
|
|
|
|
*
|
|
|
|
* Maps an PE file into memory (and checks it's a real PE file)
|
|
|
|
*/
|
2020-03-16 16:05:39 +01:00
|
|
|
BOOL pe_map_file(HANDLE file, struct image_file_map* fmap, enum module_type mt)
|
2010-03-18 21:30:55 +01:00
|
|
|
{
|
|
|
|
void* mapping;
|
|
|
|
|
2010-03-18 21:31:03 +01:00
|
|
|
fmap->modtype = mt;
|
2020-03-16 16:05:39 +01:00
|
|
|
fmap->ops = &pe_file_map_ops;
|
2020-03-16 16:05:23 +01:00
|
|
|
fmap->alternate = NULL;
|
2010-03-18 21:31:03 +01:00
|
|
|
fmap->u.pe.hMap = CreateFileMappingW(file, NULL, PAGE_READONLY, 0, 0, NULL);
|
|
|
|
if (fmap->u.pe.hMap == 0) return FALSE;
|
|
|
|
fmap->u.pe.full_count = 0;
|
|
|
|
fmap->u.pe.full_map = NULL;
|
2010-03-18 21:30:55 +01:00
|
|
|
if (!(mapping = pe_map_full(fmap, NULL))) goto error;
|
|
|
|
|
|
|
|
switch (mt)
|
|
|
|
{
|
|
|
|
case DMT_PE:
|
|
|
|
{
|
|
|
|
IMAGE_NT_HEADERS* nthdr;
|
|
|
|
IMAGE_SECTION_HEADER* section;
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
if (!(nthdr = RtlImageNtHeader(mapping))) goto error;
|
2021-08-31 13:20:26 +02:00
|
|
|
memcpy(&fmap->u.pe.file_header, &nthdr->FileHeader, sizeof(fmap->u.pe.file_header));
|
2012-02-13 21:41:34 +01:00
|
|
|
switch (nthdr->OptionalHeader.Magic)
|
|
|
|
{
|
2021-08-31 13:20:33 +02:00
|
|
|
case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
|
|
|
|
if (sizeof(void*) == 8 && !(SymGetOptions() & SYMOPT_INCLUDE_32BIT_MODULES))
|
|
|
|
{
|
|
|
|
TRACE("Won't load 32bit module in 64bit dbghelp when options don't ask for it\n");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
fmap->addr_size = 32;
|
|
|
|
memcpy(&fmap->u.pe.opt.header32, &nthdr->OptionalHeader, sizeof(fmap->u.pe.opt.header32));
|
|
|
|
break;
|
|
|
|
case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
|
|
|
|
if (sizeof(void*) == 4) return FALSE;
|
|
|
|
fmap->addr_size = 64;
|
|
|
|
memcpy(&fmap->u.pe.opt.header64, &nthdr->OptionalHeader, sizeof(fmap->u.pe.opt.header64));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return FALSE;
|
2012-02-13 21:41:34 +01:00
|
|
|
}
|
2020-03-10 14:54:31 +01:00
|
|
|
|
|
|
|
fmap->u.pe.builtin = !memcmp((const IMAGE_DOS_HEADER*)mapping + 1, builtin_signature, sizeof(builtin_signature));
|
2010-03-18 21:30:55 +01:00
|
|
|
section = (IMAGE_SECTION_HEADER*)
|
|
|
|
((char*)&nthdr->OptionalHeader + nthdr->FileHeader.SizeOfOptionalHeader);
|
2010-03-18 21:31:03 +01:00
|
|
|
fmap->u.pe.sect = HeapAlloc(GetProcessHeap(), 0,
|
|
|
|
nthdr->FileHeader.NumberOfSections * sizeof(fmap->u.pe.sect[0]));
|
|
|
|
if (!fmap->u.pe.sect) goto error;
|
2010-03-18 21:30:55 +01:00
|
|
|
for (i = 0; i < nthdr->FileHeader.NumberOfSections; i++)
|
|
|
|
{
|
2010-03-18 21:31:03 +01:00
|
|
|
memcpy(&fmap->u.pe.sect[i].shdr, section + i, sizeof(IMAGE_SECTION_HEADER));
|
|
|
|
fmap->u.pe.sect[i].mapped = IMAGE_NO_MAP;
|
2010-03-18 21:30:55 +01:00
|
|
|
}
|
|
|
|
if (nthdr->FileHeader.PointerToSymbolTable && nthdr->FileHeader.NumberOfSymbols)
|
|
|
|
{
|
2010-06-26 22:54:01 +02:00
|
|
|
LARGE_INTEGER li;
|
|
|
|
|
|
|
|
if (GetFileSizeEx(file, &li) && pe_is_valid_pointer_table(nthdr, mapping, li.QuadPart))
|
2010-05-08 21:47:16 +02:00
|
|
|
{
|
|
|
|
/* FIXME ugly: should rather map the relevant content instead of copying it */
|
|
|
|
const char* src = (const char*)mapping +
|
|
|
|
nthdr->FileHeader.PointerToSymbolTable +
|
|
|
|
nthdr->FileHeader.NumberOfSymbols * sizeof(IMAGE_SYMBOL);
|
|
|
|
char* dst;
|
|
|
|
DWORD sz = *(DWORD*)src;
|
|
|
|
|
|
|
|
if ((dst = HeapAlloc(GetProcessHeap(), 0, sz)))
|
|
|
|
memcpy(dst, src, sz);
|
|
|
|
fmap->u.pe.strtable = dst;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-06-26 22:54:01 +02:00
|
|
|
WARN("Bad coff table... wipping out\n");
|
2010-05-08 21:47:16 +02:00
|
|
|
/* we have bad information here, wipe it out */
|
2021-08-31 13:20:26 +02:00
|
|
|
fmap->u.pe.file_header.PointerToSymbolTable = 0;
|
|
|
|
fmap->u.pe.file_header.NumberOfSymbols = 0;
|
2010-05-08 21:47:16 +02:00
|
|
|
fmap->u.pe.strtable = NULL;
|
|
|
|
}
|
2010-03-18 21:30:55 +01:00
|
|
|
}
|
2010-03-18 21:31:03 +01:00
|
|
|
else fmap->u.pe.strtable = NULL;
|
2010-03-18 21:30:55 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default: assert(0); goto error;
|
|
|
|
}
|
|
|
|
pe_unmap_full(fmap);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
error:
|
|
|
|
pe_unmap_full(fmap);
|
2010-03-18 21:31:03 +01:00
|
|
|
CloseHandle(fmap->u.pe.hMap);
|
2010-03-18 21:30:55 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2010-03-20 09:47:18 +01:00
|
|
|
/******************************************************************
|
|
|
|
* pe_map_directory
|
|
|
|
*
|
|
|
|
* Maps a directory content out of a PE file
|
|
|
|
*/
|
|
|
|
const char* pe_map_directory(struct module* module, int dirno, DWORD* size)
|
|
|
|
{
|
|
|
|
IMAGE_NT_HEADERS* nth;
|
|
|
|
void* mapping;
|
|
|
|
|
|
|
|
if (module->type != DMT_PE || !module->format_info[DFI_PE]) return NULL;
|
|
|
|
if (dirno >= IMAGE_NUMBEROF_DIRECTORY_ENTRIES ||
|
|
|
|
!(mapping = pe_map_full(&module->format_info[DFI_PE]->u.pe_info->fmap, &nth)))
|
|
|
|
return NULL;
|
|
|
|
if (size) *size = nth->OptionalHeader.DataDirectory[dirno].Size;
|
|
|
|
return RtlImageRvaToVa(nth, mapping,
|
|
|
|
nth->OptionalHeader.DataDirectory[dirno].VirtualAddress, NULL);
|
|
|
|
}
|
|
|
|
|
2010-03-18 21:31:50 +01:00
|
|
|
static void pe_module_remove(struct process* pcs, struct module_format* modfmt)
|
2010-03-18 21:31:13 +01:00
|
|
|
{
|
2020-03-16 16:05:54 +01:00
|
|
|
image_unmap_file(&modfmt->u.pe_info->fmap);
|
2010-03-18 21:31:50 +01:00
|
|
|
HeapFree(GetProcessHeap(), 0, modfmt);
|
2010-03-18 21:31:13 +01:00
|
|
|
}
|
|
|
|
|
2009-12-09 20:55:27 +01:00
|
|
|
/******************************************************************
|
2010-01-23 22:13:25 +01:00
|
|
|
* pe_locate_with_coff_symbol_table
|
2009-12-09 20:55:27 +01:00
|
|
|
*
|
|
|
|
* 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)
|
|
|
|
*/
|
2010-03-18 21:31:13 +01:00
|
|
|
static BOOL pe_locate_with_coff_symbol_table(struct module* module)
|
2009-12-09 20:55:27 +01:00
|
|
|
{
|
2010-03-18 21:31:50 +01:00
|
|
|
struct image_file_map* fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
|
2009-12-09 20:55:27 +01:00
|
|
|
const IMAGE_SYMBOL* isym;
|
|
|
|
int i, numsym, naux;
|
|
|
|
char tmp[9];
|
|
|
|
const char* name;
|
|
|
|
struct hash_table_iter hti;
|
|
|
|
void* ptr;
|
|
|
|
struct symt_data* sym;
|
2010-03-18 21:30:55 +01:00
|
|
|
const char* mapping;
|
2009-12-09 20:55:27 +01:00
|
|
|
|
2021-08-31 13:20:26 +02:00
|
|
|
numsym = fmap->u.pe.file_header.NumberOfSymbols;
|
|
|
|
if (!fmap->u.pe.file_header.PointerToSymbolTable || !numsym)
|
2009-12-09 20:55:27 +01:00
|
|
|
return TRUE;
|
2010-03-18 21:30:55 +01:00
|
|
|
if (!(mapping = pe_map_full(fmap, NULL))) return FALSE;
|
2021-08-31 13:20:26 +02:00
|
|
|
isym = (const IMAGE_SYMBOL*)(mapping + fmap->u.pe.file_header.PointerToSymbolTable);
|
2009-12-09 20:55:27 +01:00
|
|
|
|
|
|
|
for (i = 0; i < numsym; i+= naux, isym += naux)
|
|
|
|
{
|
|
|
|
if (isym->StorageClass == IMAGE_SYM_CLASS_EXTERNAL &&
|
2021-08-31 13:20:26 +02:00
|
|
|
isym->SectionNumber > 0 && isym->SectionNumber <= fmap->u.pe.file_header.NumberOfSections)
|
2009-12-09 20:55:27 +01:00
|
|
|
{
|
|
|
|
if (isym->N.Name.Short)
|
|
|
|
{
|
|
|
|
name = memcpy(tmp, isym->N.ShortName, 8);
|
|
|
|
tmp[8] = '\0';
|
|
|
|
}
|
2010-03-18 21:31:03 +01:00
|
|
|
else name = fmap->u.pe.strtable + isym->N.Name.Long;
|
2009-12-09 20:55:27 +01:00
|
|
|
if (name[0] == '_') name++;
|
|
|
|
hash_table_iter_init(&module->ht_symbols, &hti, name);
|
|
|
|
while ((ptr = hash_table_iter_up(&hti)))
|
|
|
|
{
|
2016-03-08 11:44:26 +01:00
|
|
|
sym = CONTAINING_RECORD(ptr, struct symt_data, hash_elt);
|
2009-12-09 20:55:27 +01:00
|
|
|
if (sym->symt.tag == SymTagData &&
|
|
|
|
(sym->kind == DataIsGlobal || sym->kind == DataIsFileStatic) &&
|
2011-01-29 20:38:04 +01:00
|
|
|
sym->u.var.kind == loc_absolute &&
|
2009-12-09 20:55:27 +01:00
|
|
|
!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 +
|
2010-03-18 21:31:03 +01:00
|
|
|
fmap->u.pe.sect[isym->SectionNumber - 1].shdr.VirtualAddress +
|
2010-03-18 21:30:55 +01:00
|
|
|
isym->Value));
|
2009-12-09 20:55:27 +01:00
|
|
|
sym->u.var.offset = module->module.BaseOfImage +
|
2010-03-18 21:31:03 +01:00
|
|
|
fmap->u.pe.sect[isym->SectionNumber - 1].shdr.VirtualAddress + isym->Value;
|
2009-12-09 20:55:27 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
naux = isym->NumberOfAuxSymbols + 1;
|
|
|
|
}
|
2010-03-18 21:30:55 +01:00
|
|
|
pe_unmap_full(fmap);
|
2009-12-09 20:55:27 +01:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2010-01-23 22:13:25 +01:00
|
|
|
/******************************************************************
|
|
|
|
* pe_load_coff_symbol_table
|
|
|
|
*
|
|
|
|
* Load public symbols out of the COFF symbol table (if any).
|
|
|
|
*/
|
2010-03-18 21:31:13 +01:00
|
|
|
static BOOL pe_load_coff_symbol_table(struct module* module)
|
2010-01-23 22:13:25 +01:00
|
|
|
{
|
2010-03-18 21:31:50 +01:00
|
|
|
struct image_file_map* fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
|
2010-01-23 22:13:25 +01:00
|
|
|
const IMAGE_SYMBOL* isym;
|
|
|
|
int i, numsym, naux;
|
|
|
|
const char* strtable;
|
|
|
|
char tmp[9];
|
|
|
|
const char* name;
|
|
|
|
const char* lastfilename = NULL;
|
|
|
|
struct symt_compiland* compiland = NULL;
|
|
|
|
const IMAGE_SECTION_HEADER* sect;
|
2010-03-18 21:30:55 +01:00
|
|
|
const char* mapping;
|
2010-01-23 22:13:25 +01:00
|
|
|
|
2021-08-31 13:20:26 +02:00
|
|
|
numsym = fmap->u.pe.file_header.NumberOfSymbols;
|
|
|
|
if (!fmap->u.pe.file_header.PointerToSymbolTable || !numsym)
|
2010-01-23 22:13:25 +01:00
|
|
|
return TRUE;
|
2010-03-18 21:30:55 +01:00
|
|
|
if (!(mapping = pe_map_full(fmap, NULL))) return FALSE;
|
2021-08-31 13:20:26 +02:00
|
|
|
isym = (const IMAGE_SYMBOL*)(mapping + fmap->u.pe.file_header.PointerToSymbolTable);
|
2010-01-23 22:13:25 +01:00
|
|
|
/* FIXME: no way to get strtable size */
|
|
|
|
strtable = (const char*)&isym[numsym];
|
2010-06-26 22:53:55 +02:00
|
|
|
sect = IMAGE_FIRST_SECTION(RtlImageNtHeader((HMODULE)mapping));
|
2010-01-23 22:13:25 +01:00
|
|
|
|
|
|
|
for (i = 0; i < numsym; i+= naux, isym += naux)
|
|
|
|
{
|
|
|
|
if (isym->StorageClass == IMAGE_SYM_CLASS_FILE)
|
|
|
|
{
|
|
|
|
lastfilename = (const char*)(isym + 1);
|
|
|
|
compiland = NULL;
|
|
|
|
}
|
|
|
|
if (isym->StorageClass == IMAGE_SYM_CLASS_EXTERNAL &&
|
2021-08-31 13:20:26 +02:00
|
|
|
isym->SectionNumber > 0 && isym->SectionNumber <= fmap->u.pe.file_header.NumberOfSections)
|
2010-01-23 22:13:25 +01:00
|
|
|
{
|
|
|
|
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++;
|
|
|
|
|
|
|
|
if (!compiland && lastfilename)
|
|
|
|
compiland = symt_new_compiland(module, 0,
|
|
|
|
source_new(module, NULL, lastfilename));
|
2010-03-18 21:31:26 +01:00
|
|
|
|
|
|
|
if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
|
2018-11-25 10:15:48 +01:00
|
|
|
symt_new_public(module, compiland, name, FALSE,
|
2010-03-18 21:31:26 +01:00
|
|
|
module->module.BaseOfImage + sect[isym->SectionNumber - 1].VirtualAddress +
|
|
|
|
isym->Value,
|
|
|
|
1);
|
2010-01-23 22:13:25 +01:00
|
|
|
}
|
|
|
|
naux = isym->NumberOfAuxSymbols + 1;
|
|
|
|
}
|
|
|
|
module->module.SymType = SymCoff;
|
|
|
|
module->module.LineNumbers = FALSE;
|
|
|
|
module->module.GlobalSymbols = FALSE;
|
|
|
|
module->module.TypeInfo = FALSE;
|
|
|
|
module->module.SourceIndexed = FALSE;
|
|
|
|
module->module.Publics = TRUE;
|
2010-03-18 21:30:55 +01:00
|
|
|
pe_unmap_full(fmap);
|
2010-01-23 22:13:25 +01:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
*/
|
2010-03-18 21:31:13 +01:00
|
|
|
static BOOL pe_load_stabs(const struct process* pcs, struct module* module)
|
2004-04-06 00:21:27 +02:00
|
|
|
{
|
2010-03-18 21:31:50 +01:00
|
|
|
struct image_file_map* fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
|
2010-03-18 21:31:03 +01:00
|
|
|
struct image_section_map sect_stabs, sect_stabstr;
|
2009-12-28 21:13:10 +01:00
|
|
|
BOOL ret = FALSE;
|
2004-04-06 00:21:27 +02:00
|
|
|
|
2010-03-18 21:30:55 +01:00
|
|
|
if (pe_find_section(fmap, ".stab", §_stabs) && pe_find_section(fmap, ".stabstr", §_stabstr))
|
2004-04-06 00:21:27 +02:00
|
|
|
{
|
2010-03-18 21:30:55 +01:00
|
|
|
const char* stab;
|
|
|
|
const char* stabstr;
|
|
|
|
|
2010-03-18 21:31:03 +01:00
|
|
|
stab = image_map_section(§_stabs);
|
|
|
|
stabstr = image_map_section(§_stabstr);
|
2010-03-18 21:30:55 +01:00
|
|
|
if (stab != IMAGE_NO_MAP && stabstr != IMAGE_NO_MAP)
|
|
|
|
{
|
|
|
|
ret = stabs_parse(module,
|
2021-08-31 13:20:33 +02:00
|
|
|
module->module.BaseOfImage - PE_FROM_OPTHDR(fmap, ImageBase),
|
2020-03-29 21:03:34 +02:00
|
|
|
stab, image_get_map_size(§_stabs) / sizeof(struct stab_nlist), sizeof(struct stab_nlist),
|
2010-03-18 21:31:03 +01:00
|
|
|
stabstr, image_get_map_size(§_stabstr),
|
2010-03-18 21:30:55 +01:00
|
|
|
NULL, NULL);
|
|
|
|
}
|
2010-03-18 21:31:03 +01:00
|
|
|
image_unmap_section(§_stabs);
|
|
|
|
image_unmap_section(§_stabstr);
|
2010-03-18 21:31:13 +01:00
|
|
|
if (ret) pe_locate_with_coff_symbol_table(module);
|
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)
|
|
|
|
*/
|
2010-03-18 21:31:26 +01:00
|
|
|
static BOOL pe_load_dwarf(struct module* module)
|
2009-12-23 22:05:25 +01:00
|
|
|
{
|
2010-03-18 21:31:50 +01:00
|
|
|
struct image_file_map* fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
|
2014-11-20 02:36:14 +01:00
|
|
|
BOOL ret;
|
2009-12-23 22:05:25 +01:00
|
|
|
|
2010-03-18 21:31:26 +01:00
|
|
|
ret = dwarf2_parse(module,
|
2021-08-31 13:20:33 +02:00
|
|
|
module->module.BaseOfImage - PE_FROM_OPTHDR(fmap, ImageBase),
|
2010-03-18 21:31:26 +01:00
|
|
|
NULL, /* FIXME: some thunks to deal with ? */
|
|
|
|
fmap);
|
2009-12-23 22:05:25 +01:00
|
|
|
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
|
|
|
{
|
2018-11-15 14:24:58 +01:00
|
|
|
WCHAR 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
|
|
|
|
2020-03-25 17:41:26 +01:00
|
|
|
if (path_find_symbol_file(pcs, module, dbg_name, DMT_DBG, NULL, timestamp, 0, tmp, &module->module.DbgUnmatched) &&
|
2018-11-15 14:24:58 +01:00
|
|
|
(hFile = CreateFileW(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
|
2018-11-15 14:24:58 +01:00
|
|
|
ERR("Couldn't find .DBG file %s (%s)\n", debugstr_a(dbg_name), debugstr_w(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.
|
|
|
|
*/
|
2010-03-18 21:31:13 +01:00
|
|
|
static BOOL pe_load_msc_debug_info(const struct process* pcs, struct module* module)
|
2004-04-06 00:21:27 +02:00
|
|
|
{
|
2010-03-18 21:31:50 +01:00
|
|
|
struct image_file_map* fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
|
2004-08-30 21:31:13 +02:00
|
|
|
BOOL ret = FALSE;
|
2020-02-18 17:02:06 +01:00
|
|
|
const IMAGE_DEBUG_DIRECTORY*dbg;
|
|
|
|
ULONG nDbg;
|
2010-03-18 21:30:55 +01:00
|
|
|
void* mapping;
|
|
|
|
IMAGE_NT_HEADERS* nth;
|
2004-04-06 00:21:27 +02:00
|
|
|
|
2010-03-18 21:30:55 +01:00
|
|
|
if (!(mapping = pe_map_full(fmap, &nth))) return FALSE;
|
2004-04-06 00:21:27 +02:00
|
|
|
/* Read in debug directory */
|
2020-02-18 17:02:06 +01:00
|
|
|
dbg = RtlImageDirectoryEntryToData( mapping, FALSE, IMAGE_DIRECTORY_ENTRY_DEBUG, &nDbg );
|
|
|
|
if (!dbg || !(nDbg /= sizeof(IMAGE_DEBUG_DIRECTORY))) goto done;
|
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)
|
|
|
|
{
|
2012-01-09 23:23:09 +01:00
|
|
|
ERR("-Debug info stripped, but no .DBG file in module %s\n",
|
2021-09-06 10:40:08 +02:00
|
|
|
debugstr_w(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,
|
2010-03-18 21:30:55 +01:00
|
|
|
nth->FileHeader.NumberOfSections, dbg, nDbg);
|
2004-04-06 00:21:27 +02:00
|
|
|
}
|
2010-03-18 21:30:55 +01:00
|
|
|
done:
|
|
|
|
pe_unmap_full(fmap);
|
2004-08-30 21:31:13 +02:00
|
|
|
return ret;
|
2004-04-06 00:21:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* pe_load_export_debug_info
|
|
|
|
*/
|
2010-03-18 21:31:13 +01:00
|
|
|
static BOOL pe_load_export_debug_info(const struct process* pcs, struct module* module)
|
2004-04-06 00:21:27 +02:00
|
|
|
{
|
2010-03-18 21:31:50 +01:00
|
|
|
struct image_file_map* fmap = &module->format_info[DFI_PE]->u.pe_info->fmap;
|
2004-08-30 21:31:13 +02:00
|
|
|
unsigned int i;
|
|
|
|
const IMAGE_EXPORT_DIRECTORY* exports;
|
2021-09-10 15:52:09 +02:00
|
|
|
DWORD_PTR base = module->module.BaseOfImage;
|
2004-08-30 21:31:13 +02:00
|
|
|
DWORD size;
|
2010-03-18 21:30:55 +01:00
|
|
|
IMAGE_NT_HEADERS* nth;
|
|
|
|
void* mapping;
|
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
|
|
|
|
2010-03-18 21:30:55 +01:00
|
|
|
if (!(mapping = pe_map_full(fmap, &nth))) return FALSE;
|
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 */
|
2018-11-25 10:15:48 +01:00
|
|
|
symt_new_public(module, NULL, module->module.ModuleName, FALSE, base, 1);
|
2004-04-06 00:21:27 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Add entry point */
|
2018-11-25 10:15:48 +01:00
|
|
|
symt_new_public(module, NULL, "EntryPoint", FALSE,
|
2010-01-23 22:13:18 +01:00
|
|
|
base + nth->OptionalHeader.AddressOfEntryPoint, 1);
|
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++)
|
|
|
|
{
|
2018-11-25 10:15:48 +01:00
|
|
|
symt_new_public(module, NULL, section->Name, FALSE,
|
2010-01-23 22:13:18 +01:00
|
|
|
RtlImageRvaToVa(nth, mapping, section->VirtualAddress, NULL), 1);
|
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;
|
2021-10-28 16:46:10 +02:00
|
|
|
const DWORD* functions = NULL;
|
2004-08-30 21:31:13 +02:00
|
|
|
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),
|
2018-11-25 10:15:48 +01:00
|
|
|
FALSE,
|
2010-01-23 22:13:18 +01:00
|
|
|
base + functions[ordinals[i]], 1);
|
2007-10-08 19:41:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2021-09-10 15:52:09 +02:00
|
|
|
symt_new_public(module, NULL, buffer, FALSE, base + functions[i], 1);
|
2007-10-08 19:41:37 +02:00
|
|
|
}
|
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;
|
2010-03-18 21:30:55 +01:00
|
|
|
pe_unmap_full(fmap);
|
|
|
|
|
2004-08-30 21:31:13 +02:00
|
|
|
return TRUE;
|
2004-04-06 00:21:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************
|
|
|
|
* pe_load_debug_info
|
|
|
|
*
|
|
|
|
*/
|
2010-03-18 21:31:13 +01:00
|
|
|
BOOL pe_load_debug_info(const struct process* pcs, struct module* module)
|
2010-03-18 21:30:55 +01:00
|
|
|
{
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
|
|
|
|
if (!(dbghelp_options & SYMOPT_PUBLICS_ONLY))
|
|
|
|
{
|
2020-03-17 17:21:45 +01:00
|
|
|
ret = image_check_alternate(&module->format_info[DFI_PE]->u.pe_info->fmap, module);
|
|
|
|
ret = pe_load_stabs(pcs, module) || ret;
|
2010-03-18 21:31:26 +01:00
|
|
|
ret = pe_load_dwarf(module) || ret;
|
|
|
|
ret = pe_load_msc_debug_info(pcs, module) || ret;
|
|
|
|
ret = ret || pe_load_coff_symbol_table(module); /* FIXME */
|
2010-03-18 21:30:55 +01: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
|
|
|
|
*/
|
|
|
|
}
|
2010-03-18 21:31:13 +01:00
|
|
|
/* FIXME shouldn't we check that? if (!module_get_debug(pcs, module)) */
|
|
|
|
if (pe_load_export_debug_info(pcs, module) && !ret)
|
2010-03-18 21:30:55 +01:00
|
|
|
ret = TRUE;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-03-23 16:15:58 +01:00
|
|
|
struct builtin_search
|
2020-03-10 14:54:31 +01:00
|
|
|
{
|
2020-03-23 16:15:58 +01:00
|
|
|
WCHAR *path;
|
|
|
|
struct image_file_map fmap;
|
|
|
|
};
|
2020-03-10 14:54:31 +01:00
|
|
|
|
2020-03-23 16:15:58 +01:00
|
|
|
static BOOL search_builtin_pe(void *param, HANDLE handle, const WCHAR *path)
|
|
|
|
{
|
|
|
|
struct builtin_search *search = param;
|
|
|
|
size_t size;
|
2020-03-10 14:54:31 +01:00
|
|
|
|
2020-03-23 16:15:58 +01:00
|
|
|
if (!pe_map_file(handle, &search->fmap, DMT_PE)) return FALSE;
|
2020-03-10 14:54:31 +01:00
|
|
|
|
2020-03-23 16:15:58 +01:00
|
|
|
size = (lstrlenW(path) + 1) * sizeof(WCHAR);
|
|
|
|
if ((search->path = heap_alloc(size)))
|
|
|
|
memcpy(search->path, path, size);
|
|
|
|
return TRUE;
|
2020-03-10 14:54:31 +01:00
|
|
|
}
|
|
|
|
|
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,
|
2010-10-09 08:50:50 +02:00
|
|
|
HANDLE hFile, DWORD64 base, DWORD size)
|
2004-04-06 00:21:27 +02:00
|
|
|
{
|
2010-03-18 21:31:03 +01:00
|
|
|
struct module* module = NULL;
|
|
|
|
BOOL opened = FALSE;
|
2010-03-18 21:31:50 +01:00
|
|
|
struct module_format* modfmt;
|
2010-03-18 21:31:03 +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;
|
|
|
|
}
|
2020-04-06 19:43:35 +02:00
|
|
|
else if (name) lstrcpyW(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");
|
2010-03-18 21:31:50 +01:00
|
|
|
if (!(modfmt = HeapAlloc(GetProcessHeap(), 0, sizeof(struct module_format) + sizeof(struct pe_module_info))))
|
2010-03-18 21:31:13 +01:00
|
|
|
return NULL;
|
2010-03-18 21:31:50 +01:00
|
|
|
modfmt->u.pe_info = (struct pe_module_info*)(modfmt + 1);
|
|
|
|
if (pe_map_file(hFile, &modfmt->u.pe_info->fmap, DMT_PE))
|
2004-04-06 00:21:27 +02:00
|
|
|
{
|
2020-03-23 16:15:58 +01:00
|
|
|
struct builtin_search builtin = { NULL };
|
2020-04-09 19:59:57 +02:00
|
|
|
if (modfmt->u.pe_info->fmap.u.pe.builtin && search_dll_path(pcs, loaded_name, search_builtin_pe, &builtin))
|
2020-03-10 14:54:31 +01:00
|
|
|
{
|
2020-03-23 16:15:58 +01:00
|
|
|
TRACE("reloaded %s from %s\n", debugstr_w(loaded_name), debugstr_w(builtin.path));
|
|
|
|
image_unmap_file(&modfmt->u.pe_info->fmap);
|
|
|
|
modfmt->u.pe_info->fmap = builtin.fmap;
|
2020-03-10 14:54:31 +01:00
|
|
|
}
|
2021-08-31 13:20:33 +02:00
|
|
|
if (!base) base = PE_FROM_OPTHDR(&modfmt->u.pe_info->fmap, ImageBase);
|
|
|
|
if (!size) size = PE_FROM_OPTHDR(&modfmt->u.pe_info->fmap, SizeOfImage);
|
2007-03-13 17:33:02 +01:00
|
|
|
|
2010-03-18 21:30:55 +01:00
|
|
|
module = module_new(pcs, loaded_name, DMT_PE, FALSE, base, size,
|
2021-08-31 13:20:26 +02:00
|
|
|
modfmt->u.pe_info->fmap.u.pe.file_header.TimeDateStamp,
|
2021-09-06 10:40:29 +02:00
|
|
|
PE_FROM_OPTHDR(&modfmt->u.pe_info->fmap, CheckSum),
|
|
|
|
modfmt->u.pe_info->fmap.u.pe.file_header.Machine);
|
2010-03-18 21:30:55 +01:00
|
|
|
if (module)
|
2004-04-06 00:21:27 +02:00
|
|
|
{
|
2020-03-23 16:15:58 +01:00
|
|
|
module->real_path = builtin.path;
|
2010-03-18 21:31:50 +01:00
|
|
|
modfmt->module = module;
|
|
|
|
modfmt->remove = pe_module_remove;
|
|
|
|
modfmt->loc_compute = NULL;
|
|
|
|
|
|
|
|
module->format_info[DFI_PE] = modfmt;
|
2010-03-18 21:30:55 +01:00
|
|
|
if (dbghelp_options & SYMOPT_DEFERRED_LOADS)
|
|
|
|
module->module.SymType = SymDeferred;
|
|
|
|
else
|
2010-03-18 21:31:13 +01:00
|
|
|
pe_load_debug_info(pcs, module);
|
2021-08-31 13:20:33 +02:00
|
|
|
module->reloc_delta = base - PE_FROM_OPTHDR(&modfmt->u.pe_info->fmap, ImageBase);
|
2010-03-18 21:31:13 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ERR("could not load the module '%s'\n", debugstr_w(loaded_name));
|
2020-03-23 16:15:58 +01:00
|
|
|
heap_free(builtin.path);
|
2020-03-16 16:05:54 +01:00
|
|
|
image_unmap_file(&modfmt->u.pe_info->fmap);
|
2004-04-06 00:21:27 +02:00
|
|
|
}
|
|
|
|
}
|
2010-03-18 21:31:50 +01:00
|
|
|
if (!module) HeapFree(GetProcessHeap(), 0, modfmt);
|
2010-03-18 21:31:13 +01:00
|
|
|
|
2004-04-06 00:21:27 +02:00
|
|
|
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,
|
2021-09-06 10:40:29 +02:00
|
|
|
nth.OptionalHeader.CheckSum,
|
|
|
|
nth.FileHeader.Machine);
|
2007-03-13 17:33:02 +01:00
|
|
|
}
|
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;
|
2021-09-10 15:52:09 +02:00
|
|
|
DWORD_PTR addr;
|
2008-01-11 13:42:17 +01:00
|
|
|
|
|
|
|
*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;
|
2020-02-18 17:01:59 +01:00
|
|
|
if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
|
|
|
|
{
|
|
|
|
const IMAGE_NT_HEADERS64 *nt64 = (const IMAGE_NT_HEADERS64 *)nt;
|
2008-01-11 13:42:17 +01:00
|
|
|
|
2020-02-18 17:01:59 +01:00
|
|
|
if (dir >= nt64->OptionalHeader.NumberOfRvaAndSizes) return NULL;
|
|
|
|
if (!(addr = nt64->OptionalHeader.DataDirectory[dir].VirtualAddress)) return NULL;
|
|
|
|
*size = nt64->OptionalHeader.DataDirectory[dir].Size;
|
|
|
|
if (image || addr < nt64->OptionalHeader.SizeOfHeaders) return (char *)base + addr;
|
|
|
|
}
|
|
|
|
else if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC)
|
|
|
|
{
|
|
|
|
const IMAGE_NT_HEADERS32 *nt32 = (const IMAGE_NT_HEADERS32 *)nt;
|
|
|
|
|
|
|
|
if (dir >= nt32->OptionalHeader.NumberOfRvaAndSizes) return NULL;
|
|
|
|
if (!(addr = nt32->OptionalHeader.DataDirectory[dir].VirtualAddress)) return NULL;
|
|
|
|
*size = nt32->OptionalHeader.DataDirectory[dir].Size;
|
|
|
|
if (image || addr < nt32->OptionalHeader.SizeOfHeaders) return (char *)base + addr;
|
|
|
|
}
|
|
|
|
else return NULL;
|
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 );
|
|
|
|
}
|