wined3d: Delete the static critical sections when unloading the dll.
[wine] / dlls / imagehlp / modify.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
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
30 WINE_DEFAULT_DEBUG_CHANNEL(imagehlp);
31
32 static WORD CalcCheckSum(DWORD StartValue, LPVOID BaseAddress, DWORD WordCount);
33
34
35 /***********************************************************************
36  *              BindImage (IMAGEHLP.@)
37  */
38 BOOL WINAPI BindImage(
39   PCSTR ImageName, PCSTR DllPath, PCSTR SymbolPath)
40 {
41   return BindImageEx(0, ImageName, DllPath, SymbolPath, NULL);
42 }
43
44 /***********************************************************************
45  *              BindImageEx (IMAGEHLP.@)
46  */
47 BOOL WINAPI BindImageEx(
48   DWORD Flags, PCSTR ImageName, PCSTR DllPath, PCSTR SymbolPath,
49   PIMAGEHLP_STATUS_ROUTINE StatusRoutine)
50 {
51   FIXME("(%d, %s, %s, %s, %p): stub\n",
52     Flags, debugstr_a(ImageName), debugstr_a(DllPath),
53     debugstr_a(SymbolPath), StatusRoutine
54   );
55   return TRUE;
56 }
57
58
59 /***********************************************************************
60  *              CheckSum (internal)
61  */
62 static WORD CalcCheckSum(
63   DWORD StartValue, LPVOID BaseAddress, DWORD WordCount)
64 {
65    LPWORD Ptr;
66    DWORD Sum;
67    DWORD i;
68
69    Sum = StartValue;
70    Ptr = (LPWORD)BaseAddress;
71    for (i = 0; i < WordCount; i++)
72      {
73         Sum += *Ptr;
74         if (HIWORD(Sum) != 0)
75           {
76              Sum = LOWORD(Sum) + HIWORD(Sum);
77           }
78         Ptr++;
79      }
80
81    return (WORD)(LOWORD(Sum) + HIWORD(Sum));
82 }
83
84
85 /***********************************************************************
86  *              CheckSumMappedFile (IMAGEHLP.@)
87  */
88 PIMAGE_NT_HEADERS WINAPI CheckSumMappedFile(
89   LPVOID BaseAddress, DWORD FileLength,
90   LPDWORD HeaderSum, LPDWORD CheckSum)
91 {
92   IMAGE_DOS_HEADER *dos = (IMAGE_DOS_HEADER *) BaseAddress;
93   PIMAGE_NT_HEADERS32 Header32;
94   PIMAGE_NT_HEADERS64 Header64;
95   DWORD *ChecksumFile;
96   DWORD CalcSum;
97   DWORD HdrSum;
98
99   TRACE("(%p, %d, %p, %p)\n",
100     BaseAddress, FileLength, HeaderSum, CheckSum
101   );
102
103   CalcSum = (DWORD)CalcCheckSum(0,
104                                 BaseAddress,
105                                 (FileLength + 1) / sizeof(WORD));
106
107   if (dos->e_magic != IMAGE_DOS_SIGNATURE)
108     return NULL;
109
110   Header32 = (IMAGE_NT_HEADERS32 *)((char *)dos + dos->e_lfanew);
111
112   if (Header32->Signature != IMAGE_NT_SIGNATURE)
113     return NULL;
114
115   if (Header32->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC)
116     ChecksumFile = &Header32->OptionalHeader.CheckSum;
117   else if (Header32->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
118   {
119     Header64 = (IMAGE_NT_HEADERS64 *)Header32;
120     ChecksumFile = &Header64->OptionalHeader.CheckSum;
121   }
122   else
123     return NULL;
124
125   HdrSum = *ChecksumFile;
126
127   /* Subtract image checksum from calculated checksum. */
128   /* fix low word of checksum */
129   if (LOWORD(CalcSum) >= LOWORD(HdrSum))
130   {
131     CalcSum -= LOWORD(HdrSum);
132   }
133   else
134   {
135     CalcSum = ((LOWORD(CalcSum) - LOWORD(HdrSum)) & 0xFFFF) - 1;
136   }
137
138    /* fix high word of checksum */
139   if (LOWORD(CalcSum) >= HIWORD(HdrSum))
140   {
141     CalcSum -= HIWORD(HdrSum);
142   }
143   else
144   {
145     CalcSum = ((LOWORD(CalcSum) - HIWORD(HdrSum)) & 0xFFFF) - 1;
146   }
147
148   /* add file length */
149   CalcSum += FileLength;
150
151   *CheckSum = CalcSum;
152   *HeaderSum = *ChecksumFile;
153
154   return (PIMAGE_NT_HEADERS) Header32;
155 }
156
157 /***********************************************************************
158  *              MapFileAndCheckSumA (IMAGEHLP.@)
159  */
160 DWORD WINAPI MapFileAndCheckSumA(
161   PCSTR Filename, PDWORD HeaderSum, PDWORD CheckSum)
162 {
163   HANDLE hFile;
164   HANDLE hMapping;
165   LPVOID BaseAddress;
166   DWORD FileLength;
167
168   TRACE("(%s, %p, %p): stub\n",
169     debugstr_a(Filename), HeaderSum, CheckSum
170   );
171
172   hFile = CreateFileA(Filename,
173                       GENERIC_READ,
174                       FILE_SHARE_READ | FILE_SHARE_WRITE,
175                       NULL,
176                       OPEN_EXISTING,
177                       FILE_ATTRIBUTE_NORMAL,
178                       0);
179   if (hFile == INVALID_HANDLE_VALUE)
180   {
181     return CHECKSUM_OPEN_FAILURE;
182   }
183
184   hMapping = CreateFileMappingW(hFile,
185                                NULL,
186                                PAGE_READONLY,
187                                0,
188                                0,
189                                NULL);
190   if (hMapping == 0)
191   {
192     CloseHandle(hFile);
193     return CHECKSUM_MAP_FAILURE;
194   }
195
196   BaseAddress = MapViewOfFile(hMapping,
197                               FILE_MAP_READ,
198                               0,
199                               0,
200                               0);
201   if (BaseAddress == 0)
202   {
203     CloseHandle(hMapping);
204     CloseHandle(hFile);
205     return CHECKSUM_MAPVIEW_FAILURE;
206   }
207
208   FileLength = GetFileSize(hFile,
209                            NULL);
210
211   CheckSumMappedFile(BaseAddress,
212                      FileLength,
213                      HeaderSum,
214                      CheckSum);
215
216   UnmapViewOfFile(BaseAddress);
217   CloseHandle(hMapping);
218   CloseHandle(hFile);
219
220   return 0;
221 }
222
223 /***********************************************************************
224  *              MapFileAndCheckSumW (IMAGEHLP.@)
225  */
226 DWORD WINAPI MapFileAndCheckSumW(
227   PCWSTR Filename, PDWORD HeaderSum, PDWORD CheckSum)
228 {
229   HANDLE hFile;
230   HANDLE hMapping;
231   LPVOID BaseAddress;
232   DWORD FileLength;
233
234   TRACE("(%s, %p, %p): stub\n",
235     debugstr_w(Filename), HeaderSum, CheckSum
236   );
237
238   hFile = CreateFileW(Filename,
239                       GENERIC_READ,
240                       FILE_SHARE_READ | FILE_SHARE_WRITE,
241                       NULL,
242                       OPEN_EXISTING,
243                       FILE_ATTRIBUTE_NORMAL,
244                       0);
245   if (hFile == INVALID_HANDLE_VALUE)
246   {
247   return CHECKSUM_OPEN_FAILURE;
248   }
249
250   hMapping = CreateFileMappingW(hFile,
251                                NULL,
252                                PAGE_READONLY,
253                                0,
254                                0,
255                                NULL);
256   if (hMapping == 0)
257   {
258     CloseHandle(hFile);
259     return CHECKSUM_MAP_FAILURE;
260   }
261
262   BaseAddress = MapViewOfFile(hMapping,
263                               FILE_MAP_READ,
264                               0,
265                               0,
266                               0);
267   if (BaseAddress == 0)
268   {
269     CloseHandle(hMapping);
270     CloseHandle(hFile);
271     return CHECKSUM_MAPVIEW_FAILURE;
272   }
273
274   FileLength = GetFileSize(hFile,
275                            NULL);
276
277   CheckSumMappedFile(BaseAddress,
278                      FileLength,
279                      HeaderSum,
280                      CheckSum);
281
282   UnmapViewOfFile(BaseAddress);
283   CloseHandle(hMapping);
284   CloseHandle(hFile);
285
286   return 0;
287 }
288
289 /***********************************************************************
290  *              ReBaseImage (IMAGEHLP.@)
291  */
292 BOOL WINAPI ReBaseImage(
293   PCSTR CurrentImageName, PCSTR SymbolPath, BOOL fReBase,
294   BOOL fRebaseSysfileOk, BOOL fGoingDown, ULONG CheckImageSize,
295   ULONG *OldImageSize, ULONG_PTR *OldImageBase, ULONG *NewImageSize,
296   ULONG_PTR *NewImageBase, ULONG TimeStamp)
297 {
298   FIXME(
299     "(%s, %s, %d, %d, %d, %d, %p, %p, %p, %p, %d): stub\n",
300       debugstr_a(CurrentImageName),debugstr_a(SymbolPath), fReBase,
301       fRebaseSysfileOk, fGoingDown, CheckImageSize, OldImageSize,
302       OldImageBase, NewImageSize, NewImageBase, TimeStamp
303   );
304   SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
305   return FALSE;
306 }
307
308 /***********************************************************************
309  *              RemovePrivateCvSymbolic (IMAGEHLP.@)
310  */
311 BOOL WINAPI RemovePrivateCvSymbolic(
312   PCHAR DebugData, PCHAR *NewDebugData, ULONG *NewDebugSize)
313 {
314   FIXME("(%p, %p, %p): stub\n",
315     DebugData, NewDebugData, NewDebugSize
316   );
317   SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
318   return FALSE;
319 }
320
321 /***********************************************************************
322  *              RemoveRelocations (IMAGEHLP.@)
323  */
324 VOID WINAPI RemoveRelocations(PCHAR ImageName)
325 {
326   FIXME("(%p): stub\n", ImageName);
327   SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
328 }
329
330 /***********************************************************************
331  *              SplitSymbols (IMAGEHLP.@)
332  */
333 BOOL WINAPI SplitSymbols(
334   PSTR ImageName, PCSTR SymbolsPath,
335   PSTR SymbolFilePath, ULONG Flags)
336 {
337   FIXME("(%s, %s, %s, %d): stub\n",
338     debugstr_a(ImageName), debugstr_a(SymbolsPath),
339     debugstr_a(SymbolFilePath), Flags
340   );
341   SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
342   return FALSE;
343 }
344
345 /***********************************************************************
346  *              UpdateDebugInfoFile (IMAGEHLP.@)
347  */
348 BOOL WINAPI UpdateDebugInfoFile(
349   PCSTR ImageFileName, PCSTR SymbolPath,
350   PSTR DebugFilePath, PIMAGE_NT_HEADERS32 NtHeaders)
351 {
352   FIXME("(%s, %s, %s, %p): stub\n",
353     debugstr_a(ImageFileName), debugstr_a(SymbolPath),
354     debugstr_a(DebugFilePath), NtHeaders
355   );
356   SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
357   return FALSE;
358 }
359
360 /***********************************************************************
361  *              UpdateDebugInfoFileEx (IMAGEHLP.@)
362  */
363 BOOL WINAPI UpdateDebugInfoFileEx(
364   PCSTR ImageFileName, PCSTR SymbolPath, PSTR DebugFilePath,
365   PIMAGE_NT_HEADERS32 NtHeaders, DWORD OldChecksum)
366 {
367   FIXME("(%s, %s, %s, %p, %d): stub\n",
368     debugstr_a(ImageFileName), debugstr_a(SymbolPath),
369     debugstr_a(DebugFilePath), NtHeaders, OldChecksum
370   );
371   SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
372   return FALSE;
373 }