wineoss: Renamed the dlls/winmm/wineoss directory to dlls/wineoss.drv.
[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-2004, 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 "winreg.h"
31 #include "winternl.h"
32 #include "wine/debug.h"
33 #include "winnls.h"
34
35 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
36
37 static const char * const ext[] = {".acm", ".dll", ".drv", ".exe", ".ocx", ".vxd", NULL};
38
39 static int match_ext(const char* ptr, size_t len)
40 {
41     const char * const *e;
42     size_t      l;
43
44     for (e = ext; *e; e++)
45     {
46         l = strlen(*e);
47         if (l >= len) return FALSE;
48         if (strncasecmp(&ptr[len - l], *e, l)) continue;
49         return l;
50     }
51     return 0;
52 }
53         
54 static void module_fill_module(const char* in, char* out, size_t size)
55 {
56     const char  *ptr,*endptr;
57     size_t      len, l;
58
59     endptr = in + strlen(in);
60     for (ptr = endptr - 1;
61          ptr >= in && *ptr != '/' && *ptr != '\\';
62          ptr--);
63     ptr++;
64     len = min(endptr-ptr,size-1);
65     memcpy(out, ptr, len);
66     out[len] = '\0';
67     if (len > 4 && (l = match_ext(out, len)))
68         out[len - l] = '\0';
69     else if (len > 12 &&
70              (!strcasecmp(out + len - 12, "wine-pthread") || 
71               !strcasecmp(out + len - 12, "wine-kthread")))
72         lstrcpynA(out, "<wine-loader>", size);
73     else
74     {
75         if (len > 3 && !strcasecmp(&out[len - 3], ".so") &&
76             (l = match_ext(out, len - 3)))
77             strcpy(&out[len - l - 3], "<elf>");
78     }
79     while ((*out = tolower(*out))) out++;
80 }
81
82 static const char*      get_module_type(enum module_type type, BOOL virtual)
83 {
84     switch (type)
85     {
86     case DMT_ELF: return virtual ? "Virtual ELF" : "ELF";
87     case DMT_PE: return virtual ? "Virtual PE" : "PE";
88     default: return "---";
89     }
90 }
91
92 /***********************************************************************
93  * Creates and links a new module to a process 
94  */
95 struct module* module_new(struct process* pcs, const char* name, 
96                           enum module_type type, BOOL virtual,
97                           unsigned long mod_addr, unsigned long size,
98                           unsigned long stamp, unsigned long checksum) 
99 {
100     struct module*      module;
101
102     assert(type == DMT_ELF || type == DMT_PE);
103     if (!(module = HeapAlloc(GetProcessHeap(), 0, sizeof(*module))))
104         return NULL;
105
106     memset(module, 0, sizeof(*module));
107
108     module->next = pcs->lmodules;
109     pcs->lmodules = module;
110
111     TRACE("=> %s %08lx-%08lx %s\n", 
112           get_module_type(type, virtual), mod_addr, mod_addr + size, name);
113
114     pool_init(&module->pool, 65536);
115     
116     module->module.SizeOfStruct = sizeof(module->module);
117     module->module.BaseOfImage = mod_addr;
118     module->module.ImageSize = size;
119     module_fill_module(name, module->module.ModuleName,
120                        sizeof(module->module.ModuleName));
121     module->module.ImageName[0] = '\0';
122     lstrcpynA(module->module.LoadedImageName, name, sizeof(module->module.LoadedImageName));
123     module->module.SymType = SymNone;
124     module->module.NumSyms = 0;
125     module->module.TimeDateStamp = stamp;
126     module->module.CheckSum = checksum;
127
128     memset(module->module.LoadedPdbName, 0, sizeof(module->module.CVData));
129     module->module.CVSig = 0;
130     memset(module->module.CVData, 0, sizeof(module->module.CVData));
131     module->module.PdbSig = 0;
132     memset(&module->module.PdbSig70, 0, sizeof(module->module.PdbSig70));
133     module->module.PdbAge = 0;
134     module->module.PdbUnmatched = FALSE;
135     module->module.DbgUnmatched = FALSE;
136     module->module.LineNumbers = FALSE;
137     module->module.GlobalSymbols = FALSE;
138     module->module.TypeInfo = FALSE;
139     module->module.SourceIndexed = FALSE;
140     module->module.Publics = FALSE;
141
142     module->type              = type;
143     module->is_virtual        = virtual ? TRUE : FALSE;
144     module->sortlist_valid    = FALSE;
145     module->addr_sorttab      = NULL;
146     /* FIXME: this seems a bit too high (on a per module basis)
147      * need some statistics about this
148      */
149     hash_table_init(&module->pool, &module->ht_symbols, 4096);
150     hash_table_init(&module->pool, &module->ht_types,   4096);
151     vector_init(&module->vtypes, sizeof(struct symt*),  32);
152
153     module->sources_used      = 0;
154     module->sources_alloc     = 0;
155     module->sources           = 0;
156
157     return module;
158 }
159
160 /***********************************************************************
161  *      module_find_by_name
162  *
163  */
164 struct module* module_find_by_name(const struct process* pcs, 
165                                    const char* name, enum module_type type)
166 {
167     struct module*      module;
168
169     if (type == DMT_UNKNOWN)
170     {
171         if ((module = module_find_by_name(pcs, name, DMT_PE)) ||
172             (module = module_find_by_name(pcs, name, DMT_ELF)))
173             return module;
174     }
175     else
176     {
177         char                modname[MAX_PATH];
178
179         for (module = pcs->lmodules; module; module = module->next)
180         {
181             if (type == module->type &&
182                 !strcasecmp(name, module->module.LoadedImageName)) 
183                 return module;
184         }
185         module_fill_module(name, modname, sizeof(modname));
186         for (module = pcs->lmodules; module; module = module->next)
187         {
188             if (type == module->type &&
189                 !strcasecmp(modname, module->module.ModuleName)) 
190                 return module;
191         }
192     }
193     SetLastError(ERROR_INVALID_NAME);
194     return NULL;
195 }
196
197 /***********************************************************************
198  *           module_get_container
199  *
200  */
201 struct module* module_get_container(const struct process* pcs, 
202                                     const struct module* inner)
203 {
204     struct module*      module;
205      
206     for (module = pcs->lmodules; module; module = module->next)
207     {
208         if (module != inner &&
209             module->module.BaseOfImage <= inner->module.BaseOfImage &&
210             module->module.BaseOfImage + module->module.ImageSize >=
211             inner->module.BaseOfImage + inner->module.ImageSize)
212             return module;
213     }
214     return NULL;
215 }
216
217 /***********************************************************************
218  *           module_get_containee
219  *
220  */
221 struct module* module_get_containee(const struct process* pcs, 
222                                     const struct module* outter)
223 {
224     struct module*      module;
225      
226     for (module = pcs->lmodules; module; module = module->next)
227     {
228         if (module != outter &&
229             outter->module.BaseOfImage <= module->module.BaseOfImage &&
230             outter->module.BaseOfImage + outter->module.ImageSize >=
231             module->module.BaseOfImage + module->module.ImageSize)
232             return module;
233     }
234     return NULL;
235 }
236
237 /******************************************************************
238  *              module_get_debug
239  *
240  * get the debug information from a module:
241  * - if the module's type is deferred, then force loading of debug info (and return
242  *   the module itself)
243  * - if the module has no debug info and has an ELF container, then return the ELF
244  *   container (and also force the ELF container's debug info loading if deferred)
245  * - otherwise return the module itself if it has some debug info
246  */
247 BOOL module_get_debug(struct module_pair* pair)
248 {
249     IMAGEHLP_DEFERRED_SYMBOL_LOAD64     idsl64;
250
251     if (!pair->requested) return FALSE;
252     /* for a PE builtin, always get info from container */
253     if (!(pair->effective = module_get_container(pair->pcs, pair->requested)))
254         pair->effective = pair->requested;
255     /* if deferred, force loading */
256     if (pair->effective->module.SymType == SymDeferred)
257     {
258         BOOL ret;
259         
260         if (pair->effective->is_virtual) ret = FALSE;
261         else switch (pair->effective->type)
262         {
263         case DMT_ELF:
264             ret = elf_load_debug_info(pair->effective, NULL);
265             break;
266         case DMT_PE:
267             idsl64.SizeOfStruct = sizeof(idsl64);
268             idsl64.BaseOfImage = pair->effective->module.BaseOfImage;
269             idsl64.CheckSum = pair->effective->module.CheckSum;
270             idsl64.TimeDateStamp = pair->effective->module.TimeDateStamp;
271             strcpy(idsl64.FileName, pair->effective->module.ImageName);
272             idsl64.Reparse = FALSE;
273             idsl64.hFile = INVALID_HANDLE_VALUE;
274
275             pcs_callback(pair->pcs, CBA_DEFERRED_SYMBOL_LOAD_START, &idsl64);
276             ret = pe_load_debug_info(pair->pcs, pair->effective);
277             pcs_callback(pair->pcs,
278                          ret ? CBA_DEFERRED_SYMBOL_LOAD_COMPLETE : CBA_DEFERRED_SYMBOL_LOAD_FAILURE,
279                          &idsl64);
280             break;
281         default:
282             ret = FALSE;
283             break;
284         }
285         if (!ret) pair->effective->module.SymType = SymNone;
286         assert(pair->effective->module.SymType != SymDeferred);
287         pair->effective->module.NumSyms = pair->effective->ht_symbols.num_elts;
288     }
289     return pair->effective->module.SymType != SymNone;
290 }
291
292 /***********************************************************************
293  *      module_find_by_addr
294  *
295  * either the addr where module is loaded, or any address inside the 
296  * module
297  */
298 struct module* module_find_by_addr(const struct process* pcs, unsigned long addr, 
299                                    enum module_type type)
300 {
301     struct module*      module;
302     
303     if (type == DMT_UNKNOWN)
304     {
305         if ((module = module_find_by_addr(pcs, addr, DMT_PE)) ||
306             (module = module_find_by_addr(pcs, addr, DMT_ELF)))
307             return module;
308     }
309     else
310     {
311         for (module = pcs->lmodules; module; module = module->next)
312         {
313             if (type == module->type && addr >= module->module.BaseOfImage &&
314                 addr < module->module.BaseOfImage + module->module.ImageSize) 
315                 return module;
316         }
317     }
318     SetLastError(ERROR_INVALID_ADDRESS);
319     return module;
320 }
321
322 static BOOL module_is_elf_container_loaded(struct process* pcs, const char* ImageName,
323                                            const char* ModuleName)
324 {
325     char                buffer[MAX_PATH];
326     size_t              len;
327     struct module*      module;
328
329     if (!ModuleName)
330     {
331         module_fill_module(ImageName, buffer, sizeof(buffer));
332         ModuleName = buffer;
333     }
334     len = strlen(ModuleName);
335     for (module = pcs->lmodules; module; module = module->next)
336     {
337         if (!strncasecmp(module->module.ModuleName, ModuleName, len) &&
338             module->type == DMT_ELF &&
339             !strcmp(module->module.ModuleName + len, "<elf>"))
340             return TRUE;
341     }
342     return FALSE;
343 }
344
345 /******************************************************************
346  *              module_get_type_by_name
347  *
348  * Guesses a filename type from its extension
349  */
350 enum module_type module_get_type_by_name(const char* name)
351 {
352     const char* ptr;
353     int         len = strlen(name);
354
355     /* check for terminating .so or .so.[digit] */
356     ptr = strrchr(name, '.');
357     if (ptr)
358     {
359         if (!strcmp(ptr, ".so") ||
360             (isdigit(ptr[1]) && !ptr[2] && ptr >= name + 3 && !memcmp(ptr - 3, ".so", 3)))
361             return DMT_ELF;
362         else if (!strcasecmp(ptr, ".pdb"))
363             return DMT_PDB;
364     }
365     /* wine-[kp]thread is also an ELF module */
366     else if (((len > 12 && name[len - 13] == '/') || len == 12) && 
367              (!strcasecmp(name + len - 12, "wine-pthread") || 
368               !strcasecmp(name + len - 12, "wine-kthread")))
369     {
370         return DMT_ELF;
371     }
372     return DMT_PE;
373 }
374
375 /***********************************************************************
376  *                      SymLoadModule (DBGHELP.@)
377  */
378 DWORD WINAPI SymLoadModule(HANDLE hProcess, HANDLE hFile, const char* ImageName,
379                            const char* ModuleName, DWORD BaseOfDll, DWORD SizeOfDll)
380 {
381     struct process*     pcs;
382     struct module*      module = NULL;
383
384     TRACE("(%p %p %s %s %08x %08x)\n",
385           hProcess, hFile, debugstr_a(ImageName), debugstr_a(ModuleName),
386           BaseOfDll, SizeOfDll);
387
388     pcs = process_find_by_handle(hProcess);
389     if (!pcs) return FALSE;
390
391     /* force transparent ELF loading / unloading */
392     elf_synchronize_module_list(pcs);
393
394     /* this is a Wine extension to the API just to redo the synchronisation */
395     if (!ImageName && !hFile) return 0;
396
397     if (module_is_elf_container_loaded(pcs, ImageName, ModuleName))
398     {
399         /* force the loading of DLL as builtin */
400         if ((module = pe_load_module_from_pcs(pcs, ImageName, ModuleName,
401                                               BaseOfDll, SizeOfDll)))
402             goto done;
403         WARN("Couldn't locate %s\n", ImageName);
404         return 0;
405     }
406     TRACE("Assuming %s as native DLL\n", ImageName);
407     if (!(module = pe_load_module(pcs, ImageName, hFile, BaseOfDll, SizeOfDll)))
408     {
409         if (module_get_type_by_name(ImageName) == DMT_ELF &&
410             (module = elf_load_module(pcs, ImageName, BaseOfDll)))
411             goto done;
412         FIXME("Should have successfully loaded debug information for image %s\n",
413               ImageName);
414         if ((module = pe_load_module_from_pcs(pcs, ImageName, ModuleName,
415                                               BaseOfDll, SizeOfDll)))
416             goto done;
417         WARN("Couldn't locate %s\n", ImageName);
418         return 0;
419     }
420     module->module.NumSyms = module->ht_symbols.num_elts;
421 done:
422     /* by default pe_load_module fills module.ModuleName from a derivation 
423      * of ImageName. Overwrite it, if we have better information
424      */
425     if (ModuleName)
426         lstrcpynA(module->module.ModuleName, ModuleName, sizeof(module->module.ModuleName));
427     lstrcpynA(module->module.ImageName, ImageName, sizeof(module->module.ImageName));
428
429     return module->module.BaseOfImage;
430 }
431
432 /***********************************************************************
433  *                      SymLoadModuleEx (DBGHELP.@)
434  */
435 DWORD64 WINAPI  SymLoadModuleEx(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
436                                 PCSTR ModuleName, DWORD64 BaseOfDll, DWORD DllSize,
437                                 PMODLOAD_DATA Data, DWORD Flags)
438 {
439     TRACE("(%p %p %s %s %s %08x %p %08x)\n",
440           hProcess, hFile, debugstr_a(ImageName), debugstr_a(ModuleName),
441           wine_dbgstr_longlong(BaseOfDll), DllSize, Data, Flags);
442
443     if (Data)
444         FIXME("Unsupported load data parameter %p for %s\n", Data, ImageName);
445     if (!validate_addr64(BaseOfDll)) return FALSE;
446     if (Flags & SLMFLAG_VIRTUAL)
447     {
448         struct process* pcs = process_find_by_handle(hProcess);
449         struct module* module;
450         if (!pcs) return FALSE;
451
452         module = module_new(pcs, ImageName, module_get_type_by_name(ImageName), TRUE, 
453                             (DWORD)BaseOfDll, DllSize, 0, 0);
454         if (!module) return FALSE;
455         if (ModuleName)
456             lstrcpynA(module->module.ModuleName, ModuleName, sizeof(module->module.ModuleName));
457         module->module.SymType = SymVirtual;
458
459         return TRUE;
460     }
461     if (Flags & ~(SLMFLAG_VIRTUAL))
462         FIXME("Unsupported Flags %08x for %s\n", Flags, ImageName);
463
464     return SymLoadModule(hProcess, hFile, ImageName, ModuleName, (DWORD)BaseOfDll, DllSize);
465 }
466
467 /***********************************************************************
468  *                      SymLoadModuleExW (DBGHELP.@)
469  */
470 DWORD64 WINAPI  SymLoadModuleExW(HANDLE hProcess, HANDLE hFile, PCWSTR wImageName,
471                                  PCWSTR wModuleName, DWORD64 BaseOfDll, DWORD DllSize,
472                                  PMODLOAD_DATA Data, DWORD Flags)
473 {
474     LPSTR       ImageName, ModuleName;
475     unsigned    len;
476     BOOL        ret;
477
478     if (wImageName)
479     {
480         len = WideCharToMultiByte(CP_ACP,0, wImageName, -1, NULL, 0, NULL, NULL);
481         ImageName = HeapAlloc(GetProcessHeap(), 0, len);
482         WideCharToMultiByte(CP_ACP,0, wImageName, -1, ImageName, len, NULL, NULL);
483     }
484     else ImageName = NULL;
485     if (wModuleName)
486     {
487         len = WideCharToMultiByte(CP_ACP,0, wModuleName, -1, NULL, 0, NULL, NULL);
488         ModuleName = HeapAlloc(GetProcessHeap(), 0, len);
489         WideCharToMultiByte(CP_ACP,0, wModuleName, -1, ModuleName, len, NULL, NULL);
490     }
491     else ModuleName = NULL;
492
493     ret = SymLoadModuleEx(hProcess, hFile, ImageName, ModuleName,
494                           BaseOfDll, DllSize, Data, Flags);
495     HeapFree(GetProcessHeap(), 0, ImageName);
496     HeapFree(GetProcessHeap(), 0, ModuleName);
497     return ret;
498 }
499
500 /***********************************************************************
501  *                     SymLoadModule64 (DBGHELP.@)
502  */
503 DWORD64 WINAPI SymLoadModule64(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
504                                PCSTR ModuleName, DWORD64 BaseOfDll, DWORD SizeOfDll)
505 {
506     if (!validate_addr64(BaseOfDll)) return FALSE;
507     return SymLoadModule(hProcess, hFile, ImageName, ModuleName, (DWORD)BaseOfDll, SizeOfDll);
508 }
509
510 /******************************************************************
511  *              module_remove
512  *
513  */
514 BOOL module_remove(struct process* pcs, struct module* module)
515 {
516     struct module**     p;
517
518     TRACE("%s (%p)\n", module->module.ModuleName, module);
519     hash_table_destroy(&module->ht_symbols);
520     hash_table_destroy(&module->ht_types);
521     HeapFree(GetProcessHeap(), 0, (char*)module->sources);
522     HeapFree(GetProcessHeap(), 0, module->addr_sorttab);
523     HeapFree(GetProcessHeap(), 0, module->dwarf2_info);
524     pool_destroy(&module->pool);
525     /* native dbghelp doesn't invoke registered callback(,CBA_SYMBOLS_UNLOADED,) here
526      * so do we
527      */
528     for (p = &pcs->lmodules; *p; p = &(*p)->next)
529     {
530         if (*p == module)
531         {
532             *p = module->next;
533             HeapFree(GetProcessHeap(), 0, module);
534             return TRUE;
535         }
536     }
537     FIXME("This shouldn't happen\n");
538     return FALSE;
539 }
540
541 /******************************************************************
542  *              SymUnloadModule (DBGHELP.@)
543  *
544  */
545 BOOL WINAPI SymUnloadModule(HANDLE hProcess, DWORD BaseOfDll)
546 {
547     struct process*     pcs;
548     struct module*      module;
549
550     pcs = process_find_by_handle(hProcess);
551     if (!pcs) return FALSE;
552     module = module_find_by_addr(pcs, BaseOfDll, DMT_UNKNOWN);
553     if (!module) return FALSE;
554     return module_remove(pcs, module);
555 }
556
557 /******************************************************************
558  *              SymUnloadModule64 (DBGHELP.@)
559  *
560  */
561 BOOL WINAPI SymUnloadModule64(HANDLE hProcess, DWORD64 BaseOfDll)
562 {
563     struct process*     pcs;
564     struct module*      module;
565
566     pcs = process_find_by_handle(hProcess);
567     if (!pcs) return FALSE;
568     if (!validate_addr64(BaseOfDll)) return FALSE;
569     module = module_find_by_addr(pcs, (DWORD)BaseOfDll, DMT_UNKNOWN);
570     if (!module) return FALSE;
571     return module_remove(pcs, module);
572 }
573
574 /******************************************************************
575  *              SymEnumerateModules (DBGHELP.@)
576  *
577  */
578 BOOL  WINAPI SymEnumerateModules(HANDLE hProcess,
579                                  PSYM_ENUMMODULES_CALLBACK EnumModulesCallback,  
580                                  PVOID UserContext)
581 {
582     struct process*     pcs = process_find_by_handle(hProcess);
583     struct module*      module;
584
585     if (!pcs) return FALSE;
586     
587     for (module = pcs->lmodules; module; module = module->next)
588     {
589         if (!(dbghelp_options & SYMOPT_WINE_WITH_ELF_MODULES) && module->type == DMT_ELF)
590             continue;
591         if (!EnumModulesCallback(module->module.ModuleName, 
592                                  module->module.BaseOfImage, UserContext))
593             break;
594     }
595     return TRUE;
596 }
597
598 /******************************************************************
599  *              SymEnumerateModules64 (DBGHELP.@)
600  *
601  */
602 BOOL  WINAPI SymEnumerateModules64(HANDLE hProcess,
603                                    PSYM_ENUMMODULES_CALLBACK64 EnumModulesCallback,  
604                                    PVOID UserContext)
605 {
606     struct process*     pcs = process_find_by_handle(hProcess);
607     struct module*      module;
608
609     if (!pcs) return FALSE;
610     
611     for (module = pcs->lmodules; module; module = module->next)
612     {
613         if (!(dbghelp_options & SYMOPT_WINE_WITH_ELF_MODULES) && module->type == DMT_ELF)
614             continue;
615         if (!EnumModulesCallback(module->module.ModuleName, 
616                                  module->module.BaseOfImage, UserContext))
617             break;
618     }
619     return TRUE;
620 }
621
622 /******************************************************************
623  *              EnumerateLoadedModules64 (DBGHELP.@)
624  *
625  */
626 BOOL  WINAPI EnumerateLoadedModules64(HANDLE hProcess,
627                                       PENUMLOADED_MODULES_CALLBACK64 EnumLoadedModulesCallback,
628                                       PVOID UserContext)
629 {
630     HMODULE*    hMods;
631     char        base[256], mod[256];
632     DWORD       i, sz;
633     MODULEINFO  mi;
634
635     hMods = HeapAlloc(GetProcessHeap(), 0, 256 * sizeof(hMods[0]));
636     if (!hMods) return FALSE;
637
638     if (!EnumProcessModules(hProcess, hMods, 256 * sizeof(hMods[0]), &sz))
639     {
640         /* hProcess should also be a valid process handle !! */
641         FIXME("If this happens, bump the number in mod\n");
642         HeapFree(GetProcessHeap(), 0, hMods);
643         return FALSE;
644     }
645     sz /= sizeof(HMODULE);
646     for (i = 0; i < sz; i++)
647     {
648         if (!GetModuleInformation(hProcess, hMods[i], &mi, sizeof(mi)) ||
649             !GetModuleBaseNameA(hProcess, hMods[i], base, sizeof(base)))
650             continue;
651         module_fill_module(base, mod, sizeof(mod));
652         EnumLoadedModulesCallback(mod, (DWORD_PTR)mi.lpBaseOfDll, mi.SizeOfImage,
653                                   UserContext);
654     }
655     HeapFree(GetProcessHeap(), 0, hMods);
656
657     return sz != 0 && i == sz;
658 }
659
660 /******************************************************************
661  *              EnumerateLoadedModules (DBGHELP.@)
662  *
663  */
664 struct enum_load_mod64_32
665 {
666     PENUMLOADED_MODULES_CALLBACK        cb;
667     PVOID                               user;
668 };
669
670 static BOOL CALLBACK enum_load_mod64_32(PSTR name, DWORD64 base, ULONG size,
671                                         PVOID user)
672 {
673     struct enum_load_mod64_32*  x = user;
674     return x->cb(name, (DWORD)base, size, x->user);
675 }
676
677 BOOL  WINAPI EnumerateLoadedModules(HANDLE hProcess,
678                                     PENUMLOADED_MODULES_CALLBACK EnumLoadedModulesCallback,
679                                     PVOID UserContext)
680 {
681     struct enum_load_mod64_32   x;
682
683     x.cb = EnumLoadedModulesCallback;
684     x.user = UserContext;
685
686     return EnumerateLoadedModules64(hProcess, enum_load_mod64_32, &x);
687 }
688
689 /******************************************************************
690  *              EnumerateLoadedModulesW64 (DBGHELP.@)
691  *
692  */
693 struct enum_load_mod64_W64
694 {
695     PENUMLOADED_MODULES_CALLBACKW64     cb;
696     PVOID                               user;
697     WCHAR                               module[MAX_PATH];
698 };
699
700 static BOOL CALLBACK enum_load_mod64_W64(PSTR name, DWORD64 base, ULONG size,
701                                          PVOID user)
702 {
703     struct enum_load_mod64_W64* x = user;
704
705     MultiByteToWideChar(CP_ACP, 0, name, -1,
706                         x->module, sizeof(x->module) / sizeof(WCHAR));
707     return x->cb(x->module, base, size, x->user);
708 }
709
710 BOOL  WINAPI EnumerateLoadedModulesW64(HANDLE hProcess,
711                                        PENUMLOADED_MODULES_CALLBACKW64 EnumLoadedModulesCallback,
712                                        PVOID UserContext)
713 {
714     struct enum_load_mod64_W64  x;
715
716     x.cb = EnumLoadedModulesCallback;
717     x.user = UserContext;
718
719     return EnumerateLoadedModules64(hProcess, enum_load_mod64_W64, &x);
720 }
721
722 /******************************************************************
723  *              SymGetModuleInfo (DBGHELP.@)
724  *
725  */
726 BOOL  WINAPI SymGetModuleInfo(HANDLE hProcess, DWORD dwAddr, 
727                               PIMAGEHLP_MODULE ModuleInfo)
728 {
729     struct process*     pcs = process_find_by_handle(hProcess);
730     struct module*      module;
731     IMAGEHLP_MODULE     mod;
732
733     if (!pcs) return FALSE;
734     if (ModuleInfo->SizeOfStruct < sizeof(*ModuleInfo)) return FALSE;
735     module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
736     if (!module) return FALSE;
737
738     mod.SizeOfStruct = ModuleInfo->SizeOfStruct;
739     mod.BaseOfImage = module->module.BaseOfImage;
740     mod.ImageSize = module->module.ImageSize;
741     mod.TimeDateStamp = module->module.TimeDateStamp;
742     mod.CheckSum = module->module.CheckSum;
743     mod.NumSyms = module->module.NumSyms;
744     mod.SymType = module->module.SymType;
745     strcpy(mod.ModuleName, module->module.ModuleName);
746     strcpy(mod.ImageName, module->module.ImageName);
747     strcpy(mod.LoadedImageName, module->module.LoadedImageName);
748
749     if (module->module.SymType == SymNone)
750     {
751         module = module_get_container(pcs, module);
752         if (module && module->module.SymType != SymNone)
753         {
754             mod.SymType = module->module.SymType;
755             mod.NumSyms = module->module.NumSyms;
756         }
757     }
758     memcpy(ModuleInfo, &mod, ModuleInfo->SizeOfStruct);
759     return TRUE;
760 }
761
762 /******************************************************************
763  *              SymGetModuleInfoW (DBGHELP.@)
764  *
765  */
766 BOOL  WINAPI SymGetModuleInfoW(HANDLE hProcess, DWORD dwAddr, 
767                                PIMAGEHLP_MODULEW ModuleInfo)
768 {
769     IMAGEHLP_MODULE     mi;
770     IMAGEHLP_MODULEW    miw;
771
772     if (sizeof(miw) < ModuleInfo->SizeOfStruct) FIXME("Wrong size\n");
773
774     mi.SizeOfStruct = sizeof(mi);
775     if (!SymGetModuleInfo(hProcess, dwAddr, &mi)) return FALSE;
776
777     miw.SizeOfStruct  = mi.SizeOfStruct;
778     miw.BaseOfImage   = mi.BaseOfImage;
779     miw.ImageSize     = mi.ImageSize;
780     miw.TimeDateStamp = mi.TimeDateStamp;
781     miw.CheckSum      = mi.CheckSum;
782     miw.NumSyms       = mi.NumSyms;
783     miw.SymType       = mi.SymType;
784     MultiByteToWideChar(CP_ACP, 0, mi.ModuleName, -1,   
785                         miw.ModuleName, sizeof(miw.ModuleName) / sizeof(WCHAR));
786     MultiByteToWideChar(CP_ACP, 0, mi.ImageName, -1,   
787                         miw.ImageName, sizeof(miw.ImageName) / sizeof(WCHAR));
788     MultiByteToWideChar(CP_ACP, 0, mi.LoadedImageName, -1,   
789                         miw.LoadedImageName, sizeof(miw.LoadedImageName) / sizeof(WCHAR));
790     memcpy(ModuleInfo, &miw, ModuleInfo->SizeOfStruct);
791
792     return TRUE;
793 }
794
795 /******************************************************************
796  *              SymGetModuleInfo64 (DBGHELP.@)
797  *
798  */
799 BOOL  WINAPI SymGetModuleInfo64(HANDLE hProcess, DWORD64 dwAddr, 
800                                 PIMAGEHLP_MODULE64 ModuleInfo)
801 {
802     struct process*     pcs = process_find_by_handle(hProcess);
803     struct module*      module;
804
805     TRACE("%p %s %p\n", hProcess, wine_dbgstr_longlong(dwAddr), ModuleInfo);
806
807     if (!pcs) return FALSE;
808     if (ModuleInfo->SizeOfStruct > sizeof(*ModuleInfo)) return FALSE;
809     module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
810     if (!module) return FALSE;
811
812     memcpy(ModuleInfo, &module->module, ModuleInfo->SizeOfStruct);
813
814     if (module->module.SymType == SymNone)
815     {
816         module = module_get_container(pcs, module);
817         if (module && module->module.SymType != SymNone)
818         {
819             ModuleInfo->SymType = module->module.SymType;
820             ModuleInfo->NumSyms = module->module.NumSyms;
821         }
822     }
823     return TRUE;
824 }
825
826 /******************************************************************
827  *              SymGetModuleInfoW64 (DBGHELP.@)
828  *
829  */
830 BOOL  WINAPI SymGetModuleInfoW64(HANDLE hProcess, DWORD64 dwAddr, 
831                                  PIMAGEHLP_MODULEW64 ModuleInfo)
832 {
833     IMAGEHLP_MODULE64   mi;
834     IMAGEHLP_MODULEW64  miw;
835
836     if (sizeof(miw) < ModuleInfo->SizeOfStruct) FIXME("Wrong size\n");
837
838     mi.SizeOfStruct = sizeof(mi);
839     if (!SymGetModuleInfo64(hProcess, dwAddr, &mi)) return FALSE;
840
841     miw.SizeOfStruct  = mi.SizeOfStruct;
842     miw.BaseOfImage   = mi.BaseOfImage;
843     miw.ImageSize     = mi.ImageSize;
844     miw.TimeDateStamp = mi.TimeDateStamp;
845     miw.CheckSum      = mi.CheckSum;
846     miw.NumSyms       = mi.NumSyms;
847     miw.SymType       = mi.SymType;
848     MultiByteToWideChar(CP_ACP, 0, mi.ModuleName, -1,   
849                         miw.ModuleName, sizeof(miw.ModuleName) / sizeof(WCHAR));
850     MultiByteToWideChar(CP_ACP, 0, mi.ImageName, -1,   
851                         miw.ImageName, sizeof(miw.ImageName) / sizeof(WCHAR));
852     MultiByteToWideChar(CP_ACP, 0, mi.LoadedImageName, -1,   
853                         miw.LoadedImageName, sizeof(miw.LoadedImageName) / sizeof(WCHAR));
854     MultiByteToWideChar(CP_ACP, 0, mi.LoadedPdbName, -1,   
855                         miw.LoadedPdbName, sizeof(miw.LoadedPdbName) / sizeof(WCHAR));
856
857     miw.CVSig         = mi.CVSig;
858     MultiByteToWideChar(CP_ACP, 0, mi.CVData, -1,   
859                         miw.CVData, sizeof(miw.CVData) / sizeof(WCHAR));
860     miw.PdbSig        = mi.PdbSig;
861     miw.PdbSig70      = mi.PdbSig70;
862     miw.PdbAge        = mi.PdbAge;
863     miw.PdbUnmatched  = mi.PdbUnmatched;
864     miw.DbgUnmatched  = mi.DbgUnmatched;
865     miw.LineNumbers   = mi.LineNumbers;
866     miw.GlobalSymbols = mi.GlobalSymbols;
867     miw.TypeInfo      = mi.TypeInfo;
868     miw.SourceIndexed = mi.SourceIndexed;
869     miw.Publics       = mi.Publics;
870
871     memcpy(ModuleInfo, &miw, ModuleInfo->SizeOfStruct);
872
873     return TRUE;
874 }
875
876 /***********************************************************************
877  *              SymGetModuleBase (DBGHELP.@)
878  */
879 DWORD WINAPI SymGetModuleBase(HANDLE hProcess, DWORD dwAddr)
880 {
881     struct process*     pcs = process_find_by_handle(hProcess);
882     struct module*      module;
883
884     if (!pcs) return 0;
885     module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
886     if (!module) return 0;
887     return module->module.BaseOfImage;
888 }
889
890 /***********************************************************************
891  *              SymGetModuleBase64 (DBGHELP.@)
892  */
893 DWORD64 WINAPI SymGetModuleBase64(HANDLE hProcess, DWORD64 dwAddr)
894 {
895     if (!validate_addr64(dwAddr)) return 0;
896     return SymGetModuleBase(hProcess, (DWORD)dwAddr);
897 }
898
899 /******************************************************************
900  *              module_reset_debug_info
901  * Removes any debug information linked to a given module.
902  */
903 void module_reset_debug_info(struct module* module)
904 {
905     module->sortlist_valid = TRUE;
906     module->addr_sorttab = NULL;
907     hash_table_destroy(&module->ht_symbols);
908     module->ht_symbols.num_buckets = 0;
909     module->ht_symbols.buckets = NULL;
910     hash_table_destroy(&module->ht_types);
911     module->ht_types.num_buckets = 0;
912     module->ht_types.buckets = NULL;
913     module->vtypes.num_elts = 0;
914     hash_table_destroy(&module->ht_symbols);
915     module->sources_used = module->sources_alloc = 0;
916     module->sources = NULL;
917 }