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