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