4 * Copyright 1998 Patrik Stridvall
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.
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.
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
28 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(imagehlp
);
33 static WORD
CalcCheckSum(DWORD StartValue
, LPVOID BaseAddress
, DWORD WordCount
);
36 /***********************************************************************
37 * BindImage (IMAGEHLP.@)
39 BOOL WINAPI
BindImage(
40 PCSTR ImageName
, PCSTR DllPath
, PCSTR SymbolPath
)
42 return BindImageEx(0, ImageName
, DllPath
, SymbolPath
, NULL
);
45 /***********************************************************************
46 * BindImageEx (IMAGEHLP.@)
48 BOOL WINAPI
BindImageEx(DWORD flags
, const char *module
, const char *dll_path
,
49 const char *symbol_path
, PIMAGEHLP_STATUS_ROUTINE cb
)
51 const IMAGE_IMPORT_DESCRIPTOR
*import
;
55 TRACE("flags %#lx, module %s, dll_path %s, symbol_path %s, cb %p.\n",
56 flags
, debugstr_a(module
), debugstr_a(dll_path
), debugstr_a(symbol_path
), cb
);
58 if (!(flags
& BIND_NO_UPDATE
))
59 FIXME("Image modification is not implemented.\n");
60 if (flags
& ~BIND_NO_UPDATE
)
61 FIXME("Ignoring flags %#lx.\n", flags
);
63 if (!MapAndLoad(module
, dll_path
, &image
, TRUE
, TRUE
))
66 if (!(import
= ImageDirectoryEntryToData(image
.MappedAddress
, FALSE
, IMAGE_DIRECTORY_ENTRY_IMPORT
, &size
)))
69 return TRUE
; /* no imports */
72 if (image
.FileHeader
->OptionalHeader
.Magic
!= IMAGE_NT_OPTIONAL_HDR_MAGIC
)
74 FIXME("Unhandled architecture %#x.\n", image
.FileHeader
->OptionalHeader
.Magic
);
79 for (; import
->Name
&& import
->FirstThunk
; ++import
)
81 char full_path
[MAX_PATH
];
82 IMAGE_THUNK_DATA
*thunk
;
86 if (!(dll_name
= ImageRvaToVa(image
.FileHeader
, image
.MappedAddress
, import
->Name
, 0)))
88 ERR("Failed to get VA for import name RVA %#lx.\n", import
->Name
);
92 if (cb
) cb(BindImportModule
, module
, dll_name
, 0, 0);
94 if (!SearchPathA(dll_path
, dll_name
, 0, sizeof(full_path
), full_path
, 0))
96 ERR("Import %s was not found.\n", debugstr_a(dll_path
));
100 thunk_rva
= import
->OriginalFirstThunk
? import
->OriginalFirstThunk
: import
->FirstThunk
;
101 if (!(thunk
= ImageRvaToVa(image
.FileHeader
, image
.MappedAddress
, thunk_rva
, 0)))
103 ERR("Failed to get VA for import thunk RVA %#lx.\n", thunk_rva
);
107 for (; thunk
->u1
.Ordinal
; ++thunk
)
109 if (IMAGE_SNAP_BY_ORDINAL(thunk
->u1
.Ordinal
))
111 /* FIXME: We apparently need to subtract the actual module's
113 FIXME("Ordinal imports are not implemented.\n");
117 IMAGE_IMPORT_BY_NAME
*name
;
119 if (!(name
= ImageRvaToVa(image
.FileHeader
, image
.MappedAddress
, thunk
->u1
.AddressOfData
, 0)))
121 ERR("Failed to get VA for name RVA %#Ix.\n", thunk
->u1
.AddressOfData
);
125 if (cb
) cb(BindImportProcedure
, module
, full_path
, 0, (ULONG_PTR
)name
->Name
);
130 UnMapAndLoad(&image
);
135 /***********************************************************************
136 * CheckSum (internal)
138 static WORD
CalcCheckSum(
139 DWORD StartValue
, LPVOID BaseAddress
, DWORD WordCount
)
146 Ptr
= (LPWORD
)BaseAddress
;
147 for (i
= 0; i
< WordCount
; i
++)
150 if (HIWORD(Sum
) != 0)
152 Sum
= LOWORD(Sum
) + HIWORD(Sum
);
157 return (WORD
)(LOWORD(Sum
) + HIWORD(Sum
));
161 /***********************************************************************
162 * CheckSumMappedFile (IMAGEHLP.@)
164 PIMAGE_NT_HEADERS WINAPI
CheckSumMappedFile(
165 LPVOID BaseAddress
, DWORD FileLength
,
166 LPDWORD HeaderSum
, LPDWORD CheckSum
)
168 PIMAGE_NT_HEADERS header
;
172 TRACE("(%p, %ld, %p, %p)\n", BaseAddress
, FileLength
, HeaderSum
, CheckSum
);
174 CalcSum
= CalcCheckSum(0, BaseAddress
, (FileLength
+ 1) / sizeof(WORD
));
175 header
= RtlImageNtHeader(BaseAddress
);
180 *HeaderSum
= HdrSum
= header
->OptionalHeader
.CheckSum
;
182 /* Subtract image checksum from calculated checksum. */
183 /* fix low word of checksum */
184 if (LOWORD(CalcSum
) >= LOWORD(HdrSum
))
186 CalcSum
-= LOWORD(HdrSum
);
190 CalcSum
= ((LOWORD(CalcSum
) - LOWORD(HdrSum
)) & 0xFFFF) - 1;
193 /* fix high word of checksum */
194 if (LOWORD(CalcSum
) >= HIWORD(HdrSum
))
196 CalcSum
-= HIWORD(HdrSum
);
200 CalcSum
= ((LOWORD(CalcSum
) - HIWORD(HdrSum
)) & 0xFFFF) - 1;
203 /* add file length */
204 CalcSum
+= FileLength
;
211 /***********************************************************************
212 * MapFileAndCheckSumA (IMAGEHLP.@)
214 DWORD WINAPI
MapFileAndCheckSumA(
215 PCSTR Filename
, PDWORD HeaderSum
, PDWORD CheckSum
)
222 TRACE("(%s, %p, %p): stub\n",
223 debugstr_a(Filename
), HeaderSum
, CheckSum
226 hFile
= CreateFileA(Filename
,
228 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
231 FILE_ATTRIBUTE_NORMAL
,
233 if (hFile
== INVALID_HANDLE_VALUE
)
235 return CHECKSUM_OPEN_FAILURE
;
238 hMapping
= CreateFileMappingW(hFile
,
247 return CHECKSUM_MAP_FAILURE
;
250 BaseAddress
= MapViewOfFile(hMapping
,
255 if (BaseAddress
== 0)
257 CloseHandle(hMapping
);
259 return CHECKSUM_MAPVIEW_FAILURE
;
262 FileLength
= GetFileSize(hFile
,
265 CheckSumMappedFile(BaseAddress
,
270 UnmapViewOfFile(BaseAddress
);
271 CloseHandle(hMapping
);
277 /***********************************************************************
278 * MapFileAndCheckSumW (IMAGEHLP.@)
280 DWORD WINAPI
MapFileAndCheckSumW(
281 PCWSTR Filename
, PDWORD HeaderSum
, PDWORD CheckSum
)
288 TRACE("(%s, %p, %p): stub\n",
289 debugstr_w(Filename
), HeaderSum
, CheckSum
292 hFile
= CreateFileW(Filename
,
294 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
297 FILE_ATTRIBUTE_NORMAL
,
299 if (hFile
== INVALID_HANDLE_VALUE
)
301 return CHECKSUM_OPEN_FAILURE
;
304 hMapping
= CreateFileMappingW(hFile
,
313 return CHECKSUM_MAP_FAILURE
;
316 BaseAddress
= MapViewOfFile(hMapping
,
321 if (BaseAddress
== 0)
323 CloseHandle(hMapping
);
325 return CHECKSUM_MAPVIEW_FAILURE
;
328 FileLength
= GetFileSize(hFile
,
331 CheckSumMappedFile(BaseAddress
,
336 UnmapViewOfFile(BaseAddress
);
337 CloseHandle(hMapping
);
343 /***********************************************************************
344 * ReBaseImage (IMAGEHLP.@)
346 BOOL WINAPI
ReBaseImage(
347 PCSTR CurrentImageName
, PCSTR SymbolPath
, BOOL fReBase
,
348 BOOL fRebaseSysfileOk
, BOOL fGoingDown
, ULONG CheckImageSize
,
349 ULONG
*OldImageSize
, ULONG_PTR
*OldImageBase
, ULONG
*NewImageSize
,
350 ULONG_PTR
*NewImageBase
, ULONG TimeStamp
)
353 "(%s, %s, %d, %d, %d, %ld, %p, %p, %p, %p, %ld): stub\n",
354 debugstr_a(CurrentImageName
),debugstr_a(SymbolPath
), fReBase
,
355 fRebaseSysfileOk
, fGoingDown
, CheckImageSize
, OldImageSize
,
356 OldImageBase
, NewImageSize
, NewImageBase
, TimeStamp
358 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
362 /***********************************************************************
363 * RemovePrivateCvSymbolic (IMAGEHLP.@)
365 BOOL WINAPI
RemovePrivateCvSymbolic(
366 PCHAR DebugData
, PCHAR
*NewDebugData
, ULONG
*NewDebugSize
)
368 FIXME("(%p, %p, %p): stub\n",
369 DebugData
, NewDebugData
, NewDebugSize
371 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
375 /***********************************************************************
376 * RemoveRelocations (IMAGEHLP.@)
378 VOID WINAPI
RemoveRelocations(PCHAR ImageName
)
380 FIXME("(%p): stub\n", ImageName
);
381 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
384 /***********************************************************************
385 * SplitSymbols (IMAGEHLP.@)
387 BOOL WINAPI
SplitSymbols(
388 PSTR ImageName
, PCSTR SymbolsPath
,
389 PSTR SymbolFilePath
, ULONG Flags
)
391 FIXME("(%s, %s, %s, %ld): stub\n",
392 debugstr_a(ImageName
), debugstr_a(SymbolsPath
),
393 debugstr_a(SymbolFilePath
), Flags
395 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
399 /***********************************************************************
400 * UpdateDebugInfoFile (IMAGEHLP.@)
402 BOOL WINAPI
UpdateDebugInfoFile(
403 PCSTR ImageFileName
, PCSTR SymbolPath
,
404 PSTR DebugFilePath
, PIMAGE_NT_HEADERS32 NtHeaders
)
406 FIXME("(%s, %s, %s, %p): stub\n",
407 debugstr_a(ImageFileName
), debugstr_a(SymbolPath
),
408 debugstr_a(DebugFilePath
), NtHeaders
410 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
414 /***********************************************************************
415 * UpdateDebugInfoFileEx (IMAGEHLP.@)
417 BOOL WINAPI
UpdateDebugInfoFileEx(
418 PCSTR ImageFileName
, PCSTR SymbolPath
, PSTR DebugFilePath
,
419 PIMAGE_NT_HEADERS32 NtHeaders
, DWORD OldChecksum
)
421 FIXME("(%s, %s, %s, %p, %ld): stub\n",
422 debugstr_a(ImageFileName
), debugstr_a(SymbolPath
),
423 debugstr_a(DebugFilePath
), NtHeaders
, OldChecksum
425 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);