imagehlp: Correctly handle files without NT headers in MapAndLoad.
[wine] / dlls / imagehlp / access.c
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 <stdarg.h>
22 #include <string.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winnt.h"
26 #include "winreg.h"
27 #include "winternl.h"
28 #include "winerror.h"
29 #include "wine/debug.h"
30 #include "imagehlp.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(imagehlp);
33
34 /***********************************************************************
35  *           Data
36  */
37
38 static PLOADED_IMAGE IMAGEHLP_pFirstLoadedImage=NULL;
39 static PLOADED_IMAGE IMAGEHLP_pLastLoadedImage=NULL;
40
41 static LOADED_IMAGE IMAGEHLP_EmptyLoadedImage = {
42   NULL,       /* ModuleName */
43   0,          /* hFile */
44   NULL,       /* MappedAddress */
45   NULL,       /* FileHeader */
46   NULL,       /* LastRvaSection */
47   0,          /* NumberOfSections */
48   NULL,       /* Sections */
49   1,          /* Characteristics */
50   FALSE,      /* fSystemImage */
51   FALSE,      /* fDOSImage */
52   { &IMAGEHLP_EmptyLoadedImage.Links, &IMAGEHLP_EmptyLoadedImage.Links }, /* Links */
53   148,        /* SizeOfImage; */
54 };
55
56 extern HANDLE IMAGEHLP_hHeap;
57
58 /***********************************************************************
59  *              GetImageConfigInformation (IMAGEHLP.@)
60  */
61 BOOL WINAPI GetImageConfigInformation(
62   PLOADED_IMAGE LoadedImage,
63   PIMAGE_LOAD_CONFIG_DIRECTORY ImageConfigInformation)
64 {
65   FIXME("(%p, %p): stub\n",
66     LoadedImage, ImageConfigInformation
67   );
68   SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
69   return FALSE;
70 }
71
72 /***********************************************************************
73  *              GetImageUnusedHeaderBytes (IMAGEHLP.@)
74  */
75 DWORD WINAPI GetImageUnusedHeaderBytes(
76   PLOADED_IMAGE LoadedImage,
77   LPDWORD SizeUnusedHeaderBytes)
78 {
79   FIXME("(%p, %p): stub\n",
80     LoadedImage, SizeUnusedHeaderBytes
81   );
82   SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
83   return 0;
84 }
85
86 /***********************************************************************
87  *              ImageLoad (IMAGEHLP.@)
88  */
89 PLOADED_IMAGE WINAPI ImageLoad(LPSTR DllName, LPSTR DllPath)
90 {
91   PLOADED_IMAGE pLoadedImage;
92
93   FIXME("(%s, %s): stub\n", DllName, DllPath);
94           
95   pLoadedImage = HeapAlloc(IMAGEHLP_hHeap, 0, sizeof(LOADED_IMAGE));
96   if (pLoadedImage)
97     pLoadedImage->FileHeader = HeapAlloc(IMAGEHLP_hHeap, 0, sizeof(IMAGE_NT_HEADERS));
98   
99   return pLoadedImage;
100 }
101
102 /***********************************************************************
103  *              ImageUnload (IMAGEHLP.@)
104  */
105 BOOL WINAPI ImageUnload(PLOADED_IMAGE pLoadedImage)
106 {
107   LIST_ENTRY *pCurrent, *pFind;
108
109   TRACE("(%p)\n", pLoadedImage);
110   
111   if(!IMAGEHLP_pFirstLoadedImage || !pLoadedImage)
112     {
113       /* No image loaded or null pointer */
114       SetLastError(ERROR_INVALID_PARAMETER);
115       return FALSE;
116     }
117
118   pFind=&pLoadedImage->Links;
119   pCurrent=&IMAGEHLP_pFirstLoadedImage->Links;
120   while((pCurrent != pFind) &&
121     (pCurrent != NULL))
122       pCurrent = pCurrent->Flink;
123   if(!pCurrent)
124     {
125       /* Not found */
126       SetLastError(ERROR_INVALID_PARAMETER);
127       return FALSE;
128     }
129
130   if(pCurrent->Blink)
131     pCurrent->Blink->Flink = pCurrent->Flink;
132   else
133     IMAGEHLP_pFirstLoadedImage = pCurrent->Flink?CONTAINING_RECORD(
134       pCurrent->Flink, LOADED_IMAGE, Links):NULL;
135
136   if(pCurrent->Flink)
137     pCurrent->Flink->Blink = pCurrent->Blink;
138   else
139     IMAGEHLP_pLastLoadedImage = pCurrent->Blink?CONTAINING_RECORD(
140       pCurrent->Blink, LOADED_IMAGE, Links):NULL;
141
142   return FALSE;
143 }
144
145 /***********************************************************************
146  *              MapAndLoad (IMAGEHLP.@)
147  */
148 BOOL WINAPI MapAndLoad(LPSTR pszImageName, LPSTR pszDllPath, PLOADED_IMAGE pLoadedImage,
149                        BOOL bDotDll, BOOL bReadOnly)
150 {
151     CHAR szFileName[MAX_PATH];
152     HANDLE hFile = INVALID_HANDLE_VALUE;
153     HANDLE hFileMapping = NULL;
154     PVOID mapping = NULL;
155     PIMAGE_NT_HEADERS pNtHeader = NULL;
156
157     TRACE("(%s, %s, %p, %d, %d)\n",
158           pszImageName, pszDllPath, pLoadedImage, bDotDll, bReadOnly);
159
160     if (!SearchPathA(pszDllPath, pszImageName, bDotDll ? ".DLL" : ".EXE",
161                      sizeof(szFileName), szFileName, NULL))
162     {
163         SetLastError(ERROR_FILE_NOT_FOUND);
164         goto Error;
165     }
166
167     hFile = CreateFileA(szFileName, GENERIC_READ, FILE_SHARE_READ,
168                         NULL, OPEN_EXISTING, 0, NULL);
169     if (hFile == INVALID_HANDLE_VALUE)
170     {
171         WARN("CreateFile: Error = %d\n", GetLastError());
172         goto Error;
173     }
174
175     hFileMapping = CreateFileMappingA(hFile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL);
176     if (!hFileMapping)
177     {
178         WARN("CreateFileMapping: Error = %d\n", GetLastError());
179         goto Error;
180     }
181
182     mapping = MapViewOfFile(hFileMapping, FILE_MAP_READ, 0, 0, 0);
183     CloseHandle(hFileMapping);
184     if (!mapping)
185     {
186         WARN("MapViewOfFile: Error = %d\n", GetLastError());
187         goto Error;
188     }
189
190     if (!(pNtHeader = RtlImageNtHeader(mapping)))
191     {
192         WARN("Not an NT header\n");
193         UnmapViewOfFile(mapping);
194         goto Error;
195     }
196
197     pLoadedImage->ModuleName       = HeapAlloc(GetProcessHeap(), 0,
198                                                strlen(szFileName) + 1);
199     if (pLoadedImage->ModuleName) strcpy(pLoadedImage->ModuleName, szFileName);
200     pLoadedImage->hFile            = hFile;
201     pLoadedImage->MappedAddress    = mapping;
202     pLoadedImage->FileHeader       = pNtHeader;
203     pLoadedImage->Sections         = (PIMAGE_SECTION_HEADER)
204         ((LPBYTE) &pNtHeader->OptionalHeader +
205          pNtHeader->FileHeader.SizeOfOptionalHeader);
206     pLoadedImage->NumberOfSections = pNtHeader->FileHeader.NumberOfSections;
207     pLoadedImage->SizeOfImage      = GetFileSize(hFile, NULL);
208     pLoadedImage->Characteristics  = pNtHeader->FileHeader.Characteristics;
209     pLoadedImage->LastRvaSection   = pLoadedImage->Sections;
210
211     pLoadedImage->fSystemImage     = FALSE; /* FIXME */
212     pLoadedImage->fDOSImage        = FALSE; /* FIXME */
213
214     pLoadedImage->Links.Flink      = &pLoadedImage->Links;
215     pLoadedImage->Links.Blink      = &pLoadedImage->Links;
216
217     return TRUE;
218
219 Error:
220     if (hFile != INVALID_HANDLE_VALUE) CloseHandle(hFile);
221     return FALSE;
222 }
223
224 /***********************************************************************
225  *              SetImageConfigInformation (IMAGEHLP.@)
226  */
227 BOOL WINAPI SetImageConfigInformation(
228   PLOADED_IMAGE LoadedImage,
229   PIMAGE_LOAD_CONFIG_DIRECTORY ImageConfigInformation)
230 {
231   FIXME("(%p, %p): stub\n",
232     LoadedImage, ImageConfigInformation
233   );
234   SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
235   return FALSE;
236 }
237
238 /***********************************************************************
239  *              UnMapAndLoad (IMAGEHLP.@)
240  */
241 BOOL WINAPI UnMapAndLoad(PLOADED_IMAGE pLoadedImage)
242 {
243     HeapFree(GetProcessHeap(), 0, pLoadedImage->ModuleName);
244     /* FIXME: MSDN states that a new checksum is computed and stored into the file */
245     if (pLoadedImage->MappedAddress) UnmapViewOfFile(pLoadedImage->MappedAddress);
246     if (pLoadedImage->hFile != INVALID_HANDLE_VALUE) CloseHandle(pLoadedImage->hFile);
247     return TRUE;
248 }