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