sapi: Use standard C functions for memory allocation in token.c.
[wine.git] / dlls / imagehlp / modify.c
blob3cb5dd800fd705e473c96b4635bad4b4a28d088c
1 /*
2 * IMAGEHLP library
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
21 #include <stdarg.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winternl.h"
26 #include "winerror.h"
27 #include "winver.h"
28 #include "wine/debug.h"
29 #include "imagehlp.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;
52 LOADED_IMAGE image;
53 ULONG size;
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))
64 return FALSE;
66 if (!(import = ImageDirectoryEntryToData(image.MappedAddress, FALSE, IMAGE_DIRECTORY_ENTRY_IMPORT, &size)))
68 UnMapAndLoad(&image);
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);
75 UnMapAndLoad(&image);
76 return TRUE;
79 for (; import->Name && import->FirstThunk; ++import)
81 char full_path[MAX_PATH];
82 IMAGE_THUNK_DATA *thunk;
83 const char *dll_name;
84 DWORD thunk_rva;
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);
89 continue;
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));
97 continue;
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);
104 continue;
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
112 * ordinal base. */
113 FIXME("Ordinal imports are not implemented.\n");
115 else
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);
122 continue;
125 if (cb) cb(BindImportProcedure, module, full_path, 0, (ULONG_PTR)name->Name);
130 UnMapAndLoad(&image);
131 return TRUE;
135 /***********************************************************************
136 * CheckSum (internal)
138 static WORD CalcCheckSum(
139 DWORD StartValue, LPVOID BaseAddress, DWORD WordCount)
141 LPWORD Ptr;
142 DWORD Sum;
143 DWORD i;
145 Sum = StartValue;
146 Ptr = (LPWORD)BaseAddress;
147 for (i = 0; i < WordCount; i++)
149 Sum += *Ptr;
150 if (HIWORD(Sum) != 0)
152 Sum = LOWORD(Sum) + HIWORD(Sum);
154 Ptr++;
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;
169 DWORD CalcSum;
170 DWORD HdrSum;
172 TRACE("(%p, %ld, %p, %p)\n", BaseAddress, FileLength, HeaderSum, CheckSum);
174 CalcSum = CalcCheckSum(0, BaseAddress, (FileLength + 1) / sizeof(WORD));
175 header = RtlImageNtHeader(BaseAddress);
177 if (!header)
178 return NULL;
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);
188 else
190 CalcSum = ((LOWORD(CalcSum) - LOWORD(HdrSum)) & 0xFFFF) - 1;
193 /* fix high word of checksum */
194 if (LOWORD(CalcSum) >= HIWORD(HdrSum))
196 CalcSum -= HIWORD(HdrSum);
198 else
200 CalcSum = ((LOWORD(CalcSum) - HIWORD(HdrSum)) & 0xFFFF) - 1;
203 /* add file length */
204 CalcSum += FileLength;
206 *CheckSum = CalcSum;
208 return header;
211 /***********************************************************************
212 * MapFileAndCheckSumA (IMAGEHLP.@)
214 DWORD WINAPI MapFileAndCheckSumA(
215 PCSTR Filename, PDWORD HeaderSum, PDWORD CheckSum)
217 HANDLE hFile;
218 HANDLE hMapping;
219 LPVOID BaseAddress;
220 DWORD FileLength;
222 TRACE("(%s, %p, %p): stub\n",
223 debugstr_a(Filename), HeaderSum, CheckSum
226 hFile = CreateFileA(Filename,
227 GENERIC_READ,
228 FILE_SHARE_READ | FILE_SHARE_WRITE,
229 NULL,
230 OPEN_EXISTING,
231 FILE_ATTRIBUTE_NORMAL,
233 if (hFile == INVALID_HANDLE_VALUE)
235 return CHECKSUM_OPEN_FAILURE;
238 hMapping = CreateFileMappingW(hFile,
239 NULL,
240 PAGE_READONLY,
243 NULL);
244 if (hMapping == 0)
246 CloseHandle(hFile);
247 return CHECKSUM_MAP_FAILURE;
250 BaseAddress = MapViewOfFile(hMapping,
251 FILE_MAP_READ,
255 if (BaseAddress == 0)
257 CloseHandle(hMapping);
258 CloseHandle(hFile);
259 return CHECKSUM_MAPVIEW_FAILURE;
262 FileLength = GetFileSize(hFile,
263 NULL);
265 CheckSumMappedFile(BaseAddress,
266 FileLength,
267 HeaderSum,
268 CheckSum);
270 UnmapViewOfFile(BaseAddress);
271 CloseHandle(hMapping);
272 CloseHandle(hFile);
274 return 0;
277 /***********************************************************************
278 * MapFileAndCheckSumW (IMAGEHLP.@)
280 DWORD WINAPI MapFileAndCheckSumW(
281 PCWSTR Filename, PDWORD HeaderSum, PDWORD CheckSum)
283 HANDLE hFile;
284 HANDLE hMapping;
285 LPVOID BaseAddress;
286 DWORD FileLength;
288 TRACE("(%s, %p, %p): stub\n",
289 debugstr_w(Filename), HeaderSum, CheckSum
292 hFile = CreateFileW(Filename,
293 GENERIC_READ,
294 FILE_SHARE_READ | FILE_SHARE_WRITE,
295 NULL,
296 OPEN_EXISTING,
297 FILE_ATTRIBUTE_NORMAL,
299 if (hFile == INVALID_HANDLE_VALUE)
301 return CHECKSUM_OPEN_FAILURE;
304 hMapping = CreateFileMappingW(hFile,
305 NULL,
306 PAGE_READONLY,
309 NULL);
310 if (hMapping == 0)
312 CloseHandle(hFile);
313 return CHECKSUM_MAP_FAILURE;
316 BaseAddress = MapViewOfFile(hMapping,
317 FILE_MAP_READ,
321 if (BaseAddress == 0)
323 CloseHandle(hMapping);
324 CloseHandle(hFile);
325 return CHECKSUM_MAPVIEW_FAILURE;
328 FileLength = GetFileSize(hFile,
329 NULL);
331 CheckSumMappedFile(BaseAddress,
332 FileLength,
333 HeaderSum,
334 CheckSum);
336 UnmapViewOfFile(BaseAddress);
337 CloseHandle(hMapping);
338 CloseHandle(hFile);
340 return 0;
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)
352 FIXME(
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);
359 return FALSE;
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);
372 return FALSE;
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);
396 return FALSE;
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);
411 return FALSE;
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);
426 return FALSE;