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