1998-10-11 19:04:17 +02:00
|
|
|
/*
|
|
|
|
* IMAGEHLP library
|
|
|
|
*
|
|
|
|
* Copyright 1998 Patrik Stridvall
|
2002-03-10 00:29:33 +01: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
|
1998-10-11 19:04:17 +02:00
|
|
|
*/
|
|
|
|
|
2003-09-06 01:08:26 +02:00
|
|
|
#include <stdarg.h>
|
2001-01-26 21:43:40 +01:00
|
|
|
#include <string.h>
|
2003-09-06 01:08:26 +02:00
|
|
|
#include "windef.h"
|
1998-10-11 19:04:17 +02:00
|
|
|
#include "winbase.h"
|
|
|
|
#include "winnt.h"
|
2003-11-21 22:31:35 +01:00
|
|
|
#include "winreg.h"
|
|
|
|
#include "winternl.h"
|
1998-10-11 19:04:17 +02:00
|
|
|
#include "winerror.h"
|
2002-03-10 00:29:33 +01:00
|
|
|
#include "wine/debug.h"
|
1998-10-11 19:04:17 +02:00
|
|
|
#include "imagehlp.h"
|
|
|
|
|
2002-03-10 00:29:33 +01:00
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(imagehlp);
|
1999-04-19 16:56:29 +02:00
|
|
|
|
1998-10-11 19:04:17 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* Data
|
|
|
|
*/
|
|
|
|
|
1999-02-26 12:11:13 +01:00
|
|
|
static PLOADED_IMAGE IMAGEHLP_pFirstLoadedImage=NULL;
|
|
|
|
static PLOADED_IMAGE IMAGEHLP_pLastLoadedImage=NULL;
|
1998-10-11 19:04:17 +02:00
|
|
|
|
1999-02-26 12:11:13 +01:00
|
|
|
static LOADED_IMAGE IMAGEHLP_EmptyLoadedImage = {
|
1998-10-11 19:04:17 +02:00
|
|
|
NULL, /* ModuleName */
|
2002-10-04 02:27:10 +02:00
|
|
|
0, /* hFile */
|
1998-10-11 19:04:17 +02:00
|
|
|
NULL, /* MappedAddress */
|
|
|
|
NULL, /* FileHeader */
|
|
|
|
NULL, /* LastRvaSection */
|
|
|
|
0, /* NumberOfSections */
|
|
|
|
NULL, /* Sections */
|
|
|
|
1, /* Characteristics */
|
|
|
|
FALSE, /* fSystemImage */
|
|
|
|
FALSE, /* fDOSImage */
|
1999-02-26 12:11:13 +01:00
|
|
|
{ &IMAGEHLP_EmptyLoadedImage.Links, &IMAGEHLP_EmptyLoadedImage.Links }, /* Links */
|
1998-10-11 19:04:17 +02:00
|
|
|
148, /* SizeOfImage; */
|
|
|
|
};
|
|
|
|
|
2003-06-28 00:24:23 +02:00
|
|
|
extern HANDLE IMAGEHLP_hHeap;
|
|
|
|
|
1998-10-11 19:04:17 +02:00
|
|
|
/***********************************************************************
|
2000-03-28 22:22:59 +02:00
|
|
|
* GetImageConfigInformation (IMAGEHLP.@)
|
1998-10-11 19:04:17 +02:00
|
|
|
*/
|
1999-02-26 12:11:13 +01:00
|
|
|
BOOL WINAPI GetImageConfigInformation(
|
|
|
|
PLOADED_IMAGE LoadedImage,
|
|
|
|
PIMAGE_LOAD_CONFIG_DIRECTORY ImageConfigInformation)
|
1998-10-11 19:04:17 +02:00
|
|
|
{
|
1999-05-14 10:17:14 +02:00
|
|
|
FIXME("(%p, %p): stub\n",
|
1998-10-11 19:04:17 +02:00
|
|
|
LoadedImage, ImageConfigInformation
|
|
|
|
);
|
|
|
|
SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
2000-03-28 22:22:59 +02:00
|
|
|
* GetImageUnusedHeaderBytes (IMAGEHLP.@)
|
1998-10-11 19:04:17 +02:00
|
|
|
*/
|
1999-02-26 12:11:13 +01:00
|
|
|
DWORD WINAPI GetImageUnusedHeaderBytes(
|
|
|
|
PLOADED_IMAGE LoadedImage,
|
1998-10-11 19:04:17 +02:00
|
|
|
LPDWORD SizeUnusedHeaderBytes)
|
|
|
|
{
|
1999-05-14 10:17:14 +02:00
|
|
|
FIXME("(%p, %p): stub\n",
|
1998-10-11 19:04:17 +02:00
|
|
|
LoadedImage, SizeUnusedHeaderBytes
|
|
|
|
);
|
|
|
|
SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
2000-03-28 22:22:59 +02:00
|
|
|
* ImageLoad (IMAGEHLP.@)
|
1998-10-11 19:04:17 +02:00
|
|
|
*/
|
1999-02-26 12:11:13 +01:00
|
|
|
PLOADED_IMAGE WINAPI ImageLoad(LPSTR DllName, LPSTR DllPath)
|
1998-10-11 19:04:17 +02:00
|
|
|
{
|
2003-08-19 02:57:29 +02:00
|
|
|
PLOADED_IMAGE pLoadedImage;
|
|
|
|
|
|
|
|
FIXME("(%s, %s): stub\n", DllName, DllPath);
|
|
|
|
|
|
|
|
pLoadedImage = HeapAlloc(IMAGEHLP_hHeap, 0, sizeof(LOADED_IMAGE));
|
|
|
|
if (pLoadedImage)
|
|
|
|
pLoadedImage->FileHeader = HeapAlloc(IMAGEHLP_hHeap, 0, sizeof(IMAGE_NT_HEADERS));
|
|
|
|
|
1998-10-11 19:04:17 +02:00
|
|
|
return pLoadedImage;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
2000-03-28 22:22:59 +02:00
|
|
|
* ImageUnload (IMAGEHLP.@)
|
1998-10-11 19:04:17 +02:00
|
|
|
*/
|
1999-02-26 12:11:13 +01:00
|
|
|
BOOL WINAPI ImageUnload(PLOADED_IMAGE pLoadedImage)
|
1998-10-11 19:04:17 +02:00
|
|
|
{
|
1999-02-26 12:11:13 +01:00
|
|
|
LIST_ENTRY *pCurrent, *pFind;
|
2003-08-19 02:57:29 +02:00
|
|
|
|
|
|
|
TRACE("(%p)\n", pLoadedImage);
|
|
|
|
|
1999-02-26 12:11:13 +01:00
|
|
|
if(!IMAGEHLP_pFirstLoadedImage || !pLoadedImage)
|
1998-10-11 19:04:17 +02:00
|
|
|
{
|
|
|
|
/* No image loaded or null pointer */
|
|
|
|
SetLastError(ERROR_INVALID_PARAMETER);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
pFind=&pLoadedImage->Links;
|
1999-02-26 12:11:13 +01:00
|
|
|
pCurrent=&IMAGEHLP_pFirstLoadedImage->Links;
|
2002-06-01 01:06:46 +02:00
|
|
|
while((pCurrent != pFind) &&
|
|
|
|
(pCurrent != NULL))
|
1998-10-11 19:04:17 +02:00
|
|
|
pCurrent = pCurrent->Flink;
|
|
|
|
if(!pCurrent)
|
|
|
|
{
|
|
|
|
/* Not found */
|
|
|
|
SetLastError(ERROR_INVALID_PARAMETER);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(pCurrent->Blink)
|
|
|
|
pCurrent->Blink->Flink = pCurrent->Flink;
|
|
|
|
else
|
1999-02-26 12:11:13 +01:00
|
|
|
IMAGEHLP_pFirstLoadedImage = pCurrent->Flink?CONTAINING_RECORD(
|
|
|
|
pCurrent->Flink, LOADED_IMAGE, Links):NULL;
|
1998-10-11 19:04:17 +02:00
|
|
|
|
|
|
|
if(pCurrent->Flink)
|
|
|
|
pCurrent->Flink->Blink = pCurrent->Blink;
|
|
|
|
else
|
1999-02-26 12:11:13 +01:00
|
|
|
IMAGEHLP_pLastLoadedImage = pCurrent->Blink?CONTAINING_RECORD(
|
|
|
|
pCurrent->Blink, LOADED_IMAGE, Links):NULL;
|
1998-10-11 19:04:17 +02:00
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
2000-03-28 22:22:59 +02:00
|
|
|
* MapAndLoad (IMAGEHLP.@)
|
1998-10-11 19:04:17 +02:00
|
|
|
*/
|
2006-12-01 21:56:31 +01:00
|
|
|
BOOL WINAPI MapAndLoad(LPSTR pszImageName, LPSTR pszDllPath, PLOADED_IMAGE pLoadedImage,
|
|
|
|
BOOL bDotDll, BOOL bReadOnly)
|
1998-10-11 19:04:17 +02:00
|
|
|
{
|
2006-12-01 21:56:31 +01:00
|
|
|
CHAR szFileName[MAX_PATH];
|
|
|
|
HANDLE hFile = INVALID_HANDLE_VALUE;
|
|
|
|
HANDLE hFileMapping = NULL;
|
|
|
|
PVOID mapping = NULL;
|
|
|
|
PIMAGE_NT_HEADERS pNtHeader = NULL;
|
1998-10-11 19:04:17 +02:00
|
|
|
|
2006-12-01 21:56:31 +01:00
|
|
|
TRACE("(%s, %s, %p, %d, %d)\n",
|
|
|
|
pszImageName, pszDllPath, pLoadedImage, bDotDll, bReadOnly);
|
1998-10-11 19:04:17 +02:00
|
|
|
|
2006-12-01 21:56:31 +01:00
|
|
|
if (!SearchPathA(pszDllPath, pszImageName, bDotDll ? ".DLL" : ".EXE",
|
|
|
|
sizeof(szFileName), szFileName, NULL))
|
1998-10-11 19:04:17 +02:00
|
|
|
{
|
2006-12-01 21:56:31 +01:00
|
|
|
SetLastError(ERROR_FILE_NOT_FOUND);
|
|
|
|
goto Error;
|
1998-10-11 19:04:17 +02:00
|
|
|
}
|
|
|
|
|
2007-01-03 14:39:33 +01:00
|
|
|
hFile = CreateFileA(szFileName,
|
|
|
|
GENERIC_READ | (bReadOnly ? 0 : GENERIC_WRITE),
|
|
|
|
FILE_SHARE_READ,
|
2006-12-01 21:56:31 +01:00
|
|
|
NULL, OPEN_EXISTING, 0, NULL);
|
|
|
|
if (hFile == INVALID_HANDLE_VALUE)
|
1998-10-11 19:04:17 +02:00
|
|
|
{
|
2006-12-01 21:56:31 +01:00
|
|
|
WARN("CreateFile: Error = %d\n", GetLastError());
|
|
|
|
goto Error;
|
1998-10-11 19:04:17 +02:00
|
|
|
}
|
|
|
|
|
2007-01-03 14:39:33 +01:00
|
|
|
hFileMapping = CreateFileMappingA(hFile, NULL,
|
|
|
|
(bReadOnly ? PAGE_READONLY : PAGE_READWRITE) | SEC_COMMIT,
|
|
|
|
0, 0, NULL);
|
2006-12-01 21:56:31 +01:00
|
|
|
if (!hFileMapping)
|
1998-10-11 19:04:17 +02:00
|
|
|
{
|
2006-12-01 21:56:31 +01:00
|
|
|
WARN("CreateFileMapping: Error = %d\n", GetLastError());
|
|
|
|
goto Error;
|
1998-10-11 19:04:17 +02:00
|
|
|
}
|
|
|
|
|
2007-01-03 14:39:33 +01:00
|
|
|
mapping = MapViewOfFile(hFileMapping, bReadOnly ? FILE_MAP_READ : FILE_MAP_WRITE, 0, 0, 0);
|
2006-12-01 21:56:31 +01:00
|
|
|
CloseHandle(hFileMapping);
|
|
|
|
if (!mapping)
|
|
|
|
{
|
|
|
|
WARN("MapViewOfFile: Error = %d\n", GetLastError());
|
|
|
|
goto Error;
|
|
|
|
}
|
1998-10-11 19:04:17 +02:00
|
|
|
|
2007-01-03 14:39:33 +01:00
|
|
|
if (!(pNtHeader = RtlImageNtHeader(mapping)))
|
|
|
|
{
|
|
|
|
WARN("Not an NT header\n");
|
|
|
|
UnmapViewOfFile(mapping);
|
|
|
|
goto Error;
|
|
|
|
}
|
1998-10-11 19:04:17 +02:00
|
|
|
|
2006-12-01 21:56:31 +01:00
|
|
|
pLoadedImage->ModuleName = HeapAlloc(GetProcessHeap(), 0,
|
|
|
|
strlen(szFileName) + 1);
|
|
|
|
if (pLoadedImage->ModuleName) strcpy(pLoadedImage->ModuleName, szFileName);
|
|
|
|
pLoadedImage->hFile = hFile;
|
|
|
|
pLoadedImage->MappedAddress = mapping;
|
|
|
|
pLoadedImage->FileHeader = pNtHeader;
|
|
|
|
pLoadedImage->Sections = (PIMAGE_SECTION_HEADER)
|
|
|
|
((LPBYTE) &pNtHeader->OptionalHeader +
|
|
|
|
pNtHeader->FileHeader.SizeOfOptionalHeader);
|
|
|
|
pLoadedImage->NumberOfSections = pNtHeader->FileHeader.NumberOfSections;
|
2007-01-03 14:39:33 +01:00
|
|
|
pLoadedImage->SizeOfImage = GetFileSize(hFile, NULL);
|
2006-12-01 21:56:31 +01:00
|
|
|
pLoadedImage->Characteristics = pNtHeader->FileHeader.Characteristics;
|
|
|
|
pLoadedImage->LastRvaSection = pLoadedImage->Sections;
|
1998-10-11 19:04:17 +02:00
|
|
|
|
2006-12-01 21:56:31 +01:00
|
|
|
pLoadedImage->fSystemImage = FALSE; /* FIXME */
|
|
|
|
pLoadedImage->fDOSImage = FALSE; /* FIXME */
|
1998-10-11 19:04:17 +02:00
|
|
|
|
2006-12-01 21:56:31 +01:00
|
|
|
pLoadedImage->Links.Flink = &pLoadedImage->Links;
|
|
|
|
pLoadedImage->Links.Blink = &pLoadedImage->Links;
|
1998-10-11 19:04:17 +02:00
|
|
|
|
2006-12-01 21:56:31 +01:00
|
|
|
return TRUE;
|
1998-10-11 19:04:17 +02:00
|
|
|
|
|
|
|
Error:
|
2006-12-01 21:56:31 +01:00
|
|
|
if (hFile != INVALID_HANDLE_VALUE) CloseHandle(hFile);
|
|
|
|
return FALSE;
|
1998-10-11 19:04:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
2000-03-28 22:22:59 +02:00
|
|
|
* SetImageConfigInformation (IMAGEHLP.@)
|
1998-10-11 19:04:17 +02:00
|
|
|
*/
|
1999-02-26 12:11:13 +01:00
|
|
|
BOOL WINAPI SetImageConfigInformation(
|
|
|
|
PLOADED_IMAGE LoadedImage,
|
|
|
|
PIMAGE_LOAD_CONFIG_DIRECTORY ImageConfigInformation)
|
1998-10-11 19:04:17 +02:00
|
|
|
{
|
1999-05-14 10:17:14 +02:00
|
|
|
FIXME("(%p, %p): stub\n",
|
1998-10-11 19:04:17 +02:00
|
|
|
LoadedImage, ImageConfigInformation
|
|
|
|
);
|
|
|
|
SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
2000-03-28 22:22:59 +02:00
|
|
|
* UnMapAndLoad (IMAGEHLP.@)
|
1998-10-11 19:04:17 +02:00
|
|
|
*/
|
2006-12-01 21:56:31 +01:00
|
|
|
BOOL WINAPI UnMapAndLoad(PLOADED_IMAGE pLoadedImage)
|
1998-10-11 19:04:17 +02:00
|
|
|
{
|
2006-12-01 21:56:31 +01:00
|
|
|
HeapFree(GetProcessHeap(), 0, pLoadedImage->ModuleName);
|
|
|
|
/* FIXME: MSDN states that a new checksum is computed and stored into the file */
|
|
|
|
if (pLoadedImage->MappedAddress) UnmapViewOfFile(pLoadedImage->MappedAddress);
|
|
|
|
if (pLoadedImage->hFile != INVALID_HANDLE_VALUE) CloseHandle(pLoadedImage->hFile);
|
|
|
|
return TRUE;
|
1998-10-11 19:04:17 +02:00
|
|
|
}
|