Sync with trunk r58151 to bring the latest changes from Amine and Timo.
[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 WideCharToMultiByte(CP_ACP, 0, module->module.ModuleName, -1,
105 module->module_name, sizeof(module->module_name),
106 NULL, NULL);
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 ? TRUE : FALSE;
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 case DMT_ELF:
353 ret = elf_load_debug_info(pair->effective);
354 break;
355 case DMT_PE:
356 idslW64.SizeOfStruct = sizeof(idslW64);
357 idslW64.BaseOfImage = pair->effective->module.BaseOfImage;
358 idslW64.CheckSum = pair->effective->module.CheckSum;
359 idslW64.TimeDateStamp = pair->effective->module.TimeDateStamp;
360 memcpy(idslW64.FileName, pair->effective->module.ImageName,
361 sizeof(pair->effective->module.ImageName));
362 idslW64.Reparse = FALSE;
363 idslW64.hFile = INVALID_HANDLE_VALUE;
364
365 pcs_callback(pair->pcs, CBA_DEFERRED_SYMBOL_LOAD_START, &idslW64);
366 ret = pe_load_debug_info(pair->pcs, pair->effective);
367 pcs_callback(pair->pcs,
368 ret ? CBA_DEFERRED_SYMBOL_LOAD_COMPLETE : CBA_DEFERRED_SYMBOL_LOAD_FAILURE,
369 &idslW64);
370 break;
371 case DMT_MACHO:
372 ret = macho_load_debug_info(pair->effective, NULL);
373 break;
374 default:
375 ret = FALSE;
376 break;
377 }
378 if (!ret) pair->effective->module.SymType = SymNone;
379 assert(pair->effective->module.SymType != SymDeferred);
380 pair->effective->module.NumSyms = pair->effective->ht_symbols.num_elts;
381 }
382 return pair->effective->module.SymType != SymNone;
383 }
384
385 /***********************************************************************
386 * module_find_by_addr
387 *
388 * either the addr where module is loaded, or any address inside the
389 * module
390 */
391 struct module* module_find_by_addr(const struct process* pcs, unsigned long addr,
392 enum module_type type)
393 {
394 struct module* module;
395
396 if (type == DMT_UNKNOWN)
397 {
398 if ((module = module_find_by_addr(pcs, addr, DMT_PE)) ||
399 (module = module_find_by_addr(pcs, addr, DMT_ELF)) ||
400 (module = module_find_by_addr(pcs, addr, DMT_MACHO)))
401 return module;
402 }
403 else
404 {
405 for (module = pcs->lmodules; module; module = module->next)
406 {
407 if (type == module->type && addr >= module->module.BaseOfImage &&
408 addr < module->module.BaseOfImage + module->module.ImageSize)
409 return module;
410 }
411 }
412 SetLastError(ERROR_INVALID_ADDRESS);
413 return module;
414 }
415
416 /******************************************************************
417 * module_is_container_loaded
418 *
419 * checks whether the native container, for a (supposed) PE builtin is
420 * already loaded
421 */
422 static BOOL module_is_container_loaded(const struct process* pcs,
423 const WCHAR* ImageName, DWORD64 base)
424 {
425 size_t len;
426 struct module* module;
427 PCWSTR filename, modname;
428
429 if (!base) return FALSE;
430 filename = get_filename(ImageName, NULL);
431 len = strlenW(filename);
432
433 for (module = pcs->lmodules; module; module = module->next)
434 {
435 if ((module->type == DMT_ELF || module->type == DMT_MACHO) &&
436 base >= module->module.BaseOfImage &&
437 base < module->module.BaseOfImage + module->module.ImageSize)
438 {
439 modname = get_filename(module->module.LoadedImageName, NULL);
440 if (!strncmpiW(modname, filename, len) &&
441 !memcmp(modname + len, S_DotSoW, 3 * sizeof(WCHAR)))
442 {
443 return TRUE;
444 }
445 }
446 }
447 /* likely a native PE module */
448 WARN("Couldn't find container for %s\n", debugstr_w(ImageName));
449 return FALSE;
450 }
451
452 /******************************************************************
453 * module_get_type_by_name
454 *
455 * Guesses a filename type from its extension
456 */
457 enum module_type module_get_type_by_name(const WCHAR* name)
458 {
459 int loader_len, len = strlenW(name);
460 const WCHAR *loader;
461
462 /* Skip all version extensions (.[digits]) regex: "(\.\d+)*$" */
463 do
464 {
465 int i = len;
466
467 while (i && isdigit(name[i - 1])) i--;
468
469 if (i && name[i - 1] == '.')
470 len = i - 1;
471 else
472 break;
473 } while (len);
474
475 /* check for terminating .so or .so.[digit] */
476 /* FIXME: Can't rely solely on extension; have to check magic or
477 * stop using .so on Mac OS X. For now, base on platform. */
478 if (len > 3 && !memcmp(name + len - 3, S_DotSoW, 3))
479 #ifdef __APPLE__
480 return DMT_MACHO;
481 #else
482 return DMT_ELF;
483 #endif
484
485 if (len > 6 && !strncmpiW(name + len - 6, S_DotDylibW, 6))
486 return DMT_MACHO;
487
488 if (len > 4 && !strncmpiW(name + len - 4, S_DotPdbW, 4))
489 return DMT_PDB;
490
491 if (len > 4 && !strncmpiW(name + len - 4, S_DotDbgW, 4))
492 return DMT_DBG;
493
494 /* wine is also a native module (Mach-O on Mac OS X, ELF elsewhere) */
495 loader = get_wine_loader_name();
496 loader_len = strlenW( loader );
497 if ((len == loader_len || (len > loader_len && name[len - loader_len - 1] == '/')) &&
498 !strcmpiW(name + len - loader_len, loader))
499 {
500 #ifdef __APPLE__
501 return DMT_MACHO;
502 #else
503 return DMT_ELF;
504 #endif
505 }
506 return DMT_PE;
507 }
508
509 /******************************************************************
510 * refresh_module_list
511 */
512 static BOOL refresh_module_list(struct process* pcs)
513 {
514 /* force transparent ELF and Mach-O loading / unloading */
515 return elf_synchronize_module_list(pcs) || macho_synchronize_module_list(pcs);
516 }
517
518 /***********************************************************************
519 * SymLoadModule (DBGHELP.@)
520 */
521 DWORD WINAPI SymLoadModule(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
522 PCSTR ModuleName, DWORD BaseOfDll, DWORD SizeOfDll)
523 {
524 return SymLoadModuleEx(hProcess, hFile, ImageName, ModuleName, BaseOfDll,
525 SizeOfDll, NULL, 0);
526 }
527
528 /***********************************************************************
529 * SymLoadModuleEx (DBGHELP.@)
530 */
531 DWORD64 WINAPI SymLoadModuleEx(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
532 PCSTR ModuleName, DWORD64 BaseOfDll, DWORD DllSize,
533 PMODLOAD_DATA Data, DWORD Flags)
534 {
535 PWSTR wImageName, wModuleName;
536 unsigned len;
537 DWORD64 ret;
538
539 TRACE("(%p %p %s %s %s %08x %p %08x)\n",
540 hProcess, hFile, debugstr_a(ImageName), debugstr_a(ModuleName),
541 wine_dbgstr_longlong(BaseOfDll), DllSize, Data, Flags);
542
543 if (ImageName)
544 {
545 len = MultiByteToWideChar(CP_ACP, 0, ImageName, -1, NULL, 0);
546 wImageName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
547 MultiByteToWideChar(CP_ACP, 0, ImageName, -1, wImageName, len);
548 }
549 else wImageName = NULL;
550 if (ModuleName)
551 {
552 len = MultiByteToWideChar(CP_ACP, 0, ModuleName, -1, NULL, 0);
553 wModuleName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
554 MultiByteToWideChar(CP_ACP, 0, ModuleName, -1, wModuleName, len);
555 }
556 else wModuleName = NULL;
557
558 ret = SymLoadModuleExW(hProcess, hFile, wImageName, wModuleName,
559 BaseOfDll, DllSize, Data, Flags);
560 HeapFree(GetProcessHeap(), 0, wImageName);
561 HeapFree(GetProcessHeap(), 0, wModuleName);
562 return ret;
563 }
564
565 /***********************************************************************
566 * SymLoadModuleExW (DBGHELP.@)
567 */
568 DWORD64 WINAPI SymLoadModuleExW(HANDLE hProcess, HANDLE hFile, PCWSTR wImageName,
569 PCWSTR wModuleName, DWORD64 BaseOfDll, DWORD SizeOfDll,
570 PMODLOAD_DATA Data, DWORD Flags)
571 {
572 struct process* pcs;
573 struct module* module = NULL;
574
575 TRACE("(%p %p %s %s %s %08x %p %08x)\n",
576 hProcess, hFile, debugstr_w(wImageName), debugstr_w(wModuleName),
577 wine_dbgstr_longlong(BaseOfDll), SizeOfDll, Data, Flags);
578
579 if (Data)
580 FIXME("Unsupported load data parameter %p for %s\n",
581 Data, debugstr_w(wImageName));
582 if (!validate_addr64(BaseOfDll)) return FALSE;
583
584 if (!(pcs = process_find_by_handle(hProcess))) return FALSE;
585
586 if (Flags & SLMFLAG_VIRTUAL)
587 {
588 if (!wImageName) return FALSE;
589 module = module_new(pcs, wImageName, module_get_type_by_name(wImageName),
590 TRUE, BaseOfDll, SizeOfDll, 0, 0);
591 if (!module) return FALSE;
592 if (wModuleName) module_set_module(module, wModuleName);
593 module->module.SymType = SymVirtual;
594
595 return TRUE;
596 }
597 if (Flags & ~(SLMFLAG_VIRTUAL))
598 FIXME("Unsupported Flags %08x for %s\n", Flags, debugstr_w(wImageName));
599
600 refresh_module_list(pcs);
601
602 /* this is a Wine extension to the API just to redo the synchronisation */
603 if (!wImageName && !hFile) return 0;
604
605 /* check if the module is already loaded, or if it's a builtin PE module with
606 * an containing ELF module
607 */
608 if (wImageName)
609 {
610 module = module_is_already_loaded(pcs, wImageName);
611 if (!module && module_is_container_loaded(pcs, wImageName, BaseOfDll))
612 {
613 /* force the loading of DLL as builtin */
614 module = pe_load_builtin_module(pcs, wImageName, BaseOfDll, SizeOfDll);
615 }
616 }
617 if (!module)
618 {
619 /* otherwise, try a regular PE module */
620 if (!(module = pe_load_native_module(pcs, wImageName, hFile, BaseOfDll, SizeOfDll)) &&
621 wImageName)
622 {
623 /* and finally an ELF or Mach-O module */
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 }
637 }
638 if (!module)
639 {
640 WARN("Couldn't locate %s\n", debugstr_w(wImageName));
641 return 0;
642 }
643 module->module.NumSyms = module->ht_symbols.num_elts;
644 /* by default module_new fills module.ModuleName from a derivation
645 * of LoadedImageName. Overwrite it, if we have better information
646 */
647 if (wModuleName)
648 module_set_module(module, wModuleName);
649 if (wImageName)
650 lstrcpynW(module->module.ImageName, wImageName,
651 sizeof(module->module.ImageName) / sizeof(WCHAR));
652
653 return module->module.BaseOfImage;
654 }
655
656 /***********************************************************************
657 * SymLoadModule64 (DBGHELP.@)
658 */
659 DWORD64 WINAPI SymLoadModule64(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
660 PCSTR ModuleName, DWORD64 BaseOfDll, DWORD SizeOfDll)
661 {
662 if (!validate_addr64(BaseOfDll)) return FALSE;
663 return SymLoadModule(hProcess, hFile, ImageName, ModuleName, (DWORD)BaseOfDll, SizeOfDll);
664 }
665
666 /******************************************************************
667 * module_remove
668 *
669 */
670 BOOL module_remove(struct process* pcs, struct module* module)
671 {
672 struct module_format*modfmt;
673 struct module** p;
674 unsigned i;
675
676 TRACE("%s (%p)\n", debugstr_w(module->module.ModuleName), module);
677
678 for (i = 0; i < DFI_LAST; i++)
679 {
680 if ((modfmt = module->format_info[i]) && modfmt->remove)
681 modfmt->remove(pcs, module->format_info[i]);
682 }
683 hash_table_destroy(&module->ht_symbols);
684 hash_table_destroy(&module->ht_types);
685 wine_rb_destroy(&module->sources_offsets_tree, NULL, NULL);
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, (DWORD)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 /******************************************************************
826 * EnumerateLoadedModules64 (DBGHELP.@)
827 *
828 */
829 struct enum_load_modW64_64
830 {
831 PENUMLOADED_MODULES_CALLBACK64 cb;
832 PVOID user;
833 char module[MAX_PATH];
834 };
835
836 static BOOL CALLBACK enum_load_modW64_64(PCWSTR name, DWORD64 base, ULONG size,
837 PVOID user)
838 {
839 struct enum_load_modW64_64* x = user;
840
841 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
842 return x->cb(x->module, base, size, x->user);
843 }
844
845 BOOL WINAPI EnumerateLoadedModules64(HANDLE hProcess,
846 PENUMLOADED_MODULES_CALLBACK64 EnumLoadedModulesCallback,
847 PVOID UserContext)
848 {
849 struct enum_load_modW64_64 x;
850
851 x.cb = EnumLoadedModulesCallback;
852 x.user = UserContext;
853
854 return EnumerateLoadedModulesW64(hProcess, enum_load_modW64_64, &x);
855 }
856
857 /******************************************************************
858 * EnumerateLoadedModules (DBGHELP.@)
859 *
860 */
861 struct enum_load_modW64_32
862 {
863 PENUMLOADED_MODULES_CALLBACK cb;
864 PVOID user;
865 char module[MAX_PATH];
866 };
867
868 static BOOL CALLBACK enum_load_modW64_32(PCWSTR name, DWORD64 base, ULONG size,
869 PVOID user)
870 {
871 struct enum_load_modW64_32* x = user;
872 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
873 return x->cb(x->module, (DWORD)base, size, x->user);
874 }
875
876 BOOL WINAPI EnumerateLoadedModules(HANDLE hProcess,
877 PENUMLOADED_MODULES_CALLBACK EnumLoadedModulesCallback,
878 PVOID UserContext)
879 {
880 struct enum_load_modW64_32 x;
881
882 x.cb = EnumLoadedModulesCallback;
883 x.user = UserContext;
884
885 return EnumerateLoadedModulesW64(hProcess, enum_load_modW64_32, &x);
886 }
887
888 /******************************************************************
889 * EnumerateLoadedModulesW64 (DBGHELP.@)
890 *
891 */
892 BOOL WINAPI EnumerateLoadedModulesW64(HANDLE hProcess,
893 PENUMLOADED_MODULES_CALLBACKW64 EnumLoadedModulesCallback,
894 PVOID UserContext)
895 {
896 HMODULE* hMods;
897 WCHAR baseW[256], modW[256];
898 DWORD i, sz;
899 MODULEINFO mi;
900
901 hMods = HeapAlloc(GetProcessHeap(), 0, 256 * sizeof(hMods[0]));
902 if (!hMods) return FALSE;
903
904 if (!EnumProcessModules(hProcess, hMods, 256 * sizeof(hMods[0]), &sz))
905 {
906 /* hProcess should also be a valid process handle !! */
907 FIXME("If this happens, bump the number in mod\n");
908 HeapFree(GetProcessHeap(), 0, hMods);
909 return FALSE;
910 }
911 sz /= sizeof(HMODULE);
912 for (i = 0; i < sz; i++)
913 {
914 if (!GetModuleInformation(hProcess, hMods[i], &mi, sizeof(mi)) ||
915 !GetModuleBaseNameW(hProcess, hMods[i], baseW, sizeof(baseW) / sizeof(WCHAR)))
916 continue;
917 module_fill_module(baseW, modW, sizeof(modW) / sizeof(CHAR));
918 EnumLoadedModulesCallback(modW, (DWORD_PTR)mi.lpBaseOfDll, mi.SizeOfImage,
919 UserContext);
920 }
921 HeapFree(GetProcessHeap(), 0, hMods);
922
923 return sz != 0 && i == sz;
924 }
925
926 /******************************************************************
927 * SymGetModuleInfo (DBGHELP.@)
928 *
929 */
930 BOOL WINAPI SymGetModuleInfo(HANDLE hProcess, DWORD dwAddr,
931 PIMAGEHLP_MODULE ModuleInfo)
932 {
933 IMAGEHLP_MODULE mi;
934 IMAGEHLP_MODULEW64 miw64;
935
936 if (sizeof(mi) < ModuleInfo->SizeOfStruct) FIXME("Wrong size\n");
937
938 miw64.SizeOfStruct = sizeof(miw64);
939 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
940
941 mi.SizeOfStruct = miw64.SizeOfStruct;
942 mi.BaseOfImage = miw64.BaseOfImage;
943 mi.ImageSize = miw64.ImageSize;
944 mi.TimeDateStamp = miw64.TimeDateStamp;
945 mi.CheckSum = miw64.CheckSum;
946 mi.NumSyms = miw64.NumSyms;
947 mi.SymType = miw64.SymType;
948 WideCharToMultiByte(CP_ACP, 0, miw64.ModuleName, -1,
949 mi.ModuleName, sizeof(mi.ModuleName), NULL, NULL);
950 WideCharToMultiByte(CP_ACP, 0, miw64.ImageName, -1,
951 mi.ImageName, sizeof(mi.ImageName), NULL, NULL);
952 WideCharToMultiByte(CP_ACP, 0, miw64.LoadedImageName, -1,
953 mi.LoadedImageName, sizeof(mi.LoadedImageName), NULL, NULL);
954
955 memcpy(ModuleInfo, &mi, ModuleInfo->SizeOfStruct);
956
957 return TRUE;
958 }
959
960 /******************************************************************
961 * SymGetModuleInfoW (DBGHELP.@)
962 *
963 */
964 BOOL WINAPI SymGetModuleInfoW(HANDLE hProcess, DWORD dwAddr,
965 PIMAGEHLP_MODULEW ModuleInfo)
966 {
967 IMAGEHLP_MODULEW64 miw64;
968 IMAGEHLP_MODULEW miw;
969
970 if (sizeof(miw) < ModuleInfo->SizeOfStruct) FIXME("Wrong size\n");
971
972 miw64.SizeOfStruct = sizeof(miw64);
973 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
974
975 miw.SizeOfStruct = miw64.SizeOfStruct;
976 miw.BaseOfImage = miw64.BaseOfImage;
977 miw.ImageSize = miw64.ImageSize;
978 miw.TimeDateStamp = miw64.TimeDateStamp;
979 miw.CheckSum = miw64.CheckSum;
980 miw.NumSyms = miw64.NumSyms;
981 miw.SymType = miw64.SymType;
982 strcpyW(miw.ModuleName, miw64.ModuleName);
983 strcpyW(miw.ImageName, miw64.ImageName);
984 strcpyW(miw.LoadedImageName, miw64.LoadedImageName);
985 memcpy(ModuleInfo, &miw, ModuleInfo->SizeOfStruct);
986
987 return TRUE;
988 }
989
990 /******************************************************************
991 * SymGetModuleInfo64 (DBGHELP.@)
992 *
993 */
994 BOOL WINAPI SymGetModuleInfo64(HANDLE hProcess, DWORD64 dwAddr,
995 PIMAGEHLP_MODULE64 ModuleInfo)
996 {
997 IMAGEHLP_MODULE64 mi64;
998 IMAGEHLP_MODULEW64 miw64;
999
1000 if (sizeof(mi64) < ModuleInfo->SizeOfStruct)
1001 {
1002 SetLastError(ERROR_MOD_NOT_FOUND); /* NOTE: native returns this error */
1003 WARN("Wrong size %u\n", ModuleInfo->SizeOfStruct);
1004 return FALSE;
1005 }
1006
1007 miw64.SizeOfStruct = sizeof(miw64);
1008 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
1009
1010 mi64.SizeOfStruct = miw64.SizeOfStruct;
1011 mi64.BaseOfImage = miw64.BaseOfImage;
1012 mi64.ImageSize = miw64.ImageSize;
1013 mi64.TimeDateStamp = miw64.TimeDateStamp;
1014 mi64.CheckSum = miw64.CheckSum;
1015 mi64.NumSyms = miw64.NumSyms;
1016 mi64.SymType = miw64.SymType;
1017 WideCharToMultiByte(CP_ACP, 0, miw64.ModuleName, -1,
1018 mi64.ModuleName, sizeof(mi64.ModuleName), NULL, NULL);
1019 WideCharToMultiByte(CP_ACP, 0, miw64.ImageName, -1,
1020 mi64.ImageName, sizeof(mi64.ImageName), NULL, NULL);
1021 WideCharToMultiByte(CP_ACP, 0, miw64.LoadedImageName, -1,
1022 mi64.LoadedImageName, sizeof(mi64.LoadedImageName), NULL, NULL);
1023 WideCharToMultiByte(CP_ACP, 0, miw64.LoadedPdbName, -1,
1024 mi64.LoadedPdbName, sizeof(mi64.LoadedPdbName), NULL, NULL);
1025
1026 mi64.CVSig = miw64.CVSig;
1027 WideCharToMultiByte(CP_ACP, 0, miw64.CVData, -1,
1028 mi64.CVData, sizeof(mi64.CVData), NULL, NULL);
1029 mi64.PdbSig = miw64.PdbSig;
1030 mi64.PdbSig70 = miw64.PdbSig70;
1031 mi64.PdbAge = miw64.PdbAge;
1032 mi64.PdbUnmatched = miw64.PdbUnmatched;
1033 mi64.DbgUnmatched = miw64.DbgUnmatched;
1034 mi64.LineNumbers = miw64.LineNumbers;
1035 mi64.GlobalSymbols = miw64.GlobalSymbols;
1036 mi64.TypeInfo = miw64.TypeInfo;
1037 mi64.SourceIndexed = miw64.SourceIndexed;
1038 mi64.Publics = miw64.Publics;
1039
1040 memcpy(ModuleInfo, &mi64, ModuleInfo->SizeOfStruct);
1041
1042 return TRUE;
1043 }
1044
1045 /******************************************************************
1046 * SymGetModuleInfoW64 (DBGHELP.@)
1047 *
1048 */
1049 BOOL WINAPI SymGetModuleInfoW64(HANDLE hProcess, DWORD64 dwAddr,
1050 PIMAGEHLP_MODULEW64 ModuleInfo)
1051 {
1052 struct process* pcs = process_find_by_handle(hProcess);
1053 struct module* module;
1054 IMAGEHLP_MODULEW64 miw64;
1055
1056 TRACE("%p %s %p\n", hProcess, wine_dbgstr_longlong(dwAddr), ModuleInfo);
1057
1058 if (!pcs) return FALSE;
1059 if (ModuleInfo->SizeOfStruct > sizeof(*ModuleInfo)) return FALSE;
1060 module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
1061 if (!module) return FALSE;
1062
1063 miw64 = module->module;
1064
1065 /* update debug information from container if any */
1066 if (module->module.SymType == SymNone)
1067 {
1068 module = module_get_container(pcs, module);
1069 if (module && module->module.SymType != SymNone)
1070 {
1071 miw64.SymType = module->module.SymType;
1072 miw64.NumSyms = module->module.NumSyms;
1073 }
1074 }
1075 memcpy(ModuleInfo, &miw64, ModuleInfo->SizeOfStruct);
1076 return TRUE;
1077 }
1078
1079 /***********************************************************************
1080 * SymGetModuleBase (DBGHELP.@)
1081 */
1082 DWORD WINAPI SymGetModuleBase(HANDLE hProcess, DWORD dwAddr)
1083 {
1084 DWORD64 ret;
1085
1086 ret = SymGetModuleBase64(hProcess, dwAddr);
1087 return validate_addr64(ret) ? ret : 0;
1088 }
1089
1090 /***********************************************************************
1091 * SymGetModuleBase64 (DBGHELP.@)
1092 */
1093 DWORD64 WINAPI SymGetModuleBase64(HANDLE hProcess, DWORD64 dwAddr)
1094 {
1095 struct process* pcs = process_find_by_handle(hProcess);
1096 struct module* module;
1097
1098 if (!pcs) return 0;
1099 module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
1100 if (!module) return 0;
1101 return module->module.BaseOfImage;
1102 }
1103
1104 /******************************************************************
1105 * module_reset_debug_info
1106 * Removes any debug information linked to a given module.
1107 */
1108 void module_reset_debug_info(struct module* module)
1109 {
1110 module->sortlist_valid = TRUE;
1111 module->sorttab_size = 0;
1112 module->addr_sorttab = NULL;
1113 module->num_sorttab = module->num_symbols = 0;
1114 hash_table_destroy(&module->ht_symbols);
1115 module->ht_symbols.num_buckets = 0;
1116 module->ht_symbols.buckets = NULL;
1117 hash_table_destroy(&module->ht_types);
1118 module->ht_types.num_buckets = 0;
1119 module->ht_types.buckets = NULL;
1120 module->vtypes.num_elts = 0;
1121 hash_table_destroy(&module->ht_symbols);
1122 module->sources_used = module->sources_alloc = 0;
1123 module->sources = NULL;
1124 }
1125
1126 /******************************************************************
1127 * SymRefreshModuleList (DBGHELP.@)
1128 */
1129 BOOL WINAPI SymRefreshModuleList(HANDLE hProcess)
1130 {
1131 struct process* pcs;
1132
1133 TRACE("(%p)\n", hProcess);
1134
1135 if (!(pcs = process_find_by_handle(hProcess))) return FALSE;
1136
1137 return refresh_module_list(pcs);
1138 }
1139
1140 /***********************************************************************
1141 * SymFunctionTableAccess (DBGHELP.@)
1142 */
1143 PVOID WINAPI SymFunctionTableAccess(HANDLE hProcess, DWORD AddrBase)
1144 {
1145 return SymFunctionTableAccess64(hProcess, AddrBase);
1146 }
1147
1148 /***********************************************************************
1149 * SymFunctionTableAccess64 (DBGHELP.@)
1150 */
1151 PVOID WINAPI SymFunctionTableAccess64(HANDLE hProcess, DWORD64 AddrBase)
1152 {
1153 struct process* pcs = process_find_by_handle(hProcess);
1154 struct module* module;
1155
1156 if (!pcs || !dbghelp_current_cpu->find_runtime_function) return NULL;
1157 module = module_find_by_addr(pcs, AddrBase, DMT_UNKNOWN);
1158 if (!module) return NULL;
1159
1160 return dbghelp_current_cpu->find_runtime_function(module, AddrBase);
1161 }