1 /* 2 * IMAGEHLP library 3 * 4 * Copyright 1998 Patrik Stridvall 5 * 6 * This library is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU Lesser General Public 8 * License as published by the Free Software Foundation; either 9 * version 2.1 of the License, or (at your option) any later version. 10 * 11 * This library is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public 17 * 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 "precomp.h" 22 23 #include <winternl.h> 24 25 /*********************************************************************** 26 * Data 27 */ 28 29 static PLOADED_IMAGE IMAGEHLP_pFirstLoadedImage=NULL; 30 31 DECLSPEC_HIDDEN extern HANDLE IMAGEHLP_hHeap; 32 33 /*********************************************************************** 34 * GetImageConfigInformation (IMAGEHLP.@) 35 */ 36 BOOL WINAPI GetImageConfigInformation( 37 PLOADED_IMAGE LoadedImage, 38 PIMAGE_LOAD_CONFIG_DIRECTORY ImageConfigInformation) 39 { 40 FIXME("(%p, %p): stub\n", 41 LoadedImage, ImageConfigInformation 42 ); 43 SetLastError(ERROR_CALL_NOT_IMPLEMENTED); 44 return FALSE; 45 } 46 47 /*********************************************************************** 48 * GetImageUnusedHeaderBytes (IMAGEHLP.@) 49 */ 50 DWORD WINAPI GetImageUnusedHeaderBytes( 51 PLOADED_IMAGE LoadedImage, 52 LPDWORD SizeUnusedHeaderBytes) 53 { 54 FIXME("(%p, %p): stub\n", 55 LoadedImage, SizeUnusedHeaderBytes 56 ); 57 SetLastError(ERROR_CALL_NOT_IMPLEMENTED); 58 return 0; 59 } 60 61 /*********************************************************************** 62 * ImageLoad (IMAGEHLP.@) 63 */ 64 PLOADED_IMAGE WINAPI ImageLoad(PCSTR DllName, PCSTR DllPath) 65 { 66 PLOADED_IMAGE pLoadedImage; 67 68 FIXME("(%s, %s): stub\n", DllName, DllPath); 69 70 pLoadedImage = HeapAlloc(IMAGEHLP_hHeap, 0, sizeof(LOADED_IMAGE)); 71 if (pLoadedImage) 72 pLoadedImage->FileHeader = HeapAlloc(IMAGEHLP_hHeap, 0, sizeof(IMAGE_NT_HEADERS)); 73 74 return pLoadedImage; 75 } 76 77 /*********************************************************************** 78 * ImageUnload (IMAGEHLP.@) 79 */ 80 BOOL WINAPI ImageUnload(PLOADED_IMAGE pLoadedImage) 81 { 82 LIST_ENTRY *pCurrent, *pFind; 83 84 TRACE("(%p)\n", pLoadedImage); 85 86 if(!IMAGEHLP_pFirstLoadedImage || !pLoadedImage) 87 { 88 /* No image loaded or null pointer */ 89 SetLastError(ERROR_INVALID_PARAMETER); 90 return FALSE; 91 } 92 93 pFind=&pLoadedImage->Links; 94 pCurrent=&IMAGEHLP_pFirstLoadedImage->Links; 95 while((pCurrent != pFind) && 96 (pCurrent != NULL)) 97 pCurrent = pCurrent->Flink; 98 if(!pCurrent) 99 { 100 /* Not found */ 101 SetLastError(ERROR_INVALID_PARAMETER); 102 return FALSE; 103 } 104 105 if(pCurrent->Blink) 106 pCurrent->Blink->Flink = pCurrent->Flink; 107 else 108 IMAGEHLP_pFirstLoadedImage = pCurrent->Flink?CONTAINING_RECORD( 109 pCurrent->Flink, LOADED_IMAGE, Links):NULL; 110 111 if(pCurrent->Flink) 112 pCurrent->Flink->Blink = pCurrent->Blink; 113 114 return FALSE; 115 } 116 117 /*********************************************************************** 118 * MapAndLoad (IMAGEHLP.@) 119 */ 120 BOOL WINAPI MapAndLoad(PCSTR pszImageName, PCSTR pszDllPath, PLOADED_IMAGE pLoadedImage, 121 BOOL bDotDll, BOOL bReadOnly) 122 { 123 CHAR szFileName[MAX_PATH]; 124 HANDLE hFile = INVALID_HANDLE_VALUE; 125 HANDLE hFileMapping = NULL; 126 PVOID mapping = NULL; 127 PIMAGE_NT_HEADERS pNtHeader = NULL; 128 129 TRACE("(%s, %s, %p, %d, %d)\n", 130 pszImageName, pszDllPath, pLoadedImage, bDotDll, bReadOnly); 131 132 if (!SearchPathA(pszDllPath, pszImageName, bDotDll ? ".DLL" : ".EXE", 133 sizeof(szFileName), szFileName, NULL)) 134 { 135 SetLastError(ERROR_FILE_NOT_FOUND); 136 goto Error; 137 } 138 139 hFile = CreateFileA(szFileName, 140 GENERIC_READ | (bReadOnly ? 0 : GENERIC_WRITE), 141 FILE_SHARE_READ, 142 NULL, OPEN_EXISTING, 0, NULL); 143 if (hFile == INVALID_HANDLE_VALUE) 144 { 145 WARN("CreateFile: Error = %d\n", GetLastError()); 146 goto Error; 147 } 148 149 hFileMapping = CreateFileMappingA(hFile, NULL, 150 (bReadOnly ? PAGE_READONLY : PAGE_READWRITE) | SEC_COMMIT, 151 0, 0, NULL); 152 if (!hFileMapping) 153 { 154 WARN("CreateFileMapping: Error = %d\n", GetLastError()); 155 goto Error; 156 } 157 158 mapping = MapViewOfFile(hFileMapping, bReadOnly ? FILE_MAP_READ : FILE_MAP_WRITE, 0, 0, 0); 159 CloseHandle(hFileMapping); 160 if (!mapping) 161 { 162 WARN("MapViewOfFile: Error = %d\n", GetLastError()); 163 goto Error; 164 } 165 166 if (!(pNtHeader = RtlImageNtHeader(mapping))) 167 { 168 WARN("Not an NT header\n"); 169 UnmapViewOfFile(mapping); 170 goto Error; 171 } 172 173 pLoadedImage->ModuleName = HeapAlloc(GetProcessHeap(), 0, 174 strlen(szFileName) + 1); 175 if (pLoadedImage->ModuleName) strcpy(pLoadedImage->ModuleName, szFileName); 176 pLoadedImage->hFile = hFile; 177 pLoadedImage->MappedAddress = mapping; 178 pLoadedImage->FileHeader = pNtHeader; 179 pLoadedImage->Sections = (PIMAGE_SECTION_HEADER) 180 ((LPBYTE) &pNtHeader->OptionalHeader + 181 pNtHeader->FileHeader.SizeOfOptionalHeader); 182 pLoadedImage->NumberOfSections = pNtHeader->FileHeader.NumberOfSections; 183 pLoadedImage->SizeOfImage = GetFileSize(hFile, NULL); 184 pLoadedImage->Characteristics = pNtHeader->FileHeader.Characteristics; 185 pLoadedImage->LastRvaSection = pLoadedImage->Sections; 186 187 pLoadedImage->fSystemImage = FALSE; /* FIXME */ 188 pLoadedImage->fDOSImage = FALSE; /* FIXME */ 189 190 pLoadedImage->Links.Flink = &pLoadedImage->Links; 191 pLoadedImage->Links.Blink = &pLoadedImage->Links; 192 193 return TRUE; 194 195 Error: 196 if (hFile != INVALID_HANDLE_VALUE) CloseHandle(hFile); 197 return FALSE; 198 } 199 200 /*********************************************************************** 201 * SetImageConfigInformation (IMAGEHLP.@) 202 */ 203 BOOL WINAPI SetImageConfigInformation( 204 PLOADED_IMAGE LoadedImage, 205 PIMAGE_LOAD_CONFIG_DIRECTORY ImageConfigInformation) 206 { 207 FIXME("(%p, %p): stub\n", 208 LoadedImage, ImageConfigInformation 209 ); 210 SetLastError(ERROR_CALL_NOT_IMPLEMENTED); 211 return FALSE; 212 } 213 214 /*********************************************************************** 215 * UnMapAndLoad (IMAGEHLP.@) 216 */ 217 BOOL WINAPI UnMapAndLoad(PLOADED_IMAGE pLoadedImage) 218 { 219 HeapFree(GetProcessHeap(), 0, pLoadedImage->ModuleName); 220 /* FIXME: MSDN states that a new checksum is computed and stored into the file */ 221 if (pLoadedImage->MappedAddress) UnmapViewOfFile(pLoadedImage->MappedAddress); 222 if (pLoadedImage->hFile != INVALID_HANDLE_VALUE) CloseHandle(pLoadedImage->hFile); 223 return TRUE; 224 } 225