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