access.c 7.72 KB
Newer Older
1 2 3 4
/*
 *	IMAGEHLP library
 *
 *	Copyright 1998	Patrik Stridvall
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * 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
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 20
 */

21
#include <stdarg.h>
22
#include <string.h>
23
#include "windef.h"
24 25
#include "winbase.h"
#include "winnt.h"
26
#include "winternl.h"
27
#include "winerror.h"
28
#include "wine/debug.h"
29 30
#include "imagehlp.h"

31
WINE_DEFAULT_DEBUG_CHANNEL(imagehlp);
32

33 34 35 36
/***********************************************************************
 *           Data
 */

37 38
static PLOADED_IMAGE IMAGEHLP_pFirstLoadedImage=NULL;
static PLOADED_IMAGE IMAGEHLP_pLastLoadedImage=NULL;
39

40
static LOADED_IMAGE IMAGEHLP_EmptyLoadedImage = {
41
  NULL,       /* ModuleName */
42
  0,          /* hFile */
43 44 45 46 47 48 49 50
  NULL,       /* MappedAddress */
  NULL,       /* FileHeader */
  NULL,       /* LastRvaSection */
  0,          /* NumberOfSections */
  NULL,       /* Sections */
  1,          /* Characteristics */
  FALSE,      /* fSystemImage */
  FALSE,      /* fDOSImage */
51 52
  FALSE,      /* fReadOnly */
  0,          /* Version */
53
  { &IMAGEHLP_EmptyLoadedImage.Links, &IMAGEHLP_EmptyLoadedImage.Links }, /* Links */
54 55 56
  148,        /* SizeOfImage; */
};

57 58
extern HANDLE IMAGEHLP_hHeap;

59
/***********************************************************************
60
 *		GetImageConfigInformation (IMAGEHLP.@)
61
 */
62 63 64
BOOL WINAPI GetImageConfigInformation(
  PLOADED_IMAGE LoadedImage,
  PIMAGE_LOAD_CONFIG_DIRECTORY ImageConfigInformation)
65
{
66
  FIXME("(%p, %p): stub\n",
67 68 69 70 71 72 73
    LoadedImage, ImageConfigInformation
  );
  SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
  return FALSE;
}

/***********************************************************************
74
 *		GetImageUnusedHeaderBytes (IMAGEHLP.@)
75
 */
76 77
DWORD WINAPI GetImageUnusedHeaderBytes(
  PLOADED_IMAGE LoadedImage,
78 79
  LPDWORD SizeUnusedHeaderBytes)
{
80
  FIXME("(%p, %p): stub\n",
81 82 83 84 85 86 87
    LoadedImage, SizeUnusedHeaderBytes
  );
  SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
  return 0;
}

/***********************************************************************
88
 *		ImageLoad (IMAGEHLP.@)
89
 */
90
PLOADED_IMAGE WINAPI ImageLoad(PCSTR DllName, PCSTR DllPath)
91
{
92 93 94 95 96 97 98 99
  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));
  
100 101 102 103
  return pLoadedImage;
}

/***********************************************************************
104
 *		ImageUnload (IMAGEHLP.@)
105
 */
106
BOOL WINAPI ImageUnload(PLOADED_IMAGE pLoadedImage)
107
{
108
  LIST_ENTRY *pCurrent, *pFind;
109 110 111

  TRACE("(%p)\n", pLoadedImage);
  
112
  if(!IMAGEHLP_pFirstLoadedImage || !pLoadedImage)
113 114 115 116 117 118 119
    {
      /* No image loaded or null pointer */
      SetLastError(ERROR_INVALID_PARAMETER);
      return FALSE;
    }

  pFind=&pLoadedImage->Links;
120
  pCurrent=&IMAGEHLP_pFirstLoadedImage->Links;
121 122
  while((pCurrent != pFind) &&
    (pCurrent != NULL))
123 124 125 126 127 128 129 130 131 132 133
      pCurrent = pCurrent->Flink;
  if(!pCurrent)
    {
      /* Not found */
      SetLastError(ERROR_INVALID_PARAMETER);
      return FALSE;
    }

  if(pCurrent->Blink)
    pCurrent->Blink->Flink = pCurrent->Flink;
  else
134 135
    IMAGEHLP_pFirstLoadedImage = pCurrent->Flink?CONTAINING_RECORD(
      pCurrent->Flink, LOADED_IMAGE, Links):NULL;
136 137 138 139

  if(pCurrent->Flink)
    pCurrent->Flink->Blink = pCurrent->Blink;
  else
140 141
    IMAGEHLP_pLastLoadedImage = pCurrent->Blink?CONTAINING_RECORD(
      pCurrent->Blink, LOADED_IMAGE, Links):NULL;
142 143 144 145 146

  return FALSE;
}

/***********************************************************************
147
 *		MapAndLoad (IMAGEHLP.@)
148
 */
149
BOOL WINAPI MapAndLoad(PCSTR pszImageName, PCSTR pszDllPath, PLOADED_IMAGE pLoadedImage,
150
                       BOOL bDotDll, BOOL bReadOnly)
151
{
152 153 154 155 156
    CHAR szFileName[MAX_PATH];
    HANDLE hFile = INVALID_HANDLE_VALUE;
    HANDLE hFileMapping = NULL;
    PVOID mapping = NULL;
    PIMAGE_NT_HEADERS pNtHeader = NULL;
157

158 159
    TRACE("(%s, %s, %p, %d, %d)\n",
          pszImageName, pszDllPath, pLoadedImage, bDotDll, bReadOnly);
160

161 162
    if (!SearchPathA(pszDllPath, pszImageName, bDotDll ? ".DLL" : ".EXE",
                     sizeof(szFileName), szFileName, NULL))
163
    {
164 165
        SetLastError(ERROR_FILE_NOT_FOUND);
        goto Error;
166 167
    }

168 169 170
    hFile = CreateFileA(szFileName,
                        GENERIC_READ | (bReadOnly ? 0 : GENERIC_WRITE),
                        FILE_SHARE_READ,
171 172
                        NULL, OPEN_EXISTING, 0, NULL);
    if (hFile == INVALID_HANDLE_VALUE)
173
    {
174 175
        WARN("CreateFile: Error = %d\n", GetLastError());
        goto Error;
176 177
    }

178 179 180
    hFileMapping = CreateFileMappingA(hFile, NULL, 
                                      (bReadOnly ? PAGE_READONLY : PAGE_READWRITE) | SEC_COMMIT,
                                      0, 0, NULL);
181
    if (!hFileMapping)
182
    {
183 184
        WARN("CreateFileMapping: Error = %d\n", GetLastError());
        goto Error;
185 186
    }

187
    mapping = MapViewOfFile(hFileMapping, bReadOnly ? FILE_MAP_READ : FILE_MAP_WRITE, 0, 0, 0);
188 189 190 191 192 193
    CloseHandle(hFileMapping);
    if (!mapping)
    {
        WARN("MapViewOfFile: Error = %d\n", GetLastError());
        goto Error;
    }
194

195 196 197 198 199 200
    if (!(pNtHeader = RtlImageNtHeader(mapping)))
    {
        WARN("Not an NT header\n");
        UnmapViewOfFile(mapping);
        goto Error;
    }
201

202 203 204 205 206 207 208 209 210 211
    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;
212
    pLoadedImage->SizeOfImage      = GetFileSize(hFile, NULL);
213 214
    pLoadedImage->Characteristics  = pNtHeader->FileHeader.Characteristics;
    pLoadedImage->LastRvaSection   = pLoadedImage->Sections;
215

216 217
    pLoadedImage->fSystemImage     = FALSE; /* FIXME */
    pLoadedImage->fDOSImage        = FALSE; /* FIXME */
218

219 220
    pLoadedImage->Links.Flink      = &pLoadedImage->Links;
    pLoadedImage->Links.Blink      = &pLoadedImage->Links;
221

222
    return TRUE;
223 224

Error:
225 226
    if (hFile != INVALID_HANDLE_VALUE) CloseHandle(hFile);
    return FALSE;
227 228 229
}

/***********************************************************************
230
 *		SetImageConfigInformation (IMAGEHLP.@)
231
 */
232 233 234
BOOL WINAPI SetImageConfigInformation(
  PLOADED_IMAGE LoadedImage,
  PIMAGE_LOAD_CONFIG_DIRECTORY ImageConfigInformation)
235
{
236
  FIXME("(%p, %p): stub\n",
237 238 239 240 241 242 243
    LoadedImage, ImageConfigInformation
  );
  SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
  return FALSE;
}

/***********************************************************************
244
 *		UnMapAndLoad (IMAGEHLP.@)
245
 */
246
BOOL WINAPI UnMapAndLoad(PLOADED_IMAGE pLoadedImage)
247
{
248 249 250 251 252
    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;
253
}