dbghelp: Always ensure that 64bit module functions use real 64bit addresses.
[wine/multimedia.git] / dlls / dbghelp / module.c
blob2436a0f2bb52193991fdd3768d78f14a420dc72f
1 /*
2 * File module.c - module handling for the wine debugger
4 * Copyright (C) 1993, Eric Youngdale.
5 * 2000-2007, Eric Pouech
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.
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.
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
22 #include "config.h"
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <assert.h>
28 #include "dbghelp_private.h"
29 #include "psapi.h"
30 #include "winternl.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
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_SlashW[] = {'/','\0'};
43 static const WCHAR S_AcmW[] = {'.','a','c','m','\0'};
44 static const WCHAR S_DllW[] = {'.','d','l','l','\0'};
45 static const WCHAR S_DrvW[] = {'.','d','r','v','\0'};
46 static const WCHAR S_ExeW[] = {'.','e','x','e','\0'};
47 static const WCHAR S_OcxW[] = {'.','o','c','x','\0'};
48 static const WCHAR S_VxdW[] = {'.','v','x','d','\0'};
49 static const WCHAR * const ext[] = {S_AcmW, S_DllW, S_DrvW, S_ExeW, S_OcxW, S_VxdW, NULL};
51 static int match_ext(const WCHAR* ptr, size_t len)
53 const WCHAR* const *e;
54 size_t l;
56 for (e = ext; *e; e++)
58 l = strlenW(*e);
59 if (l >= len) return FALSE;
60 if (strncmpiW(&ptr[len - l], *e, l)) continue;
61 return l;
63 return 0;
66 static const WCHAR* get_filename(const WCHAR* name, const WCHAR* endptr)
68 const WCHAR* ptr;
70 if (!endptr) endptr = name + strlenW(name);
71 for (ptr = endptr - 1; ptr >= name; ptr--)
73 if (*ptr == '/' || *ptr == '\\') break;
75 return ++ptr;
78 static void module_fill_module(const WCHAR* in, WCHAR* out, size_t size)
80 const WCHAR *loader = get_wine_loader_name();
81 const WCHAR *ptr, *endptr;
82 size_t len, l;
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 > strlenW(loader) && !strcmpiW(out + len - strlenW(loader), loader))
91 lstrcpynW(out, S_WineLoaderW, size);
92 else
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);
98 while ((*out = tolowerW(*out))) out++;
101 void module_set_module(struct module* module, const WCHAR* name)
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);
109 const WCHAR *get_wine_loader_name(void)
111 static const int is_win64 = sizeof(void *) > sizeof(int); /* FIXME: should depend on target process */
112 static const WCHAR wineW[] = {'w','i','n','e',0};
113 static const WCHAR suffixW[] = {'6','4',0};
114 static const WCHAR *loader;
116 if (!loader)
118 WCHAR *p, *buffer;
119 const char *ptr;
121 /* All binaries are loaded with WINELOADER (if run from tree) or by the
122 * main executable
124 if ((ptr = getenv("WINELOADER")))
126 DWORD len = 2 + MultiByteToWideChar( CP_UNIXCP, 0, ptr, -1, NULL, 0 );
127 buffer = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
128 MultiByteToWideChar( CP_UNIXCP, 0, ptr, -1, buffer, len );
130 else
132 buffer = HeapAlloc( GetProcessHeap(), 0, sizeof(wineW) + 2 * sizeof(WCHAR) );
133 strcpyW( buffer, wineW );
135 p = buffer + strlenW( buffer ) - strlenW( suffixW );
136 if (p > buffer && !strcmpW( p, suffixW ))
138 if (!is_win64) *p = 0;
140 else if (is_win64) strcatW( buffer, suffixW );
142 TRACE( "returning %s\n", debugstr_w(buffer) );
143 loader = buffer;
145 return loader;
148 static const char* get_module_type(enum module_type type, BOOL virtual)
150 switch (type)
152 case DMT_ELF: return virtual ? "Virtual ELF" : "ELF";
153 case DMT_PE: return virtual ? "Virtual PE" : "PE";
154 case DMT_MACHO: return virtual ? "Virtual Mach-O" : "Mach-O";
155 default: return "---";
159 /***********************************************************************
160 * Creates and links a new module to a process
162 struct module* module_new(struct process* pcs, const WCHAR* name,
163 enum module_type type, BOOL virtual,
164 DWORD64 mod_addr, DWORD64 size,
165 unsigned long stamp, unsigned long checksum)
167 struct module* module;
168 unsigned i;
170 assert(type == DMT_ELF || type == DMT_PE || type == DMT_MACHO);
171 if (!(module = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*module))))
172 return NULL;
174 module->next = pcs->lmodules;
175 pcs->lmodules = module;
177 TRACE("=> %s %s-%s %s\n",
178 get_module_type(type, virtual),
179 wine_dbgstr_longlong(mod_addr), wine_dbgstr_longlong(mod_addr + size),
180 debugstr_w(name));
182 pool_init(&module->pool, 65536);
184 module->process = pcs;
185 module->module.SizeOfStruct = sizeof(module->module);
186 module->module.BaseOfImage = mod_addr;
187 module->module.ImageSize = size;
188 module_set_module(module, name);
189 module->module.ImageName[0] = '\0';
190 lstrcpynW(module->module.LoadedImageName, name, sizeof(module->module.LoadedImageName) / sizeof(WCHAR));
191 module->module.SymType = SymNone;
192 module->module.NumSyms = 0;
193 module->module.TimeDateStamp = stamp;
194 module->module.CheckSum = checksum;
196 memset(module->module.LoadedPdbName, 0, sizeof(module->module.LoadedPdbName));
197 module->module.CVSig = 0;
198 memset(module->module.CVData, 0, sizeof(module->module.CVData));
199 module->module.PdbSig = 0;
200 memset(&module->module.PdbSig70, 0, sizeof(module->module.PdbSig70));
201 module->module.PdbAge = 0;
202 module->module.PdbUnmatched = FALSE;
203 module->module.DbgUnmatched = FALSE;
204 module->module.LineNumbers = FALSE;
205 module->module.GlobalSymbols = FALSE;
206 module->module.TypeInfo = FALSE;
207 module->module.SourceIndexed = FALSE;
208 module->module.Publics = FALSE;
210 module->reloc_delta = 0;
211 module->type = type;
212 module->is_virtual = virtual ? TRUE : FALSE;
213 for (i = 0; i < DFI_LAST; i++) module->format_info[i] = NULL;
214 module->sortlist_valid = FALSE;
215 module->sorttab_size = 0;
216 module->addr_sorttab = NULL;
217 module->num_sorttab = 0;
218 module->num_symbols = 0;
220 vector_init(&module->vsymt, sizeof(struct symt*), 128);
221 /* FIXME: this seems a bit too high (on a per module basis)
222 * need some statistics about this
224 hash_table_init(&module->pool, &module->ht_symbols, 4096);
225 hash_table_init(&module->pool, &module->ht_types, 4096);
226 vector_init(&module->vtypes, sizeof(struct symt*), 32);
228 module->sources_used = 0;
229 module->sources_alloc = 0;
230 module->sources = 0;
231 wine_rb_init(&module->sources_offsets_tree, &source_rb_functions);
233 return module;
236 /***********************************************************************
237 * module_find_by_nameW
240 struct module* module_find_by_nameW(const struct process* pcs, const WCHAR* name)
242 struct module* module;
244 for (module = pcs->lmodules; module; module = module->next)
246 if (!strcmpiW(name, module->module.ModuleName)) return module;
248 SetLastError(ERROR_INVALID_NAME);
249 return NULL;
252 struct module* module_find_by_nameA(const struct process* pcs, const char* name)
254 WCHAR wname[MAX_PATH];
256 MultiByteToWideChar(CP_ACP, 0, name, -1, wname, sizeof(wname) / sizeof(WCHAR));
257 return module_find_by_nameW(pcs, wname);
260 /***********************************************************************
261 * module_is_already_loaded
264 struct module* module_is_already_loaded(const struct process* pcs, const WCHAR* name)
266 struct module* module;
267 const WCHAR* filename;
269 /* first compare the loaded image name... */
270 for (module = pcs->lmodules; module; module = module->next)
272 if (!strcmpiW(name, module->module.LoadedImageName))
273 return module;
275 /* then compare the standard filenames (without the path) ... */
276 filename = get_filename(name, NULL);
277 for (module = pcs->lmodules; module; module = module->next)
279 if (!strcmpiW(filename, get_filename(module->module.LoadedImageName, NULL)))
280 return module;
282 SetLastError(ERROR_INVALID_NAME);
283 return NULL;
286 /***********************************************************************
287 * module_get_container
290 static struct module* module_get_container(const struct process* pcs,
291 const struct module* inner)
293 struct module* module;
295 for (module = pcs->lmodules; module; module = module->next)
297 if (module != inner &&
298 module->module.BaseOfImage <= inner->module.BaseOfImage &&
299 module->module.BaseOfImage + module->module.ImageSize >=
300 inner->module.BaseOfImage + inner->module.ImageSize)
301 return module;
303 return NULL;
306 /***********************************************************************
307 * module_get_containee
310 struct module* module_get_containee(const struct process* pcs,
311 const struct module* outter)
313 struct module* module;
315 for (module = pcs->lmodules; module; module = module->next)
317 if (module != outter &&
318 outter->module.BaseOfImage <= module->module.BaseOfImage &&
319 outter->module.BaseOfImage + outter->module.ImageSize >=
320 module->module.BaseOfImage + module->module.ImageSize)
321 return module;
323 return NULL;
326 /******************************************************************
327 * module_get_debug
329 * get the debug information from a module:
330 * - if the module's type is deferred, then force loading of debug info (and return
331 * the module itself)
332 * - if the module has no debug info and has an ELF container, then return the ELF
333 * container (and also force the ELF container's debug info loading if deferred)
334 * - otherwise return the module itself if it has some debug info
336 BOOL module_get_debug(struct module_pair* pair)
338 IMAGEHLP_DEFERRED_SYMBOL_LOADW64 idslW64;
340 if (!pair->requested) return FALSE;
341 /* for a PE builtin, always get info from container */
342 if (!(pair->effective = module_get_container(pair->pcs, pair->requested)))
343 pair->effective = pair->requested;
344 /* if deferred, force loading */
345 if (pair->effective->module.SymType == SymDeferred)
347 BOOL ret;
349 if (pair->effective->is_virtual) ret = FALSE;
350 else switch (pair->effective->type)
352 case DMT_ELF:
353 ret = elf_load_debug_info(pair->effective);
354 break;
355 case DMT_PE:
356 idslW64.SizeOfStruct = sizeof(idslW64);
357 idslW64.BaseOfImage = pair->effective->module.BaseOfImage;
358 idslW64.CheckSum = pair->effective->module.CheckSum;
359 idslW64.TimeDateStamp = pair->effective->module.TimeDateStamp;
360 memcpy(idslW64.FileName, pair->effective->module.ImageName,
361 sizeof(pair->effective->module.ImageName));
362 idslW64.Reparse = FALSE;
363 idslW64.hFile = INVALID_HANDLE_VALUE;
365 pcs_callback(pair->pcs, CBA_DEFERRED_SYMBOL_LOAD_START, &idslW64);
366 ret = pe_load_debug_info(pair->pcs, pair->effective);
367 pcs_callback(pair->pcs,
368 ret ? CBA_DEFERRED_SYMBOL_LOAD_COMPLETE : CBA_DEFERRED_SYMBOL_LOAD_FAILURE,
369 &idslW64);
370 break;
371 case DMT_MACHO:
372 ret = macho_load_debug_info(pair->effective, NULL);
373 break;
374 default:
375 ret = FALSE;
376 break;
378 if (!ret) pair->effective->module.SymType = SymNone;
379 assert(pair->effective->module.SymType != SymDeferred);
380 pair->effective->module.NumSyms = pair->effective->ht_symbols.num_elts;
382 return pair->effective->module.SymType != SymNone;
385 /***********************************************************************
386 * module_find_by_addr
388 * either the addr where module is loaded, or any address inside the
389 * module
391 struct module* module_find_by_addr(const struct process* pcs, DWORD64 addr,
392 enum module_type type)
394 struct module* module;
396 if (type == DMT_UNKNOWN)
398 if ((module = module_find_by_addr(pcs, addr, DMT_PE)) ||
399 (module = module_find_by_addr(pcs, addr, DMT_ELF)) ||
400 (module = module_find_by_addr(pcs, addr, DMT_MACHO)))
401 return module;
403 else
405 for (module = pcs->lmodules; module; module = module->next)
407 if (type == module->type && addr >= module->module.BaseOfImage &&
408 addr < module->module.BaseOfImage + module->module.ImageSize)
409 return module;
412 SetLastError(ERROR_INVALID_ADDRESS);
413 return module;
416 /******************************************************************
417 * module_is_container_loaded
419 * checks whether the native container, for a (supposed) PE builtin is
420 * already loaded
422 static BOOL module_is_container_loaded(const struct process* pcs,
423 const WCHAR* ImageName, DWORD64 base)
425 size_t len;
426 struct module* module;
427 PCWSTR filename, modname;
429 if (!base) return FALSE;
430 filename = get_filename(ImageName, NULL);
431 len = strlenW(filename);
433 for (module = pcs->lmodules; module; module = module->next)
435 if ((module->type == DMT_ELF || module->type == DMT_MACHO) &&
436 base >= module->module.BaseOfImage &&
437 base < module->module.BaseOfImage + module->module.ImageSize)
439 modname = get_filename(module->module.LoadedImageName, NULL);
440 if (!strncmpiW(modname, filename, len) &&
441 !memcmp(modname + len, S_DotSoW, 3 * sizeof(WCHAR)))
443 return TRUE;
447 /* likely a native PE module */
448 WARN("Couldn't find container for %s\n", debugstr_w(ImageName));
449 return FALSE;
452 /******************************************************************
453 * module_get_type_by_name
455 * Guesses a filename type from its extension
457 enum module_type module_get_type_by_name(const WCHAR* name)
459 int loader_len, len = strlenW(name);
460 const WCHAR *loader;
462 /* Skip all version extensions (.[digits]) regex: "(\.\d+)*$" */
465 int i = len;
467 while (i && isdigit(name[i - 1])) i--;
469 if (i && name[i - 1] == '.')
470 len = i - 1;
471 else
472 break;
473 } while (len);
475 /* check for terminating .so or .so.[digit] */
476 /* FIXME: Can't rely solely on extension; have to check magic or
477 * stop using .so on Mac OS X. For now, base on platform. */
478 if (len > 3 && !memcmp(name + len - 3, S_DotSoW, 3))
479 #ifdef __APPLE__
480 return DMT_MACHO;
481 #else
482 return DMT_ELF;
483 #endif
485 if (len > 6 && !strncmpiW(name + len - 6, S_DotDylibW, 6))
486 return DMT_MACHO;
488 if (len > 4 && !strncmpiW(name + len - 4, S_DotPdbW, 4))
489 return DMT_PDB;
491 if (len > 4 && !strncmpiW(name + len - 4, S_DotDbgW, 4))
492 return DMT_DBG;
494 /* wine is also a native module (Mach-O on Mac OS X, ELF elsewhere) */
495 loader = get_wine_loader_name();
496 loader_len = strlenW( loader );
497 if ((len == loader_len || (len > loader_len && name[len - loader_len - 1] == '/')) &&
498 !strcmpiW(name + len - loader_len, loader))
500 #ifdef __APPLE__
501 return DMT_MACHO;
502 #else
503 return DMT_ELF;
504 #endif
506 return DMT_PE;
509 /******************************************************************
510 * refresh_module_list
512 static BOOL refresh_module_list(struct process* pcs)
514 /* force transparent ELF and Mach-O loading / unloading */
515 return elf_synchronize_module_list(pcs) || macho_synchronize_module_list(pcs);
518 /***********************************************************************
519 * SymLoadModule (DBGHELP.@)
521 DWORD WINAPI SymLoadModule(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
522 PCSTR ModuleName, DWORD BaseOfDll, DWORD SizeOfDll)
524 return SymLoadModuleEx(hProcess, hFile, ImageName, ModuleName, BaseOfDll,
525 SizeOfDll, NULL, 0);
528 /***********************************************************************
529 * SymLoadModuleEx (DBGHELP.@)
531 DWORD64 WINAPI SymLoadModuleEx(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
532 PCSTR ModuleName, DWORD64 BaseOfDll, DWORD DllSize,
533 PMODLOAD_DATA Data, DWORD Flags)
535 PWSTR wImageName, wModuleName;
536 unsigned len;
537 DWORD64 ret;
539 TRACE("(%p %p %s %s %s %08x %p %08x)\n",
540 hProcess, hFile, debugstr_a(ImageName), debugstr_a(ModuleName),
541 wine_dbgstr_longlong(BaseOfDll), DllSize, Data, Flags);
543 if (ImageName)
545 len = MultiByteToWideChar(CP_ACP, 0, ImageName, -1, NULL, 0);
546 wImageName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
547 MultiByteToWideChar(CP_ACP, 0, ImageName, -1, wImageName, len);
549 else wImageName = NULL;
550 if (ModuleName)
552 len = MultiByteToWideChar(CP_ACP, 0, ModuleName, -1, NULL, 0);
553 wModuleName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
554 MultiByteToWideChar(CP_ACP, 0, ModuleName, -1, wModuleName, len);
556 else wModuleName = NULL;
558 ret = SymLoadModuleExW(hProcess, hFile, wImageName, wModuleName,
559 BaseOfDll, DllSize, Data, Flags);
560 HeapFree(GetProcessHeap(), 0, wImageName);
561 HeapFree(GetProcessHeap(), 0, wModuleName);
562 return ret;
565 /***********************************************************************
566 * SymLoadModuleExW (DBGHELP.@)
568 DWORD64 WINAPI SymLoadModuleExW(HANDLE hProcess, HANDLE hFile, PCWSTR wImageName,
569 PCWSTR wModuleName, DWORD64 BaseOfDll, DWORD SizeOfDll,
570 PMODLOAD_DATA Data, DWORD Flags)
572 struct process* pcs;
573 struct module* module = NULL;
575 TRACE("(%p %p %s %s %s %08x %p %08x)\n",
576 hProcess, hFile, debugstr_w(wImageName), debugstr_w(wModuleName),
577 wine_dbgstr_longlong(BaseOfDll), SizeOfDll, Data, Flags);
579 if (Data)
580 FIXME("Unsupported load data parameter %p for %s\n",
581 Data, debugstr_w(wImageName));
582 if (!validate_addr64(BaseOfDll)) return FALSE;
584 if (!(pcs = process_find_by_handle(hProcess))) return FALSE;
586 if (Flags & SLMFLAG_VIRTUAL)
588 if (!wImageName) return FALSE;
589 module = module_new(pcs, wImageName, module_get_type_by_name(wImageName),
590 TRUE, BaseOfDll, SizeOfDll, 0, 0);
591 if (!module) return FALSE;
592 if (wModuleName) module_set_module(module, wModuleName);
593 module->module.SymType = SymVirtual;
595 return TRUE;
597 if (Flags & ~(SLMFLAG_VIRTUAL))
598 FIXME("Unsupported Flags %08x for %s\n", Flags, debugstr_w(wImageName));
600 refresh_module_list(pcs);
602 /* this is a Wine extension to the API just to redo the synchronisation */
603 if (!wImageName && !hFile) return 0;
605 /* check if the module is already loaded, or if it's a builtin PE module with
606 * an containing ELF module
608 if (wImageName)
610 module = module_is_already_loaded(pcs, wImageName);
611 if (!module && module_is_container_loaded(pcs, wImageName, BaseOfDll))
613 /* force the loading of DLL as builtin */
614 module = pe_load_builtin_module(pcs, wImageName, BaseOfDll, SizeOfDll);
617 if (!module)
619 /* otherwise, try a regular PE module */
620 if (!(module = pe_load_native_module(pcs, wImageName, hFile, BaseOfDll, SizeOfDll)) &&
621 wImageName)
623 /* and finally an ELF or Mach-O module */
624 switch (module_get_type_by_name(wImageName))
626 case DMT_ELF:
627 module = elf_load_module(pcs, wImageName, BaseOfDll);
628 break;
629 case DMT_MACHO:
630 module = macho_load_module(pcs, wImageName, BaseOfDll);
631 break;
632 default:
633 /* Ignored */
634 break;
638 if (!module)
640 WARN("Couldn't locate %s\n", debugstr_w(wImageName));
641 return 0;
643 module->module.NumSyms = module->ht_symbols.num_elts;
644 /* by default module_new fills module.ModuleName from a derivation
645 * of LoadedImageName. Overwrite it, if we have better information
647 if (wModuleName)
648 module_set_module(module, wModuleName);
649 if (wImageName)
650 lstrcpynW(module->module.ImageName, wImageName,
651 sizeof(module->module.ImageName) / sizeof(WCHAR));
653 return module->module.BaseOfImage;
656 /***********************************************************************
657 * SymLoadModule64 (DBGHELP.@)
659 DWORD64 WINAPI SymLoadModule64(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
660 PCSTR ModuleName, DWORD64 BaseOfDll, DWORD SizeOfDll)
662 return SymLoadModuleEx(hProcess, hFile, ImageName, ModuleName, BaseOfDll, SizeOfDll,
663 NULL, 0);
666 /******************************************************************
667 * module_remove
670 BOOL module_remove(struct process* pcs, struct module* module)
672 struct module_format*modfmt;
673 struct module** p;
674 unsigned i;
676 TRACE("%s (%p)\n", debugstr_w(module->module.ModuleName), module);
678 for (i = 0; i < DFI_LAST; i++)
680 if ((modfmt = module->format_info[i]) && modfmt->remove)
681 modfmt->remove(pcs, module->format_info[i]);
683 hash_table_destroy(&module->ht_symbols);
684 hash_table_destroy(&module->ht_types);
685 wine_rb_destroy(&module->sources_offsets_tree, NULL, NULL);
686 HeapFree(GetProcessHeap(), 0, module->sources);
687 HeapFree(GetProcessHeap(), 0, module->addr_sorttab);
688 pool_destroy(&module->pool);
689 /* native dbghelp doesn't invoke registered callback(,CBA_SYMBOLS_UNLOADED,) here
690 * so do we
692 for (p = &pcs->lmodules; *p; p = &(*p)->next)
694 if (*p == module)
696 *p = module->next;
697 HeapFree(GetProcessHeap(), 0, module);
698 return TRUE;
701 FIXME("This shouldn't happen\n");
702 return FALSE;
705 /******************************************************************
706 * SymUnloadModule (DBGHELP.@)
709 BOOL WINAPI SymUnloadModule(HANDLE hProcess, DWORD BaseOfDll)
711 struct process* pcs;
712 struct module* module;
714 pcs = process_find_by_handle(hProcess);
715 if (!pcs) return FALSE;
716 module = module_find_by_addr(pcs, BaseOfDll, DMT_UNKNOWN);
717 if (!module) return FALSE;
718 return module_remove(pcs, module);
721 /******************************************************************
722 * SymUnloadModule64 (DBGHELP.@)
725 BOOL WINAPI SymUnloadModule64(HANDLE hProcess, DWORD64 BaseOfDll)
727 struct process* pcs;
728 struct module* module;
730 pcs = process_find_by_handle(hProcess);
731 if (!pcs) return FALSE;
732 if (!validate_addr64(BaseOfDll)) return FALSE;
733 module = module_find_by_addr(pcs, BaseOfDll, DMT_UNKNOWN);
734 if (!module) return FALSE;
735 return module_remove(pcs, module);
738 /******************************************************************
739 * SymEnumerateModules (DBGHELP.@)
742 struct enum_modW64_32
744 PSYM_ENUMMODULES_CALLBACK cb;
745 PVOID user;
746 char module[MAX_PATH];
749 static BOOL CALLBACK enum_modW64_32(PCWSTR name, DWORD64 base, PVOID user)
751 struct enum_modW64_32* x = user;
753 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
754 return x->cb(x->module, (DWORD)base, x->user);
757 BOOL WINAPI SymEnumerateModules(HANDLE hProcess,
758 PSYM_ENUMMODULES_CALLBACK EnumModulesCallback,
759 PVOID UserContext)
761 struct enum_modW64_32 x;
763 x.cb = EnumModulesCallback;
764 x.user = UserContext;
766 return SymEnumerateModulesW64(hProcess, enum_modW64_32, &x);
769 /******************************************************************
770 * SymEnumerateModules64 (DBGHELP.@)
773 struct enum_modW64_64
775 PSYM_ENUMMODULES_CALLBACK64 cb;
776 PVOID user;
777 char module[MAX_PATH];
780 static BOOL CALLBACK enum_modW64_64(PCWSTR name, DWORD64 base, PVOID user)
782 struct enum_modW64_64* x = user;
784 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
785 return x->cb(x->module, base, x->user);
788 BOOL WINAPI SymEnumerateModules64(HANDLE hProcess,
789 PSYM_ENUMMODULES_CALLBACK64 EnumModulesCallback,
790 PVOID UserContext)
792 struct enum_modW64_64 x;
794 x.cb = EnumModulesCallback;
795 x.user = UserContext;
797 return SymEnumerateModulesW64(hProcess, enum_modW64_64, &x);
800 /******************************************************************
801 * SymEnumerateModulesW64 (DBGHELP.@)
804 BOOL WINAPI SymEnumerateModulesW64(HANDLE hProcess,
805 PSYM_ENUMMODULES_CALLBACKW64 EnumModulesCallback,
806 PVOID UserContext)
808 struct process* pcs = process_find_by_handle(hProcess);
809 struct module* module;
811 if (!pcs) return FALSE;
813 for (module = pcs->lmodules; module; module = module->next)
815 if (!(dbghelp_options & SYMOPT_WINE_WITH_NATIVE_MODULES) &&
816 (module->type == DMT_ELF || module->type == DMT_MACHO))
817 continue;
818 if (!EnumModulesCallback(module->module.ModuleName,
819 module->module.BaseOfImage, UserContext))
820 break;
822 return TRUE;
825 /******************************************************************
826 * EnumerateLoadedModules64 (DBGHELP.@)
829 struct enum_load_modW64_64
831 PENUMLOADED_MODULES_CALLBACK64 cb;
832 PVOID user;
833 char module[MAX_PATH];
836 static BOOL CALLBACK enum_load_modW64_64(PCWSTR name, DWORD64 base, ULONG size,
837 PVOID user)
839 struct enum_load_modW64_64* x = user;
841 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
842 return x->cb(x->module, base, size, x->user);
845 BOOL WINAPI EnumerateLoadedModules64(HANDLE hProcess,
846 PENUMLOADED_MODULES_CALLBACK64 EnumLoadedModulesCallback,
847 PVOID UserContext)
849 struct enum_load_modW64_64 x;
851 x.cb = EnumLoadedModulesCallback;
852 x.user = UserContext;
854 return EnumerateLoadedModulesW64(hProcess, enum_load_modW64_64, &x);
857 /******************************************************************
858 * EnumerateLoadedModules (DBGHELP.@)
861 struct enum_load_modW64_32
863 PENUMLOADED_MODULES_CALLBACK cb;
864 PVOID user;
865 char module[MAX_PATH];
868 static BOOL CALLBACK enum_load_modW64_32(PCWSTR name, DWORD64 base, ULONG size,
869 PVOID user)
871 struct enum_load_modW64_32* x = user;
872 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
873 return x->cb(x->module, (DWORD)base, size, x->user);
876 BOOL WINAPI EnumerateLoadedModules(HANDLE hProcess,
877 PENUMLOADED_MODULES_CALLBACK EnumLoadedModulesCallback,
878 PVOID UserContext)
880 struct enum_load_modW64_32 x;
882 x.cb = EnumLoadedModulesCallback;
883 x.user = UserContext;
885 return EnumerateLoadedModulesW64(hProcess, enum_load_modW64_32, &x);
888 /******************************************************************
889 * EnumerateLoadedModulesW64 (DBGHELP.@)
892 BOOL WINAPI EnumerateLoadedModulesW64(HANDLE hProcess,
893 PENUMLOADED_MODULES_CALLBACKW64 EnumLoadedModulesCallback,
894 PVOID UserContext)
896 HMODULE* hMods;
897 WCHAR baseW[256], modW[256];
898 DWORD i, sz;
899 MODULEINFO mi;
901 hMods = HeapAlloc(GetProcessHeap(), 0, 256 * sizeof(hMods[0]));
902 if (!hMods) return FALSE;
904 if (!EnumProcessModules(hProcess, hMods, 256 * sizeof(hMods[0]), &sz))
906 /* hProcess should also be a valid process handle !! */
907 FIXME("If this happens, bump the number in mod\n");
908 HeapFree(GetProcessHeap(), 0, hMods);
909 return FALSE;
911 sz /= sizeof(HMODULE);
912 for (i = 0; i < sz; i++)
914 if (!GetModuleInformation(hProcess, hMods[i], &mi, sizeof(mi)) ||
915 !GetModuleBaseNameW(hProcess, hMods[i], baseW, sizeof(baseW) / sizeof(WCHAR)))
916 continue;
917 module_fill_module(baseW, modW, sizeof(modW) / sizeof(CHAR));
918 EnumLoadedModulesCallback(modW, (DWORD_PTR)mi.lpBaseOfDll, mi.SizeOfImage,
919 UserContext);
921 HeapFree(GetProcessHeap(), 0, hMods);
923 return sz != 0 && i == sz;
926 /******************************************************************
927 * SymGetModuleInfo (DBGHELP.@)
930 BOOL WINAPI SymGetModuleInfo(HANDLE hProcess, DWORD dwAddr,
931 PIMAGEHLP_MODULE ModuleInfo)
933 IMAGEHLP_MODULE mi;
934 IMAGEHLP_MODULEW64 miw64;
936 if (sizeof(mi) < ModuleInfo->SizeOfStruct) FIXME("Wrong size\n");
938 miw64.SizeOfStruct = sizeof(miw64);
939 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
941 mi.SizeOfStruct = miw64.SizeOfStruct;
942 mi.BaseOfImage = miw64.BaseOfImage;
943 mi.ImageSize = miw64.ImageSize;
944 mi.TimeDateStamp = miw64.TimeDateStamp;
945 mi.CheckSum = miw64.CheckSum;
946 mi.NumSyms = miw64.NumSyms;
947 mi.SymType = miw64.SymType;
948 WideCharToMultiByte(CP_ACP, 0, miw64.ModuleName, -1,
949 mi.ModuleName, sizeof(mi.ModuleName), NULL, NULL);
950 WideCharToMultiByte(CP_ACP, 0, miw64.ImageName, -1,
951 mi.ImageName, sizeof(mi.ImageName), NULL, NULL);
952 WideCharToMultiByte(CP_ACP, 0, miw64.LoadedImageName, -1,
953 mi.LoadedImageName, sizeof(mi.LoadedImageName), NULL, NULL);
955 memcpy(ModuleInfo, &mi, ModuleInfo->SizeOfStruct);
957 return TRUE;
960 /******************************************************************
961 * SymGetModuleInfoW (DBGHELP.@)
964 BOOL WINAPI SymGetModuleInfoW(HANDLE hProcess, DWORD dwAddr,
965 PIMAGEHLP_MODULEW ModuleInfo)
967 IMAGEHLP_MODULEW64 miw64;
968 IMAGEHLP_MODULEW miw;
970 if (sizeof(miw) < ModuleInfo->SizeOfStruct) FIXME("Wrong size\n");
972 miw64.SizeOfStruct = sizeof(miw64);
973 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
975 miw.SizeOfStruct = miw64.SizeOfStruct;
976 miw.BaseOfImage = miw64.BaseOfImage;
977 miw.ImageSize = miw64.ImageSize;
978 miw.TimeDateStamp = miw64.TimeDateStamp;
979 miw.CheckSum = miw64.CheckSum;
980 miw.NumSyms = miw64.NumSyms;
981 miw.SymType = miw64.SymType;
982 strcpyW(miw.ModuleName, miw64.ModuleName);
983 strcpyW(miw.ImageName, miw64.ImageName);
984 strcpyW(miw.LoadedImageName, miw64.LoadedImageName);
985 memcpy(ModuleInfo, &miw, ModuleInfo->SizeOfStruct);
987 return TRUE;
990 /******************************************************************
991 * SymGetModuleInfo64 (DBGHELP.@)
994 BOOL WINAPI SymGetModuleInfo64(HANDLE hProcess, DWORD64 dwAddr,
995 PIMAGEHLP_MODULE64 ModuleInfo)
997 IMAGEHLP_MODULE64 mi64;
998 IMAGEHLP_MODULEW64 miw64;
1000 if (sizeof(mi64) < ModuleInfo->SizeOfStruct)
1002 SetLastError(ERROR_MOD_NOT_FOUND); /* NOTE: native returns this error */
1003 WARN("Wrong size %u\n", ModuleInfo->SizeOfStruct);
1004 return FALSE;
1007 miw64.SizeOfStruct = sizeof(miw64);
1008 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
1010 mi64.SizeOfStruct = miw64.SizeOfStruct;
1011 mi64.BaseOfImage = miw64.BaseOfImage;
1012 mi64.ImageSize = miw64.ImageSize;
1013 mi64.TimeDateStamp = miw64.TimeDateStamp;
1014 mi64.CheckSum = miw64.CheckSum;
1015 mi64.NumSyms = miw64.NumSyms;
1016 mi64.SymType = miw64.SymType;
1017 WideCharToMultiByte(CP_ACP, 0, miw64.ModuleName, -1,
1018 mi64.ModuleName, sizeof(mi64.ModuleName), NULL, NULL);
1019 WideCharToMultiByte(CP_ACP, 0, miw64.ImageName, -1,
1020 mi64.ImageName, sizeof(mi64.ImageName), NULL, NULL);
1021 WideCharToMultiByte(CP_ACP, 0, miw64.LoadedImageName, -1,
1022 mi64.LoadedImageName, sizeof(mi64.LoadedImageName), NULL, NULL);
1023 WideCharToMultiByte(CP_ACP, 0, miw64.LoadedPdbName, -1,
1024 mi64.LoadedPdbName, sizeof(mi64.LoadedPdbName), NULL, NULL);
1026 mi64.CVSig = miw64.CVSig;
1027 WideCharToMultiByte(CP_ACP, 0, miw64.CVData, -1,
1028 mi64.CVData, sizeof(mi64.CVData), NULL, NULL);
1029 mi64.PdbSig = miw64.PdbSig;
1030 mi64.PdbSig70 = miw64.PdbSig70;
1031 mi64.PdbAge = miw64.PdbAge;
1032 mi64.PdbUnmatched = miw64.PdbUnmatched;
1033 mi64.DbgUnmatched = miw64.DbgUnmatched;
1034 mi64.LineNumbers = miw64.LineNumbers;
1035 mi64.GlobalSymbols = miw64.GlobalSymbols;
1036 mi64.TypeInfo = miw64.TypeInfo;
1037 mi64.SourceIndexed = miw64.SourceIndexed;
1038 mi64.Publics = miw64.Publics;
1040 memcpy(ModuleInfo, &mi64, ModuleInfo->SizeOfStruct);
1042 return TRUE;
1045 /******************************************************************
1046 * SymGetModuleInfoW64 (DBGHELP.@)
1049 BOOL WINAPI SymGetModuleInfoW64(HANDLE hProcess, DWORD64 dwAddr,
1050 PIMAGEHLP_MODULEW64 ModuleInfo)
1052 struct process* pcs = process_find_by_handle(hProcess);
1053 struct module* module;
1054 IMAGEHLP_MODULEW64 miw64;
1056 TRACE("%p %s %p\n", hProcess, wine_dbgstr_longlong(dwAddr), ModuleInfo);
1058 if (!pcs) return FALSE;
1059 if (ModuleInfo->SizeOfStruct > sizeof(*ModuleInfo)) return FALSE;
1060 module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
1061 if (!module) return FALSE;
1063 miw64 = module->module;
1065 /* update debug information from container if any */
1066 if (module->module.SymType == SymNone)
1068 module = module_get_container(pcs, module);
1069 if (module && module->module.SymType != SymNone)
1071 miw64.SymType = module->module.SymType;
1072 miw64.NumSyms = module->module.NumSyms;
1075 memcpy(ModuleInfo, &miw64, ModuleInfo->SizeOfStruct);
1076 return TRUE;
1079 /***********************************************************************
1080 * SymGetModuleBase (DBGHELP.@)
1082 DWORD WINAPI SymGetModuleBase(HANDLE hProcess, DWORD dwAddr)
1084 DWORD64 ret;
1086 ret = SymGetModuleBase64(hProcess, dwAddr);
1087 return validate_addr64(ret) ? ret : 0;
1090 /***********************************************************************
1091 * SymGetModuleBase64 (DBGHELP.@)
1093 DWORD64 WINAPI SymGetModuleBase64(HANDLE hProcess, DWORD64 dwAddr)
1095 struct process* pcs = process_find_by_handle(hProcess);
1096 struct module* module;
1098 if (!pcs) return 0;
1099 module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
1100 if (!module) return 0;
1101 return module->module.BaseOfImage;
1104 /******************************************************************
1105 * module_reset_debug_info
1106 * Removes any debug information linked to a given module.
1108 void module_reset_debug_info(struct module* module)
1110 module->sortlist_valid = TRUE;
1111 module->sorttab_size = 0;
1112 module->addr_sorttab = NULL;
1113 module->num_sorttab = module->num_symbols = 0;
1114 hash_table_destroy(&module->ht_symbols);
1115 module->ht_symbols.num_buckets = 0;
1116 module->ht_symbols.buckets = NULL;
1117 hash_table_destroy(&module->ht_types);
1118 module->ht_types.num_buckets = 0;
1119 module->ht_types.buckets = NULL;
1120 module->vtypes.num_elts = 0;
1121 hash_table_destroy(&module->ht_symbols);
1122 module->sources_used = module->sources_alloc = 0;
1123 module->sources = NULL;
1126 /******************************************************************
1127 * SymRefreshModuleList (DBGHELP.@)
1129 BOOL WINAPI SymRefreshModuleList(HANDLE hProcess)
1131 struct process* pcs;
1133 TRACE("(%p)\n", hProcess);
1135 if (!(pcs = process_find_by_handle(hProcess))) return FALSE;
1137 return refresh_module_list(pcs);
1140 /***********************************************************************
1141 * SymFunctionTableAccess (DBGHELP.@)
1143 PVOID WINAPI SymFunctionTableAccess(HANDLE hProcess, DWORD AddrBase)
1145 return SymFunctionTableAccess64(hProcess, AddrBase);
1148 /***********************************************************************
1149 * SymFunctionTableAccess64 (DBGHELP.@)
1151 PVOID WINAPI SymFunctionTableAccess64(HANDLE hProcess, DWORD64 AddrBase)
1153 struct process* pcs = process_find_by_handle(hProcess);
1154 struct module* module;
1156 if (!pcs || !dbghelp_current_cpu->find_runtime_function) return NULL;
1157 module = module_find_by_addr(pcs, AddrBase, DMT_UNKNOWN);
1158 if (!module) return NULL;
1160 return dbghelp_current_cpu->find_runtime_function(module, AddrBase);