dmband: Rewrite band lbin list parsing.
[wine.git] / dlls / dbghelp / module.c
blob22b58950b4332cd3f9c151fbecf3e9c24b497bfb
1 /*
2 * File module.c - module handling for the wine debugger
4 * Copyright (C) 1993, Eric Youngdale.
5 * 2000-2007, Eric Pouech
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.
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.
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
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <assert.h>
27 #include "dbghelp_private.h"
28 #include "image_private.h"
29 #include "psapi.h"
30 #include "winternl.h"
31 #include "wine/debug.h"
32 #include "wine/heap.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
36 #define NOTE_GNU_BUILD_ID 3
38 const WCHAR S_ElfW[] = L"<elf>";
39 const WCHAR S_WineLoaderW[] = L"<wine-loader>";
40 static const WCHAR * const ext[] = {L".acm", L".dll", L".drv", L".exe", L".ocx", L".vxd", NULL};
42 static int match_ext(const WCHAR* ptr, size_t len)
44 const WCHAR* const *e;
45 size_t l;
47 for (e = ext; *e; e++)
49 l = lstrlenW(*e);
50 if (l >= len) return 0;
51 if (wcsnicmp(&ptr[len - l], *e, l)) continue;
52 return l;
54 return 0;
57 static const WCHAR* get_filename(const WCHAR* name, const WCHAR* endptr)
59 const WCHAR* ptr;
61 if (!endptr) endptr = name + lstrlenW(name);
62 for (ptr = endptr - 1; ptr >= name; ptr--)
64 if (*ptr == '/' || *ptr == '\\') break;
66 return ++ptr;
69 static BOOL is_wine_loader(const WCHAR *module)
71 const WCHAR *filename = get_filename(module, NULL);
72 const char *ptr;
73 BOOL ret = FALSE;
74 WCHAR *buffer;
75 DWORD len;
77 if ((ptr = getenv("WINELOADER")))
79 ptr = file_nameA(ptr);
80 len = 2 + MultiByteToWideChar( CP_UNIXCP, 0, ptr, -1, NULL, 0 );
81 buffer = heap_alloc( len * sizeof(WCHAR) );
82 MultiByteToWideChar( CP_UNIXCP, 0, ptr, -1, buffer, len );
84 else
86 buffer = heap_alloc( sizeof(L"wine") + 2 * sizeof(WCHAR) );
87 lstrcpyW( buffer, L"wine" );
90 if (!wcscmp( filename, buffer ))
91 ret = TRUE;
93 lstrcatW( buffer, L"64" );
94 if (!wcscmp( filename, buffer ))
95 ret = TRUE;
97 heap_free( buffer );
98 return ret;
101 static void module_fill_module(const WCHAR* in, WCHAR* out, size_t size)
103 const WCHAR *ptr, *endptr;
104 size_t len, l;
106 endptr = in + lstrlenW(in);
107 endptr -= match_ext(in, endptr - in);
108 ptr = get_filename(in, endptr);
109 len = min(endptr - ptr, size - 1);
110 memcpy(out, ptr, len * sizeof(WCHAR));
111 out[len] = '\0';
112 if (is_wine_loader(out))
113 lstrcpynW(out, S_WineLoaderW, size);
114 else
116 if (len > 3 && !wcsicmp(&out[len - 3], L".so") &&
117 (l = match_ext(out, len - 3)))
118 lstrcpyW(&out[len - l - 3], L"<elf>");
120 while ((*out = towlower(*out))) out++;
123 void module_set_module(struct module* module, const WCHAR* name)
125 module_fill_module(name, module->module.ModuleName, ARRAY_SIZE(module->module.ModuleName));
126 module_fill_module(name, module->modulename, ARRAY_SIZE(module->modulename));
129 /* Returned string must be freed by caller */
130 WCHAR *get_wine_loader_name(struct process *pcs)
132 const WCHAR *name;
133 WCHAR* altname;
134 unsigned len;
136 name = process_getenv(pcs, L"WINELOADER");
137 if (!name) name = pcs->is_system_64bit ? L"wine64" : L"wine";
138 len = lstrlenW(name);
140 /* WINELOADER isn't properly updated in Wow64 process calling inside Windows env block
141 * (it's updated in ELF env block though)
142 * So do the adaptation ourselves.
144 altname = HeapAlloc(GetProcessHeap(), 0, (len + 2 + 1) * sizeof(WCHAR));
145 if (altname)
147 memcpy(altname, name, len * sizeof(WCHAR));
148 if (pcs->is_system_64bit && len >= 2 && memcmp(name + len - 2, L"64", 2 * sizeof(WCHAR)) != 0)
150 lstrcpyW(altname + len, L"64");
151 /* in multi-arch wow configuration, wine64 doesn't exist */
152 if (GetFileAttributesW(altname) == INVALID_FILE_ATTRIBUTES)
153 altname[len] = L'\0';
155 else if (!pcs->is_system_64bit && len >= 2 && !memcmp(name + len - 2, L"64", 2 * sizeof(WCHAR)))
156 altname[len - 2] = '\0';
157 else
158 altname[len] = '\0';
161 TRACE("returning %s\n", debugstr_w(altname));
162 return altname;
165 static const char* get_module_type(enum module_type type, BOOL virtual)
167 switch (type)
169 case DMT_ELF: return virtual ? "Virtual ELF" : "ELF";
170 case DMT_PE: return virtual ? "Virtual PE" : "PE";
171 case DMT_MACHO: return virtual ? "Virtual Mach-O" : "Mach-O";
172 default: return "---";
176 /***********************************************************************
177 * Creates and links a new module to a process
179 struct module* module_new(struct process* pcs, const WCHAR* name,
180 enum module_type type, BOOL virtual,
181 DWORD64 mod_addr, DWORD64 size,
182 ULONG_PTR stamp, ULONG_PTR checksum, WORD machine)
184 struct module* module;
185 struct module** pmodule;
186 unsigned i;
188 assert(type == DMT_ELF || type == DMT_PE || type == DMT_MACHO);
189 if (!(module = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*module))))
190 return NULL;
192 for (pmodule = &pcs->lmodules; *pmodule; pmodule = &(*pmodule)->next);
193 module->next = NULL;
194 *pmodule = module;
196 TRACE("=> %s %I64x-%I64x %s\n",
197 get_module_type(type, virtual), mod_addr, mod_addr + size, debugstr_w(name));
199 pool_init(&module->pool, 65536);
201 module->process = pcs;
202 module->module.SizeOfStruct = sizeof(module->module);
203 module->module.BaseOfImage = mod_addr;
204 module->module.ImageSize = size;
205 module_set_module(module, name);
206 module->module.ImageName[0] = '\0';
207 lstrcpynW(module->module.LoadedImageName, name, ARRAY_SIZE(module->module.LoadedImageName));
208 module->module.SymType = SymDeferred;
209 module->module.NumSyms = 0;
210 module->module.TimeDateStamp = stamp;
211 module->module.CheckSum = checksum;
213 memset(module->module.LoadedPdbName, 0, sizeof(module->module.LoadedPdbName));
214 module->module.CVSig = 0;
215 memset(module->module.CVData, 0, sizeof(module->module.CVData));
216 module->module.PdbSig = 0;
217 memset(&module->module.PdbSig70, 0, sizeof(module->module.PdbSig70));
218 module->module.PdbAge = 0;
219 module->module.PdbUnmatched = FALSE;
220 module->module.DbgUnmatched = FALSE;
221 module->module.LineNumbers = FALSE;
222 module->module.GlobalSymbols = FALSE;
223 module->module.TypeInfo = FALSE;
224 module->module.SourceIndexed = FALSE;
225 module->module.Publics = FALSE;
226 module->module.MachineType = machine;
227 module->module.Reserved = 0;
229 module->reloc_delta = 0;
230 module->type = type;
231 module->is_virtual = virtual;
232 for (i = 0; i < DFI_LAST; i++) module->format_info[i] = NULL;
233 module->sortlist_valid = FALSE;
234 module->sorttab_size = 0;
235 module->addr_sorttab = NULL;
236 module->num_sorttab = 0;
237 module->num_symbols = 0;
238 module->cpu = cpu_find(machine);
239 if (!module->cpu)
240 module->cpu = dbghelp_current_cpu;
242 vector_init(&module->vsymt, sizeof(struct symt*), 128);
243 vector_init(&module->vcustom_symt, sizeof(struct symt*), 16);
244 /* FIXME: this seems a bit too high (on a per module basis)
245 * need some statistics about this
247 hash_table_init(&module->pool, &module->ht_symbols, 4096);
248 hash_table_init(&module->pool, &module->ht_types, 4096);
249 vector_init(&module->vtypes, sizeof(struct symt*), 32);
251 module->sources_used = 0;
252 module->sources_alloc = 0;
253 module->sources = 0;
254 wine_rb_init(&module->sources_offsets_tree, source_rb_compare);
256 /* add top level symbol */
257 module->top = symt_new_module(module);
259 return module;
262 BOOL module_init_pair(struct module_pair* pair, HANDLE hProcess, DWORD64 addr)
264 if (!(pair->pcs = process_find_by_handle(hProcess))) return FALSE;
265 pair->requested = module_find_by_addr(pair->pcs, addr, DMT_UNKNOWN);
266 return module_get_debug(pair);
269 /***********************************************************************
270 * module_find_by_nameW
273 struct module* module_find_by_nameW(const struct process* pcs, const WCHAR* name)
275 struct module* module;
277 for (module = pcs->lmodules; module; module = module->next)
279 if (!wcsicmp(name, module->modulename)) return module;
281 SetLastError(ERROR_INVALID_NAME);
282 return NULL;
285 struct module* module_find_by_nameA(const struct process* pcs, const char* name)
287 WCHAR wname[MAX_PATH];
289 MultiByteToWideChar(CP_ACP, 0, name, -1, wname, ARRAY_SIZE(wname));
290 return module_find_by_nameW(pcs, wname);
293 /***********************************************************************
294 * module_is_already_loaded
297 struct module* module_is_already_loaded(const struct process* pcs, const WCHAR* name)
299 struct module* module;
300 const WCHAR* filename;
302 /* first compare the loaded image name... */
303 for (module = pcs->lmodules; module; module = module->next)
305 if (!wcsicmp(name, module->module.LoadedImageName))
306 return module;
308 /* then compare the standard filenames (without the path) ... */
309 filename = get_filename(name, NULL);
310 for (module = pcs->lmodules; module; module = module->next)
312 if (!wcsicmp(filename, get_filename(module->module.LoadedImageName, NULL)))
313 return module;
315 SetLastError(ERROR_INVALID_NAME);
316 return NULL;
319 /***********************************************************************
320 * module_get_container
323 static struct module* module_get_container(const struct process* pcs,
324 const struct module* inner)
326 struct module* module;
328 for (module = pcs->lmodules; module; module = module->next)
330 if (module != inner &&
331 module->module.BaseOfImage <= inner->module.BaseOfImage &&
332 module->module.BaseOfImage + module->module.ImageSize >=
333 inner->module.BaseOfImage + inner->module.ImageSize)
334 return module;
336 return NULL;
339 /***********************************************************************
340 * module_get_containee
343 struct module* module_get_containee(const struct process* pcs, const struct module* outer)
345 struct module* module;
347 for (module = pcs->lmodules; module; module = module->next)
349 if (module != outer &&
350 outer->module.BaseOfImage <= module->module.BaseOfImage &&
351 outer->module.BaseOfImage + outer->module.ImageSize >=
352 module->module.BaseOfImage + module->module.ImageSize)
353 return module;
355 return NULL;
358 BOOL module_load_debug(struct module* module)
360 IMAGEHLP_DEFERRED_SYMBOL_LOADW64 idslW64;
362 /* if deferred, force loading */
363 if (module->module.SymType == SymDeferred)
365 BOOL ret;
367 if (module->is_virtual) ret = FALSE;
368 else if (module->type == DMT_PE)
370 idslW64.SizeOfStruct = sizeof(idslW64);
371 idslW64.BaseOfImage = module->module.BaseOfImage;
372 idslW64.CheckSum = module->module.CheckSum;
373 idslW64.TimeDateStamp = module->module.TimeDateStamp;
374 memcpy(idslW64.FileName, module->module.ImageName,
375 sizeof(module->module.ImageName));
376 idslW64.Reparse = FALSE;
377 idslW64.hFile = INVALID_HANDLE_VALUE;
379 pcs_callback(module->process, CBA_DEFERRED_SYMBOL_LOAD_START, &idslW64);
380 ret = pe_load_debug_info(module->process, module);
381 pcs_callback(module->process,
382 ret ? CBA_DEFERRED_SYMBOL_LOAD_COMPLETE : CBA_DEFERRED_SYMBOL_LOAD_FAILURE,
383 &idslW64);
385 else ret = module->process->loader->load_debug_info(module->process, module);
387 if (!ret) module->module.SymType = SymNone;
388 assert(module->module.SymType != SymDeferred);
389 module->module.NumSyms = module->ht_symbols.num_elts;
391 return module->module.SymType != SymNone;
394 /******************************************************************
395 * module_get_debug
397 * get the debug information from a module:
398 * - if the module's type is deferred, then force loading of debug info (and return
399 * the module itself)
400 * - if the module has no debug info and has an ELF container, then return the ELF
401 * container (and also force the ELF container's debug info loading if deferred)
402 * - otherwise return the module itself if it has some debug info
404 BOOL module_get_debug(struct module_pair* pair)
406 if (!pair->requested) return FALSE;
407 /* for a PE builtin, always get info from container */
408 if (!(pair->effective = module_get_container(pair->pcs, pair->requested)))
409 pair->effective = pair->requested;
410 return module_load_debug(pair->effective);
413 /***********************************************************************
414 * module_find_by_addr
416 * either the addr where module is loaded, or any address inside the
417 * module
419 struct module* module_find_by_addr(const struct process* pcs, DWORD64 addr,
420 enum module_type type)
422 struct module* module;
424 if (type == DMT_UNKNOWN)
426 if ((module = module_find_by_addr(pcs, addr, DMT_PE)) ||
427 (module = module_find_by_addr(pcs, addr, DMT_ELF)) ||
428 (module = module_find_by_addr(pcs, addr, DMT_MACHO)))
429 return module;
431 else
433 for (module = pcs->lmodules; module; module = module->next)
435 if (type == module->type && addr >= module->module.BaseOfImage &&
436 addr < module->module.BaseOfImage + module->module.ImageSize)
437 return module;
440 SetLastError(ERROR_MOD_NOT_FOUND);
441 return module;
444 /******************************************************************
445 * module_is_container_loaded
447 * checks whether the native container, for a (supposed) PE builtin is
448 * already loaded
450 static BOOL module_is_container_loaded(const struct process* pcs,
451 const WCHAR* ImageName, DWORD64 base)
453 size_t len;
454 struct module* module;
455 PCWSTR filename, modname;
457 if (!base) return FALSE;
458 filename = get_filename(ImageName, NULL);
459 len = lstrlenW(filename);
461 for (module = pcs->lmodules; module; module = module->next)
463 if ((module->type == DMT_ELF || module->type == DMT_MACHO) &&
464 base >= module->module.BaseOfImage &&
465 base < module->module.BaseOfImage + module->module.ImageSize)
467 modname = get_filename(module->module.LoadedImageName, NULL);
468 if (!wcsnicmp(modname, filename, len) &&
469 !memcmp(modname + len, L".so", 3 * sizeof(WCHAR)))
471 return TRUE;
475 /* likely a native PE module */
476 WARN("Couldn't find container for %s\n", debugstr_w(ImageName));
477 return FALSE;
480 static BOOL image_check_debug_link_crc(const WCHAR* file, struct image_file_map* fmap, DWORD link_crc)
482 DWORD read_bytes;
483 HANDLE handle;
484 WCHAR *path;
485 WORD magic;
486 DWORD crc;
487 BOOL ret;
489 path = get_dos_file_name(file);
490 handle = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
491 heap_free(path);
492 if (handle == INVALID_HANDLE_VALUE) return FALSE;
494 crc = calc_crc32(handle);
495 if (crc != link_crc)
497 WARN("Bad CRC for file %s (got %08lx while expecting %08lx)\n", debugstr_w(file), crc, link_crc);
498 CloseHandle(handle);
499 return FALSE;
502 SetFilePointer(handle, 0, 0, FILE_BEGIN);
503 if (ReadFile(handle, &magic, sizeof(magic), &read_bytes, NULL) && magic == IMAGE_DOS_SIGNATURE)
504 ret = pe_map_file(handle, fmap, DMT_PE);
505 else
506 ret = elf_map_handle(handle, fmap);
507 CloseHandle(handle);
508 return ret;
511 static BOOL image_check_debug_link_gnu_id(const WCHAR* file, struct image_file_map* fmap, const BYTE* id, unsigned idlen)
513 struct image_section_map buildid_sect;
514 DWORD read_bytes;
515 HANDLE handle;
516 WCHAR *path;
517 WORD magic;
518 BOOL ret;
520 path = get_dos_file_name(file);
521 handle = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
522 heap_free(path);
523 if (handle == INVALID_HANDLE_VALUE) return FALSE;
525 TRACE("Located debug information file at %s\n", debugstr_w(file));
527 if (ReadFile(handle, &magic, sizeof(magic), &read_bytes, NULL) && magic == IMAGE_DOS_SIGNATURE)
528 ret = pe_map_file(handle, fmap, DMT_PE);
529 else
530 ret = elf_map_handle(handle, fmap);
531 CloseHandle(handle);
533 if (ret && image_find_section(fmap, ".note.gnu.build-id", &buildid_sect))
535 const UINT32* note;
537 note = (const UINT32*)image_map_section(&buildid_sect);
538 if (note != IMAGE_NO_MAP)
540 /* the usual ELF note structure: name-size desc-size type <name> <desc> */
541 if (note[2] == NOTE_GNU_BUILD_ID)
543 if (note[1] == idlen && !memcmp(note + 3 + ((note[0] + 3) >> 2), id, idlen))
544 return TRUE;
545 WARN("mismatch in buildid information for %s\n", wine_dbgstr_w(file));
548 image_unmap_section(&buildid_sect);
549 image_unmap_file(fmap);
551 return FALSE;
554 /******************************************************************
555 * image_locate_debug_link
557 * Locate a filename from a .gnu_debuglink section, using the same
558 * strategy as gdb:
559 * "If the full name of the directory containing the executable is
560 * execdir, and the executable has a debug link that specifies the
561 * name debugfile, then GDB will automatically search for the
562 * debugging information file in three places:
563 * - the directory containing the executable file (that is, it
564 * will look for a file named `execdir/debugfile',
565 * - a subdirectory of that directory named `.debug' (that is, the
566 * file `execdir/.debug/debugfile', and
567 * - a subdirectory of the global debug file directory that includes
568 * the executable's full path, and the name from the link (that is,
569 * the file `globaldebugdir/execdir/debugfile', where globaldebugdir
570 * is the global debug file directory, and execdir has been turned
571 * into a relative path)." (from GDB manual)
573 static struct image_file_map* image_locate_debug_link(const struct module* module, const char* filename, DWORD crc)
575 static const WCHAR globalDebugDirW[] = {'/','u','s','r','/','l','i','b','/','d','e','b','u','g','/'};
576 static const WCHAR dotDebugW[] = {'.','d','e','b','u','g','/'};
577 const size_t globalDebugDirLen = ARRAY_SIZE(globalDebugDirW);
578 size_t filename_len, path_len;
579 WCHAR* p = NULL;
580 WCHAR* slash;
581 WCHAR* slash2;
582 struct image_file_map* fmap_link = NULL;
584 fmap_link = HeapAlloc(GetProcessHeap(), 0, sizeof(*fmap_link));
585 if (!fmap_link) return NULL;
587 filename_len = MultiByteToWideChar(CP_UNIXCP, 0, filename, -1, NULL, 0);
588 path_len = lstrlenW(module->module.LoadedImageName);
589 if (module->real_path) path_len = max(path_len, lstrlenW(module->real_path));
590 p = HeapAlloc(GetProcessHeap(), 0,
591 (globalDebugDirLen + path_len + 6 + 1 + filename_len + 1) * sizeof(WCHAR));
592 if (!p) goto found;
594 /* we prebuild the string with "execdir" */
595 lstrcpyW(p, module->module.LoadedImageName);
596 slash = p;
597 if ((slash2 = wcsrchr(slash, '/'))) slash = slash2 + 1;
598 if ((slash2 = wcsrchr(slash, '\\'))) slash = slash2 + 1;
600 /* testing execdir/filename */
601 MultiByteToWideChar(CP_UNIXCP, 0, filename, -1, slash, filename_len);
602 if (image_check_debug_link_crc(p, fmap_link, crc)) goto found;
604 /* testing execdir/.debug/filename */
605 memcpy(slash, dotDebugW, sizeof(dotDebugW));
606 MultiByteToWideChar(CP_UNIXCP, 0, filename, -1, slash + ARRAY_SIZE(dotDebugW), filename_len);
607 if (image_check_debug_link_crc(p, fmap_link, crc)) goto found;
609 if (module->real_path)
611 lstrcpyW(p, module->real_path);
612 slash = p;
613 if ((slash2 = wcsrchr(slash, '/'))) slash = slash2 + 1;
614 if ((slash2 = wcsrchr(slash, '\\'))) slash = slash2 + 1;
615 MultiByteToWideChar(CP_UNIXCP, 0, filename, -1, slash, filename_len);
616 if (image_check_debug_link_crc(p, fmap_link, crc)) goto found;
619 /* testing globaldebugdir/execdir/filename */
620 memmove(p + globalDebugDirLen, p, (slash - p) * sizeof(WCHAR));
621 memcpy(p, globalDebugDirW, globalDebugDirLen * sizeof(WCHAR));
622 slash += globalDebugDirLen;
623 MultiByteToWideChar(CP_UNIXCP, 0, filename, -1, slash, filename_len);
624 if (image_check_debug_link_crc(p, fmap_link, crc)) goto found;
626 /* finally testing filename */
627 if (image_check_debug_link_crc(slash, fmap_link, crc)) goto found;
630 WARN("Couldn't locate or map %s\n", filename);
631 HeapFree(GetProcessHeap(), 0, p);
632 HeapFree(GetProcessHeap(), 0, fmap_link);
633 return NULL;
635 found:
636 TRACE("Located debug information file %s at %s\n", filename, debugstr_w(p));
637 HeapFree(GetProcessHeap(), 0, p);
638 return fmap_link;
641 static WCHAR* append_hex(WCHAR* dst, const BYTE* id, const BYTE* end)
643 while (id < end)
645 *dst++ = "0123456789abcdef"[*id >> 4 ];
646 *dst++ = "0123456789abcdef"[*id & 0x0F];
647 id++;
649 return dst;
652 /******************************************************************
653 * image_locate_build_id_target
655 * Try to find the .so file containing the debug info out of the build-id note information
657 static struct image_file_map* image_locate_build_id_target(const BYTE* id, unsigned idlen)
659 struct image_file_map* fmap_link = NULL;
660 DWORD sz;
661 WCHAR* p;
662 WCHAR* z;
664 fmap_link = HeapAlloc(GetProcessHeap(), 0, sizeof(*fmap_link));
665 if (!fmap_link) return NULL;
667 p = malloc(sizeof(L"/usr/lib/debug/.build-id/") +
668 (idlen * 2 + 1) * sizeof(WCHAR) + sizeof(L".debug"));
669 if (!p) goto fail;
670 wcscpy(p, L"/usr/lib/debug/.build-id/");
671 z = p + wcslen(p);
672 if (idlen)
674 z = append_hex(z, id, id + 1);
675 if (idlen > 1)
677 *z++ = L'/';
678 z = append_hex(z, id + 1, id + idlen);
681 wcscpy(z, L".debug");
682 TRACE("checking %s\n", wine_dbgstr_w(p));
684 if (image_check_debug_link_gnu_id(p, fmap_link, id, idlen))
686 free(p);
687 return fmap_link;
690 sz = GetEnvironmentVariableW(L"WINEHOMEDIR", NULL, 0);
691 if (sz)
693 z = realloc(p, sz * sizeof(WCHAR) +
694 sizeof(L"\\.cache\\debuginfod_client\\") +
695 idlen * 2 * sizeof(WCHAR) + sizeof(L"\\debuginfo") + 500);
696 if (!z) goto fail;
697 p = z;
698 GetEnvironmentVariableW(L"WINEHOMEDIR", p, sz);
699 z = p + sz - 1;
700 wcscpy(z, L"\\.cache\\debuginfod_client\\");
701 z += wcslen(z);
702 z = append_hex(z, id, id + idlen);
703 wcscpy(z, L"\\debuginfo");
704 TRACE("checking %ls\n", p);
705 if (image_check_debug_link_gnu_id(p, fmap_link, id, idlen))
707 free(p);
708 return fmap_link;
712 TRACE("not found\n");
713 fail:
714 free(p);
715 HeapFree(GetProcessHeap(), 0, fmap_link);
716 return NULL;
719 /******************************************************************
720 * image_load_debugaltlink
722 * Handle a (potential) .gnu_debugaltlink section and the link to
723 * (another) alternate debug file.
724 * Return an heap-allocated image_file_map when the section .gnu_debugaltlink is present,
725 * and a matching debug file has been located.
727 struct image_file_map* image_load_debugaltlink(struct image_file_map* fmap, struct module* module)
729 struct image_section_map debugaltlink_sect;
730 const char* data;
731 struct image_file_map* fmap_link = NULL;
732 BOOL ret = FALSE;
734 if (!image_find_section(fmap, ".gnu_debugaltlink", &debugaltlink_sect))
736 TRACE("No .gnu_debugaltlink section found for %s\n", debugstr_w(module->modulename));
737 return NULL;
740 data = image_map_section(&debugaltlink_sect);
741 if (data != IMAGE_NO_MAP)
743 unsigned sect_len;
744 const BYTE* id;
745 /* The content of the section is:
746 * + a \0 terminated string (filename)
747 * + followed by the build-id
748 * We try loading the dwz_alternate:
749 * - from the filename: either as absolute path, or relative to the embedded build-id
750 * - from the build-id
751 * In both cases, checking that found .so file matches the requested build-id
753 sect_len = image_get_map_size(&debugaltlink_sect);
754 id = memchr(data, '\0', sect_len);
755 if (id++)
757 unsigned idlen = (const BYTE*)data + sect_len - id;
758 fmap_link = HeapAlloc(GetProcessHeap(), 0, sizeof(*fmap_link));
759 if (fmap_link)
761 unsigned filename_len = MultiByteToWideChar(CP_UNIXCP, 0, data, -1, NULL, 0);
762 /* Trying absolute path */
763 WCHAR* dst = HeapAlloc(GetProcessHeap(), 0, filename_len * sizeof(WCHAR));
764 if (dst)
766 MultiByteToWideChar(CP_UNIXCP, 0, data, -1, dst, filename_len);
767 ret = image_check_debug_link_gnu_id(dst, fmap_link, id, idlen);
768 HeapFree(GetProcessHeap(), 0, dst);
770 /* Trying relative path to build-id directory */
771 if (!ret)
773 dst = HeapAlloc(GetProcessHeap(), 0,
774 sizeof(L"/usr/lib/debug/.build-id/") + (3 + filename_len + idlen * 2) * sizeof(WCHAR));
775 if (dst)
777 WCHAR* p;
779 /* SIGH....
780 * some relative links are relative to /usr/lib/debug/.build-id, some others are from the directory
781 * where the alternate file is...
782 * so try both
784 p = memcpy(dst, L"/usr/lib/debug/.build-id/", sizeof(L"/usr/lib/debug/.build-id/"));
785 p += wcslen(dst);
786 MultiByteToWideChar(CP_UNIXCP, 0, data, -1, p, filename_len);
787 ret = image_check_debug_link_gnu_id(dst, fmap_link, id, idlen);
788 if (!ret)
790 p = append_hex(p, id, id + idlen);
791 *p++ = '/';
792 MultiByteToWideChar(CP_UNIXCP, 0, data, -1, p, filename_len);
793 ret = image_check_debug_link_gnu_id(dst, fmap_link, id, idlen);
795 HeapFree(GetProcessHeap(), 0, dst);
798 if (!ret)
800 HeapFree(GetProcessHeap(), 0, fmap_link);
801 /* didn't work out with filename, try file lookup based on build-id */
802 if (!(fmap_link = image_locate_build_id_target(id, idlen)))
803 WARN("Couldn't find a match for .gnu_debugaltlink section %s for %s\n", data, debugstr_w(module->modulename));
808 image_unmap_section(&debugaltlink_sect);
809 if (fmap_link) TRACE("Found match .gnu_debugaltlink section for %s\n", debugstr_w(module->modulename));
810 return fmap_link;
813 /******************************************************************
814 * image_check_alternate
816 * Load alternate files for a given image file, looking at either .note.gnu_build-id
817 * or .gnu_debuglink sections.
819 BOOL image_check_alternate(struct image_file_map* fmap, const struct module* module)
821 struct image_section_map buildid_sect, debuglink_sect;
822 struct image_file_map* fmap_link = NULL;
824 /* if present, add the .gnu_debuglink file as an alternate to current one */
825 if (image_find_section(fmap, ".note.gnu.build-id", &buildid_sect))
827 const UINT32* note;
829 note = (const UINT32*)image_map_section(&buildid_sect);
830 if (note != IMAGE_NO_MAP)
832 /* the usual ELF note structure: name-size desc-size type <name> <desc> */
833 if (note[2] == NOTE_GNU_BUILD_ID)
835 fmap_link = image_locate_build_id_target((const BYTE*)(note + 3 + ((note[0] + 3) >> 2)), note[1]);
838 image_unmap_section(&buildid_sect);
840 /* if present, add the .gnu_debuglink file as an alternate to current one */
841 if (!fmap_link && image_find_section(fmap, ".gnu_debuglink", &debuglink_sect))
843 const char* dbg_link;
845 dbg_link = image_map_section(&debuglink_sect);
846 if (dbg_link != IMAGE_NO_MAP)
848 /* The content of a debug link section is:
849 * 1/ a NULL terminated string, containing the file name for the
850 * debug info
851 * 2/ padding on 4 byte boundary
852 * 3/ CRC of the linked file
854 DWORD crc = *(const DWORD*)(dbg_link + ((DWORD_PTR)(strlen(dbg_link) + 4) & ~3));
855 if (!(fmap_link = image_locate_debug_link(module, dbg_link, crc)))
856 WARN("Couldn't load linked debug file for %s\n", debugstr_w(module->modulename));
858 image_unmap_section(&debuglink_sect);
860 if (fmap_link)
862 fmap->alternate = fmap_link;
863 return TRUE;
865 return FALSE;
868 /***********************************************************************
869 * SymLoadModule (DBGHELP.@)
871 DWORD WINAPI SymLoadModule(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
872 PCSTR ModuleName, DWORD BaseOfDll, DWORD SizeOfDll)
874 return SymLoadModuleEx(hProcess, hFile, ImageName, ModuleName, BaseOfDll,
875 SizeOfDll, NULL, 0);
878 /***********************************************************************
879 * SymLoadModuleEx (DBGHELP.@)
881 DWORD64 WINAPI SymLoadModuleEx(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
882 PCSTR ModuleName, DWORD64 BaseOfDll, DWORD DllSize,
883 PMODLOAD_DATA Data, DWORD Flags)
885 PWSTR wImageName, wModuleName;
886 unsigned len;
887 DWORD64 ret;
889 TRACE("(%p %p %s %s %I64x %08lx %p %08lx)\n",
890 hProcess, hFile, debugstr_a(ImageName), debugstr_a(ModuleName),
891 BaseOfDll, DllSize, Data, Flags);
893 if (ImageName)
895 len = MultiByteToWideChar(CP_ACP, 0, ImageName, -1, NULL, 0);
896 wImageName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
897 MultiByteToWideChar(CP_ACP, 0, ImageName, -1, wImageName, len);
899 else wImageName = NULL;
900 if (ModuleName)
902 len = MultiByteToWideChar(CP_ACP, 0, ModuleName, -1, NULL, 0);
903 wModuleName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
904 MultiByteToWideChar(CP_ACP, 0, ModuleName, -1, wModuleName, len);
906 else wModuleName = NULL;
908 ret = SymLoadModuleExW(hProcess, hFile, wImageName, wModuleName,
909 BaseOfDll, DllSize, Data, Flags);
910 HeapFree(GetProcessHeap(), 0, wImageName);
911 HeapFree(GetProcessHeap(), 0, wModuleName);
912 return ret;
915 /***********************************************************************
916 * SymLoadModuleExW (DBGHELP.@)
918 DWORD64 WINAPI SymLoadModuleExW(HANDLE hProcess, HANDLE hFile, PCWSTR wImageName,
919 PCWSTR wModuleName, DWORD64 BaseOfDll, DWORD SizeOfDll,
920 PMODLOAD_DATA Data, DWORD Flags)
922 struct process* pcs;
923 struct module* module = NULL;
924 struct module* altmodule;
926 TRACE("(%p %p %s %s %I64x %08lx %p %08lx)\n",
927 hProcess, hFile, debugstr_w(wImageName), debugstr_w(wModuleName),
928 BaseOfDll, SizeOfDll, Data, Flags);
930 if (Data)
931 FIXME("Unsupported load data parameter %p for %s\n",
932 Data, debugstr_w(wImageName));
934 if (!(pcs = process_find_by_handle(hProcess))) return 0;
936 if (Flags & ~(SLMFLAG_VIRTUAL))
937 FIXME("Unsupported Flags %08lx for %s\n", Flags, debugstr_w(wImageName));
939 pcs->loader->synchronize_module_list(pcs);
941 /* this is a Wine extension to the API just to redo the synchronisation */
942 if (!wImageName && !hFile) return 0;
944 if (Flags & SLMFLAG_VIRTUAL)
946 if (!wImageName) return 0;
947 module = module_new(pcs, wImageName, DMT_PE, TRUE, BaseOfDll, SizeOfDll, 0, 0, IMAGE_FILE_MACHINE_UNKNOWN);
948 if (!module) return 0;
949 module->module.SymType = SymVirtual;
951 /* check if it's a builtin PE module with a containing ELF module */
952 else if (wImageName && module_is_container_loaded(pcs, wImageName, BaseOfDll))
954 /* force the loading of DLL as builtin */
955 module = pe_load_builtin_module(pcs, wImageName, BaseOfDll, SizeOfDll);
957 if (!module)
959 /* otherwise, try a regular PE module */
960 if (!(module = pe_load_native_module(pcs, wImageName, hFile, BaseOfDll, SizeOfDll)) &&
961 wImageName)
963 /* and finally an ELF or Mach-O module */
964 module = pcs->loader->load_module(pcs, wImageName, BaseOfDll);
967 if (!module)
969 WARN("Couldn't locate %s\n", debugstr_w(wImageName));
970 return 0;
972 /* by default module_new fills module.ModuleName from a derivation
973 * of LoadedImageName. Overwrite it, if we have better information
975 if (wModuleName)
976 module_set_module(module, wModuleName);
977 if (wImageName)
978 lstrcpynW(module->module.ImageName, wImageName, ARRAY_SIZE(module->module.ImageName));
980 for (altmodule = pcs->lmodules; altmodule; altmodule = altmodule->next)
982 if (altmodule != module && altmodule->type == module->type &&
983 module->module.BaseOfImage >= altmodule->module.BaseOfImage &&
984 module->module.BaseOfImage < altmodule->module.BaseOfImage + altmodule->module.ImageSize)
985 break;
987 if (altmodule)
989 /* We have a conflict as the new module cannot be found by its base address
990 * (it's hidden by altmodule).
991 * We need to decide which one the two modules we need to get rid of.
993 /* loading same module at same address... don't change anything */
994 if (module->module.BaseOfImage == altmodule->module.BaseOfImage)
996 module_remove(pcs, module);
997 SetLastError(ERROR_SUCCESS);
998 return 0;
1000 /* replace old module with new one */
1001 WARN("Replace module %ls at %I64x by module %ls at %I64x\n",
1002 altmodule->module.ImageName, altmodule->module.BaseOfImage,
1003 module->module.ImageName, module->module.BaseOfImage);
1004 module_remove(pcs, altmodule);
1007 if ((dbghelp_options & SYMOPT_DEFERRED_LOADS) == 0 && !module_get_container(pcs, module))
1008 module_load_debug(module);
1009 return module->module.BaseOfImage;
1012 /***********************************************************************
1013 * SymLoadModule64 (DBGHELP.@)
1015 DWORD64 WINAPI SymLoadModule64(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
1016 PCSTR ModuleName, DWORD64 BaseOfDll, DWORD SizeOfDll)
1018 return SymLoadModuleEx(hProcess, hFile, ImageName, ModuleName, BaseOfDll, SizeOfDll,
1019 NULL, 0);
1022 /******************************************************************
1023 * module_remove
1026 BOOL module_remove(struct process* pcs, struct module* module)
1028 struct module_format*modfmt;
1029 struct module** p;
1030 unsigned i;
1032 TRACE("%s (%p)\n", debugstr_w(module->modulename), module);
1034 /* remove local scope if symbol is from this module */
1035 if (pcs->localscope_symt)
1037 struct symt* locsym = pcs->localscope_symt;
1038 if (symt_check_tag(locsym, SymTagInlineSite))
1039 locsym = &symt_get_function_from_inlined((struct symt_function*)locsym)->symt;
1040 if (symt_check_tag(locsym, SymTagFunction))
1042 locsym = ((struct symt_function*)locsym)->container;
1043 if (symt_check_tag(locsym, SymTagCompiland) &&
1044 module == ((struct symt_compiland*)locsym)->container->module)
1046 pcs->localscope_pc = 0;
1047 pcs->localscope_symt = NULL;
1051 for (i = 0; i < DFI_LAST; i++)
1053 if ((modfmt = module->format_info[i]) && modfmt->remove)
1054 modfmt->remove(pcs, module->format_info[i]);
1056 hash_table_destroy(&module->ht_symbols);
1057 hash_table_destroy(&module->ht_types);
1058 HeapFree(GetProcessHeap(), 0, module->sources);
1059 HeapFree(GetProcessHeap(), 0, module->addr_sorttab);
1060 pool_destroy(&module->pool);
1061 /* native dbghelp doesn't invoke registered callback(,CBA_SYMBOLS_UNLOADED,) here
1062 * so do we
1064 for (p = &pcs->lmodules; *p; p = &(*p)->next)
1066 if (*p == module)
1068 *p = module->next;
1069 HeapFree(GetProcessHeap(), 0, module);
1070 return TRUE;
1073 FIXME("This shouldn't happen\n");
1074 return FALSE;
1077 /******************************************************************
1078 * SymUnloadModule (DBGHELP.@)
1081 BOOL WINAPI SymUnloadModule(HANDLE hProcess, DWORD BaseOfDll)
1083 return SymUnloadModule64(hProcess, BaseOfDll);
1086 /******************************************************************
1087 * SymUnloadModule64 (DBGHELP.@)
1090 BOOL WINAPI SymUnloadModule64(HANDLE hProcess, DWORD64 BaseOfDll)
1092 struct process* pcs;
1093 struct module* module;
1095 pcs = process_find_by_handle(hProcess);
1096 if (!pcs) return FALSE;
1097 module = module_find_by_addr(pcs, BaseOfDll, DMT_UNKNOWN);
1098 if (!module) return FALSE;
1099 module_remove(pcs, module);
1100 return TRUE;
1103 /******************************************************************
1104 * SymEnumerateModules (DBGHELP.@)
1107 struct enum_modW64_32
1109 PSYM_ENUMMODULES_CALLBACK cb;
1110 PVOID user;
1111 char module[MAX_PATH];
1114 static BOOL CALLBACK enum_modW64_32(PCWSTR name, DWORD64 base, PVOID user)
1116 struct enum_modW64_32* x = user;
1118 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
1119 return x->cb(x->module, (DWORD)base, x->user);
1122 BOOL WINAPI SymEnumerateModules(HANDLE hProcess,
1123 PSYM_ENUMMODULES_CALLBACK EnumModulesCallback,
1124 PVOID UserContext)
1126 struct enum_modW64_32 x;
1128 x.cb = EnumModulesCallback;
1129 x.user = UserContext;
1131 return SymEnumerateModulesW64(hProcess, enum_modW64_32, &x);
1134 /******************************************************************
1135 * SymEnumerateModules64 (DBGHELP.@)
1138 struct enum_modW64_64
1140 PSYM_ENUMMODULES_CALLBACK64 cb;
1141 PVOID user;
1142 char module[MAX_PATH];
1145 static BOOL CALLBACK enum_modW64_64(PCWSTR name, DWORD64 base, PVOID user)
1147 struct enum_modW64_64* x = user;
1149 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
1150 return x->cb(x->module, base, x->user);
1153 BOOL WINAPI SymEnumerateModules64(HANDLE hProcess,
1154 PSYM_ENUMMODULES_CALLBACK64 EnumModulesCallback,
1155 PVOID UserContext)
1157 struct enum_modW64_64 x;
1159 x.cb = EnumModulesCallback;
1160 x.user = UserContext;
1162 return SymEnumerateModulesW64(hProcess, enum_modW64_64, &x);
1165 /******************************************************************
1166 * SymEnumerateModulesW64 (DBGHELP.@)
1169 BOOL WINAPI SymEnumerateModulesW64(HANDLE hProcess,
1170 PSYM_ENUMMODULES_CALLBACKW64 EnumModulesCallback,
1171 PVOID UserContext)
1173 struct process* pcs = process_find_by_handle(hProcess);
1174 struct module* module;
1176 if (!pcs) return FALSE;
1178 for (module = pcs->lmodules; module; module = module->next)
1180 if (!dbghelp_opt_native &&
1181 (module->type == DMT_ELF || module->type == DMT_MACHO))
1182 continue;
1183 if (!EnumModulesCallback(module->modulename,
1184 module->module.BaseOfImage, UserContext))
1185 break;
1187 return TRUE;
1190 /******************************************************************
1191 * EnumerateLoadedModules64 (DBGHELP.@)
1194 struct enum_load_modW64_64
1196 PENUMLOADED_MODULES_CALLBACK64 cb;
1197 PVOID user;
1198 char module[MAX_PATH];
1201 static BOOL CALLBACK enum_load_modW64_64(PCWSTR name, DWORD64 base, ULONG size,
1202 PVOID user)
1204 struct enum_load_modW64_64* x = user;
1206 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
1207 return x->cb(x->module, base, size, x->user);
1210 BOOL WINAPI EnumerateLoadedModules64(HANDLE hProcess,
1211 PENUMLOADED_MODULES_CALLBACK64 EnumLoadedModulesCallback,
1212 PVOID UserContext)
1214 struct enum_load_modW64_64 x;
1216 x.cb = EnumLoadedModulesCallback;
1217 x.user = UserContext;
1219 return EnumerateLoadedModulesW64(hProcess, enum_load_modW64_64, &x);
1222 /******************************************************************
1223 * EnumerateLoadedModules (DBGHELP.@)
1226 struct enum_load_modW64_32
1228 PENUMLOADED_MODULES_CALLBACK cb;
1229 PVOID user;
1230 char module[MAX_PATH];
1233 static BOOL CALLBACK enum_load_modW64_32(PCWSTR name, DWORD64 base, ULONG size,
1234 PVOID user)
1236 struct enum_load_modW64_32* x = user;
1237 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
1238 return x->cb(x->module, (DWORD)base, size, x->user);
1241 BOOL WINAPI EnumerateLoadedModules(HANDLE hProcess,
1242 PENUMLOADED_MODULES_CALLBACK EnumLoadedModulesCallback,
1243 PVOID UserContext)
1245 struct enum_load_modW64_32 x;
1247 x.cb = EnumLoadedModulesCallback;
1248 x.user = UserContext;
1250 return EnumerateLoadedModulesW64(hProcess, enum_load_modW64_32, &x);
1253 static unsigned int load_and_grow_modules(HANDLE process, HMODULE** hmods, unsigned start, unsigned* alloc, DWORD filter)
1255 DWORD needed;
1256 BOOL ret;
1258 while ((ret = EnumProcessModulesEx(process, *hmods + start, (*alloc - start) * sizeof(HMODULE),
1259 &needed, filter)) &&
1260 needed > (*alloc - start) * sizeof(HMODULE))
1262 HMODULE* new = HeapReAlloc(GetProcessHeap(), 0, *hmods, (*alloc) * 2 * sizeof(HMODULE));
1263 if (!new) return 0;
1264 *hmods = new;
1265 *alloc *= 2;
1267 return ret ? needed / sizeof(HMODULE) : 0;
1270 /******************************************************************
1271 * EnumerateLoadedModulesW64 (DBGHELP.@)
1274 BOOL WINAPI EnumerateLoadedModulesW64(HANDLE process,
1275 PENUMLOADED_MODULES_CALLBACKW64 enum_cb,
1276 PVOID user)
1278 HMODULE* hmods;
1279 unsigned alloc = 256, count, count32, i;
1280 USHORT pcs_machine, native_machine;
1281 BOOL with_32bit_modules;
1282 WCHAR imagenameW[MAX_PATH];
1283 MODULEINFO mi;
1284 WCHAR* sysdir = NULL;
1285 WCHAR* wowdir = NULL;
1286 size_t sysdir_len = 0, wowdir_len = 0;
1288 /* process might not be a handle to a live process */
1289 if (!IsWow64Process2(process, &pcs_machine, &native_machine)) return FALSE;
1290 with_32bit_modules = sizeof(void*) > sizeof(int) &&
1291 pcs_machine != IMAGE_FILE_MACHINE_UNKNOWN &&
1292 (dbghelp_options & SYMOPT_INCLUDE_32BIT_MODULES);
1294 if (!(hmods = HeapAlloc(GetProcessHeap(), 0, alloc * sizeof(hmods[0]))))
1295 return FALSE;
1297 /* Note:
1298 * - we report modules returned from kernelbase.EnumProcessModulesEx
1299 * - appending 32bit modules when possible and requested
1301 * When considering 32bit modules in a wow64 child process, required from
1302 * a 64bit process:
1303 * - native returns from kernelbase.EnumProcessModulesEx
1304 * redirected paths (that is in system32 directory), while
1305 * dbghelp.EnumerateLoadedModulesWine returns the effective path
1306 * (eg. syswow64 for x86_64).
1307 * - (Except for the main module, if gotten from syswow64, where kernelbase
1308 * will return the effective path)
1309 * - Wine kernelbase (and ntdll) incorrectly return these modules from
1310 * syswow64 (except for ntdll which is returned from system32).
1311 * => for these modules, always perform a system32 => syswow64 path
1312 * conversion (it'll work even if ntdll/kernelbase is fixed).
1314 if ((count = load_and_grow_modules(process, &hmods, 0, &alloc, LIST_MODULES_DEFAULT)) && with_32bit_modules)
1316 /* append 32bit modules when required */
1317 if ((count32 = load_and_grow_modules(process, &hmods, count, &alloc, LIST_MODULES_32BIT)))
1319 sysdir_len = GetSystemDirectoryW(NULL, 0);
1320 wowdir_len = GetSystemWow64Directory2W(NULL, 0, pcs_machine);
1322 if (!sysdir_len || !wowdir_len ||
1323 !(sysdir = HeapAlloc(GetProcessHeap(), 0, (sysdir_len + 1 + wowdir_len + 1) * sizeof(WCHAR))))
1325 HeapFree(GetProcessHeap(), 0, hmods);
1326 return FALSE;
1328 wowdir = sysdir + sysdir_len + 1;
1329 if (GetSystemDirectoryW(sysdir, sysdir_len) >= sysdir_len)
1330 FIXME("shouldn't happen\n");
1331 if (GetSystemWow64Directory2W(wowdir, wowdir_len, pcs_machine) >= wowdir_len)
1332 FIXME("shouldn't happen\n");
1333 wcscat(sysdir, L"\\");
1334 wcscat(wowdir, L"\\");
1337 else count32 = 0;
1339 for (i = 0; i < count + count32; i++)
1341 if (GetModuleInformation(process, hmods[i], &mi, sizeof(mi)) &&
1342 GetModuleFileNameExW(process, hmods[i], imagenameW, ARRAY_SIZE(imagenameW)))
1344 /* rewrite path in system32 into syswow64 for 32bit modules */
1345 if (i >= count)
1347 size_t len = wcslen(imagenameW);
1349 if (!wcsnicmp(imagenameW, sysdir, sysdir_len) &&
1350 (len - sysdir_len + wowdir_len) + 1 <= ARRAY_SIZE(imagenameW))
1352 memmove(&imagenameW[wowdir_len], &imagenameW[sysdir_len], (len - sysdir_len) * sizeof(WCHAR));
1353 memcpy(imagenameW, wowdir, wowdir_len * sizeof(WCHAR));
1356 if (!enum_cb(imagenameW, (DWORD_PTR)mi.lpBaseOfDll, mi.SizeOfImage, user))
1357 break;
1361 HeapFree(GetProcessHeap(), 0, hmods);
1362 HeapFree(GetProcessHeap(), 0, sysdir);
1364 return count != 0;
1367 static void dbghelp_str_WtoA(const WCHAR *src, char *dst, int dst_len)
1369 WideCharToMultiByte(CP_ACP, 0, src, -1, dst, dst_len - 1, NULL, NULL);
1370 dst[dst_len - 1] = 0;
1373 /******************************************************************
1374 * SymGetModuleInfo (DBGHELP.@)
1377 BOOL WINAPI SymGetModuleInfo(HANDLE hProcess, DWORD dwAddr,
1378 PIMAGEHLP_MODULE ModuleInfo)
1380 IMAGEHLP_MODULE mi;
1381 IMAGEHLP_MODULEW64 miw64;
1383 if (sizeof(mi) < ModuleInfo->SizeOfStruct) FIXME("Wrong size\n");
1385 miw64.SizeOfStruct = sizeof(miw64);
1386 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
1388 mi.SizeOfStruct = ModuleInfo->SizeOfStruct;
1389 mi.BaseOfImage = miw64.BaseOfImage;
1390 mi.ImageSize = miw64.ImageSize;
1391 mi.TimeDateStamp = miw64.TimeDateStamp;
1392 mi.CheckSum = miw64.CheckSum;
1393 mi.NumSyms = miw64.NumSyms;
1394 mi.SymType = miw64.SymType;
1395 dbghelp_str_WtoA(miw64.ModuleName, mi.ModuleName, sizeof(mi.ModuleName));
1396 dbghelp_str_WtoA(miw64.ImageName, mi.ImageName, sizeof(mi.ImageName));
1397 dbghelp_str_WtoA(miw64.LoadedImageName, mi.LoadedImageName, sizeof(mi.LoadedImageName));
1399 memcpy(ModuleInfo, &mi, ModuleInfo->SizeOfStruct);
1401 return TRUE;
1404 /******************************************************************
1405 * SymGetModuleInfoW (DBGHELP.@)
1408 BOOL WINAPI SymGetModuleInfoW(HANDLE hProcess, DWORD dwAddr,
1409 PIMAGEHLP_MODULEW ModuleInfo)
1411 IMAGEHLP_MODULEW64 miw64;
1412 IMAGEHLP_MODULEW miw;
1414 if (sizeof(miw) < ModuleInfo->SizeOfStruct) FIXME("Wrong size\n");
1416 miw64.SizeOfStruct = sizeof(miw64);
1417 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
1419 miw.SizeOfStruct = ModuleInfo->SizeOfStruct;
1420 miw.BaseOfImage = miw64.BaseOfImage;
1421 miw.ImageSize = miw64.ImageSize;
1422 miw.TimeDateStamp = miw64.TimeDateStamp;
1423 miw.CheckSum = miw64.CheckSum;
1424 miw.NumSyms = miw64.NumSyms;
1425 miw.SymType = miw64.SymType;
1426 lstrcpyW(miw.ModuleName, miw64.ModuleName);
1427 lstrcpyW(miw.ImageName, miw64.ImageName);
1428 lstrcpyW(miw.LoadedImageName, miw64.LoadedImageName);
1429 memcpy(ModuleInfo, &miw, ModuleInfo->SizeOfStruct);
1431 return TRUE;
1434 /******************************************************************
1435 * SymGetModuleInfo64 (DBGHELP.@)
1438 BOOL WINAPI SymGetModuleInfo64(HANDLE hProcess, DWORD64 dwAddr,
1439 PIMAGEHLP_MODULE64 ModuleInfo)
1441 IMAGEHLP_MODULE64 mi64;
1442 IMAGEHLP_MODULEW64 miw64;
1444 if (sizeof(mi64) < ModuleInfo->SizeOfStruct)
1446 SetLastError(ERROR_MOD_NOT_FOUND); /* NOTE: native returns this error */
1447 WARN("Wrong size %lu\n", ModuleInfo->SizeOfStruct);
1448 return FALSE;
1451 miw64.SizeOfStruct = sizeof(miw64);
1452 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
1454 mi64.SizeOfStruct = ModuleInfo->SizeOfStruct;
1455 mi64.BaseOfImage = miw64.BaseOfImage;
1456 mi64.ImageSize = miw64.ImageSize;
1457 mi64.TimeDateStamp = miw64.TimeDateStamp;
1458 mi64.CheckSum = miw64.CheckSum;
1459 mi64.NumSyms = miw64.NumSyms;
1460 mi64.SymType = miw64.SymType;
1461 dbghelp_str_WtoA(miw64.ModuleName, mi64.ModuleName, sizeof(mi64.ModuleName));
1462 dbghelp_str_WtoA(miw64.ImageName, mi64.ImageName, sizeof(mi64.ImageName));
1463 dbghelp_str_WtoA(miw64.LoadedImageName, mi64.LoadedImageName, sizeof(mi64.LoadedImageName));
1464 dbghelp_str_WtoA(miw64.LoadedPdbName, mi64.LoadedPdbName, sizeof(mi64.LoadedPdbName));
1466 mi64.CVSig = miw64.CVSig;
1467 dbghelp_str_WtoA(miw64.CVData, mi64.CVData, sizeof(mi64.CVData));
1468 mi64.PdbSig = miw64.PdbSig;
1469 mi64.PdbSig70 = miw64.PdbSig70;
1470 mi64.PdbAge = miw64.PdbAge;
1471 mi64.PdbUnmatched = miw64.PdbUnmatched;
1472 mi64.DbgUnmatched = miw64.DbgUnmatched;
1473 mi64.LineNumbers = miw64.LineNumbers;
1474 mi64.GlobalSymbols = miw64.GlobalSymbols;
1475 mi64.TypeInfo = miw64.TypeInfo;
1476 mi64.SourceIndexed = miw64.SourceIndexed;
1477 mi64.Publics = miw64.Publics;
1478 mi64.MachineType = miw64.MachineType;
1479 mi64.Reserved = miw64.Reserved;
1481 memcpy(ModuleInfo, &mi64, ModuleInfo->SizeOfStruct);
1483 return TRUE;
1486 /******************************************************************
1487 * SymGetModuleInfoW64 (DBGHELP.@)
1490 BOOL WINAPI SymGetModuleInfoW64(HANDLE hProcess, DWORD64 dwAddr,
1491 PIMAGEHLP_MODULEW64 ModuleInfo)
1493 struct process* pcs = process_find_by_handle(hProcess);
1494 struct module* module;
1495 IMAGEHLP_MODULEW64 miw64;
1497 TRACE("%p %I64x %p\n", hProcess, dwAddr, ModuleInfo);
1499 if (!pcs) return FALSE;
1500 if (ModuleInfo->SizeOfStruct > sizeof(*ModuleInfo)) return FALSE;
1501 module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
1502 if (!module) return FALSE;
1504 miw64 = module->module;
1506 if (dbghelp_opt_real_path && module->real_path)
1507 lstrcpynW(miw64.LoadedImageName, module->real_path, ARRAY_SIZE(miw64.LoadedImageName));
1509 /* update debug information from container if any */
1510 if (module->module.SymType == SymNone)
1512 module = module_get_container(pcs, module);
1513 if (module && module->module.SymType != SymNone)
1515 miw64.SymType = module->module.SymType;
1516 miw64.NumSyms = module->module.NumSyms;
1519 memcpy(ModuleInfo, &miw64, ModuleInfo->SizeOfStruct);
1520 return TRUE;
1523 /***********************************************************************
1524 * SymGetModuleBase (DBGHELP.@)
1526 DWORD WINAPI SymGetModuleBase(HANDLE hProcess, DWORD dwAddr)
1528 return (DWORD)SymGetModuleBase64(hProcess, dwAddr);
1531 /***********************************************************************
1532 * SymGetModuleBase64 (DBGHELP.@)
1534 DWORD64 WINAPI SymGetModuleBase64(HANDLE hProcess, DWORD64 dwAddr)
1536 struct process* pcs = process_find_by_handle(hProcess);
1537 struct module* module;
1539 if (!pcs) return 0;
1540 module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
1541 if (!module) return 0;
1542 return module->module.BaseOfImage;
1545 /******************************************************************
1546 * module_reset_debug_info
1547 * Removes any debug information linked to a given module.
1549 void module_reset_debug_info(struct module* module)
1551 module->sortlist_valid = TRUE;
1552 module->sorttab_size = 0;
1553 module->addr_sorttab = NULL;
1554 module->num_sorttab = module->num_symbols = 0;
1555 hash_table_destroy(&module->ht_symbols);
1556 module->ht_symbols.num_buckets = 0;
1557 module->ht_symbols.buckets = NULL;
1558 hash_table_destroy(&module->ht_types);
1559 module->ht_types.num_buckets = 0;
1560 module->ht_types.buckets = NULL;
1561 module->vtypes.num_elts = 0;
1562 hash_table_destroy(&module->ht_symbols);
1563 module->sources_used = module->sources_alloc = 0;
1564 module->sources = NULL;
1567 /******************************************************************
1568 * SymRefreshModuleList (DBGHELP.@)
1570 BOOL WINAPI SymRefreshModuleList(HANDLE hProcess)
1572 struct process* pcs;
1574 TRACE("(%p)\n", hProcess);
1576 if (!(pcs = process_find_by_handle(hProcess))) return FALSE;
1578 return pcs->loader->synchronize_module_list(pcs);
1581 /***********************************************************************
1582 * SymFunctionTableAccess (DBGHELP.@)
1584 PVOID WINAPI SymFunctionTableAccess(HANDLE hProcess, DWORD AddrBase)
1586 return SymFunctionTableAccess64(hProcess, AddrBase);
1589 /***********************************************************************
1590 * SymFunctionTableAccess64 (DBGHELP.@)
1592 PVOID WINAPI SymFunctionTableAccess64(HANDLE hProcess, DWORD64 AddrBase)
1594 struct process* pcs = process_find_by_handle(hProcess);
1595 struct module* module;
1597 if (!pcs) return NULL;
1598 module = module_find_by_addr(pcs, AddrBase, DMT_UNKNOWN);
1599 if (!module || !module->cpu->find_runtime_function) return NULL;
1601 return module->cpu->find_runtime_function(module, AddrBase);
1604 static struct module* native_load_module(struct process* pcs, const WCHAR* name, ULONG_PTR addr)
1606 return NULL;
1609 static BOOL native_load_debug_info(struct process* process, struct module* module)
1611 module->module.SymType = SymNone;
1612 return FALSE;
1615 static BOOL native_fetch_file_info(struct process* process, const WCHAR* name, ULONG_PTR load_addr, DWORD_PTR* base,
1616 DWORD* size, DWORD* checksum)
1618 return FALSE;
1621 static BOOL noloader_synchronize_module_list(struct process* pcs)
1623 return FALSE;
1626 static BOOL noloader_enum_modules(struct process *process, enum_modules_cb cb, void* user)
1628 return FALSE;
1631 static BOOL empty_synchronize_module_list(struct process* pcs)
1633 return TRUE;
1636 static BOOL empty_enum_modules(struct process *process, enum_modules_cb cb, void* user)
1638 return TRUE;
1641 /* to be used when debuggee isn't a live target */
1642 const struct loader_ops no_loader_ops =
1644 noloader_synchronize_module_list,
1645 native_load_module,
1646 native_load_debug_info,
1647 noloader_enum_modules,
1648 native_fetch_file_info,
1651 /* to be used when debuggee is a live target, but which system information isn't available */
1652 const struct loader_ops empty_loader_ops =
1654 empty_synchronize_module_list,
1655 native_load_module,
1656 native_load_debug_info,
1657 empty_enum_modules,
1658 native_fetch_file_info,