ReactOS  0.4.14-dev-317-g96040ec
modify.c
Go to the documentation of this file.
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 
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winternl.h"
26 #include "winerror.h"
27 #include "wine/debug.h"
28 #include "imagehlp.h"
29 
31 
32 static WORD CalcCheckSum(DWORD StartValue, LPVOID BaseAddress, DWORD WordCount);
33 
34 /***********************************************************************
35  * BindImage (IMAGEHLP.@)
36  *
37  * NOTES
38  * See BindImageEx
39  */
42 {
44 }
45 
46 /***********************************************************************
47  * BindImageEx (IMAGEHLP.@)
48  *
49  * Compute the virtual address of each function imported by a PE image
50  *
51  * PARAMS
52  *
53  * Flags [in] Bind options
54  * ImageName [in] File name of the image to be bound
55  * DllPath [in] Root of the fallback search path in case the ImageName file cannot be opened
56  * SymbolPath [in] Symbol file root search path
57  * StatusRoutine [in] Pointer to a status routine which will be called during the binding process
58  *
59  * RETURNS
60  * Success: TRUE
61  * Failure: FALSE
62  *
63  * NOTES
64  * Binding is not implemented yet, so far this function only enumerates
65  * all imported dlls/functions and returns TRUE.
66  */
70 {
71  LOADED_IMAGE loaded_image;
72  const IMAGE_IMPORT_DESCRIPTOR *import_desc;
73  ULONG size;
74 
75  FIXME("(%d, %s, %s, %s, %p): semi-stub\n",
78  );
79 
80  if (!(MapAndLoad(ImageName, DllPath, &loaded_image, TRUE, TRUE))) return FALSE;
81 
82  if (!(import_desc = RtlImageDirectoryEntryToData((HMODULE)loaded_image.MappedAddress, FALSE,
84  {
85  UnMapAndLoad(&loaded_image);
86  return TRUE; /* No imported modules means nothing to bind, so we're done. */
87  }
88 
89  /* FIXME: Does native imagehlp support both 32-bit and 64-bit PE executables? */
90 #ifdef _WIN64
92 #else
94 #endif
95  {
96  FIXME("Wrong architecture in PE header, unable to enumerate imports\n");
97  UnMapAndLoad(&loaded_image);
98  return TRUE;
99  }
100 
101  for (; import_desc->Name && import_desc->FirstThunk; ++import_desc)
102  {
104  char dll_fullname[MAX_PATH];
105  const char *dll_name;
106 
107  if (!(dll_name = ImageRvaToVa(loaded_image.FileHeader, loaded_image.MappedAddress,
108  import_desc->Name, 0)))
109  {
110  UnMapAndLoad(&loaded_image);
111  SetLastError(ERROR_INVALID_ACCESS); /* FIXME */
112  return FALSE;
113  }
114 
115  if (StatusRoutine)
116  StatusRoutine(BindImportModule, ImageName, dll_name, 0, 0);
117 
118  if (!SearchPathA(DllPath, dll_name, 0, sizeof(dll_fullname), dll_fullname, 0))
119  {
120  UnMapAndLoad(&loaded_image);
122  return FALSE;
123  }
124 
125  if (!(thunk = ImageRvaToVa(loaded_image.FileHeader, loaded_image.MappedAddress,
126  import_desc->OriginalFirstThunk ? import_desc->OriginalFirstThunk :
127  import_desc->FirstThunk, 0)))
128  {
129  ERR("Can't grab thunk data of %s, going to next imported DLL\n", dll_name);
130  continue;
131  }
132 
133  for (; thunk->u1.Ordinal; ++thunk)
134  {
135  /* Ignoring ordinal imports for now */
136  if(!IMAGE_SNAP_BY_ORDINAL(thunk->u1.Ordinal))
137  {
138  IMAGE_IMPORT_BY_NAME *iibn;
139 
140  if (!(iibn = ImageRvaToVa(loaded_image.FileHeader, loaded_image.MappedAddress,
141  thunk->u1.AddressOfData, 0)))
142  {
143  ERR("Can't grab import by name info, skipping to next ordinal\n");
144  continue;
145  }
146 
147  if (StatusRoutine)
148  StatusRoutine(BindImportProcedure, ImageName, dll_fullname, 0, (ULONG_PTR)iibn->Name);
149  }
150  }
151  }
152 
153  UnMapAndLoad(&loaded_image);
154  return TRUE;
155 }
156 
157 
158 /***********************************************************************
159  * CheckSum (internal)
160  */
162  DWORD StartValue, LPVOID BaseAddress, DWORD WordCount)
163 {
164  LPWORD Ptr;
165  DWORD Sum;
166  DWORD i;
167 
168  Sum = StartValue;
170  for (i = 0; i < WordCount; i++)
171  {
172  Sum += *Ptr;
173  if (HIWORD(Sum) != 0)
174  {
175  Sum = LOWORD(Sum) + HIWORD(Sum);
176  }
177  Ptr++;
178  }
179 
180  return (WORD)(LOWORD(Sum) + HIWORD(Sum));
181 }
182 
183 
184 /***********************************************************************
185  * CheckSumMappedFile (IMAGEHLP.@)
186  */
189  LPDWORD HeaderSum, LPDWORD CheckSum)
190 {
192  DWORD CalcSum;
193  DWORD HdrSum;
194 
195  TRACE("(%p, %d, %p, %p)\n", BaseAddress, FileLength, HeaderSum, CheckSum);
196 
197  CalcSum = CalcCheckSum(0, BaseAddress, (FileLength + 1) / sizeof(WORD));
199 
200  if (!header)
201  return NULL;
202 
203  *HeaderSum = HdrSum = header->OptionalHeader.CheckSum;
204 
205  /* Subtract image checksum from calculated checksum. */
206  /* fix low word of checksum */
207  if (LOWORD(CalcSum) >= LOWORD(HdrSum))
208  {
209  CalcSum -= LOWORD(HdrSum);
210  }
211  else
212  {
213  CalcSum = ((LOWORD(CalcSum) - LOWORD(HdrSum)) & 0xFFFF) - 1;
214  }
215 
216  /* fix high word of checksum */
217  if (LOWORD(CalcSum) >= HIWORD(HdrSum))
218  {
219  CalcSum -= HIWORD(HdrSum);
220  }
221  else
222  {
223  CalcSum = ((LOWORD(CalcSum) - HIWORD(HdrSum)) & 0xFFFF) - 1;
224  }
225 
226  /* add file length */
227  CalcSum += FileLength;
228 
229  *CheckSum = CalcSum;
230 
231  return header;
232 }
233 
234 /***********************************************************************
235  * MapFileAndCheckSumA (IMAGEHLP.@)
236  */
238  PCSTR Filename, PDWORD HeaderSum, PDWORD CheckSum)
239 {
240  HANDLE hFile;
241  HANDLE hMapping;
244 
245  TRACE("(%s, %p, %p): stub\n",
246  debugstr_a(Filename), HeaderSum, CheckSum
247  );
248 
250  GENERIC_READ,
252  NULL,
255  0);
257  {
258  return CHECKSUM_OPEN_FAILURE;
259  }
260 
261  hMapping = CreateFileMappingW(hFile,
262  NULL,
264  0,
265  0,
266  NULL);
267  if (hMapping == 0)
268  {
270  return CHECKSUM_MAP_FAILURE;
271  }
272 
273  BaseAddress = MapViewOfFile(hMapping,
275  0,
276  0,
277  0);
278  if (BaseAddress == 0)
279  {
280  CloseHandle(hMapping);
283  }
284 
286  NULL);
287 
289  FileLength,
290  HeaderSum,
291  CheckSum);
292 
294  CloseHandle(hMapping);
296 
297  return 0;
298 }
299 
300 /***********************************************************************
301  * MapFileAndCheckSumW (IMAGEHLP.@)
302  */
304  PCWSTR Filename, PDWORD HeaderSum, PDWORD CheckSum)
305 {
306  HANDLE hFile;
307  HANDLE hMapping;
310 
311  TRACE("(%s, %p, %p): stub\n",
312  debugstr_w(Filename), HeaderSum, CheckSum
313  );
314 
316  GENERIC_READ,
318  NULL,
321  0);
323  {
324  return CHECKSUM_OPEN_FAILURE;
325  }
326 
327  hMapping = CreateFileMappingW(hFile,
328  NULL,
330  0,
331  0,
332  NULL);
333  if (hMapping == 0)
334  {
336  return CHECKSUM_MAP_FAILURE;
337  }
338 
339  BaseAddress = MapViewOfFile(hMapping,
341  0,
342  0,
343  0);
344  if (BaseAddress == 0)
345  {
346  CloseHandle(hMapping);
349  }
350 
352  NULL);
353 
355  FileLength,
356  HeaderSum,
357  CheckSum);
358 
360  CloseHandle(hMapping);
362 
363  return 0;
364 }
365 
366 /***********************************************************************
367  * ReBaseImage (IMAGEHLP.@)
368  */
370  PCSTR CurrentImageName, PCSTR SymbolPath, BOOL fReBase,
371  BOOL fRebaseSysfileOk, BOOL fGoingDown, ULONG CheckImageSize,
372  ULONG *OldImageSize, ULONG_PTR *OldImageBase, ULONG *NewImageSize,
373  ULONG_PTR *NewImageBase, ULONG TimeStamp)
374 {
375  FIXME(
376  "(%s, %s, %d, %d, %d, %d, %p, %p, %p, %p, %d): stub\n",
377  debugstr_a(CurrentImageName),debugstr_a(SymbolPath), fReBase,
378  fRebaseSysfileOk, fGoingDown, CheckImageSize, OldImageSize,
379  OldImageBase, NewImageSize, NewImageBase, TimeStamp
380  );
382  return FALSE;
383 }
384 
385 /***********************************************************************
386  * RemovePrivateCvSymbolic (IMAGEHLP.@)
387  */
389  PCHAR DebugData, PCHAR *NewDebugData, ULONG *NewDebugSize)
390 {
391  FIXME("(%p, %p, %p): stub\n",
392  DebugData, NewDebugData, NewDebugSize
393  );
395  return FALSE;
396 }
397 
398 /***********************************************************************
399  * RemoveRelocations (IMAGEHLP.@)
400  */
402 {
403  FIXME("(%p): stub\n", ImageName);
405 }
406 
407 /***********************************************************************
408  * SplitSymbols (IMAGEHLP.@)
409  */
411  PSTR ImageName, PCSTR SymbolsPath,
412  PSTR SymbolFilePath, ULONG Flags)
413 {
414  FIXME("(%s, %s, %s, %d): stub\n",
415  debugstr_a(ImageName), debugstr_a(SymbolsPath),
416  debugstr_a(SymbolFilePath), Flags
417  );
419  return FALSE;
420 }
421 
422 /***********************************************************************
423  * UpdateDebugInfoFile (IMAGEHLP.@)
424  */
426  PCSTR ImageFileName, PCSTR SymbolPath,
427  PSTR DebugFilePath, PIMAGE_NT_HEADERS32 NtHeaders)
428 {
429  FIXME("(%s, %s, %s, %p): stub\n",
430  debugstr_a(ImageFileName), debugstr_a(SymbolPath),
431  debugstr_a(DebugFilePath), NtHeaders
432  );
434  return FALSE;
435 }
436 
437 /***********************************************************************
438  * UpdateDebugInfoFileEx (IMAGEHLP.@)
439  */
441  PCSTR ImageFileName, PCSTR SymbolPath, PSTR DebugFilePath,
442  PIMAGE_NT_HEADERS32 NtHeaders, DWORD OldChecksum)
443 {
444  FIXME("(%s, %s, %s, %p, %d): stub\n",
445  debugstr_a(ImageFileName), debugstr_a(SymbolPath),
446  debugstr_a(DebugFilePath), NtHeaders, OldChecksum
447  );
449  return FALSE;
450 }
signed char * PCHAR
Definition: retypes.h:7
BOOL WINAPI UnMapAndLoad(PLOADED_IMAGE pLoadedImage)
Definition: access.c:228
BOOL WINAPI BindImageEx(DWORD Flags, PCSTR ImageName, PCSTR DllPath, PCSTR SymbolPath, PIMAGEHLP_STATUS_ROUTINE StatusRoutine)
Definition: modify.c:67
const uint16_t * PCWSTR
Definition: typedefs.h:55
static const char const char const char PIMAGEHLP_STATUS_ROUTINE StatusRoutine
Definition: image.c:34
_Out_ PNDIS_HANDLE _Out_ PUINT FileLength
Definition: ndis.h:3227
#define CHECKSUM_MAP_FAILURE
Definition: imagehlp.h:50
#define TRUE
Definition: types.h:120
DWORD WINAPI MapFileAndCheckSumA(PCSTR Filename, PDWORD HeaderSum, PDWORD CheckSum)
Definition: modify.c:237
#define CloseHandle
Definition: compat.h:398
VOID WINAPI RemoveRelocations(PCHAR ImageName)
Definition: modify.c:401
#define MapViewOfFile
Definition: compat.h:402
Definition: cproxy.c:203
DWORD WINAPI MapFileAndCheckSumW(PCWSTR Filename, PDWORD HeaderSum, PDWORD CheckSum)
Definition: modify.c:303
#define CHECKSUM_OPEN_FAILURE
Definition: imagehlp.h:49
static WORD CalcCheckSum(DWORD StartValue, LPVOID BaseAddress, DWORD WordCount)
Definition: modify.c:161
_Must_inspect_result_ _In_ PFSRTL_PER_STREAM_CONTEXT Ptr
Definition: fsrtlfuncs.h:898
#define IMAGE_NT_OPTIONAL_HDR32_MAGIC
Definition: ntimage.h:376
#define INVALID_HANDLE_VALUE
Definition: compat.h:391
BOOL WINAPI UpdateDebugInfoFile(PCSTR ImageFileName, PCSTR SymbolPath, PSTR DebugFilePath, PIMAGE_NT_HEADERS32 NtHeaders)
Definition: modify.c:425
#define CHECKSUM_MAPVIEW_FAILURE
Definition: imagehlp.h:51
#define FILE_SHARE_WRITE
Definition: nt_native.h:681
BOOL WINAPI MapAndLoad(PCSTR pszImageName, PCSTR pszDllPath, PLOADED_IMAGE pLoadedImage, BOOL bDotDll, BOOL bReadOnly)
Definition: access.c:131
IMAGE_OPTIONAL_HEADER32 OptionalHeader
Definition: ntddk_ex.h:184
IN PVCB IN PBCB OUT PDIRENT IN USHORT IN POEM_STRING Filename
Definition: fatprocs.h:925
#define FILE_SHARE_READ
Definition: compat.h:125
WINE_DEFAULT_DEBUG_CHANNEL(imagehlp)
uint32_t ULONG_PTR
Definition: typedefs.h:63
_Must_inspect_result_ _In_ ULONG Flags
Definition: wsk.h:170
static const char const char const char * SymbolPath
Definition: image.c:34
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
DWORD WINAPI SearchPathA(IN LPCSTR lpPath OPTIONAL, IN LPCSTR lpFileName, IN LPCSTR lpExtension OPTIONAL, IN DWORD nBufferLength, OUT LPSTR lpBuffer, OUT LPSTR *lpFilePart OPTIONAL)
Definition: path.c:1122
unsigned int BOOL
Definition: ntddk_ex.h:94
#define debugstr_w
Definition: kernel32.h:32
#define FIXME(fmt,...)
Definition: debug.h:110
smooth NULL
Definition: ftsmooth.c:416
#define FILE_MAP_READ
Definition: compat.h:427
#define ERROR_FILE_NOT_FOUND
Definition: disk.h:79
#define OPEN_EXISTING
Definition: compat.h:426
BOOL WINAPI UpdateDebugInfoFileEx(PCSTR ImageFileName, PCSTR SymbolPath, PSTR DebugFilePath, PIMAGE_NT_HEADERS32 NtHeaders, DWORD OldChecksum)
Definition: modify.c:440
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID * BaseAddress
Definition: mmfuncs.h:404
#define TRACE(s)
Definition: solgame.cpp:4
BOOL(WINAPI * PIMAGEHLP_STATUS_ROUTINE)(IMAGEHLP_STATUS_REASON, PCSTR, PCSTR, ULONG_PTR, ULONG_PTR)
Definition: imagehlp.h:115
GLsizeiptr size
Definition: glext.h:5919
PUCHAR MappedAddress
Definition: dbghelp.h:101
#define debugstr_a
Definition: kernel32.h:31
#define CreateFileMappingW(a, b, c, d, e, f)
Definition: compat.h:401
#define MAX_PATH
Definition: compat.h:26
#define WINAPI
Definition: msvc.h:8
unsigned short WORD
Definition: ntddk_ex.h:93
unsigned long DWORD
Definition: ntddk_ex.h:95
UCHAR CheckSum(LPSTR p, ULONG Len)
Definition: serial.c:197
DWORD WINAPI GetFileSize(HANDLE hFile, LPDWORD lpFileSizeHigh)
Definition: fileinfo.c:481
#define SetLastError(x)
Definition: compat.h:409
#define IMAGE_NT_OPTIONAL_HDR64_MAGIC
Definition: ntimage.h:377
static const char * ImageName
Definition: image.c:34
#define FILE_ATTRIBUTE_NORMAL
Definition: compat.h:126
#define RtlImageDirectoryEntryToData
Definition: compat.h:460
#define ERROR_INVALID_ACCESS
Definition: winerror.h:115
#define GENERIC_READ
Definition: compat.h:124
_In_ HANDLE hFile
Definition: mswsock.h:90
uint16_t * LPWORD
Definition: typedefs.h:54
#define ERR(fmt,...)
Definition: debug.h:109
#define IMAGE_SNAP_BY_ORDINAL(Ordinal)
Definition: ntimage.h:567
BOOL WINAPI RemovePrivateCvSymbolic(PCHAR DebugData, PCHAR *NewDebugData, ULONG *NewDebugSize)
Definition: modify.c:388
static const char const char * DllPath
Definition: image.c:34
signed char * PSTR
Definition: retypes.h:7
PVOID WINAPI ImageRvaToVa(_In_ PIMAGE_NT_HEADERS, _In_ PVOID, _In_ ULONG, _In_opt_ PIMAGE_SECTION_HEADER *)
#define PAGE_READONLY
Definition: compat.h:127
DWORD * PDWORD
Definition: pedump.c:68
PIMAGE_NT_HEADERS32 FileHeader
Definition: dbghelp.h:105
#define RtlImageNtHeader
Definition: compat.h:457
#define CreateFileW
Definition: compat.h:400
#define IMAGE_DIRECTORY_ENTRY_IMPORT
Definition: pedump.c:260
uint32_t * LPDWORD
Definition: typedefs.h:57
#define HIWORD(l)
Definition: typedefs.h:246
unsigned int ULONG
Definition: retypes.h:1
PIMAGE_NT_HEADERS WINAPI CheckSumMappedFile(LPVOID BaseAddress, DWORD FileLength, LPDWORD HeaderSum, LPDWORD CheckSum)
Definition: modify.c:187
#define ERROR_CALL_NOT_IMPLEMENTED
Definition: compat.h:92
BOOL WINAPI ReBaseImage(PCSTR CurrentImageName, PCSTR SymbolPath, BOOL fReBase, BOOL fRebaseSysfileOk, BOOL fGoingDown, ULONG CheckImageSize, ULONG *OldImageSize, ULONG_PTR *OldImageBase, ULONG *NewImageSize, ULONG_PTR *NewImageBase, ULONG TimeStamp)
Definition: modify.c:369
const char * PCSTR
Definition: typedefs.h:51
#define CreateFileA(a, b, c, d, e, f, g)
Definition: compat.h:399
#define UnmapViewOfFile
Definition: compat.h:403
BOOL WINAPI BindImage(PCSTR ImageName, PCSTR DllPath, PCSTR SymbolPath)
Definition: modify.c:40
#define LOWORD(l)
Definition: pedump.c:82
BOOL WINAPI SplitSymbols(PSTR ImageName, PCSTR SymbolsPath, PSTR SymbolFilePath, ULONG Flags)
Definition: modify.c:410
struct CFHEADER header
Definition: fdi.c:101