4 * Copyright 1998 Patrik Stridvall
12 #include "debugtools.h"
15 DEFAULT_DEBUG_CHANNEL(imagehlp
);
17 /***********************************************************************
21 static PLOADED_IMAGE IMAGEHLP_pFirstLoadedImage
=NULL
;
22 static PLOADED_IMAGE IMAGEHLP_pLastLoadedImage
=NULL
;
24 static LOADED_IMAGE IMAGEHLP_EmptyLoadedImage
= {
25 NULL
, /* ModuleName */
26 0xffffffff, /* hFile */
27 NULL
, /* MappedAddress */
28 NULL
, /* FileHeader */
29 NULL
, /* LastRvaSection */
30 0, /* NumberOfSections */
32 1, /* Characteristics */
33 FALSE
, /* fSystemImage */
34 FALSE
, /* fDOSImage */
35 { &IMAGEHLP_EmptyLoadedImage
.Links
, &IMAGEHLP_EmptyLoadedImage
.Links
}, /* Links */
36 148, /* SizeOfImage; */
39 /***********************************************************************
40 * EnumerateLoadedModules (IMAGEHLP.@)
42 BOOL WINAPI
EnumerateLoadedModules(
44 PENUMLOADED_MODULES_CALLBACK EnumLoadedModulesCallback
,
47 FIXME("(0x%08x, %p, %p): stub\n",
48 hProcess
, EnumLoadedModulesCallback
, UserContext
50 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
54 /***********************************************************************
55 * GetTimestampForLoadedLibrary (IMAGEHLP.@)
57 DWORD WINAPI
GetTimestampForLoadedLibrary(HMODULE Module
)
59 FIXME("(0x%08x): stub\n", Module
);
60 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
64 /***********************************************************************
65 * GetImageConfigInformation (IMAGEHLP.@)
67 BOOL WINAPI
GetImageConfigInformation(
68 PLOADED_IMAGE LoadedImage
,
69 PIMAGE_LOAD_CONFIG_DIRECTORY ImageConfigInformation
)
71 FIXME("(%p, %p): stub\n",
72 LoadedImage
, ImageConfigInformation
74 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
78 /***********************************************************************
79 * GetImageUnusedHeaderBytes (IMAGEHLP.@)
81 DWORD WINAPI
GetImageUnusedHeaderBytes(
82 PLOADED_IMAGE LoadedImage
,
83 LPDWORD SizeUnusedHeaderBytes
)
85 FIXME("(%p, %p): stub\n",
86 LoadedImage
, SizeUnusedHeaderBytes
88 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
92 /***********************************************************************
93 * ImageDirectoryEntryToData (IMAGEHLP.@)
95 PVOID WINAPI
ImageDirectoryEntryToData(
96 PVOID Base
, BOOLEAN MappedAsImage
, USHORT DirectoryEntry
, PULONG Size
)
98 FIXME("(%p, %d, %hd, %p): stub\n",
99 Base
, MappedAsImage
, DirectoryEntry
, Size
101 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
105 /***********************************************************************
106 * ImageLoad (IMAGEHLP.@)
108 PLOADED_IMAGE WINAPI
ImageLoad(LPSTR DllName
, LPSTR DllPath
)
110 PLOADED_IMAGE pLoadedImage
=
111 HeapAlloc(IMAGEHLP_hHeap
, 0, sizeof(LOADED_IMAGE
));
115 /***********************************************************************
116 * ImageNtHeader (IMAGEHLP.@)
118 PIMAGE_NT_HEADERS WINAPI
ImageNtHeader(PVOID Base
)
120 return (PIMAGE_NT_HEADERS
)
121 ((LPBYTE
) Base
+ ((PIMAGE_DOS_HEADER
) Base
)->e_lfanew
);
124 /***********************************************************************
125 * ImageRvaToSection (IMAGEHLP.@)
127 PIMAGE_SECTION_HEADER WINAPI
ImageRvaToSection(
128 PIMAGE_NT_HEADERS NtHeaders
, PVOID Base
, ULONG Rva
)
130 FIXME("(%p, %p, %ld): stub\n", NtHeaders
, Base
, Rva
);
131 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
135 /***********************************************************************
136 * ImageRvaToVa (IMAGEHLP.@)
138 PVOID WINAPI
ImageRvaToVa(
139 PIMAGE_NT_HEADERS NtHeaders
, PVOID Base
, ULONG Rva
,
140 PIMAGE_SECTION_HEADER
*LastRvaSection
)
142 FIXME("(%p, %p, %ld, %p): stub\n",
143 NtHeaders
, Base
, Rva
, LastRvaSection
145 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
149 /***********************************************************************
150 * ImageUnload (IMAGEHLP.@)
152 BOOL WINAPI
ImageUnload(PLOADED_IMAGE pLoadedImage
)
154 LIST_ENTRY
*pCurrent
, *pFind
;
155 if(!IMAGEHLP_pFirstLoadedImage
|| !pLoadedImage
)
157 /* No image loaded or null pointer */
158 SetLastError(ERROR_INVALID_PARAMETER
);
162 pFind
=&pLoadedImage
->Links
;
163 pCurrent
=&IMAGEHLP_pFirstLoadedImage
->Links
;
164 while((pCurrent
!= pFind
) &&
166 pCurrent
= pCurrent
->Flink
;
170 SetLastError(ERROR_INVALID_PARAMETER
);
175 pCurrent
->Blink
->Flink
= pCurrent
->Flink
;
177 IMAGEHLP_pFirstLoadedImage
= pCurrent
->Flink
?CONTAINING_RECORD(
178 pCurrent
->Flink
, LOADED_IMAGE
, Links
):NULL
;
181 pCurrent
->Flink
->Blink
= pCurrent
->Blink
;
183 IMAGEHLP_pLastLoadedImage
= pCurrent
->Blink
?CONTAINING_RECORD(
184 pCurrent
->Blink
, LOADED_IMAGE
, Links
):NULL
;
189 /***********************************************************************
190 * MapAndLoad (IMAGEHLP.@)
192 BOOL WINAPI
MapAndLoad(
193 LPSTR pszImageName
, LPSTR pszDllPath
, PLOADED_IMAGE pLoadedImage
,
194 BOOL bDotDll
, BOOL bReadOnly
)
196 CHAR szFileName
[MAX_PATH
];
197 HANDLE hFile
= (HANDLE
) NULL
;
198 HANDLE hFileMapping
= (HANDLE
) NULL
;
199 HMODULE hModule
= (HMODULE
) NULL
;
200 PIMAGE_NT_HEADERS pNtHeader
= NULL
;
202 /* PathCombine(&szFileName, pszDllPath, pszImageName); */
203 /* PathRenameExtension(&szFileName, bDotDll?:"dll":"exe"); */
205 /* FIXME: Check if the file already loaded (use IMAGEHLP_pFirstLoadedImage) */
206 if(!(hFile
= CreateFileA(
207 szFileName
, GENERIC_READ
, 1, /* FIXME: FILE_SHARE_READ not defined */
208 NULL
, OPEN_EXISTING
, 0, (HANDLE
) NULL
)))
210 SetLastError(ERROR_FILE_NOT_FOUND
);
214 if(!(hFileMapping
= CreateFileMappingA(
215 hFile
, NULL
, PAGE_READONLY
| SEC_COMMIT
, 0, 0, NULL
)))
217 DWORD dwLastError
= GetLastError();
218 WARN("CreateFileMapping: Error = %ld\n", dwLastError
);
219 SetLastError(dwLastError
);
223 hFile
= (HANDLE
) NULL
;
225 if(!(hModule
= (HMODULE
) MapViewOfFile(
226 hFileMapping
, FILE_MAP_READ
, 0, 0, 0)))
228 DWORD dwLastError
= GetLastError();
229 WARN("MapViewOfFile: Error = %ld\n", dwLastError
);
230 SetLastError(dwLastError
);
234 CloseHandle(hFileMapping
);
235 hFileMapping
=(HANDLE
) NULL
;
237 pLoadedImage
= (PLOADED_IMAGE
) HeapAlloc(
238 IMAGEHLP_hHeap
, 0, sizeof(LOADED_IMAGE
)
241 pNtHeader
= ImageNtHeader((PVOID
) hModule
);
243 pLoadedImage
->ModuleName
= HeapAlloc(IMAGEHLP_hHeap
, 0, strlen(pszDllPath
)+1); /* FIXME: Correct? */
244 strcpy( pLoadedImage
->ModuleName
, pszDllPath
);
245 pLoadedImage
->hFile
= hFile
;
246 pLoadedImage
->MappedAddress
= (PUCHAR
) hModule
;
247 pLoadedImage
->FileHeader
= pNtHeader
;
248 pLoadedImage
->Sections
= (PIMAGE_SECTION_HEADER
)
249 ((LPBYTE
) &pNtHeader
->OptionalHeader
+
250 pNtHeader
->FileHeader
.SizeOfOptionalHeader
);
251 pLoadedImage
->NumberOfSections
=
252 pNtHeader
->FileHeader
.NumberOfSections
;
253 pLoadedImage
->SizeOfImage
=
254 pNtHeader
->OptionalHeader
.SizeOfImage
;
255 pLoadedImage
->Characteristics
=
256 pNtHeader
->FileHeader
.Characteristics
;
257 pLoadedImage
->LastRvaSection
= pLoadedImage
->Sections
;
259 pLoadedImage
->fSystemImage
= FALSE
; /* FIXME */
260 pLoadedImage
->fDOSImage
= FALSE
; /* FIXME */
262 /* FIXME: Make thread safe */
263 pLoadedImage
->Links
.Flink
= NULL
;
264 pLoadedImage
->Links
.Blink
= &IMAGEHLP_pLastLoadedImage
->Links
;
265 if(IMAGEHLP_pLastLoadedImage
)
266 IMAGEHLP_pLastLoadedImage
->Links
.Flink
= &pLoadedImage
->Links
;
267 IMAGEHLP_pLastLoadedImage
= pLoadedImage
;
268 if(!IMAGEHLP_pFirstLoadedImage
)
269 IMAGEHLP_pFirstLoadedImage
= pLoadedImage
;
275 UnmapViewOfFile((PVOID
) hModule
);
277 CloseHandle(hFileMapping
);
283 /***********************************************************************
284 * SetImageConfigInformation (IMAGEHLP.@)
286 BOOL WINAPI
SetImageConfigInformation(
287 PLOADED_IMAGE LoadedImage
,
288 PIMAGE_LOAD_CONFIG_DIRECTORY ImageConfigInformation
)
290 FIXME("(%p, %p): stub\n",
291 LoadedImage
, ImageConfigInformation
293 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
297 /***********************************************************************
298 * UnMapAndLoad (IMAGEHLP.@)
300 BOOL WINAPI
UnMapAndLoad(PLOADED_IMAGE LoadedImage
)
302 FIXME("(%p): stub\n", LoadedImage
);
303 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);