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