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