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