crypt32: Make sure we show Unicode characters (Dutch translation).
[wine] / dlls / dbghelp / module.c
1 /*
2  * File module.c - module handling for the wine debugger
3  *
4  * Copyright (C) 1993,      Eric Youngdale.
5  *               2000-2007, Eric Pouech
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include "config.h"
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <assert.h>
27
28 #include "dbghelp_private.h"
29 #include "psapi.h"
30 #include "winternl.h"
31 #include "wine/debug.h"
32
33 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
34
35 const WCHAR        S_ElfW[]         = {'<','e','l','f','>','\0'};
36 const WCHAR        S_WineLoaderW[]  = {'<','w','i','n','e','-','l','o','a','d','e','r','>','\0'};
37 static const WCHAR S_DotSoW[]       = {'.','s','o','\0'};
38 static const WCHAR S_DotDylibW[]    = {'.','d','y','l','i','b','\0'};
39 static const WCHAR S_DotPdbW[]      = {'.','p','d','b','\0'};
40 static const WCHAR S_DotDbgW[]      = {'.','d','b','g','\0'};
41 const WCHAR        S_WineW[]        = {'w','i','n','e',0};
42 const WCHAR        S_SlashW[]       = {'/','\0'};
43
44 static const WCHAR S_AcmW[] = {'.','a','c','m','\0'};
45 static const WCHAR S_DllW[] = {'.','d','l','l','\0'};
46 static const WCHAR S_DrvW[] = {'.','d','r','v','\0'};
47 static const WCHAR S_ExeW[] = {'.','e','x','e','\0'};
48 static const WCHAR S_OcxW[] = {'.','o','c','x','\0'};
49 static const WCHAR S_VxdW[] = {'.','v','x','d','\0'};
50 static const WCHAR * const ext[] = {S_AcmW, S_DllW, S_DrvW, S_ExeW, S_OcxW, S_VxdW, NULL};
51
52 static int match_ext(const WCHAR* ptr, size_t len)
53 {
54     const WCHAR* const *e;
55     size_t      l;
56
57     for (e = ext; *e; e++)
58     {
59         l = strlenW(*e);
60         if (l >= len) return FALSE;
61         if (strncmpiW(&ptr[len - l], *e, l)) continue;
62         return l;
63     }
64     return 0;
65 }
66
67 static const WCHAR* get_filename(const WCHAR* name, const WCHAR* endptr)
68 {
69     const WCHAR*        ptr;
70
71     if (!endptr) endptr = name + strlenW(name);
72     for (ptr = endptr - 1; ptr >= name; ptr--)
73     {
74         if (*ptr == '/' || *ptr == '\\') break;
75     }
76     return ++ptr;
77 }
78
79 static void module_fill_module(const WCHAR* in, WCHAR* out, size_t size)
80 {
81     const WCHAR *ptr, *endptr;
82     size_t      len, l;
83
84     ptr = get_filename(in, endptr = in + strlenW(in));
85     len = min(endptr - ptr, size - 1);
86     memcpy(out, ptr, len * sizeof(WCHAR));
87     out[len] = '\0';
88     if (len > 4 && (l = match_ext(out, len)))
89         out[len - l] = '\0';
90     else if (len > 4 && !strcmpiW(out + len - 4, S_WineW))
91         lstrcpynW(out, S_WineLoaderW, size);
92     else
93     {
94         if (len > 3 && !strcmpiW(&out[len - 3], S_DotSoW) &&
95             (l = match_ext(out, len - 3)))
96             strcpyW(&out[len - l - 3], S_ElfW);
97     }
98     while ((*out = tolowerW(*out))) out++;
99 }
100
101 void module_set_module(struct module* module, const WCHAR* name)
102 {
103     module_fill_module(name, module->module.ModuleName, sizeof(module->module.ModuleName));
104     WideCharToMultiByte(CP_ACP, 0, module->module.ModuleName, -1,
105                         module->module_name, sizeof(module->module_name),
106                         NULL, NULL);
107 }
108
109 static const char*      get_module_type(enum module_type type, BOOL virtual)
110 {
111     switch (type)
112     {
113     case DMT_ELF: return virtual ? "Virtual ELF" : "ELF";
114     case DMT_PE: return virtual ? "Virtual PE" : "PE";
115     case DMT_MACHO: return virtual ? "Virtual Mach-O" : "Mach-O";
116     default: return "---";
117     }
118 }
119
120 /***********************************************************************
121  * Creates and links a new module to a process
122  */
123 struct module* module_new(struct process* pcs, const WCHAR* name,
124                           enum module_type type, BOOL virtual,
125                           unsigned long mod_addr, unsigned long size,
126                           unsigned long stamp, unsigned long checksum)
127 {
128     struct module*      module;
129
130     assert(type == DMT_ELF || type == DMT_PE || type == DMT_MACHO);
131     if (!(module = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*module))))
132         return NULL;
133
134     module->next = pcs->lmodules;
135     pcs->lmodules = module;
136
137     TRACE("=> %s %08lx-%08lx %s\n",
138           get_module_type(type, virtual), mod_addr, mod_addr + size,
139           debugstr_w(name));
140
141     pool_init(&module->pool, 65536);
142
143     module->module.SizeOfStruct = sizeof(module->module);
144     module->module.BaseOfImage = mod_addr;
145     module->module.ImageSize = size;
146     module_set_module(module, name);
147     module->module.ImageName[0] = '\0';
148     lstrcpynW(module->module.LoadedImageName, name, sizeof(module->module.LoadedImageName) / sizeof(WCHAR));
149     module->module.SymType = SymNone;
150     module->module.NumSyms = 0;
151     module->module.TimeDateStamp = stamp;
152     module->module.CheckSum = checksum;
153
154     memset(module->module.LoadedPdbName, 0, sizeof(module->module.CVData));
155     module->module.CVSig = 0;
156     memset(module->module.CVData, 0, sizeof(module->module.CVData));
157     module->module.PdbSig = 0;
158     memset(&module->module.PdbSig70, 0, sizeof(module->module.PdbSig70));
159     module->module.PdbAge = 0;
160     module->module.PdbUnmatched = FALSE;
161     module->module.DbgUnmatched = FALSE;
162     module->module.LineNumbers = FALSE;
163     module->module.GlobalSymbols = FALSE;
164     module->module.TypeInfo = FALSE;
165     module->module.SourceIndexed = FALSE;
166     module->module.Publics = FALSE;
167
168     module->type              = type;
169     module->is_virtual        = virtual ? TRUE : FALSE;
170     module->sortlist_valid    = FALSE;
171     module->addr_sorttab      = NULL;
172     module->num_sorttab       = 0;
173     module->num_symbols       = 0;
174     /* FIXME: this seems a bit too high (on a per module basis)
175      * need some statistics about this
176      */
177     hash_table_init(&module->pool, &module->ht_symbols, 4096);
178     hash_table_init(&module->pool, &module->ht_types,   4096);
179     vector_init(&module->vtypes, sizeof(struct symt*),  32);
180
181     module->sources_used      = 0;
182     module->sources_alloc     = 0;
183     module->sources           = 0;
184
185     return module;
186 }
187
188 /***********************************************************************
189  *      module_find_by_name
190  *
191  */
192 static struct module* module_find_by_name(const struct process* pcs, const WCHAR* name)
193 {
194     struct module*      module;
195
196     for (module = pcs->lmodules; module; module = module->next)
197     {
198         if (!strcmpiW(name, module->module.ModuleName)) return module;
199     }
200     SetLastError(ERROR_INVALID_NAME);
201     return NULL;
202 }
203
204 struct module* module_find_by_nameA(const struct process* pcs, const char* name)
205 {
206     WCHAR wname[MAX_PATH];
207
208     MultiByteToWideChar(CP_ACP, 0, name, -1, wname, sizeof(wname) / sizeof(WCHAR));
209     return module_find_by_name(pcs, wname);
210 }
211
212 /***********************************************************************
213  *      module_is_already_loaded
214  *
215  */
216 struct module* module_is_already_loaded(const struct process* pcs, const WCHAR* name)
217 {
218     struct module*      module;
219     const WCHAR*        filename;
220
221     /* first compare the loaded image name... */
222     for (module = pcs->lmodules; module; module = module->next)
223     {
224         if (!strcmpiW(name, module->module.LoadedImageName))
225             return module;
226     }
227     /* then compare the standard filenames (without the path) ... */
228     filename = get_filename(name, NULL);
229     for (module = pcs->lmodules; module; module = module->next)
230     {
231         if (!strcmpiW(filename, get_filename(module->module.LoadedImageName, NULL)))
232             return module;
233     }
234     SetLastError(ERROR_INVALID_NAME);
235     return NULL;
236 }
237
238 /***********************************************************************
239  *           module_get_container
240  *
241  */
242 static struct module* module_get_container(const struct process* pcs,
243                                     const struct module* inner)
244 {
245     struct module*      module;
246      
247     for (module = pcs->lmodules; module; module = module->next)
248     {
249         if (module != inner &&
250             module->module.BaseOfImage <= inner->module.BaseOfImage &&
251             module->module.BaseOfImage + module->module.ImageSize >=
252             inner->module.BaseOfImage + inner->module.ImageSize)
253             return module;
254     }
255     return NULL;
256 }
257
258 /***********************************************************************
259  *           module_get_containee
260  *
261  */
262 struct module* module_get_containee(const struct process* pcs, 
263                                     const struct module* outter)
264 {
265     struct module*      module;
266      
267     for (module = pcs->lmodules; module; module = module->next)
268     {
269         if (module != outter &&
270             outter->module.BaseOfImage <= module->module.BaseOfImage &&
271             outter->module.BaseOfImage + outter->module.ImageSize >=
272             module->module.BaseOfImage + module->module.ImageSize)
273             return module;
274     }
275     return NULL;
276 }
277
278 /******************************************************************
279  *              module_get_debug
280  *
281  * get the debug information from a module:
282  * - if the module's type is deferred, then force loading of debug info (and return
283  *   the module itself)
284  * - if the module has no debug info and has an ELF container, then return the ELF
285  *   container (and also force the ELF container's debug info loading if deferred)
286  * - otherwise return the module itself if it has some debug info
287  */
288 BOOL module_get_debug(struct module_pair* pair)
289 {
290     IMAGEHLP_DEFERRED_SYMBOL_LOADW64    idslW64;
291
292     if (!pair->requested) return FALSE;
293     /* for a PE builtin, always get info from container */
294     if (!(pair->effective = module_get_container(pair->pcs, pair->requested)))
295         pair->effective = pair->requested;
296     /* if deferred, force loading */
297     if (pair->effective->module.SymType == SymDeferred)
298     {
299         BOOL ret;
300         
301         if (pair->effective->is_virtual) ret = FALSE;
302         else switch (pair->effective->type)
303         {
304         case DMT_ELF:
305             ret = elf_load_debug_info(pair->effective, NULL);
306             break;
307         case DMT_PE:
308             idslW64.SizeOfStruct = sizeof(idslW64);
309             idslW64.BaseOfImage = pair->effective->module.BaseOfImage;
310             idslW64.CheckSum = pair->effective->module.CheckSum;
311             idslW64.TimeDateStamp = pair->effective->module.TimeDateStamp;
312             memcpy(idslW64.FileName, pair->effective->module.ImageName,
313                    sizeof(pair->effective->module.ImageName));
314             idslW64.Reparse = FALSE;
315             idslW64.hFile = INVALID_HANDLE_VALUE;
316
317             pcs_callback(pair->pcs, CBA_DEFERRED_SYMBOL_LOAD_START, &idslW64);
318             ret = pe_load_debug_info(pair->pcs, pair->effective);
319             pcs_callback(pair->pcs,
320                          ret ? CBA_DEFERRED_SYMBOL_LOAD_COMPLETE : CBA_DEFERRED_SYMBOL_LOAD_FAILURE,
321                          &idslW64);
322             break;
323         case DMT_MACHO:
324             ret = macho_load_debug_info(pair->effective, NULL);
325             break;
326         default:
327             ret = FALSE;
328             break;
329         }
330         if (!ret) pair->effective->module.SymType = SymNone;
331         assert(pair->effective->module.SymType != SymDeferred);
332         pair->effective->module.NumSyms = pair->effective->ht_symbols.num_elts;
333     }
334     return pair->effective->module.SymType != SymNone;
335 }
336
337 /***********************************************************************
338  *      module_find_by_addr
339  *
340  * either the addr where module is loaded, or any address inside the 
341  * module
342  */
343 struct module* module_find_by_addr(const struct process* pcs, unsigned long addr, 
344                                    enum module_type type)
345 {
346     struct module*      module;
347     
348     if (type == DMT_UNKNOWN)
349     {
350         if ((module = module_find_by_addr(pcs, addr, DMT_PE)) ||
351             (module = module_find_by_addr(pcs, addr, DMT_ELF)) ||
352             (module = module_find_by_addr(pcs, addr, DMT_MACHO)))
353             return module;
354     }
355     else
356     {
357         for (module = pcs->lmodules; module; module = module->next)
358         {
359             if (type == module->type && addr >= module->module.BaseOfImage &&
360                 addr < module->module.BaseOfImage + module->module.ImageSize) 
361                 return module;
362         }
363     }
364     SetLastError(ERROR_INVALID_ADDRESS);
365     return module;
366 }
367
368 /******************************************************************
369  *              module_is_container_loaded
370  *
371  * checks whether the native container, for a (supposed) PE builtin is
372  * already loaded
373  */
374 static BOOL module_is_container_loaded(const struct process* pcs,
375                                        const WCHAR* ImageName, DWORD base)
376 {
377     size_t              len;
378     struct module*      module;
379     PCWSTR              filename, modname;
380
381     if (!base) return FALSE;
382     filename = get_filename(ImageName, NULL);
383     len = strlenW(filename);
384
385     for (module = pcs->lmodules; module; module = module->next)
386     {
387         if ((module->type == DMT_ELF || module->type == DMT_MACHO) &&
388             base >= module->module.BaseOfImage &&
389             base < module->module.BaseOfImage + module->module.ImageSize)
390         {
391             modname = get_filename(module->module.LoadedImageName, NULL);
392             if (!strncmpiW(modname, filename, len) &&
393                 !memcmp(modname + len, S_DotSoW, 3 * sizeof(WCHAR)))
394             {
395                 return TRUE;
396             }
397         }
398     }
399     /* likely a native PE module */
400     WARN("Couldn't find container for %s\n", debugstr_w(ImageName));
401     return FALSE;
402 }
403
404 /******************************************************************
405  *              module_get_type_by_name
406  *
407  * Guesses a filename type from its extension
408  */
409 enum module_type module_get_type_by_name(const WCHAR* name)
410 {
411     int len = strlenW(name);
412
413     /* Skip all version extensions (.[digits]) regex: "(\.\d+)*$" */
414     do
415     {
416         int i = len;
417
418         while (i && isdigit(name[i - 1])) i--;
419
420         if (i && name[i - 1] == '.')
421             len = i - 1;
422         else
423             break;
424     } while (len);
425
426     /* check for terminating .so or .so.[digit] */
427     /* FIXME: Can't rely solely on extension; have to check magic or
428      *        stop using .so on Mac OS X.  For now, base on platform. */
429     if (len > 3 && !memcmp(name + len - 3, S_DotSoW, 3))
430 #ifdef __APPLE__
431         return DMT_MACHO;
432 #else
433         return DMT_ELF;
434 #endif
435
436     if (len > 6 && !strncmpiW(name + len - 6, S_DotDylibW, 6))
437         return DMT_MACHO;
438
439     if (len > 4 && !strncmpiW(name + len - 4, S_DotPdbW, 4))
440         return DMT_PDB;
441
442     if (len > 4 && !strncmpiW(name + len - 4, S_DotDbgW, 4))
443         return DMT_DBG;
444
445     /* wine is also a native module (Mach-O on Mac OS X, ELF elsewhere) */
446     if (((len > 4 && name[len - 5] == '/') || len == 4) && !strcmpiW(name + len - 4, S_WineW))
447     {
448 #ifdef __APPLE__
449         return DMT_MACHO;
450 #else
451         return DMT_ELF;
452 #endif
453     }
454     return DMT_PE;
455 }
456
457 /***********************************************************************
458  *                      SymLoadModule (DBGHELP.@)
459  */
460 DWORD WINAPI SymLoadModule(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
461                            PCSTR ModuleName, DWORD BaseOfDll, DWORD SizeOfDll)
462 {
463     return SymLoadModuleEx(hProcess, hFile, ImageName, ModuleName, BaseOfDll,
464                            SizeOfDll, NULL, 0);
465 }
466
467 /***********************************************************************
468  *                      SymLoadModuleEx (DBGHELP.@)
469  */
470 DWORD64 WINAPI  SymLoadModuleEx(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
471                                 PCSTR ModuleName, DWORD64 BaseOfDll, DWORD DllSize,
472                                 PMODLOAD_DATA Data, DWORD Flags)
473 {
474     PWSTR       wImageName, wModuleName;
475     unsigned    len;
476     DWORD64     ret;
477
478     TRACE("(%p %p %s %s %s %08x %p %08x)\n",
479           hProcess, hFile, debugstr_a(ImageName), debugstr_a(ModuleName),
480           wine_dbgstr_longlong(BaseOfDll), DllSize, Data, Flags);
481
482     if (ImageName)
483     {
484         len = MultiByteToWideChar(CP_ACP, 0, ImageName, -1, NULL, 0);
485         wImageName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
486         MultiByteToWideChar(CP_ACP, 0, ImageName, -1, wImageName, len);
487     }
488     else wImageName = NULL;
489     if (ModuleName)
490     {
491         len = MultiByteToWideChar(CP_ACP, 0, ModuleName, -1, NULL, 0);
492         wModuleName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
493         MultiByteToWideChar(CP_ACP, 0, ModuleName, -1, wModuleName, len);
494     }
495     else wModuleName = NULL;
496
497     ret = SymLoadModuleExW(hProcess, hFile, wImageName, wModuleName,
498                           BaseOfDll, DllSize, Data, Flags);
499     HeapFree(GetProcessHeap(), 0, wImageName);
500     HeapFree(GetProcessHeap(), 0, wModuleName);
501     return ret;
502 }
503
504 /***********************************************************************
505  *                      SymLoadModuleExW (DBGHELP.@)
506  */
507 DWORD64 WINAPI  SymLoadModuleExW(HANDLE hProcess, HANDLE hFile, PCWSTR wImageName,
508                                  PCWSTR wModuleName, DWORD64 BaseOfDll, DWORD SizeOfDll,
509                                  PMODLOAD_DATA Data, DWORD Flags)
510 {
511     struct process*     pcs;
512     struct module*      module = NULL;
513
514     TRACE("(%p %p %s %s %s %08x %p %08x)\n",
515           hProcess, hFile, debugstr_w(wImageName), debugstr_w(wModuleName),
516           wine_dbgstr_longlong(BaseOfDll), SizeOfDll, Data, Flags);
517
518     if (Data)
519         FIXME("Unsupported load data parameter %p for %s\n",
520               Data, debugstr_w(wImageName));
521     if (!validate_addr64(BaseOfDll)) return FALSE;
522
523     if (!(pcs = process_find_by_handle(hProcess))) return FALSE;
524
525     if (Flags & SLMFLAG_VIRTUAL)
526     {
527         module = module_new(pcs, wImageName, module_get_type_by_name(wImageName),
528                             TRUE, (DWORD)BaseOfDll, SizeOfDll, 0, 0);
529         if (!module) return FALSE;
530         if (wModuleName) module_set_module(module, wModuleName);
531         module->module.SymType = SymVirtual;
532
533         return TRUE;
534     }
535     if (Flags & ~(SLMFLAG_VIRTUAL))
536         FIXME("Unsupported Flags %08x for %s\n", Flags, debugstr_w(wImageName));
537
538     /* force transparent ELF and Mach-O loading / unloading */
539     elf_synchronize_module_list(pcs);
540     macho_synchronize_module_list(pcs);
541
542     /* this is a Wine extension to the API just to redo the synchronisation */
543     if (!wImageName && !hFile) return 0;
544
545     /* check if the module is already loaded, or if it's a builtin PE module with
546      * an containing ELF module
547      */
548     if (wImageName)
549     {
550         module = module_is_already_loaded(pcs, wImageName);
551         if (!module && module_is_container_loaded(pcs, wImageName, BaseOfDll))
552         {
553             /* force the loading of DLL as builtin */
554             module = pe_load_builtin_module(pcs, wImageName, BaseOfDll, SizeOfDll);
555         }
556     }
557     if (!module)
558     {
559         /* otherwise, try a regular PE module */
560         if (!(module = pe_load_native_module(pcs, wImageName, hFile, BaseOfDll, SizeOfDll)) &&
561             wImageName)
562         {
563             /* and finally an ELF or Mach-O module */
564             switch (module_get_type_by_name(wImageName))
565             {
566                 case DMT_ELF:
567                     module = elf_load_module(pcs, wImageName, BaseOfDll);
568                     break;
569                 case DMT_MACHO:
570                     module = macho_load_module(pcs, wImageName, BaseOfDll);
571                     break;
572                 default:
573                     /* Ignored */
574                     break;
575             }
576         }
577     }
578     if (!module)
579     {
580         WARN("Couldn't locate %s\n", debugstr_w(wImageName));
581         return 0;
582     }
583     module->module.NumSyms = module->ht_symbols.num_elts;
584     /* by default module_new fills module.ModuleName from a derivation
585      * of LoadedImageName. Overwrite it, if we have better information
586      */
587     if (wModuleName)
588         module_set_module(module, wModuleName);
589     lstrcpynW(module->module.ImageName, wImageName,
590               sizeof(module->module.ImageName) / sizeof(WCHAR));
591
592     return module->module.BaseOfImage;
593 }
594
595 /***********************************************************************
596  *                     SymLoadModule64 (DBGHELP.@)
597  */
598 DWORD64 WINAPI SymLoadModule64(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
599                                PCSTR ModuleName, DWORD64 BaseOfDll, DWORD SizeOfDll)
600 {
601     if (!validate_addr64(BaseOfDll)) return FALSE;
602     return SymLoadModule(hProcess, hFile, ImageName, ModuleName, (DWORD)BaseOfDll, SizeOfDll);
603 }
604
605 /******************************************************************
606  *              module_remove
607  *
608  */
609 BOOL module_remove(struct process* pcs, struct module* module)
610 {
611     struct module**     p;
612
613     TRACE("%s (%p)\n", debugstr_w(module->module.ModuleName), module);
614     hash_table_destroy(&module->ht_symbols);
615     hash_table_destroy(&module->ht_types);
616     HeapFree(GetProcessHeap(), 0, module->sources);
617     HeapFree(GetProcessHeap(), 0, module->addr_sorttab);
618     HeapFree(GetProcessHeap(), 0, module->dwarf2_info);
619     pool_destroy(&module->pool);
620     /* native dbghelp doesn't invoke registered callback(,CBA_SYMBOLS_UNLOADED,) here
621      * so do we
622      */
623     for (p = &pcs->lmodules; *p; p = &(*p)->next)
624     {
625         if (*p == module)
626         {
627             *p = module->next;
628             HeapFree(GetProcessHeap(), 0, module);
629             return TRUE;
630         }
631     }
632     FIXME("This shouldn't happen\n");
633     return FALSE;
634 }
635
636 /******************************************************************
637  *              SymUnloadModule (DBGHELP.@)
638  *
639  */
640 BOOL WINAPI SymUnloadModule(HANDLE hProcess, DWORD BaseOfDll)
641 {
642     struct process*     pcs;
643     struct module*      module;
644
645     pcs = process_find_by_handle(hProcess);
646     if (!pcs) return FALSE;
647     module = module_find_by_addr(pcs, BaseOfDll, DMT_UNKNOWN);
648     if (!module) return FALSE;
649     return module_remove(pcs, module);
650 }
651
652 /******************************************************************
653  *              SymUnloadModule64 (DBGHELP.@)
654  *
655  */
656 BOOL WINAPI SymUnloadModule64(HANDLE hProcess, DWORD64 BaseOfDll)
657 {
658     struct process*     pcs;
659     struct module*      module;
660
661     pcs = process_find_by_handle(hProcess);
662     if (!pcs) return FALSE;
663     if (!validate_addr64(BaseOfDll)) return FALSE;
664     module = module_find_by_addr(pcs, (DWORD)BaseOfDll, DMT_UNKNOWN);
665     if (!module) return FALSE;
666     return module_remove(pcs, module);
667 }
668
669 /******************************************************************
670  *              SymEnumerateModules (DBGHELP.@)
671  *
672  */
673 struct enum_modW64_32
674 {
675     PSYM_ENUMMODULES_CALLBACK   cb;
676     PVOID                       user;
677     char                        module[MAX_PATH];
678 };
679
680 static BOOL CALLBACK enum_modW64_32(PCWSTR name, DWORD64 base, PVOID user)
681 {
682     struct enum_modW64_32*      x = user;
683
684     WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
685     return x->cb(x->module, (DWORD)base, x->user);
686 }
687
688 BOOL  WINAPI SymEnumerateModules(HANDLE hProcess,
689                                  PSYM_ENUMMODULES_CALLBACK EnumModulesCallback,  
690                                  PVOID UserContext)
691 {
692     struct enum_modW64_32       x;
693
694     x.cb = EnumModulesCallback;
695     x.user = UserContext;
696
697     return SymEnumerateModulesW64(hProcess, enum_modW64_32, &x);
698 }
699
700 /******************************************************************
701  *              SymEnumerateModules64 (DBGHELP.@)
702  *
703  */
704 struct enum_modW64_64
705 {
706     PSYM_ENUMMODULES_CALLBACK64 cb;
707     PVOID                       user;
708     char                        module[MAX_PATH];
709 };
710
711 static BOOL CALLBACK enum_modW64_64(PCWSTR name, DWORD64 base, PVOID user)
712 {
713     struct enum_modW64_64*      x = user;
714
715     WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
716     return x->cb(x->module, base, x->user);
717 }
718
719 BOOL  WINAPI SymEnumerateModules64(HANDLE hProcess,
720                                    PSYM_ENUMMODULES_CALLBACK64 EnumModulesCallback,  
721                                    PVOID UserContext)
722 {
723     struct enum_modW64_64       x;
724
725     x.cb = EnumModulesCallback;
726     x.user = UserContext;
727
728     return SymEnumerateModulesW64(hProcess, enum_modW64_64, &x);
729 }
730
731 /******************************************************************
732  *              SymEnumerateModulesW64 (DBGHELP.@)
733  *
734  */
735 BOOL  WINAPI SymEnumerateModulesW64(HANDLE hProcess,
736                                     PSYM_ENUMMODULES_CALLBACKW64 EnumModulesCallback,
737                                     PVOID UserContext)
738 {
739     struct process*     pcs = process_find_by_handle(hProcess);
740     struct module*      module;
741
742     if (!pcs) return FALSE;
743     
744     for (module = pcs->lmodules; module; module = module->next)
745     {
746         if (!(dbghelp_options & SYMOPT_WINE_WITH_NATIVE_MODULES) &&
747             (module->type == DMT_ELF || module->type == DMT_MACHO))
748             continue;
749         if (!EnumModulesCallback(module->module.ModuleName,
750                                  module->module.BaseOfImage, UserContext))
751             break;
752     }
753     return TRUE;
754 }
755
756 /******************************************************************
757  *              EnumerateLoadedModules64 (DBGHELP.@)
758  *
759  */
760 struct enum_load_modW64_64
761 {
762     PENUMLOADED_MODULES_CALLBACK64      cb;
763     PVOID                               user;
764     char                                module[MAX_PATH];
765 };
766
767 static BOOL CALLBACK enum_load_modW64_64(PCWSTR name, DWORD64 base, ULONG size,
768                                          PVOID user)
769 {
770     struct enum_load_modW64_64* x = user;
771
772     WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
773     return x->cb(x->module, base, size, x->user);
774 }
775
776 BOOL  WINAPI EnumerateLoadedModules64(HANDLE hProcess,
777                                       PENUMLOADED_MODULES_CALLBACK64 EnumLoadedModulesCallback,
778                                       PVOID UserContext)
779 {
780     struct enum_load_modW64_64  x;
781
782     x.cb = EnumLoadedModulesCallback;
783     x.user = UserContext;
784
785     return EnumerateLoadedModulesW64(hProcess, enum_load_modW64_64, &x);
786 }
787
788 /******************************************************************
789  *              EnumerateLoadedModules (DBGHELP.@)
790  *
791  */
792 struct enum_load_modW64_32
793 {
794     PENUMLOADED_MODULES_CALLBACK        cb;
795     PVOID                               user;
796     char                                module[MAX_PATH];
797 };
798
799 static BOOL CALLBACK enum_load_modW64_32(PCWSTR name, DWORD64 base, ULONG size,
800                                          PVOID user)
801 {
802     struct enum_load_modW64_32* x = user;
803     WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
804     return x->cb(x->module, (DWORD)base, size, x->user);
805 }
806
807 BOOL  WINAPI EnumerateLoadedModules(HANDLE hProcess,
808                                     PENUMLOADED_MODULES_CALLBACK EnumLoadedModulesCallback,
809                                     PVOID UserContext)
810 {
811     struct enum_load_modW64_32  x;
812
813     x.cb = EnumLoadedModulesCallback;
814     x.user = UserContext;
815
816     return EnumerateLoadedModulesW64(hProcess, enum_load_modW64_32, &x);
817 }
818
819 /******************************************************************
820  *              EnumerateLoadedModulesW64 (DBGHELP.@)
821  *
822  */
823 BOOL  WINAPI EnumerateLoadedModulesW64(HANDLE hProcess,
824                                        PENUMLOADED_MODULES_CALLBACKW64 EnumLoadedModulesCallback,
825                                        PVOID UserContext)
826 {
827     HMODULE*    hMods;
828     WCHAR       baseW[256], modW[256];
829     DWORD       i, sz;
830     MODULEINFO  mi;
831
832     hMods = HeapAlloc(GetProcessHeap(), 0, 256 * sizeof(hMods[0]));
833     if (!hMods) return FALSE;
834
835     if (!EnumProcessModules(hProcess, hMods, 256 * sizeof(hMods[0]), &sz))
836     {
837         /* hProcess should also be a valid process handle !! */
838         FIXME("If this happens, bump the number in mod\n");
839         HeapFree(GetProcessHeap(), 0, hMods);
840         return FALSE;
841     }
842     sz /= sizeof(HMODULE);
843     for (i = 0; i < sz; i++)
844     {
845         if (!GetModuleInformation(hProcess, hMods[i], &mi, sizeof(mi)) ||
846             !GetModuleBaseNameW(hProcess, hMods[i], baseW, sizeof(baseW) / sizeof(WCHAR)))
847             continue;
848         module_fill_module(baseW, modW, sizeof(modW) / sizeof(CHAR));
849         EnumLoadedModulesCallback(modW, (DWORD_PTR)mi.lpBaseOfDll, mi.SizeOfImage,
850                                   UserContext);
851     }
852     HeapFree(GetProcessHeap(), 0, hMods);
853
854     return sz != 0 && i == sz;
855 }
856
857 /******************************************************************
858  *              SymGetModuleInfo (DBGHELP.@)
859  *
860  */
861 BOOL  WINAPI SymGetModuleInfo(HANDLE hProcess, DWORD dwAddr,
862                               PIMAGEHLP_MODULE ModuleInfo)
863 {
864     IMAGEHLP_MODULE     mi;
865     IMAGEHLP_MODULEW64  miw64;
866
867     if (sizeof(mi) < ModuleInfo->SizeOfStruct) FIXME("Wrong size\n");
868
869     miw64.SizeOfStruct = sizeof(miw64);
870     if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
871
872     mi.SizeOfStruct  = miw64.SizeOfStruct;
873     mi.BaseOfImage   = miw64.BaseOfImage;
874     mi.ImageSize     = miw64.ImageSize;
875     mi.TimeDateStamp = miw64.TimeDateStamp;
876     mi.CheckSum      = miw64.CheckSum;
877     mi.NumSyms       = miw64.NumSyms;
878     mi.SymType       = miw64.SymType;
879     WideCharToMultiByte(CP_ACP, 0, miw64.ModuleName, -1,
880                         mi.ModuleName, sizeof(mi.ModuleName), NULL, NULL);
881     WideCharToMultiByte(CP_ACP, 0, miw64.ImageName, -1,
882                         mi.ImageName, sizeof(mi.ImageName), NULL, NULL);
883     WideCharToMultiByte(CP_ACP, 0, miw64.LoadedImageName, -1,
884                         mi.LoadedImageName, sizeof(mi.LoadedImageName), NULL, NULL);
885
886     memcpy(ModuleInfo, &mi, ModuleInfo->SizeOfStruct);
887
888     return TRUE;
889 }
890
891 /******************************************************************
892  *              SymGetModuleInfoW (DBGHELP.@)
893  *
894  */
895 BOOL  WINAPI SymGetModuleInfoW(HANDLE hProcess, DWORD dwAddr,
896                                PIMAGEHLP_MODULEW ModuleInfo)
897 {
898     IMAGEHLP_MODULEW64  miw64;
899     IMAGEHLP_MODULEW    miw;
900
901     if (sizeof(miw) < ModuleInfo->SizeOfStruct) FIXME("Wrong size\n");
902
903     miw64.SizeOfStruct = sizeof(miw64);
904     if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
905
906     miw.SizeOfStruct  = miw64.SizeOfStruct;
907     miw.BaseOfImage   = miw64.BaseOfImage;
908     miw.ImageSize     = miw64.ImageSize;
909     miw.TimeDateStamp = miw64.TimeDateStamp;
910     miw.CheckSum      = miw64.CheckSum;
911     miw.NumSyms       = miw64.NumSyms;
912     miw.SymType       = miw64.SymType;
913     strcpyW(miw.ModuleName, miw64.ModuleName);
914     strcpyW(miw.ImageName, miw64.ImageName);
915     strcpyW(miw.LoadedImageName, miw64.LoadedImageName);
916     memcpy(ModuleInfo, &miw, ModuleInfo->SizeOfStruct);
917
918     return TRUE;
919 }
920
921 /******************************************************************
922  *              SymGetModuleInfo64 (DBGHELP.@)
923  *
924  */
925 BOOL  WINAPI SymGetModuleInfo64(HANDLE hProcess, DWORD64 dwAddr,
926                                 PIMAGEHLP_MODULE64 ModuleInfo)
927 {
928     IMAGEHLP_MODULE64   mi64;
929     IMAGEHLP_MODULEW64  miw64;
930
931     if (sizeof(mi64) < ModuleInfo->SizeOfStruct)
932     {
933         SetLastError(ERROR_MOD_NOT_FOUND); /* NOTE: native returns this error */
934         WARN("Wrong size %u\n", ModuleInfo->SizeOfStruct);
935         return FALSE;
936     }
937
938     miw64.SizeOfStruct = sizeof(miw64);
939     if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
940
941     mi64.SizeOfStruct  = miw64.SizeOfStruct;
942     mi64.BaseOfImage   = miw64.BaseOfImage;
943     mi64.ImageSize     = miw64.ImageSize;
944     mi64.TimeDateStamp = miw64.TimeDateStamp;
945     mi64.CheckSum      = miw64.CheckSum;
946     mi64.NumSyms       = miw64.NumSyms;
947     mi64.SymType       = miw64.SymType;
948     WideCharToMultiByte(CP_ACP, 0, miw64.ModuleName, -1,
949                         mi64.ModuleName, sizeof(mi64.ModuleName), NULL, NULL);
950     WideCharToMultiByte(CP_ACP, 0, miw64.ImageName, -1,
951                         mi64.ImageName, sizeof(mi64.ImageName), NULL, NULL);
952     WideCharToMultiByte(CP_ACP, 0, miw64.LoadedImageName, -1,
953                         mi64.LoadedImageName, sizeof(mi64.LoadedImageName), NULL, NULL);
954     WideCharToMultiByte(CP_ACP, 0, miw64.LoadedPdbName, -1,
955                         mi64.LoadedPdbName, sizeof(mi64.LoadedPdbName), NULL, NULL);
956
957     mi64.CVSig         = miw64.CVSig;
958     WideCharToMultiByte(CP_ACP, 0, miw64.CVData, -1,
959                         mi64.CVData, sizeof(mi64.CVData), NULL, NULL);
960     mi64.PdbSig        = miw64.PdbSig;
961     mi64.PdbSig70      = miw64.PdbSig70;
962     mi64.PdbAge        = miw64.PdbAge;
963     mi64.PdbUnmatched  = miw64.PdbUnmatched;
964     mi64.DbgUnmatched  = miw64.DbgUnmatched;
965     mi64.LineNumbers   = miw64.LineNumbers;
966     mi64.GlobalSymbols = miw64.GlobalSymbols;
967     mi64.TypeInfo      = miw64.TypeInfo;
968     mi64.SourceIndexed = miw64.SourceIndexed;
969     mi64.Publics       = miw64.Publics;
970
971     memcpy(ModuleInfo, &mi64, ModuleInfo->SizeOfStruct);
972
973     return TRUE;
974 }
975
976 /******************************************************************
977  *              SymGetModuleInfoW64 (DBGHELP.@)
978  *
979  */
980 BOOL  WINAPI SymGetModuleInfoW64(HANDLE hProcess, DWORD64 dwAddr,
981                                  PIMAGEHLP_MODULEW64 ModuleInfo)
982 {
983     struct process*     pcs = process_find_by_handle(hProcess);
984     struct module*      module;
985     IMAGEHLP_MODULEW64  miw64;
986
987     TRACE("%p %s %p\n", hProcess, wine_dbgstr_longlong(dwAddr), ModuleInfo);
988
989     if (!pcs) return FALSE;
990     if (ModuleInfo->SizeOfStruct > sizeof(*ModuleInfo)) return FALSE;
991     module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
992     if (!module) return FALSE;
993
994     miw64 = module->module;
995
996     /* update debug information from container if any */
997     if (module->module.SymType == SymNone)
998     {
999         module = module_get_container(pcs, module);
1000         if (module && module->module.SymType != SymNone)
1001         {
1002             miw64.SymType = module->module.SymType;
1003             miw64.NumSyms = module->module.NumSyms;
1004         }
1005     }
1006     memcpy(ModuleInfo, &miw64, ModuleInfo->SizeOfStruct);
1007     return TRUE;
1008 }
1009
1010 /***********************************************************************
1011  *              SymGetModuleBase (DBGHELP.@)
1012  */
1013 DWORD WINAPI SymGetModuleBase(HANDLE hProcess, DWORD dwAddr)
1014 {
1015     struct process*     pcs = process_find_by_handle(hProcess);
1016     struct module*      module;
1017
1018     if (!pcs) return 0;
1019     module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
1020     if (!module) return 0;
1021     return module->module.BaseOfImage;
1022 }
1023
1024 /***********************************************************************
1025  *              SymGetModuleBase64 (DBGHELP.@)
1026  */
1027 DWORD64 WINAPI SymGetModuleBase64(HANDLE hProcess, DWORD64 dwAddr)
1028 {
1029     if (!validate_addr64(dwAddr)) return 0;
1030     return SymGetModuleBase(hProcess, (DWORD)dwAddr);
1031 }
1032
1033 /******************************************************************
1034  *              module_reset_debug_info
1035  * Removes any debug information linked to a given module.
1036  */
1037 void module_reset_debug_info(struct module* module)
1038 {
1039     module->sortlist_valid = TRUE;
1040     module->addr_sorttab = NULL;
1041     module->num_sorttab = module->num_symbols = 0;
1042     hash_table_destroy(&module->ht_symbols);
1043     module->ht_symbols.num_buckets = 0;
1044     module->ht_symbols.buckets = NULL;
1045     hash_table_destroy(&module->ht_types);
1046     module->ht_types.num_buckets = 0;
1047     module->ht_types.buckets = NULL;
1048     module->vtypes.num_elts = 0;
1049     hash_table_destroy(&module->ht_symbols);
1050     module->sources_used = module->sources_alloc = 0;
1051     module->sources = NULL;
1052 }