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