dbghelp: Pretend mach-o is present in case of failure.
[wine.git] / dlls / dbghelp / module.c
blobb12007d270e25030b69da5d49c0c4d94da27f981
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_WineLoaderW[] = L"<wine-loader>";
39 static const WCHAR * const ext[] = {L".acm", L".dll", L".drv", L".exe", L".ocx", L".vxd", NULL};
41 static int match_ext(const WCHAR* ptr, size_t len)
43 const WCHAR* const *e;
44 size_t l;
46 for (e = ext; *e; e++)
48 l = lstrlenW(*e);
49 if (l >= len) return 0;
50 if (wcsnicmp(&ptr[len - l], *e, l)) continue;
51 return l;
53 return 0;
56 /* FIXME: implemented from checking on modulename (ie foo.dll.so)
57 * and Wine loader, but fails to identify unixlib.
58 * Would require a stronger tagging of ELF modules.
60 BOOL module_is_wine_host(const WCHAR* module_name, const WCHAR* ext)
62 size_t len, extlen;
63 if (!wcscmp(module_name, S_WineLoaderW)) return TRUE;
64 len = wcslen(module_name);
65 extlen = wcslen(ext);
66 return len > extlen && !wcsicmp(&module_name[len - extlen], ext) &&
67 match_ext(module_name, len - extlen);
70 static const WCHAR* get_filename(const WCHAR* name, const WCHAR* endptr)
72 const WCHAR* ptr;
74 if (!endptr) endptr = name + lstrlenW(name);
75 for (ptr = endptr - 1; ptr >= name; ptr--)
77 if (*ptr == '/' || *ptr == '\\') break;
79 return ++ptr;
82 static BOOL is_wine_loader(const WCHAR *module)
84 const WCHAR *filename = get_filename(module, NULL);
85 const char *ptr;
86 BOOL ret = FALSE;
87 WCHAR *buffer;
88 DWORD len;
90 if ((ptr = getenv("WINELOADER")))
92 ptr = file_nameA(ptr);
93 len = 2 + MultiByteToWideChar( CP_UNIXCP, 0, ptr, -1, NULL, 0 );
94 buffer = heap_alloc( len * sizeof(WCHAR) );
95 MultiByteToWideChar( CP_UNIXCP, 0, ptr, -1, buffer, len );
97 else
99 buffer = heap_alloc( sizeof(L"wine") + 2 * sizeof(WCHAR) );
100 lstrcpyW( buffer, L"wine" );
103 if (!wcscmp( filename, buffer ))
104 ret = TRUE;
106 lstrcatW( buffer, L"64" );
107 if (!wcscmp( filename, buffer ))
108 ret = TRUE;
110 heap_free( buffer );
111 return ret;
114 static void module_fill_module(const WCHAR* in, WCHAR* out, size_t size)
116 const WCHAR *ptr, *endptr;
117 size_t len;
119 endptr = in + lstrlenW(in);
120 endptr -= match_ext(in, endptr - in);
121 ptr = get_filename(in, endptr);
122 len = min(endptr - ptr, size - 1);
123 memcpy(out, ptr, len * sizeof(WCHAR));
124 out[len] = '\0';
125 if (is_wine_loader(out))
126 lstrcpynW(out, S_WineLoaderW, size);
127 while ((*out = towlower(*out))) out++;
130 void module_set_module(struct module* module, const WCHAR* name)
132 module_fill_module(name, module->module.ModuleName, ARRAY_SIZE(module->module.ModuleName));
133 module_fill_module(name, module->modulename, ARRAY_SIZE(module->modulename));
136 /* Returned string must be freed by caller */
137 WCHAR *get_wine_loader_name(struct process *pcs)
139 const WCHAR *name;
140 WCHAR* altname;
141 unsigned len;
143 name = process_getenv(pcs, L"WINELOADER");
144 if (!name) name = pcs->is_host_64bit ? L"wine64" : L"wine";
145 len = lstrlenW(name);
147 /* WINELOADER isn't properly updated in Wow64 process calling inside Windows env block
148 * (it's updated in ELF env block though)
149 * So do the adaptation ourselves.
151 altname = HeapAlloc(GetProcessHeap(), 0, (len + 2 + 1) * sizeof(WCHAR));
152 if (altname)
154 memcpy(altname, name, len * sizeof(WCHAR));
155 if (pcs->is_host_64bit && len >= 2 && memcmp(name + len - 2, L"64", 2 * sizeof(WCHAR)) != 0)
157 lstrcpyW(altname + len, L"64");
158 /* in multi-arch wow configuration, wine64 doesn't exist */
159 if (GetFileAttributesW(altname) == INVALID_FILE_ATTRIBUTES)
160 altname[len] = L'\0';
162 else if (!pcs->is_host_64bit && len >= 2 && !memcmp(name + len - 2, L"64", 2 * sizeof(WCHAR)))
163 altname[len - 2] = '\0';
164 else
165 altname[len] = '\0';
168 TRACE("returning %s\n", debugstr_w(altname));
169 return altname;
172 static const char* get_module_type(struct module* module)
174 switch (module->type)
176 case DMT_ELF: return "ELF";
177 case DMT_MACHO: return "Mach-O";
178 case DMT_PE: return module->is_wine_builtin ? "PE (builtin)" : "PE";
179 default: return "---";
183 /***********************************************************************
184 * Creates and links a new module to a process
186 struct module* module_new(struct process* pcs, const WCHAR* name,
187 enum dhext_module_type type, BOOL builtin, BOOL virtual,
188 DWORD64 mod_addr, DWORD64 size,
189 ULONG_PTR stamp, ULONG_PTR checksum, WORD machine)
191 struct module* module;
192 struct module** pmodule;
193 unsigned i;
195 assert(type == DMT_ELF || type == DMT_PE || type == DMT_MACHO);
196 if (!(module = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*module))))
197 return NULL;
199 for (pmodule = &pcs->lmodules; *pmodule; pmodule = &(*pmodule)->next);
200 module->next = NULL;
201 *pmodule = module;
203 TRACE("=> %s%s%s %I64x-%I64x %s\n", virtual ? "virtual " : "", builtin ? "built-in " : "",
204 get_module_type(module), mod_addr, mod_addr + size, debugstr_w(name));
206 pool_init(&module->pool, 65536);
208 module->process = pcs;
209 module->module.SizeOfStruct = sizeof(module->module);
210 module->module.BaseOfImage = mod_addr;
211 module->module.ImageSize = size;
212 module_set_module(module, name);
213 module->module.ImageName[0] = '\0';
214 lstrcpynW(module->module.LoadedImageName, name, ARRAY_SIZE(module->module.LoadedImageName));
215 module->module.SymType = SymDeferred;
216 module->module.NumSyms = 0;
217 module->module.TimeDateStamp = stamp;
218 module->module.CheckSum = checksum;
220 memset(module->module.LoadedPdbName, 0, sizeof(module->module.LoadedPdbName));
221 module->module.CVSig = 0;
222 memset(module->module.CVData, 0, sizeof(module->module.CVData));
223 module->module.PdbSig = 0;
224 memset(&module->module.PdbSig70, 0, sizeof(module->module.PdbSig70));
225 module->module.PdbAge = 0;
226 module->module.PdbUnmatched = FALSE;
227 module->module.DbgUnmatched = FALSE;
228 module->module.LineNumbers = FALSE;
229 module->module.GlobalSymbols = FALSE;
230 module->module.TypeInfo = FALSE;
231 module->module.SourceIndexed = FALSE;
232 module->module.Publics = FALSE;
233 module->module.MachineType = machine;
234 module->module.Reserved = 0;
236 module->reloc_delta = 0;
237 module->type = type;
238 module->is_virtual = !!virtual;
239 module->is_wine_builtin = !!builtin;
240 module->has_file_image = TRUE;
242 for (i = 0; i < DFI_LAST; i++) module->format_info[i] = NULL;
243 module->sortlist_valid = FALSE;
244 module->sorttab_size = 0;
245 module->addr_sorttab = NULL;
246 module->num_sorttab = 0;
247 module->num_symbols = 0;
248 module->cpu = cpu_find(machine);
249 if (!module->cpu)
250 module->cpu = dbghelp_current_cpu;
251 module->debug_format_bitmask = 0;
253 vector_init(&module->vsymt, sizeof(struct symt*), 128);
254 vector_init(&module->vcustom_symt, sizeof(struct symt*), 16);
255 /* FIXME: this seems a bit too high (on a per module basis)
256 * need some statistics about this
258 hash_table_init(&module->pool, &module->ht_symbols, 4096);
259 hash_table_init(&module->pool, &module->ht_types, 4096);
260 vector_init(&module->vtypes, sizeof(struct symt*), 32);
262 module->sources_used = 0;
263 module->sources_alloc = 0;
264 module->sources = 0;
265 wine_rb_init(&module->sources_offsets_tree, source_rb_compare);
267 /* add top level symbol */
268 module->top = symt_new_module(module);
270 return module;
273 BOOL module_init_pair(struct module_pair* pair, HANDLE hProcess, DWORD64 addr)
275 if (!(pair->pcs = process_find_by_handle(hProcess))) return FALSE;
276 pair->requested = module_find_by_addr(pair->pcs, addr);
277 return module_get_debug(pair);
280 /***********************************************************************
281 * module_find_by_nameW
284 struct module* module_find_by_nameW(const struct process* pcs, const WCHAR* name)
286 struct module* module;
288 for (module = pcs->lmodules; module; module = module->next)
290 if (!wcsicmp(name, module->modulename)) return module;
292 SetLastError(ERROR_INVALID_NAME);
293 return NULL;
296 struct module* module_find_by_nameA(const struct process* pcs, const char* name)
298 WCHAR wname[MAX_PATH];
300 MultiByteToWideChar(CP_ACP, 0, name, -1, wname, ARRAY_SIZE(wname));
301 return module_find_by_nameW(pcs, wname);
304 /***********************************************************************
305 * module_is_already_loaded
308 struct module* module_is_already_loaded(const struct process* pcs, const WCHAR* name)
310 struct module* module;
311 const WCHAR* filename;
313 /* first compare the loaded image name... */
314 for (module = pcs->lmodules; module; module = module->next)
316 if (!wcsicmp(name, module->module.LoadedImageName))
317 return module;
319 /* then compare the standard filenames (without the path) ... */
320 filename = get_filename(name, NULL);
321 for (module = pcs->lmodules; module; module = module->next)
323 if (!wcsicmp(filename, get_filename(module->module.LoadedImageName, NULL)))
324 return module;
326 SetLastError(ERROR_INVALID_NAME);
327 return NULL;
330 /***********************************************************************
331 * module_get_container
334 static struct module* module_get_container(const struct process* pcs,
335 const struct module* inner)
337 struct module* module;
339 for (module = pcs->lmodules; module; module = module->next)
341 if (module != inner &&
342 module->module.BaseOfImage <= inner->module.BaseOfImage &&
343 module->module.BaseOfImage + module->module.ImageSize >=
344 inner->module.BaseOfImage + inner->module.ImageSize)
345 return module;
347 return NULL;
350 /***********************************************************************
351 * module_get_containee
354 struct module* module_get_containee(const struct process* pcs, const struct module* outer)
356 struct module* module;
358 for (module = pcs->lmodules; module; module = module->next)
360 if (module != outer &&
361 outer->module.BaseOfImage <= module->module.BaseOfImage &&
362 outer->module.BaseOfImage + outer->module.ImageSize >=
363 module->module.BaseOfImage + module->module.ImageSize)
364 return module;
366 return NULL;
369 BOOL module_load_debug(struct module* module)
371 IMAGEHLP_DEFERRED_SYMBOL_LOADW64 idslW64;
373 /* if deferred, force loading */
374 if (module->module.SymType == SymDeferred)
376 BOOL ret;
378 if (module->is_virtual) ret = FALSE;
379 else if (module->type == DMT_PE)
381 idslW64.SizeOfStruct = sizeof(idslW64);
382 idslW64.BaseOfImage = module->module.BaseOfImage;
383 idslW64.CheckSum = module->module.CheckSum;
384 idslW64.TimeDateStamp = module->module.TimeDateStamp;
385 memcpy(idslW64.FileName, module->module.ImageName,
386 sizeof(module->module.ImageName));
387 idslW64.Reparse = FALSE;
388 idslW64.hFile = INVALID_HANDLE_VALUE;
390 pcs_callback(module->process, CBA_DEFERRED_SYMBOL_LOAD_START, &idslW64);
391 ret = pe_load_debug_info(module->process, module);
392 pcs_callback(module->process,
393 ret ? CBA_DEFERRED_SYMBOL_LOAD_COMPLETE : CBA_DEFERRED_SYMBOL_LOAD_FAILURE,
394 &idslW64);
396 else ret = module->process->loader->load_debug_info(module->process, module);
398 if (!ret) module->module.SymType = SymNone;
399 assert(module->module.SymType != SymDeferred);
400 module->module.NumSyms = module->ht_symbols.num_elts;
402 return module->module.SymType != SymNone;
405 /******************************************************************
406 * module_get_debug
408 * get the debug information from a module:
409 * - if the module's type is deferred, then force loading of debug info (and return
410 * the module itself)
411 * - if the module has no debug info and has an ELF container, then return the ELF
412 * container (and also force the ELF container's debug info loading if deferred)
413 * - otherwise return the module itself if it has some debug info
415 BOOL module_get_debug(struct module_pair* pair)
417 if (!pair->requested) return FALSE;
418 /* for a PE builtin, always get info from container */
419 if (!(pair->effective = module_get_container(pair->pcs, pair->requested)))
420 pair->effective = pair->requested;
421 return module_load_debug(pair->effective);
424 /***********************************************************************
425 * module_find_by_addr
427 * either the addr where module is loaded, or any address inside the
428 * module
430 struct module* module_find_by_addr(const struct process* pcs, DWORD64 addr)
432 struct module* module;
434 for (module = pcs->lmodules; module; module = module->next)
436 if (module->type == DMT_PE && addr >= module->module.BaseOfImage &&
437 addr < module->module.BaseOfImage + module->module.ImageSize)
438 return module;
440 for (module = pcs->lmodules; module; module = module->next)
442 if ((module->type == DMT_ELF || module->type == DMT_MACHO) &&
443 addr >= module->module.BaseOfImage &&
444 addr < module->module.BaseOfImage + module->module.ImageSize)
445 return module;
447 SetLastError(ERROR_MOD_NOT_FOUND);
448 return module;
451 /******************************************************************
452 * module_is_container_loaded
454 * checks whether the native container, for a (supposed) PE builtin is
455 * already loaded
457 static BOOL module_is_container_loaded(const struct process* pcs,
458 const WCHAR* ImageName, DWORD64 base)
460 size_t len;
461 struct module* module;
462 PCWSTR filename, modname;
464 if (!base) return FALSE;
465 filename = get_filename(ImageName, NULL);
466 len = lstrlenW(filename);
468 for (module = pcs->lmodules; module; module = module->next)
470 if ((module->type == DMT_ELF || module->type == DMT_MACHO) &&
471 base >= module->module.BaseOfImage &&
472 base < module->module.BaseOfImage + module->module.ImageSize)
474 modname = get_filename(module->module.LoadedImageName, NULL);
475 if (!wcsnicmp(modname, filename, len) &&
476 !memcmp(modname + len, L".so", 3 * sizeof(WCHAR)))
478 return TRUE;
482 /* likely a native PE module */
483 WARN("Couldn't find container for %s\n", debugstr_w(ImageName));
484 return FALSE;
487 static BOOL image_check_debug_link_crc(const WCHAR* file, struct image_file_map* fmap, DWORD link_crc)
489 DWORD read_bytes;
490 HANDLE handle;
491 WCHAR *path;
492 WORD magic;
493 DWORD crc;
494 BOOL ret;
496 path = get_dos_file_name(file);
497 handle = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
498 heap_free(path);
499 if (handle == INVALID_HANDLE_VALUE) return FALSE;
501 crc = calc_crc32(handle);
502 if (crc != link_crc)
504 WARN("Bad CRC for file %s (got %08lx while expecting %08lx)\n", debugstr_w(file), crc, link_crc);
505 CloseHandle(handle);
506 return FALSE;
509 SetFilePointer(handle, 0, 0, FILE_BEGIN);
510 if (ReadFile(handle, &magic, sizeof(magic), &read_bytes, NULL) && magic == IMAGE_DOS_SIGNATURE)
511 ret = pe_map_file(handle, fmap);
512 else
513 ret = elf_map_handle(handle, fmap);
514 CloseHandle(handle);
515 return ret;
518 static BOOL image_check_debug_link_gnu_id(const WCHAR* file, struct image_file_map* fmap, const BYTE* id, unsigned idlen)
520 struct image_section_map buildid_sect;
521 DWORD read_bytes;
522 HANDLE handle;
523 WCHAR *path;
524 WORD magic;
525 BOOL ret;
527 path = get_dos_file_name(file);
528 handle = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
529 heap_free(path);
530 if (handle == INVALID_HANDLE_VALUE) return FALSE;
532 TRACE("Located debug information file at %s\n", debugstr_w(file));
534 if (ReadFile(handle, &magic, sizeof(magic), &read_bytes, NULL) && magic == IMAGE_DOS_SIGNATURE)
535 ret = pe_map_file(handle, fmap);
536 else
537 ret = elf_map_handle(handle, fmap);
538 CloseHandle(handle);
540 if (ret && image_find_section(fmap, ".note.gnu.build-id", &buildid_sect))
542 const UINT32* note;
544 note = (const UINT32*)image_map_section(&buildid_sect);
545 if (note != IMAGE_NO_MAP)
547 /* the usual ELF note structure: name-size desc-size type <name> <desc> */
548 if (note[2] == NOTE_GNU_BUILD_ID)
550 if (note[1] == idlen && !memcmp(note + 3 + ((note[0] + 3) >> 2), id, idlen))
551 return TRUE;
552 WARN("mismatch in buildid information for %s\n", wine_dbgstr_w(file));
555 image_unmap_section(&buildid_sect);
556 image_unmap_file(fmap);
558 return FALSE;
561 /******************************************************************
562 * image_locate_debug_link
564 * Locate a filename from a .gnu_debuglink section, using the same
565 * strategy as gdb:
566 * "If the full name of the directory containing the executable is
567 * execdir, and the executable has a debug link that specifies the
568 * name debugfile, then GDB will automatically search for the
569 * debugging information file in three places:
570 * - the directory containing the executable file (that is, it
571 * will look for a file named `execdir/debugfile',
572 * - a subdirectory of that directory named `.debug' (that is, the
573 * file `execdir/.debug/debugfile', and
574 * - a subdirectory of the global debug file directory that includes
575 * the executable's full path, and the name from the link (that is,
576 * the file `globaldebugdir/execdir/debugfile', where globaldebugdir
577 * is the global debug file directory, and execdir has been turned
578 * into a relative path)." (from GDB manual)
580 static struct image_file_map* image_locate_debug_link(const struct module* module, const char* filename, DWORD crc)
582 static const WCHAR globalDebugDirW[] = {'/','u','s','r','/','l','i','b','/','d','e','b','u','g','/'};
583 static const WCHAR dotDebugW[] = {'.','d','e','b','u','g','/'};
584 const size_t globalDebugDirLen = ARRAY_SIZE(globalDebugDirW);
585 size_t filename_len, path_len;
586 WCHAR* p = NULL;
587 WCHAR* slash;
588 WCHAR* slash2;
589 struct image_file_map* fmap_link = NULL;
591 fmap_link = HeapAlloc(GetProcessHeap(), 0, sizeof(*fmap_link));
592 if (!fmap_link) return NULL;
594 filename_len = MultiByteToWideChar(CP_UNIXCP, 0, filename, -1, NULL, 0);
595 path_len = lstrlenW(module->module.LoadedImageName);
596 if (module->real_path) path_len = max(path_len, lstrlenW(module->real_path));
597 p = HeapAlloc(GetProcessHeap(), 0,
598 (globalDebugDirLen + path_len + 6 + 1 + filename_len + 1) * sizeof(WCHAR));
599 if (!p) goto found;
601 /* we prebuild the string with "execdir" */
602 lstrcpyW(p, module->module.LoadedImageName);
603 slash = p;
604 if ((slash2 = wcsrchr(slash, '/'))) slash = slash2 + 1;
605 if ((slash2 = wcsrchr(slash, '\\'))) slash = slash2 + 1;
607 /* testing execdir/filename */
608 MultiByteToWideChar(CP_UNIXCP, 0, filename, -1, slash, filename_len);
609 if (image_check_debug_link_crc(p, fmap_link, crc)) goto found;
611 /* testing execdir/.debug/filename */
612 memcpy(slash, dotDebugW, sizeof(dotDebugW));
613 MultiByteToWideChar(CP_UNIXCP, 0, filename, -1, slash + ARRAY_SIZE(dotDebugW), filename_len);
614 if (image_check_debug_link_crc(p, fmap_link, crc)) goto found;
616 if (module->real_path)
618 lstrcpyW(p, module->real_path);
619 slash = p;
620 if ((slash2 = wcsrchr(slash, '/'))) slash = slash2 + 1;
621 if ((slash2 = wcsrchr(slash, '\\'))) slash = slash2 + 1;
622 MultiByteToWideChar(CP_UNIXCP, 0, filename, -1, slash, filename_len);
623 if (image_check_debug_link_crc(p, fmap_link, crc)) goto found;
626 /* testing globaldebugdir/execdir/filename */
627 memmove(p + globalDebugDirLen, p, (slash - p) * sizeof(WCHAR));
628 memcpy(p, globalDebugDirW, globalDebugDirLen * sizeof(WCHAR));
629 slash += globalDebugDirLen;
630 MultiByteToWideChar(CP_UNIXCP, 0, filename, -1, slash, filename_len);
631 if (image_check_debug_link_crc(p, fmap_link, crc)) goto found;
633 /* finally testing filename */
634 if (image_check_debug_link_crc(slash, fmap_link, crc)) goto found;
637 WARN("Couldn't locate or map %s\n", filename);
638 HeapFree(GetProcessHeap(), 0, p);
639 HeapFree(GetProcessHeap(), 0, fmap_link);
640 return NULL;
642 found:
643 TRACE("Located debug information file %s at %s\n", filename, debugstr_w(p));
644 HeapFree(GetProcessHeap(), 0, p);
645 return fmap_link;
648 static WCHAR* append_hex(WCHAR* dst, const BYTE* id, const BYTE* end)
650 while (id < end)
652 *dst++ = "0123456789abcdef"[*id >> 4 ];
653 *dst++ = "0123456789abcdef"[*id & 0x0F];
654 id++;
656 return dst;
659 /******************************************************************
660 * image_locate_build_id_target
662 * Try to find the .so file containing the debug info out of the build-id note information
664 static struct image_file_map* image_locate_build_id_target(const BYTE* id, unsigned idlen)
666 struct image_file_map* fmap_link = NULL;
667 DWORD sz;
668 WCHAR* p;
669 WCHAR* z;
671 fmap_link = HeapAlloc(GetProcessHeap(), 0, sizeof(*fmap_link));
672 if (!fmap_link) return NULL;
674 p = malloc(sizeof(L"/usr/lib/debug/.build-id/") +
675 (idlen * 2 + 1) * sizeof(WCHAR) + sizeof(L".debug"));
676 if (!p) goto fail;
677 wcscpy(p, L"/usr/lib/debug/.build-id/");
678 z = p + wcslen(p);
679 if (idlen)
681 z = append_hex(z, id, id + 1);
682 if (idlen > 1)
684 *z++ = L'/';
685 z = append_hex(z, id + 1, id + idlen);
688 wcscpy(z, L".debug");
689 TRACE("checking %s\n", wine_dbgstr_w(p));
691 if (image_check_debug_link_gnu_id(p, fmap_link, id, idlen))
693 free(p);
694 return fmap_link;
697 sz = GetEnvironmentVariableW(L"WINEHOMEDIR", NULL, 0);
698 if (sz)
700 z = realloc(p, sz * sizeof(WCHAR) +
701 sizeof(L"\\.cache\\debuginfod_client\\") +
702 idlen * 2 * sizeof(WCHAR) + sizeof(L"\\debuginfo") + 500);
703 if (!z) goto fail;
704 p = z;
705 GetEnvironmentVariableW(L"WINEHOMEDIR", p, sz);
706 z = p + sz - 1;
707 wcscpy(z, L"\\.cache\\debuginfod_client\\");
708 z += wcslen(z);
709 z = append_hex(z, id, id + idlen);
710 wcscpy(z, L"\\debuginfo");
711 TRACE("checking %ls\n", p);
712 if (image_check_debug_link_gnu_id(p, fmap_link, id, idlen))
714 free(p);
715 return fmap_link;
719 TRACE("not found\n");
720 fail:
721 free(p);
722 HeapFree(GetProcessHeap(), 0, fmap_link);
723 return NULL;
726 /******************************************************************
727 * image_load_debugaltlink
729 * Handle a (potential) .gnu_debugaltlink section and the link to
730 * (another) alternate debug file.
731 * Return an heap-allocated image_file_map when the section .gnu_debugaltlink is present,
732 * and a matching debug file has been located.
734 struct image_file_map* image_load_debugaltlink(struct image_file_map* fmap, struct module* module)
736 struct image_section_map debugaltlink_sect;
737 const char* data;
738 struct image_file_map* fmap_link = NULL;
739 BOOL ret = FALSE;
741 if (!image_find_section(fmap, ".gnu_debugaltlink", &debugaltlink_sect))
743 TRACE("No .gnu_debugaltlink section found for %s\n", debugstr_w(module->modulename));
744 return NULL;
747 data = image_map_section(&debugaltlink_sect);
748 if (data != IMAGE_NO_MAP)
750 unsigned sect_len;
751 const BYTE* id;
752 /* The content of the section is:
753 * + a \0 terminated string (filename)
754 * + followed by the build-id
755 * We try loading the dwz_alternate:
756 * - from the filename: either as absolute path, or relative to the embedded build-id
757 * - from the build-id
758 * In both cases, checking that found .so file matches the requested build-id
760 sect_len = image_get_map_size(&debugaltlink_sect);
761 id = memchr(data, '\0', sect_len);
762 if (id++)
764 unsigned idlen = (const BYTE*)data + sect_len - id;
765 fmap_link = HeapAlloc(GetProcessHeap(), 0, sizeof(*fmap_link));
766 if (fmap_link)
768 unsigned filename_len = MultiByteToWideChar(CP_UNIXCP, 0, data, -1, NULL, 0);
769 /* Trying absolute path */
770 WCHAR* dst = HeapAlloc(GetProcessHeap(), 0, filename_len * sizeof(WCHAR));
771 if (dst)
773 MultiByteToWideChar(CP_UNIXCP, 0, data, -1, dst, filename_len);
774 ret = image_check_debug_link_gnu_id(dst, fmap_link, id, idlen);
775 HeapFree(GetProcessHeap(), 0, dst);
777 /* Trying relative path to build-id directory */
778 if (!ret)
780 dst = HeapAlloc(GetProcessHeap(), 0,
781 sizeof(L"/usr/lib/debug/.build-id/") + (3 + filename_len + idlen * 2) * sizeof(WCHAR));
782 if (dst)
784 WCHAR* p;
786 /* SIGH....
787 * some relative links are relative to /usr/lib/debug/.build-id, some others are from the directory
788 * where the alternate file is...
789 * so try both
791 p = memcpy(dst, L"/usr/lib/debug/.build-id/", sizeof(L"/usr/lib/debug/.build-id/"));
792 p += wcslen(dst);
793 MultiByteToWideChar(CP_UNIXCP, 0, data, -1, p, filename_len);
794 ret = image_check_debug_link_gnu_id(dst, fmap_link, id, idlen);
795 if (!ret)
797 p = append_hex(p, id, id + idlen);
798 *p++ = '/';
799 MultiByteToWideChar(CP_UNIXCP, 0, data, -1, p, filename_len);
800 ret = image_check_debug_link_gnu_id(dst, fmap_link, id, idlen);
802 HeapFree(GetProcessHeap(), 0, dst);
805 if (!ret)
807 HeapFree(GetProcessHeap(), 0, fmap_link);
808 /* didn't work out with filename, try file lookup based on build-id */
809 if (!(fmap_link = image_locate_build_id_target(id, idlen)))
810 WARN("Couldn't find a match for .gnu_debugaltlink section %s for %s\n", data, debugstr_w(module->modulename));
815 image_unmap_section(&debugaltlink_sect);
816 if (fmap_link) TRACE("Found match .gnu_debugaltlink section for %s\n", debugstr_w(module->modulename));
817 return fmap_link;
820 /******************************************************************
821 * image_check_alternate
823 * Load alternate files for a given image file, looking at either .note.gnu_build-id
824 * or .gnu_debuglink sections.
826 BOOL image_check_alternate(struct image_file_map* fmap, const struct module* module)
828 struct image_section_map buildid_sect, debuglink_sect;
829 struct image_file_map* fmap_link = NULL;
831 /* if present, add the .gnu_debuglink file as an alternate to current one */
832 if (image_find_section(fmap, ".note.gnu.build-id", &buildid_sect))
834 const UINT32* note;
836 note = (const UINT32*)image_map_section(&buildid_sect);
837 if (note != IMAGE_NO_MAP)
839 /* the usual ELF note structure: name-size desc-size type <name> <desc> */
840 if (note[2] == NOTE_GNU_BUILD_ID)
842 fmap_link = image_locate_build_id_target((const BYTE*)(note + 3 + ((note[0] + 3) >> 2)), note[1]);
845 image_unmap_section(&buildid_sect);
847 /* if present, add the .gnu_debuglink file as an alternate to current one */
848 if (!fmap_link && image_find_section(fmap, ".gnu_debuglink", &debuglink_sect))
850 const char* dbg_link;
852 dbg_link = image_map_section(&debuglink_sect);
853 if (dbg_link != IMAGE_NO_MAP)
855 /* The content of a debug link section is:
856 * 1/ a NULL terminated string, containing the file name for the
857 * debug info
858 * 2/ padding on 4 byte boundary
859 * 3/ CRC of the linked file
861 DWORD crc = *(const DWORD*)(dbg_link + ((DWORD_PTR)(strlen(dbg_link) + 4) & ~3));
862 if (!(fmap_link = image_locate_debug_link(module, dbg_link, crc)))
863 WARN("Couldn't load linked debug file for %s\n", debugstr_w(module->modulename));
865 image_unmap_section(&debuglink_sect);
867 if (fmap_link)
869 fmap->alternate = fmap_link;
870 return TRUE;
872 return FALSE;
875 /***********************************************************************
876 * SymLoadModule (DBGHELP.@)
878 DWORD WINAPI SymLoadModule(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
879 PCSTR ModuleName, DWORD BaseOfDll, DWORD SizeOfDll)
881 return SymLoadModuleEx(hProcess, hFile, ImageName, ModuleName, BaseOfDll,
882 SizeOfDll, NULL, 0);
885 /***********************************************************************
886 * SymLoadModuleEx (DBGHELP.@)
888 DWORD64 WINAPI SymLoadModuleEx(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
889 PCSTR ModuleName, DWORD64 BaseOfDll, DWORD DllSize,
890 PMODLOAD_DATA Data, DWORD Flags)
892 PWSTR wImageName, wModuleName;
893 unsigned len;
894 DWORD64 ret;
896 TRACE("(%p %p %s %s %I64x %08lx %p %08lx)\n",
897 hProcess, hFile, debugstr_a(ImageName), debugstr_a(ModuleName),
898 BaseOfDll, DllSize, Data, Flags);
900 if (ImageName)
902 len = MultiByteToWideChar(CP_ACP, 0, ImageName, -1, NULL, 0);
903 wImageName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
904 MultiByteToWideChar(CP_ACP, 0, ImageName, -1, wImageName, len);
906 else wImageName = NULL;
907 if (ModuleName)
909 len = MultiByteToWideChar(CP_ACP, 0, ModuleName, -1, NULL, 0);
910 wModuleName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
911 MultiByteToWideChar(CP_ACP, 0, ModuleName, -1, wModuleName, len);
913 else wModuleName = NULL;
915 ret = SymLoadModuleExW(hProcess, hFile, wImageName, wModuleName,
916 BaseOfDll, DllSize, Data, Flags);
917 HeapFree(GetProcessHeap(), 0, wImageName);
918 HeapFree(GetProcessHeap(), 0, wModuleName);
919 return ret;
922 /***********************************************************************
923 * SymLoadModuleExW (DBGHELP.@)
925 DWORD64 WINAPI SymLoadModuleExW(HANDLE hProcess, HANDLE hFile, PCWSTR wImageName,
926 PCWSTR wModuleName, DWORD64 BaseOfDll, DWORD SizeOfDll,
927 PMODLOAD_DATA Data, DWORD Flags)
929 struct process* pcs;
930 struct module* module = NULL;
931 struct module* altmodule;
933 TRACE("(%p %p %s %s %I64x %08lx %p %08lx)\n",
934 hProcess, hFile, debugstr_w(wImageName), debugstr_w(wModuleName),
935 BaseOfDll, SizeOfDll, Data, Flags);
937 if (Data)
938 FIXME("Unsupported load data parameter %p for %s\n",
939 Data, debugstr_w(wImageName));
941 if (!(pcs = process_find_by_handle(hProcess))) return 0;
943 if (Flags & ~(SLMFLAG_VIRTUAL))
944 FIXME("Unsupported Flags %08lx for %s\n", Flags, debugstr_w(wImageName));
946 pcs->loader->synchronize_module_list(pcs);
948 /* this is a Wine extension to the API just to redo the synchronisation */
949 if (!wImageName && !hFile) return 0;
951 if (Flags & SLMFLAG_VIRTUAL)
953 if (!wImageName) return 0;
954 module = module_new(pcs, wImageName, DMT_PE, FALSE, TRUE, BaseOfDll, SizeOfDll, 0, 0, IMAGE_FILE_MACHINE_UNKNOWN);
955 if (!module) return 0;
956 module->module.SymType = SymVirtual;
958 /* check if it's a builtin PE module with a containing ELF module */
959 else if (wImageName && module_is_container_loaded(pcs, wImageName, BaseOfDll))
961 /* force the loading of DLL as builtin */
962 module = pe_load_builtin_module(pcs, wImageName, BaseOfDll, SizeOfDll);
964 if (!module)
966 /* otherwise, try a regular PE module */
967 if (!(module = pe_load_native_module(pcs, wImageName, hFile, BaseOfDll, SizeOfDll)) &&
968 wImageName)
970 /* and finally an ELF or Mach-O module */
971 module = pcs->loader->load_module(pcs, wImageName, BaseOfDll);
974 if (!module)
976 WARN("Couldn't locate %s\n", debugstr_w(wImageName));
977 return 0;
979 /* by default module_new fills module.ModuleName from a derivation
980 * of LoadedImageName. Overwrite it, if we have better information
982 if (wModuleName)
983 module_set_module(module, wModuleName);
984 if (wImageName)
985 lstrcpynW(module->module.ImageName, wImageName, ARRAY_SIZE(module->module.ImageName));
987 for (altmodule = pcs->lmodules; altmodule; altmodule = altmodule->next)
989 if (altmodule != module && altmodule->type == module->type &&
990 module->module.BaseOfImage >= altmodule->module.BaseOfImage &&
991 module->module.BaseOfImage < altmodule->module.BaseOfImage + altmodule->module.ImageSize)
992 break;
994 if (altmodule)
996 /* We have a conflict as the new module cannot be found by its base address
997 * (it's hidden by altmodule).
998 * We need to decide which one the two modules we need to get rid of.
1000 /* loading same module at same address... don't change anything */
1001 if (module->module.BaseOfImage == altmodule->module.BaseOfImage)
1003 module_remove(pcs, module);
1004 SetLastError(ERROR_SUCCESS);
1005 return 0;
1007 /* replace old module with new one */
1008 WARN("Replace module %ls at %I64x by module %ls at %I64x\n",
1009 altmodule->module.ImageName, altmodule->module.BaseOfImage,
1010 module->module.ImageName, module->module.BaseOfImage);
1011 module_remove(pcs, altmodule);
1014 if ((dbghelp_options & SYMOPT_DEFERRED_LOADS) == 0 && !module_get_container(pcs, module))
1015 module_load_debug(module);
1016 return module->module.BaseOfImage;
1019 /***********************************************************************
1020 * SymLoadModule64 (DBGHELP.@)
1022 DWORD64 WINAPI SymLoadModule64(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
1023 PCSTR ModuleName, DWORD64 BaseOfDll, DWORD SizeOfDll)
1025 return SymLoadModuleEx(hProcess, hFile, ImageName, ModuleName, BaseOfDll, SizeOfDll,
1026 NULL, 0);
1029 /******************************************************************
1030 * module_remove
1033 BOOL module_remove(struct process* pcs, struct module* module)
1035 struct module_format*modfmt;
1036 struct module** p;
1037 unsigned i;
1039 TRACE("%s (%p)\n", debugstr_w(module->modulename), module);
1041 /* remove local scope if symbol is from this module */
1042 if (pcs->localscope_symt)
1044 struct symt* locsym = pcs->localscope_symt;
1045 if (symt_check_tag(locsym, SymTagInlineSite))
1046 locsym = &symt_get_function_from_inlined((struct symt_function*)locsym)->symt;
1047 if (symt_check_tag(locsym, SymTagFunction))
1049 locsym = ((struct symt_function*)locsym)->container;
1050 if (symt_check_tag(locsym, SymTagCompiland) &&
1051 module == ((struct symt_compiland*)locsym)->container->module)
1053 pcs->localscope_pc = 0;
1054 pcs->localscope_symt = NULL;
1058 for (i = 0; i < DFI_LAST; i++)
1060 if ((modfmt = module->format_info[i]) && modfmt->remove)
1061 modfmt->remove(pcs, module->format_info[i]);
1063 hash_table_destroy(&module->ht_symbols);
1064 hash_table_destroy(&module->ht_types);
1065 HeapFree(GetProcessHeap(), 0, module->sources);
1066 HeapFree(GetProcessHeap(), 0, module->addr_sorttab);
1067 pool_destroy(&module->pool);
1068 /* native dbghelp doesn't invoke registered callback(,CBA_SYMBOLS_UNLOADED,) here
1069 * so do we
1071 for (p = &pcs->lmodules; *p; p = &(*p)->next)
1073 if (*p == module)
1075 *p = module->next;
1076 HeapFree(GetProcessHeap(), 0, module);
1077 return TRUE;
1080 FIXME("This shouldn't happen\n");
1081 return FALSE;
1084 /******************************************************************
1085 * SymUnloadModule (DBGHELP.@)
1088 BOOL WINAPI SymUnloadModule(HANDLE hProcess, DWORD BaseOfDll)
1090 return SymUnloadModule64(hProcess, BaseOfDll);
1093 /******************************************************************
1094 * SymUnloadModule64 (DBGHELP.@)
1097 BOOL WINAPI SymUnloadModule64(HANDLE hProcess, DWORD64 BaseOfDll)
1099 struct process* pcs;
1100 struct module* module;
1102 pcs = process_find_by_handle(hProcess);
1103 if (!pcs) return FALSE;
1104 module = module_find_by_addr(pcs, BaseOfDll);
1105 if (!module) return FALSE;
1106 module_remove(pcs, module);
1107 return TRUE;
1110 /******************************************************************
1111 * SymEnumerateModules (DBGHELP.@)
1114 struct enum_modW64_32
1116 PSYM_ENUMMODULES_CALLBACK cb;
1117 PVOID user;
1118 char module[MAX_PATH];
1121 static BOOL CALLBACK enum_modW64_32(PCWSTR name, DWORD64 base, PVOID user)
1123 struct enum_modW64_32* x = user;
1125 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
1126 return x->cb(x->module, (DWORD)base, x->user);
1129 BOOL WINAPI SymEnumerateModules(HANDLE hProcess,
1130 PSYM_ENUMMODULES_CALLBACK EnumModulesCallback,
1131 PVOID UserContext)
1133 struct enum_modW64_32 x;
1135 x.cb = EnumModulesCallback;
1136 x.user = UserContext;
1138 return SymEnumerateModulesW64(hProcess, enum_modW64_32, &x);
1141 /******************************************************************
1142 * SymEnumerateModules64 (DBGHELP.@)
1145 struct enum_modW64_64
1147 PSYM_ENUMMODULES_CALLBACK64 cb;
1148 PVOID user;
1149 char module[MAX_PATH];
1152 static BOOL CALLBACK enum_modW64_64(PCWSTR name, DWORD64 base, PVOID user)
1154 struct enum_modW64_64* x = user;
1156 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
1157 return x->cb(x->module, base, x->user);
1160 BOOL WINAPI SymEnumerateModules64(HANDLE hProcess,
1161 PSYM_ENUMMODULES_CALLBACK64 EnumModulesCallback,
1162 PVOID UserContext)
1164 struct enum_modW64_64 x;
1166 x.cb = EnumModulesCallback;
1167 x.user = UserContext;
1169 return SymEnumerateModulesW64(hProcess, enum_modW64_64, &x);
1172 /******************************************************************
1173 * SymEnumerateModulesW64 (DBGHELP.@)
1176 BOOL WINAPI SymEnumerateModulesW64(HANDLE hProcess,
1177 PSYM_ENUMMODULES_CALLBACKW64 EnumModulesCallback,
1178 PVOID UserContext)
1180 struct process* pcs = process_find_by_handle(hProcess);
1181 struct module* module;
1183 if (!pcs) return FALSE;
1185 for (module = pcs->lmodules; module; module = module->next)
1187 if (!dbghelp_opt_native &&
1188 (module->type == DMT_ELF || module->type == DMT_MACHO))
1189 continue;
1190 if (!EnumModulesCallback(module->modulename,
1191 module->module.BaseOfImage, UserContext))
1192 break;
1194 return TRUE;
1197 /******************************************************************
1198 * EnumerateLoadedModules64 (DBGHELP.@)
1201 struct enum_load_modW64_64
1203 PENUMLOADED_MODULES_CALLBACK64 cb;
1204 PVOID user;
1205 char module[MAX_PATH];
1208 static BOOL CALLBACK enum_load_modW64_64(PCWSTR name, DWORD64 base, ULONG size,
1209 PVOID user)
1211 struct enum_load_modW64_64* x = user;
1213 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
1214 return x->cb(x->module, base, size, x->user);
1217 BOOL WINAPI EnumerateLoadedModules64(HANDLE hProcess,
1218 PENUMLOADED_MODULES_CALLBACK64 EnumLoadedModulesCallback,
1219 PVOID UserContext)
1221 struct enum_load_modW64_64 x;
1223 x.cb = EnumLoadedModulesCallback;
1224 x.user = UserContext;
1226 return EnumerateLoadedModulesW64(hProcess, enum_load_modW64_64, &x);
1229 /******************************************************************
1230 * EnumerateLoadedModules (DBGHELP.@)
1233 struct enum_load_modW64_32
1235 PENUMLOADED_MODULES_CALLBACK cb;
1236 PVOID user;
1237 char module[MAX_PATH];
1240 static BOOL CALLBACK enum_load_modW64_32(PCWSTR name, DWORD64 base, ULONG size,
1241 PVOID user)
1243 struct enum_load_modW64_32* x = user;
1244 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
1245 return x->cb(x->module, (DWORD)base, size, x->user);
1248 BOOL WINAPI EnumerateLoadedModules(HANDLE hProcess,
1249 PENUMLOADED_MODULES_CALLBACK EnumLoadedModulesCallback,
1250 PVOID UserContext)
1252 struct enum_load_modW64_32 x;
1254 x.cb = EnumLoadedModulesCallback;
1255 x.user = UserContext;
1257 return EnumerateLoadedModulesW64(hProcess, enum_load_modW64_32, &x);
1260 static unsigned int load_and_grow_modules(HANDLE process, HMODULE** hmods, unsigned start, unsigned* alloc, DWORD filter)
1262 DWORD needed;
1263 BOOL ret;
1265 while ((ret = EnumProcessModulesEx(process, *hmods + start, (*alloc - start) * sizeof(HMODULE),
1266 &needed, filter)) &&
1267 needed > (*alloc - start) * sizeof(HMODULE))
1269 HMODULE* new = HeapReAlloc(GetProcessHeap(), 0, *hmods, (*alloc) * 2 * sizeof(HMODULE));
1270 if (!new) return 0;
1271 *hmods = new;
1272 *alloc *= 2;
1274 return ret ? needed / sizeof(HMODULE) : 0;
1277 /******************************************************************
1278 * EnumerateLoadedModulesW64 (DBGHELP.@)
1281 BOOL WINAPI EnumerateLoadedModulesW64(HANDLE process,
1282 PENUMLOADED_MODULES_CALLBACKW64 enum_cb,
1283 PVOID user)
1285 HMODULE* hmods;
1286 unsigned alloc = 256, count, count32, i;
1287 USHORT pcs_machine, native_machine;
1288 BOOL with_32bit_modules;
1289 WCHAR imagenameW[MAX_PATH];
1290 MODULEINFO mi;
1291 WCHAR* sysdir = NULL;
1292 WCHAR* wowdir = NULL;
1293 size_t sysdir_len = 0, wowdir_len = 0;
1295 /* process might not be a handle to a live process */
1296 if (!IsWow64Process2(process, &pcs_machine, &native_machine)) return FALSE;
1297 with_32bit_modules = sizeof(void*) > sizeof(int) &&
1298 pcs_machine != IMAGE_FILE_MACHINE_UNKNOWN &&
1299 (dbghelp_options & SYMOPT_INCLUDE_32BIT_MODULES);
1301 if (!(hmods = HeapAlloc(GetProcessHeap(), 0, alloc * sizeof(hmods[0]))))
1302 return FALSE;
1304 /* Note:
1305 * - we report modules returned from kernelbase.EnumProcessModulesEx
1306 * - appending 32bit modules when possible and requested
1308 * When considering 32bit modules in a wow64 child process, required from
1309 * a 64bit process:
1310 * - native returns from kernelbase.EnumProcessModulesEx
1311 * redirected paths (that is in system32 directory), while
1312 * dbghelp.EnumerateLoadedModulesWine returns the effective path
1313 * (eg. syswow64 for x86_64).
1314 * - (Except for the main module, if gotten from syswow64, where kernelbase
1315 * will return the effective path)
1316 * - Wine kernelbase (and ntdll) incorrectly return these modules from
1317 * syswow64 (except for ntdll which is returned from system32).
1318 * => for these modules, always perform a system32 => syswow64 path
1319 * conversion (it'll work even if ntdll/kernelbase is fixed).
1321 if ((count = load_and_grow_modules(process, &hmods, 0, &alloc, LIST_MODULES_DEFAULT)) && with_32bit_modules)
1323 /* append 32bit modules when required */
1324 if ((count32 = load_and_grow_modules(process, &hmods, count, &alloc, LIST_MODULES_32BIT)))
1326 sysdir_len = GetSystemDirectoryW(NULL, 0);
1327 wowdir_len = GetSystemWow64Directory2W(NULL, 0, pcs_machine);
1329 if (!sysdir_len || !wowdir_len ||
1330 !(sysdir = HeapAlloc(GetProcessHeap(), 0, (sysdir_len + 1 + wowdir_len + 1) * sizeof(WCHAR))))
1332 HeapFree(GetProcessHeap(), 0, hmods);
1333 return FALSE;
1335 wowdir = sysdir + sysdir_len + 1;
1336 if (GetSystemDirectoryW(sysdir, sysdir_len) >= sysdir_len)
1337 FIXME("shouldn't happen\n");
1338 if (GetSystemWow64Directory2W(wowdir, wowdir_len, pcs_machine) >= wowdir_len)
1339 FIXME("shouldn't happen\n");
1340 wcscat(sysdir, L"\\");
1341 wcscat(wowdir, L"\\");
1344 else count32 = 0;
1346 for (i = 0; i < count + count32; i++)
1348 if (GetModuleInformation(process, hmods[i], &mi, sizeof(mi)) &&
1349 GetModuleFileNameExW(process, hmods[i], imagenameW, ARRAY_SIZE(imagenameW)))
1351 /* rewrite path in system32 into syswow64 for 32bit modules */
1352 if (i >= count)
1354 size_t len = wcslen(imagenameW);
1356 if (!wcsnicmp(imagenameW, sysdir, sysdir_len) &&
1357 (len - sysdir_len + wowdir_len) + 1 <= ARRAY_SIZE(imagenameW))
1359 memmove(&imagenameW[wowdir_len], &imagenameW[sysdir_len], (len - sysdir_len) * sizeof(WCHAR));
1360 memcpy(imagenameW, wowdir, wowdir_len * sizeof(WCHAR));
1363 if (!enum_cb(imagenameW, (DWORD_PTR)mi.lpBaseOfDll, mi.SizeOfImage, user))
1364 break;
1368 HeapFree(GetProcessHeap(), 0, hmods);
1369 HeapFree(GetProcessHeap(), 0, sysdir);
1371 return count != 0;
1374 static void dbghelp_str_WtoA(const WCHAR *src, char *dst, int dst_len)
1376 WideCharToMultiByte(CP_ACP, 0, src, -1, dst, dst_len - 1, NULL, NULL);
1377 dst[dst_len - 1] = 0;
1380 /******************************************************************
1381 * SymGetModuleInfo (DBGHELP.@)
1384 BOOL WINAPI SymGetModuleInfo(HANDLE hProcess, DWORD dwAddr,
1385 PIMAGEHLP_MODULE ModuleInfo)
1387 IMAGEHLP_MODULE mi;
1388 IMAGEHLP_MODULEW64 miw64;
1390 if (sizeof(mi) < ModuleInfo->SizeOfStruct) FIXME("Wrong size\n");
1392 miw64.SizeOfStruct = sizeof(miw64);
1393 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
1395 mi.SizeOfStruct = ModuleInfo->SizeOfStruct;
1396 mi.BaseOfImage = miw64.BaseOfImage;
1397 mi.ImageSize = miw64.ImageSize;
1398 mi.TimeDateStamp = miw64.TimeDateStamp;
1399 mi.CheckSum = miw64.CheckSum;
1400 mi.NumSyms = miw64.NumSyms;
1401 mi.SymType = miw64.SymType;
1402 dbghelp_str_WtoA(miw64.ModuleName, mi.ModuleName, sizeof(mi.ModuleName));
1403 dbghelp_str_WtoA(miw64.ImageName, mi.ImageName, sizeof(mi.ImageName));
1404 dbghelp_str_WtoA(miw64.LoadedImageName, mi.LoadedImageName, sizeof(mi.LoadedImageName));
1406 memcpy(ModuleInfo, &mi, ModuleInfo->SizeOfStruct);
1408 return TRUE;
1411 /******************************************************************
1412 * SymGetModuleInfoW (DBGHELP.@)
1415 BOOL WINAPI SymGetModuleInfoW(HANDLE hProcess, DWORD dwAddr,
1416 PIMAGEHLP_MODULEW ModuleInfo)
1418 IMAGEHLP_MODULEW64 miw64;
1419 IMAGEHLP_MODULEW miw;
1421 if (sizeof(miw) < ModuleInfo->SizeOfStruct) FIXME("Wrong size\n");
1423 miw64.SizeOfStruct = sizeof(miw64);
1424 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
1426 miw.SizeOfStruct = ModuleInfo->SizeOfStruct;
1427 miw.BaseOfImage = miw64.BaseOfImage;
1428 miw.ImageSize = miw64.ImageSize;
1429 miw.TimeDateStamp = miw64.TimeDateStamp;
1430 miw.CheckSum = miw64.CheckSum;
1431 miw.NumSyms = miw64.NumSyms;
1432 miw.SymType = miw64.SymType;
1433 lstrcpyW(miw.ModuleName, miw64.ModuleName);
1434 lstrcpyW(miw.ImageName, miw64.ImageName);
1435 lstrcpyW(miw.LoadedImageName, miw64.LoadedImageName);
1436 memcpy(ModuleInfo, &miw, ModuleInfo->SizeOfStruct);
1438 return TRUE;
1441 /******************************************************************
1442 * SymGetModuleInfo64 (DBGHELP.@)
1445 BOOL WINAPI SymGetModuleInfo64(HANDLE hProcess, DWORD64 dwAddr,
1446 PIMAGEHLP_MODULE64 ModuleInfo)
1448 IMAGEHLP_MODULE64 mi64;
1449 IMAGEHLP_MODULEW64 miw64;
1451 if (sizeof(mi64) < ModuleInfo->SizeOfStruct)
1453 SetLastError(ERROR_MOD_NOT_FOUND); /* NOTE: native returns this error */
1454 WARN("Wrong size %lu\n", ModuleInfo->SizeOfStruct);
1455 return FALSE;
1458 miw64.SizeOfStruct = sizeof(miw64);
1459 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
1461 mi64.SizeOfStruct = ModuleInfo->SizeOfStruct;
1462 mi64.BaseOfImage = miw64.BaseOfImage;
1463 mi64.ImageSize = miw64.ImageSize;
1464 mi64.TimeDateStamp = miw64.TimeDateStamp;
1465 mi64.CheckSum = miw64.CheckSum;
1466 mi64.NumSyms = miw64.NumSyms;
1467 mi64.SymType = miw64.SymType;
1468 dbghelp_str_WtoA(miw64.ModuleName, mi64.ModuleName, sizeof(mi64.ModuleName));
1469 dbghelp_str_WtoA(miw64.ImageName, mi64.ImageName, sizeof(mi64.ImageName));
1470 dbghelp_str_WtoA(miw64.LoadedImageName, mi64.LoadedImageName, sizeof(mi64.LoadedImageName));
1471 dbghelp_str_WtoA(miw64.LoadedPdbName, mi64.LoadedPdbName, sizeof(mi64.LoadedPdbName));
1473 mi64.CVSig = miw64.CVSig;
1474 dbghelp_str_WtoA(miw64.CVData, mi64.CVData, sizeof(mi64.CVData));
1475 mi64.PdbSig = miw64.PdbSig;
1476 mi64.PdbSig70 = miw64.PdbSig70;
1477 mi64.PdbAge = miw64.PdbAge;
1478 mi64.PdbUnmatched = miw64.PdbUnmatched;
1479 mi64.DbgUnmatched = miw64.DbgUnmatched;
1480 mi64.LineNumbers = miw64.LineNumbers;
1481 mi64.GlobalSymbols = miw64.GlobalSymbols;
1482 mi64.TypeInfo = miw64.TypeInfo;
1483 mi64.SourceIndexed = miw64.SourceIndexed;
1484 mi64.Publics = miw64.Publics;
1485 mi64.MachineType = miw64.MachineType;
1486 mi64.Reserved = miw64.Reserved;
1488 memcpy(ModuleInfo, &mi64, ModuleInfo->SizeOfStruct);
1490 return TRUE;
1493 /******************************************************************
1494 * SymGetModuleInfoW64 (DBGHELP.@)
1497 BOOL WINAPI SymGetModuleInfoW64(HANDLE hProcess, DWORD64 dwAddr,
1498 PIMAGEHLP_MODULEW64 ModuleInfo)
1500 struct process* pcs = process_find_by_handle(hProcess);
1501 struct module* module;
1502 IMAGEHLP_MODULEW64 miw64;
1504 TRACE("%p %I64x %p\n", hProcess, dwAddr, ModuleInfo);
1506 if (!pcs) return FALSE;
1507 if (ModuleInfo->SizeOfStruct > sizeof(*ModuleInfo)) return FALSE;
1508 module = module_find_by_addr(pcs, dwAddr);
1509 if (!module) return FALSE;
1511 miw64 = module->module;
1513 if (dbghelp_opt_real_path && module->real_path)
1514 lstrcpynW(miw64.LoadedImageName, module->real_path, ARRAY_SIZE(miw64.LoadedImageName));
1516 /* update debug information from container if any */
1517 if (module->module.SymType == SymNone)
1519 module = module_get_container(pcs, module);
1520 if (module && module->module.SymType != SymNone)
1522 miw64.SymType = module->module.SymType;
1523 miw64.NumSyms = module->module.NumSyms;
1526 memcpy(ModuleInfo, &miw64, ModuleInfo->SizeOfStruct);
1527 return TRUE;
1530 /***********************************************************************
1531 * SymGetModuleBase (DBGHELP.@)
1533 DWORD WINAPI SymGetModuleBase(HANDLE hProcess, DWORD dwAddr)
1535 return (DWORD)SymGetModuleBase64(hProcess, dwAddr);
1538 /***********************************************************************
1539 * SymGetModuleBase64 (DBGHELP.@)
1541 DWORD64 WINAPI SymGetModuleBase64(HANDLE hProcess, DWORD64 dwAddr)
1543 struct process* pcs = process_find_by_handle(hProcess);
1544 struct module* module;
1546 if (!pcs) return 0;
1547 module = module_find_by_addr(pcs, dwAddr);
1548 if (!module) return 0;
1549 return module->module.BaseOfImage;
1552 /******************************************************************
1553 * module_reset_debug_info
1554 * Removes any debug information linked to a given module.
1556 void module_reset_debug_info(struct module* module)
1558 module->sortlist_valid = TRUE;
1559 module->sorttab_size = 0;
1560 module->addr_sorttab = NULL;
1561 module->num_sorttab = module->num_symbols = 0;
1562 hash_table_destroy(&module->ht_symbols);
1563 module->ht_symbols.num_buckets = 0;
1564 module->ht_symbols.buckets = NULL;
1565 hash_table_destroy(&module->ht_types);
1566 module->ht_types.num_buckets = 0;
1567 module->ht_types.buckets = NULL;
1568 module->vtypes.num_elts = 0;
1569 hash_table_destroy(&module->ht_symbols);
1570 module->sources_used = module->sources_alloc = 0;
1571 module->sources = NULL;
1574 /******************************************************************
1575 * SymRefreshModuleList (DBGHELP.@)
1577 BOOL WINAPI SymRefreshModuleList(HANDLE hProcess)
1579 struct process* pcs;
1581 TRACE("(%p)\n", hProcess);
1583 if (!(pcs = process_find_by_handle(hProcess))) return FALSE;
1585 return pcs->loader->synchronize_module_list(pcs);
1588 /***********************************************************************
1589 * SymFunctionTableAccess (DBGHELP.@)
1591 PVOID WINAPI SymFunctionTableAccess(HANDLE hProcess, DWORD AddrBase)
1593 return SymFunctionTableAccess64(hProcess, AddrBase);
1596 /***********************************************************************
1597 * SymFunctionTableAccess64 (DBGHELP.@)
1599 PVOID WINAPI SymFunctionTableAccess64(HANDLE hProcess, DWORD64 AddrBase)
1601 struct process* pcs = process_find_by_handle(hProcess);
1602 struct module* module;
1604 if (!pcs) return NULL;
1605 module = module_find_by_addr(pcs, AddrBase);
1606 if (!module || !module->cpu->find_runtime_function) return NULL;
1608 return module->cpu->find_runtime_function(module, AddrBase);
1611 static struct module* native_load_module(struct process* pcs, const WCHAR* name, ULONG_PTR addr)
1613 return NULL;
1616 static BOOL native_load_debug_info(struct process* process, struct module* module)
1618 module->module.SymType = SymNone;
1619 return FALSE;
1622 static BOOL native_fetch_file_info(struct process* process, const WCHAR* name, ULONG_PTR load_addr, DWORD_PTR* base,
1623 DWORD* size, DWORD* checksum)
1625 return FALSE;
1628 static BOOL noloader_synchronize_module_list(struct process* pcs)
1630 return FALSE;
1633 static BOOL noloader_enum_modules(struct process *process, enum_modules_cb cb, void* user)
1635 return FALSE;
1638 static BOOL empty_synchronize_module_list(struct process* pcs)
1640 return TRUE;
1643 static BOOL empty_enum_modules(struct process *process, enum_modules_cb cb, void* user)
1645 return TRUE;
1648 /* to be used when debuggee isn't a live target */
1649 const struct loader_ops no_loader_ops =
1651 noloader_synchronize_module_list,
1652 native_load_module,
1653 native_load_debug_info,
1654 noloader_enum_modules,
1655 native_fetch_file_info,
1658 /* to be used when debuggee is a live target, but which system information isn't available */
1659 const struct loader_ops empty_loader_ops =
1661 empty_synchronize_module_list,
1662 native_load_module,
1663 native_load_debug_info,
1664 empty_enum_modules,
1665 native_fetch_file_info,
1668 BOOL WINAPI wine_get_module_information(HANDLE proc, DWORD64 base, struct dhext_module_information* wmi, unsigned len)
1670 struct process* pcs;
1671 struct module* module;
1672 struct dhext_module_information dhmi;
1674 /* could be interpreted as a WinDbg extension */
1675 if (!dbghelp_opt_extension_api)
1677 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1678 return FALSE;
1681 TRACE("(%p %I64x %p %u\n", proc, base, wmi, len);
1683 if (!(pcs = process_find_by_handle(proc))) return FALSE;
1684 if (len > sizeof(*wmi)) return FALSE;
1686 module = module_find_by_addr(pcs, base);
1687 if (!module) return FALSE;
1689 dhmi.type = module->type;
1690 dhmi.is_virtual = module->is_virtual;
1691 dhmi.is_wine_builtin = module->is_wine_builtin;
1692 dhmi.has_file_image = module->has_file_image;
1693 dhmi.debug_format_bitmask = module->debug_format_bitmask;
1694 if ((module = module_get_container(pcs, module)))
1696 dhmi.debug_format_bitmask |= module->debug_format_bitmask;
1698 memcpy(wmi, &dhmi, len);
1700 return TRUE;