include/mscvpdb.h: Use flexible array members for the rest of structures.
[wine.git] / dlls / dbghelp / module.c
blobd06c4417be6eaae776f1e665d2fad053ed438204
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->alt_modulename = NULL;
214 module->module.ImageName[0] = '\0';
215 lstrcpynW(module->module.LoadedImageName, name, ARRAY_SIZE(module->module.LoadedImageName));
216 module->module.SymType = SymDeferred;
217 module->module.NumSyms = 0;
218 module->module.TimeDateStamp = stamp;
219 module->module.CheckSum = checksum;
221 memset(module->module.LoadedPdbName, 0, sizeof(module->module.LoadedPdbName));
222 module->module.CVSig = 0;
223 memset(module->module.CVData, 0, sizeof(module->module.CVData));
224 module->module.PdbSig = 0;
225 memset(&module->module.PdbSig70, 0, sizeof(module->module.PdbSig70));
226 module->module.PdbAge = 0;
227 module->module.PdbUnmatched = FALSE;
228 module->module.DbgUnmatched = FALSE;
229 module->module.LineNumbers = FALSE;
230 module->module.GlobalSymbols = FALSE;
231 module->module.TypeInfo = FALSE;
232 module->module.SourceIndexed = FALSE;
233 module->module.Publics = FALSE;
234 module->module.MachineType = machine;
235 module->module.Reserved = 0;
237 module->reloc_delta = 0;
238 module->type = type;
239 module->is_virtual = !!virtual;
240 module->is_wine_builtin = !!builtin;
241 module->has_file_image = TRUE;
243 for (i = 0; i < DFI_LAST; i++) module->format_info[i] = NULL;
244 module->sortlist_valid = FALSE;
245 module->sorttab_size = 0;
246 module->addr_sorttab = NULL;
247 module->num_sorttab = 0;
248 module->num_symbols = 0;
249 module->cpu = cpu_find(machine);
250 if (!module->cpu)
251 module->cpu = dbghelp_current_cpu;
252 module->debug_format_bitmask = 0;
254 vector_init(&module->vsymt, sizeof(struct symt*), 128);
255 vector_init(&module->vcustom_symt, sizeof(struct symt*), 16);
256 /* FIXME: this seems a bit too high (on a per module basis)
257 * need some statistics about this
259 hash_table_init(&module->pool, &module->ht_symbols, 4096);
260 hash_table_init(&module->pool, &module->ht_types, 4096);
261 vector_init(&module->vtypes, sizeof(struct symt*), 32);
263 module->sources_used = 0;
264 module->sources_alloc = 0;
265 module->sources = 0;
266 wine_rb_init(&module->sources_offsets_tree, source_rb_compare);
268 /* add top level symbol */
269 module->top = symt_new_module(module);
271 return module;
274 BOOL module_init_pair(struct module_pair* pair, HANDLE hProcess, DWORD64 addr)
276 if (!(pair->pcs = process_find_by_handle(hProcess))) return FALSE;
277 pair->requested = module_find_by_addr(pair->pcs, addr);
278 return module_get_debug(pair);
281 /***********************************************************************
282 * module_find_by_nameW
285 struct module* module_find_by_nameW(const struct process* pcs, const WCHAR* name)
287 struct module* module;
289 for (module = pcs->lmodules; module; module = module->next)
291 if (!wcsicmp(name, module->modulename)) return module;
292 if (module->alt_modulename && !wcsicmp(name, module->alt_modulename)) return module;
294 SetLastError(ERROR_INVALID_NAME);
295 return NULL;
298 struct module* module_find_by_nameA(const struct process* pcs, const char* name)
300 WCHAR wname[MAX_PATH];
302 MultiByteToWideChar(CP_ACP, 0, name, -1, wname, ARRAY_SIZE(wname));
303 return module_find_by_nameW(pcs, wname);
306 /***********************************************************************
307 * module_is_already_loaded
310 struct module* module_is_already_loaded(const struct process* pcs, const WCHAR* name)
312 struct module* module;
313 const WCHAR* filename;
315 /* first compare the loaded image name... */
316 for (module = pcs->lmodules; module; module = module->next)
318 if (!wcsicmp(name, module->module.LoadedImageName))
319 return module;
321 /* then compare the standard filenames (without the path) ... */
322 filename = get_filename(name, NULL);
323 for (module = pcs->lmodules; module; module = module->next)
325 if (!wcsicmp(filename, get_filename(module->module.LoadedImageName, NULL)))
326 return module;
328 SetLastError(ERROR_INVALID_NAME);
329 return NULL;
332 /***********************************************************************
333 * module_get_container
336 static struct module* module_get_container(const struct process* pcs,
337 const struct module* inner)
339 struct module* module;
341 for (module = pcs->lmodules; module; module = module->next)
343 if (module != inner &&
344 module->module.BaseOfImage <= inner->module.BaseOfImage &&
345 module->module.BaseOfImage + module->module.ImageSize >=
346 inner->module.BaseOfImage + inner->module.ImageSize)
347 return module;
349 return NULL;
352 /***********************************************************************
353 * module_get_containee
356 struct module* module_get_containee(const struct process* pcs, const struct module* outer)
358 struct module* module;
360 for (module = pcs->lmodules; module; module = module->next)
362 if (module != outer &&
363 outer->module.BaseOfImage <= module->module.BaseOfImage &&
364 outer->module.BaseOfImage + outer->module.ImageSize >=
365 module->module.BaseOfImage + module->module.ImageSize)
366 return module;
368 return NULL;
371 BOOL module_load_debug(struct module* module)
373 IMAGEHLP_DEFERRED_SYMBOL_LOADW64 idslW64;
375 /* if deferred, force loading */
376 if (module->module.SymType == SymDeferred)
378 BOOL ret;
380 if (module->is_virtual)
382 module->module.SymType = SymVirtual;
383 ret = TRUE;
385 else if (module->type == DMT_PE)
387 idslW64.SizeOfStruct = sizeof(idslW64);
388 idslW64.BaseOfImage = module->module.BaseOfImage;
389 idslW64.CheckSum = module->module.CheckSum;
390 idslW64.TimeDateStamp = module->module.TimeDateStamp;
391 memcpy(idslW64.FileName, module->module.ImageName,
392 sizeof(module->module.ImageName));
393 idslW64.Reparse = FALSE;
394 idslW64.hFile = INVALID_HANDLE_VALUE;
396 pcs_callback(module->process, CBA_DEFERRED_SYMBOL_LOAD_START, &idslW64);
397 ret = pe_load_debug_info(module->process, module);
398 pcs_callback(module->process,
399 ret ? CBA_DEFERRED_SYMBOL_LOAD_COMPLETE : CBA_DEFERRED_SYMBOL_LOAD_FAILURE,
400 &idslW64);
402 else ret = module->process->loader->load_debug_info(module->process, module);
404 if (!ret) module->module.SymType = SymNone;
405 assert(module->module.SymType != SymDeferred);
406 module->module.NumSyms = module->ht_symbols.num_elts;
407 return ret;
409 return TRUE;
412 /******************************************************************
413 * module_get_debug
415 * get the debug information from a module:
416 * - if the module's type is deferred, then force loading of debug info (and return
417 * the module itself)
418 * - if the module has no debug info and has an ELF container, then return the ELF
419 * container (and also force the ELF container's debug info loading if deferred)
421 BOOL module_get_debug(struct module_pair* pair)
423 if (!pair->requested) return FALSE;
424 /* for a PE builtin, always get info from container */
425 if (!(pair->effective = module_get_container(pair->pcs, pair->requested)))
426 pair->effective = pair->requested;
427 return module_load_debug(pair->effective);
430 /***********************************************************************
431 * module_find_by_addr
433 * either the addr where module is loaded, or any address inside the
434 * module
436 struct module* module_find_by_addr(const struct process* pcs, DWORD64 addr)
438 struct module* module;
440 for (module = pcs->lmodules; module; module = module->next)
442 if (module->type == DMT_PE && addr >= module->module.BaseOfImage &&
443 addr < module->module.BaseOfImage + module->module.ImageSize)
444 return module;
446 for (module = pcs->lmodules; module; module = module->next)
448 if ((module->type == DMT_ELF || module->type == DMT_MACHO) &&
449 addr >= module->module.BaseOfImage &&
450 addr < module->module.BaseOfImage + module->module.ImageSize)
451 return module;
453 SetLastError(ERROR_MOD_NOT_FOUND);
454 return module;
457 /******************************************************************
458 * module_is_container_loaded
460 * checks whether the native container, for a (supposed) PE builtin is
461 * already loaded
463 static BOOL module_is_container_loaded(const struct process* pcs,
464 const WCHAR* ImageName, DWORD64 base)
466 size_t len;
467 struct module* module;
468 PCWSTR filename, modname;
470 if (!base) return FALSE;
471 filename = get_filename(ImageName, NULL);
472 len = lstrlenW(filename);
474 for (module = pcs->lmodules; module; module = module->next)
476 if ((module->type == DMT_ELF || module->type == DMT_MACHO) &&
477 base >= module->module.BaseOfImage &&
478 base < module->module.BaseOfImage + module->module.ImageSize)
480 modname = get_filename(module->module.LoadedImageName, NULL);
481 if (!wcsnicmp(modname, filename, len) &&
482 !memcmp(modname + len, L".so", 3 * sizeof(WCHAR)))
484 return TRUE;
488 /* likely a native PE module */
489 WARN("Couldn't find container for %s\n", debugstr_w(ImageName));
490 return FALSE;
493 static BOOL image_check_debug_link_crc(const WCHAR* file, struct image_file_map* fmap, DWORD link_crc)
495 DWORD read_bytes;
496 HANDLE handle;
497 WCHAR *path;
498 WORD magic;
499 DWORD crc;
500 BOOL ret;
502 path = get_dos_file_name(file);
503 handle = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
504 heap_free(path);
505 if (handle == INVALID_HANDLE_VALUE) return FALSE;
507 crc = calc_crc32(handle);
508 if (crc != link_crc)
510 WARN("Bad CRC for file %s (got %08lx while expecting %08lx)\n", debugstr_w(file), crc, link_crc);
511 CloseHandle(handle);
512 return FALSE;
515 SetFilePointer(handle, 0, 0, FILE_BEGIN);
516 if (ReadFile(handle, &magic, sizeof(magic), &read_bytes, NULL) && magic == IMAGE_DOS_SIGNATURE)
517 ret = pe_map_file(handle, fmap);
518 else
519 ret = elf_map_handle(handle, fmap);
520 CloseHandle(handle);
521 return ret;
524 static BOOL image_check_debug_link_gnu_id(const WCHAR* file, struct image_file_map* fmap, const BYTE* id, unsigned idlen)
526 struct image_section_map buildid_sect;
527 DWORD read_bytes;
528 HANDLE handle;
529 WCHAR *path;
530 WORD magic;
531 BOOL ret;
533 path = get_dos_file_name(file);
534 handle = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
535 heap_free(path);
536 if (handle == INVALID_HANDLE_VALUE) return FALSE;
538 TRACE("Located debug information file at %s\n", debugstr_w(file));
540 if (ReadFile(handle, &magic, sizeof(magic), &read_bytes, NULL) && magic == IMAGE_DOS_SIGNATURE)
541 ret = pe_map_file(handle, fmap);
542 else
543 ret = elf_map_handle(handle, fmap);
544 CloseHandle(handle);
546 if (ret && image_find_section(fmap, ".note.gnu.build-id", &buildid_sect))
548 const UINT32* note;
550 note = (const UINT32*)image_map_section(&buildid_sect);
551 if (note != IMAGE_NO_MAP)
553 /* the usual ELF note structure: name-size desc-size type <name> <desc> */
554 if (note[2] == NOTE_GNU_BUILD_ID)
556 if (note[1] == idlen && !memcmp(note + 3 + ((note[0] + 3) >> 2), id, idlen))
557 return TRUE;
558 WARN("mismatch in buildid information for %s\n", wine_dbgstr_w(file));
561 image_unmap_section(&buildid_sect);
562 image_unmap_file(fmap);
564 return FALSE;
567 /******************************************************************
568 * image_locate_debug_link
570 * Locate a filename from a .gnu_debuglink section, using the same
571 * strategy as gdb:
572 * "If the full name of the directory containing the executable is
573 * execdir, and the executable has a debug link that specifies the
574 * name debugfile, then GDB will automatically search for the
575 * debugging information file in three places:
576 * - the directory containing the executable file (that is, it
577 * will look for a file named `execdir/debugfile',
578 * - a subdirectory of that directory named `.debug' (that is, the
579 * file `execdir/.debug/debugfile', and
580 * - a subdirectory of the global debug file directory that includes
581 * the executable's full path, and the name from the link (that is,
582 * the file `globaldebugdir/execdir/debugfile', where globaldebugdir
583 * is the global debug file directory, and execdir has been turned
584 * into a relative path)." (from GDB manual)
586 static struct image_file_map* image_locate_debug_link(const struct module* module, const char* filename, DWORD crc)
588 static const WCHAR globalDebugDirW[] = {'/','u','s','r','/','l','i','b','/','d','e','b','u','g','/'};
589 static const WCHAR dotDebugW[] = {'.','d','e','b','u','g','/'};
590 const size_t globalDebugDirLen = ARRAY_SIZE(globalDebugDirW);
591 size_t filename_len, path_len;
592 WCHAR* p = NULL;
593 WCHAR* slash;
594 WCHAR* slash2;
595 struct image_file_map* fmap_link = NULL;
597 fmap_link = HeapAlloc(GetProcessHeap(), 0, sizeof(*fmap_link));
598 if (!fmap_link) return NULL;
600 filename_len = MultiByteToWideChar(CP_UNIXCP, 0, filename, -1, NULL, 0);
601 path_len = lstrlenW(module->module.LoadedImageName);
602 if (module->real_path) path_len = max(path_len, lstrlenW(module->real_path));
603 p = HeapAlloc(GetProcessHeap(), 0,
604 (globalDebugDirLen + path_len + 6 + 1 + filename_len + 1) * sizeof(WCHAR));
605 if (!p) goto found;
607 /* we prebuild the string with "execdir" */
608 lstrcpyW(p, module->module.LoadedImageName);
609 slash = p;
610 if ((slash2 = wcsrchr(slash, '/'))) slash = slash2 + 1;
611 if ((slash2 = wcsrchr(slash, '\\'))) slash = slash2 + 1;
613 /* testing execdir/filename */
614 MultiByteToWideChar(CP_UNIXCP, 0, filename, -1, slash, filename_len);
615 if (image_check_debug_link_crc(p, fmap_link, crc)) goto found;
617 /* testing execdir/.debug/filename */
618 memcpy(slash, dotDebugW, sizeof(dotDebugW));
619 MultiByteToWideChar(CP_UNIXCP, 0, filename, -1, slash + ARRAY_SIZE(dotDebugW), filename_len);
620 if (image_check_debug_link_crc(p, fmap_link, crc)) goto found;
622 if (module->real_path)
624 lstrcpyW(p, module->real_path);
625 slash = p;
626 if ((slash2 = wcsrchr(slash, '/'))) slash = slash2 + 1;
627 if ((slash2 = wcsrchr(slash, '\\'))) slash = slash2 + 1;
628 MultiByteToWideChar(CP_UNIXCP, 0, filename, -1, slash, filename_len);
629 if (image_check_debug_link_crc(p, fmap_link, crc)) goto found;
632 /* testing globaldebugdir/execdir/filename */
633 memmove(p + globalDebugDirLen, p, (slash - p) * sizeof(WCHAR));
634 memcpy(p, globalDebugDirW, globalDebugDirLen * sizeof(WCHAR));
635 slash += globalDebugDirLen;
636 MultiByteToWideChar(CP_UNIXCP, 0, filename, -1, slash, filename_len);
637 if (image_check_debug_link_crc(p, fmap_link, crc)) goto found;
639 /* finally testing filename */
640 if (image_check_debug_link_crc(slash, fmap_link, crc)) goto found;
643 WARN("Couldn't locate or map %s\n", filename);
644 HeapFree(GetProcessHeap(), 0, p);
645 HeapFree(GetProcessHeap(), 0, fmap_link);
646 return NULL;
648 found:
649 TRACE("Located debug information file %s at %s\n", filename, debugstr_w(p));
650 HeapFree(GetProcessHeap(), 0, p);
651 return fmap_link;
654 static WCHAR* append_hex(WCHAR* dst, const BYTE* id, const BYTE* end)
656 while (id < end)
658 *dst++ = "0123456789abcdef"[*id >> 4 ];
659 *dst++ = "0123456789abcdef"[*id & 0x0F];
660 id++;
662 return dst;
665 /******************************************************************
666 * image_locate_build_id_target
668 * Try to find the .so file containing the debug info out of the build-id note information
670 static struct image_file_map* image_locate_build_id_target(const BYTE* id, unsigned idlen)
672 struct image_file_map* fmap_link = NULL;
673 DWORD sz;
674 WCHAR* p;
675 WCHAR* z;
677 fmap_link = HeapAlloc(GetProcessHeap(), 0, sizeof(*fmap_link));
678 if (!fmap_link) return NULL;
680 p = malloc(sizeof(L"/usr/lib/debug/.build-id/") +
681 (idlen * 2 + 1) * sizeof(WCHAR) + sizeof(L".debug"));
682 if (!p) goto fail;
683 wcscpy(p, L"/usr/lib/debug/.build-id/");
684 z = p + wcslen(p);
685 if (idlen)
687 z = append_hex(z, id, id + 1);
688 if (idlen > 1)
690 *z++ = L'/';
691 z = append_hex(z, id + 1, id + idlen);
694 wcscpy(z, L".debug");
695 TRACE("checking %s\n", wine_dbgstr_w(p));
697 if (image_check_debug_link_gnu_id(p, fmap_link, id, idlen))
699 free(p);
700 return fmap_link;
703 sz = GetEnvironmentVariableW(L"WINEHOMEDIR", NULL, 0);
704 if (sz)
706 z = realloc(p, sz * sizeof(WCHAR) +
707 sizeof(L"\\.cache\\debuginfod_client\\") +
708 idlen * 2 * sizeof(WCHAR) + sizeof(L"\\debuginfo") + 500);
709 if (!z) goto fail;
710 p = z;
711 GetEnvironmentVariableW(L"WINEHOMEDIR", p, sz);
712 z = p + sz - 1;
713 wcscpy(z, L"\\.cache\\debuginfod_client\\");
714 z += wcslen(z);
715 z = append_hex(z, id, id + idlen);
716 wcscpy(z, L"\\debuginfo");
717 TRACE("checking %ls\n", p);
718 if (image_check_debug_link_gnu_id(p, fmap_link, id, idlen))
720 free(p);
721 return fmap_link;
725 TRACE("not found\n");
726 fail:
727 free(p);
728 HeapFree(GetProcessHeap(), 0, fmap_link);
729 return NULL;
732 /******************************************************************
733 * image_load_debugaltlink
735 * Handle a (potential) .gnu_debugaltlink section and the link to
736 * (another) alternate debug file.
737 * Return an heap-allocated image_file_map when the section .gnu_debugaltlink is present,
738 * and a matching debug file has been located.
740 struct image_file_map* image_load_debugaltlink(struct image_file_map* fmap, struct module* module)
742 struct image_section_map debugaltlink_sect;
743 const char* data;
744 struct image_file_map* fmap_link = NULL;
745 BOOL ret = FALSE;
747 if (!image_find_section(fmap, ".gnu_debugaltlink", &debugaltlink_sect))
749 TRACE("No .gnu_debugaltlink section found for %s\n", debugstr_w(module->modulename));
750 return NULL;
753 data = image_map_section(&debugaltlink_sect);
754 if (data != IMAGE_NO_MAP)
756 unsigned sect_len;
757 const BYTE* id;
758 /* The content of the section is:
759 * + a \0 terminated string (filename)
760 * + followed by the build-id
761 * We try loading the dwz_alternate:
762 * - from the filename: either as absolute path, or relative to the embedded build-id
763 * - from the build-id
764 * In both cases, checking that found .so file matches the requested build-id
766 sect_len = image_get_map_size(&debugaltlink_sect);
767 id = memchr(data, '\0', sect_len);
768 if (id++)
770 unsigned idlen = (const BYTE*)data + sect_len - id;
771 fmap_link = HeapAlloc(GetProcessHeap(), 0, sizeof(*fmap_link));
772 if (fmap_link)
774 unsigned filename_len = MultiByteToWideChar(CP_UNIXCP, 0, data, -1, NULL, 0);
775 /* Trying absolute path */
776 WCHAR* dst = HeapAlloc(GetProcessHeap(), 0, filename_len * sizeof(WCHAR));
777 if (dst)
779 MultiByteToWideChar(CP_UNIXCP, 0, data, -1, dst, filename_len);
780 ret = image_check_debug_link_gnu_id(dst, fmap_link, id, idlen);
781 HeapFree(GetProcessHeap(), 0, dst);
783 /* Trying relative path to build-id directory */
784 if (!ret)
786 dst = HeapAlloc(GetProcessHeap(), 0,
787 sizeof(L"/usr/lib/debug/.build-id/") + (3 + filename_len + idlen * 2) * sizeof(WCHAR));
788 if (dst)
790 WCHAR* p;
792 /* SIGH....
793 * some relative links are relative to /usr/lib/debug/.build-id, some others are from the directory
794 * where the alternate file is...
795 * so try both
797 p = memcpy(dst, L"/usr/lib/debug/.build-id/", sizeof(L"/usr/lib/debug/.build-id/"));
798 p += wcslen(dst);
799 MultiByteToWideChar(CP_UNIXCP, 0, data, -1, p, filename_len);
800 ret = image_check_debug_link_gnu_id(dst, fmap_link, id, idlen);
801 if (!ret)
803 p = append_hex(p, id, id + idlen);
804 *p++ = '/';
805 MultiByteToWideChar(CP_UNIXCP, 0, data, -1, p, filename_len);
806 ret = image_check_debug_link_gnu_id(dst, fmap_link, id, idlen);
808 HeapFree(GetProcessHeap(), 0, dst);
811 if (!ret)
813 HeapFree(GetProcessHeap(), 0, fmap_link);
814 /* didn't work out with filename, try file lookup based on build-id */
815 if (!(fmap_link = image_locate_build_id_target(id, idlen)))
816 WARN("Couldn't find a match for .gnu_debugaltlink section %s for %s\n", data, debugstr_w(module->modulename));
821 image_unmap_section(&debugaltlink_sect);
822 if (fmap_link) TRACE("Found match .gnu_debugaltlink section for %s\n", debugstr_w(module->modulename));
823 return fmap_link;
826 /******************************************************************
827 * image_check_alternate
829 * Load alternate files for a given image file, looking at either .note.gnu_build-id
830 * or .gnu_debuglink sections.
832 BOOL image_check_alternate(struct image_file_map* fmap, const struct module* module)
834 struct image_section_map buildid_sect, debuglink_sect;
835 struct image_file_map* fmap_link = NULL;
837 /* if present, add the .gnu_debuglink file as an alternate to current one */
838 if (image_find_section(fmap, ".note.gnu.build-id", &buildid_sect))
840 const UINT32* note;
842 note = (const UINT32*)image_map_section(&buildid_sect);
843 if (note != IMAGE_NO_MAP)
845 /* the usual ELF note structure: name-size desc-size type <name> <desc> */
846 if (note[2] == NOTE_GNU_BUILD_ID)
848 fmap_link = image_locate_build_id_target((const BYTE*)(note + 3 + ((note[0] + 3) >> 2)), note[1]);
851 image_unmap_section(&buildid_sect);
853 /* if present, add the .gnu_debuglink file as an alternate to current one */
854 if (!fmap_link && image_find_section(fmap, ".gnu_debuglink", &debuglink_sect))
856 const char* dbg_link;
858 dbg_link = image_map_section(&debuglink_sect);
859 if (dbg_link != IMAGE_NO_MAP)
861 /* The content of a debug link section is:
862 * 1/ a NULL terminated string, containing the file name for the
863 * debug info
864 * 2/ padding on 4 byte boundary
865 * 3/ CRC of the linked file
867 DWORD crc = *(const DWORD*)(dbg_link + ((DWORD_PTR)(strlen(dbg_link) + 4) & ~3));
868 if (!(fmap_link = image_locate_debug_link(module, dbg_link, crc)))
869 WARN("Couldn't load linked debug file for %s\n", debugstr_w(module->modulename));
871 image_unmap_section(&debuglink_sect);
873 if (fmap_link)
875 fmap->alternate = fmap_link;
876 return TRUE;
878 return FALSE;
881 /***********************************************************************
882 * SymLoadModule (DBGHELP.@)
884 DWORD WINAPI SymLoadModule(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
885 PCSTR ModuleName, DWORD BaseOfDll, DWORD SizeOfDll)
887 return SymLoadModuleEx(hProcess, hFile, ImageName, ModuleName, BaseOfDll,
888 SizeOfDll, NULL, 0);
891 /***********************************************************************
892 * SymLoadModuleEx (DBGHELP.@)
894 DWORD64 WINAPI SymLoadModuleEx(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
895 PCSTR ModuleName, DWORD64 BaseOfDll, DWORD DllSize,
896 PMODLOAD_DATA Data, DWORD Flags)
898 PWSTR wImageName, wModuleName;
899 unsigned len;
900 DWORD64 ret;
902 TRACE("(%p %p %s %s %I64x %08lx %p %08lx)\n",
903 hProcess, hFile, debugstr_a(ImageName), debugstr_a(ModuleName),
904 BaseOfDll, DllSize, Data, Flags);
906 if (ImageName)
908 len = MultiByteToWideChar(CP_ACP, 0, ImageName, -1, NULL, 0);
909 wImageName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
910 MultiByteToWideChar(CP_ACP, 0, ImageName, -1, wImageName, len);
912 else wImageName = NULL;
913 if (ModuleName)
915 len = MultiByteToWideChar(CP_ACP, 0, ModuleName, -1, NULL, 0);
916 wModuleName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
917 MultiByteToWideChar(CP_ACP, 0, ModuleName, -1, wModuleName, len);
919 else wModuleName = NULL;
921 ret = SymLoadModuleExW(hProcess, hFile, wImageName, wModuleName,
922 BaseOfDll, DllSize, Data, Flags);
923 HeapFree(GetProcessHeap(), 0, wImageName);
924 HeapFree(GetProcessHeap(), 0, wModuleName);
925 return ret;
928 /***********************************************************************
929 * SymLoadModuleExW (DBGHELP.@)
931 DWORD64 WINAPI SymLoadModuleExW(HANDLE hProcess, HANDLE hFile, PCWSTR wImageName,
932 PCWSTR wModuleName, DWORD64 BaseOfDll, DWORD SizeOfDll,
933 PMODLOAD_DATA Data, DWORD Flags)
935 struct process* pcs;
936 struct module* module = NULL;
937 struct module* altmodule;
939 TRACE("(%p %p %s %s %I64x %08lx %p %08lx)\n",
940 hProcess, hFile, debugstr_w(wImageName), debugstr_w(wModuleName),
941 BaseOfDll, SizeOfDll, Data, Flags);
943 if (Data)
944 FIXME("Unsupported load data parameter %p for %s\n",
945 Data, debugstr_w(wImageName));
947 if (!(pcs = process_find_by_handle(hProcess))) return 0;
949 if (Flags & ~(SLMFLAG_VIRTUAL | SLMFLAG_NO_SYMBOLS))
950 FIXME("Unsupported Flags %08lx for %s\n", Flags, debugstr_w(wImageName));
952 /* Trying to load a new module at the same address of an existing one,
953 * native simply keeps the old one in place.
955 if (BaseOfDll)
956 for (altmodule = pcs->lmodules; altmodule; altmodule = altmodule->next)
958 if (altmodule->type == DMT_PE && BaseOfDll == altmodule->module.BaseOfImage)
960 SetLastError(ERROR_SUCCESS);
961 return 0;
965 /* this is a Wine extension to the API just to redo the synchronisation */
966 if (!wImageName && !hFile && !Flags)
968 pcs->loader->synchronize_module_list(pcs);
969 return 0;
972 if (Flags & SLMFLAG_VIRTUAL)
974 if (!wImageName) wImageName = L"";
975 module = module_new(pcs, wImageName, DMT_PE, FALSE, TRUE, BaseOfDll, SizeOfDll, 0, 0, IMAGE_FILE_MACHINE_UNKNOWN);
976 if (!module) return 0;
978 else
980 /* try PE image */
981 module = pe_load_native_module(pcs, wImageName, hFile, BaseOfDll, SizeOfDll);
982 if (!module && wImageName)
984 /* It could be either a dll.so file (for which we need the corresponding
985 * system module) or a system module.
986 * In both cases, ensure system module list is up-to-date.
988 pcs->loader->synchronize_module_list(pcs);
989 if (module_is_container_loaded(pcs, wImageName, BaseOfDll))
990 module = pe_load_builtin_module(pcs, wImageName, BaseOfDll, SizeOfDll);
991 /* at last, try ELF or Mach-O module */
992 if (!module)
993 module = pcs->loader->load_module(pcs, wImageName, BaseOfDll);
995 if (!module)
997 WARN("Couldn't locate %s\n", debugstr_w(wImageName));
998 SetLastError(ERROR_NO_MORE_FILES);
999 return 0;
1002 if (Flags & SLMFLAG_NO_SYMBOLS) module->dont_load_symbols = 1;
1004 /* Store alternate name for module when provided. */
1005 if (wModuleName)
1006 module->alt_modulename = pool_wcsdup(&module->pool, wModuleName);
1007 if (wImageName)
1008 lstrcpynW(module->module.ImageName, wImageName, ARRAY_SIZE(module->module.ImageName));
1010 for (altmodule = pcs->lmodules; altmodule; altmodule = altmodule->next)
1012 if (altmodule != module && altmodule->type == module->type &&
1013 module->module.BaseOfImage >= altmodule->module.BaseOfImage &&
1014 module->module.BaseOfImage < altmodule->module.BaseOfImage + altmodule->module.ImageSize)
1015 break;
1017 if (altmodule)
1019 /* We have a conflict as the new module cannot be found by its base address
1020 * (it's hidden by altmodule).
1021 * We need to decide which one the two modules we need to get rid of.
1023 /* loading same module at same address... we can only get here when BaseOfDll is 0 */
1024 if (module->module.BaseOfImage == altmodule->module.BaseOfImage)
1026 module_remove(pcs, module);
1027 SetLastError(ERROR_INVALID_ADDRESS);
1028 return 0;
1030 /* replace old module with new one */
1031 WARN("Replace module %ls at %I64x by module %ls at %I64x\n",
1032 altmodule->module.ImageName, altmodule->module.BaseOfImage,
1033 module->module.ImageName, module->module.BaseOfImage);
1034 module_remove(pcs, altmodule);
1037 if ((dbghelp_options & SYMOPT_DEFERRED_LOADS) == 0 && !module_get_container(pcs, module))
1038 module_load_debug(module);
1039 return module->module.BaseOfImage;
1042 /***********************************************************************
1043 * SymLoadModule64 (DBGHELP.@)
1045 DWORD64 WINAPI SymLoadModule64(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
1046 PCSTR ModuleName, DWORD64 BaseOfDll, DWORD SizeOfDll)
1048 return SymLoadModuleEx(hProcess, hFile, ImageName, ModuleName, BaseOfDll, SizeOfDll,
1049 NULL, 0);
1052 /******************************************************************
1053 * module_remove
1056 BOOL module_remove(struct process* pcs, struct module* module)
1058 struct module_format*modfmt;
1059 struct module** p;
1060 unsigned i;
1062 TRACE("%s (%p)\n", debugstr_w(module->modulename), module);
1064 /* remove local scope if symbol is from this module */
1065 if (pcs->localscope_symt)
1067 struct symt* locsym = pcs->localscope_symt;
1068 if (symt_check_tag(locsym, SymTagInlineSite))
1069 locsym = &symt_get_function_from_inlined((struct symt_function*)locsym)->symt;
1070 if (symt_check_tag(locsym, SymTagFunction))
1072 locsym = ((struct symt_function*)locsym)->container;
1073 if (symt_check_tag(locsym, SymTagCompiland) &&
1074 module == ((struct symt_compiland*)locsym)->container->module)
1076 pcs->localscope_pc = 0;
1077 pcs->localscope_symt = NULL;
1081 for (i = 0; i < DFI_LAST; i++)
1083 if ((modfmt = module->format_info[i]) && modfmt->remove)
1084 modfmt->remove(pcs, module->format_info[i]);
1086 hash_table_destroy(&module->ht_symbols);
1087 hash_table_destroy(&module->ht_types);
1088 HeapFree(GetProcessHeap(), 0, module->sources);
1089 HeapFree(GetProcessHeap(), 0, module->addr_sorttab);
1090 pool_destroy(&module->pool);
1091 /* native dbghelp doesn't invoke registered callback(,CBA_SYMBOLS_UNLOADED,) here
1092 * so do we
1094 for (p = &pcs->lmodules; *p; p = &(*p)->next)
1096 if (*p == module)
1098 *p = module->next;
1099 HeapFree(GetProcessHeap(), 0, module);
1100 return TRUE;
1103 FIXME("This shouldn't happen\n");
1104 return FALSE;
1107 /******************************************************************
1108 * SymUnloadModule (DBGHELP.@)
1111 BOOL WINAPI SymUnloadModule(HANDLE hProcess, DWORD BaseOfDll)
1113 return SymUnloadModule64(hProcess, BaseOfDll);
1116 /******************************************************************
1117 * SymUnloadModule64 (DBGHELP.@)
1120 BOOL WINAPI SymUnloadModule64(HANDLE hProcess, DWORD64 BaseOfDll)
1122 struct process* pcs;
1123 struct module* module;
1125 pcs = process_find_by_handle(hProcess);
1126 if (!pcs) return FALSE;
1127 module = module_find_by_addr(pcs, BaseOfDll);
1128 if (!module) return FALSE;
1129 module_remove(pcs, module);
1130 return TRUE;
1133 /******************************************************************
1134 * SymEnumerateModules (DBGHELP.@)
1137 struct enum_modW64_32
1139 PSYM_ENUMMODULES_CALLBACK cb;
1140 PVOID user;
1141 char module[MAX_PATH];
1144 static BOOL CALLBACK enum_modW64_32(PCWSTR name, DWORD64 base, PVOID user)
1146 struct enum_modW64_32* x = user;
1148 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
1149 return x->cb(x->module, (DWORD)base, x->user);
1152 BOOL WINAPI SymEnumerateModules(HANDLE hProcess,
1153 PSYM_ENUMMODULES_CALLBACK EnumModulesCallback,
1154 PVOID UserContext)
1156 struct enum_modW64_32 x;
1158 x.cb = EnumModulesCallback;
1159 x.user = UserContext;
1161 return SymEnumerateModulesW64(hProcess, enum_modW64_32, &x);
1164 /******************************************************************
1165 * SymEnumerateModules64 (DBGHELP.@)
1168 struct enum_modW64_64
1170 PSYM_ENUMMODULES_CALLBACK64 cb;
1171 PVOID user;
1172 char module[MAX_PATH];
1175 static BOOL CALLBACK enum_modW64_64(PCWSTR name, DWORD64 base, PVOID user)
1177 struct enum_modW64_64* x = user;
1179 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
1180 return x->cb(x->module, base, x->user);
1183 BOOL WINAPI SymEnumerateModules64(HANDLE hProcess,
1184 PSYM_ENUMMODULES_CALLBACK64 EnumModulesCallback,
1185 PVOID UserContext)
1187 struct enum_modW64_64 x;
1189 x.cb = EnumModulesCallback;
1190 x.user = UserContext;
1192 return SymEnumerateModulesW64(hProcess, enum_modW64_64, &x);
1195 /******************************************************************
1196 * SymEnumerateModulesW64 (DBGHELP.@)
1199 BOOL WINAPI SymEnumerateModulesW64(HANDLE hProcess,
1200 PSYM_ENUMMODULES_CALLBACKW64 EnumModulesCallback,
1201 PVOID UserContext)
1203 struct process* pcs = process_find_by_handle(hProcess);
1204 struct module* module;
1206 if (!pcs) return FALSE;
1208 for (module = pcs->lmodules; module; module = module->next)
1210 if (!dbghelp_opt_native &&
1211 (module->type == DMT_ELF || module->type == DMT_MACHO))
1212 continue;
1213 if (!EnumModulesCallback(module->modulename,
1214 module->module.BaseOfImage, UserContext))
1215 break;
1217 return TRUE;
1220 /******************************************************************
1221 * EnumerateLoadedModules64 (DBGHELP.@)
1224 struct enum_load_modW64_64
1226 PENUMLOADED_MODULES_CALLBACK64 cb;
1227 PVOID user;
1228 char module[MAX_PATH];
1231 static BOOL CALLBACK enum_load_modW64_64(PCWSTR name, DWORD64 base, ULONG size,
1232 PVOID user)
1234 struct enum_load_modW64_64* x = user;
1236 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
1237 return x->cb(x->module, base, size, x->user);
1240 BOOL WINAPI EnumerateLoadedModules64(HANDLE hProcess,
1241 PENUMLOADED_MODULES_CALLBACK64 EnumLoadedModulesCallback,
1242 PVOID UserContext)
1244 struct enum_load_modW64_64 x;
1246 x.cb = EnumLoadedModulesCallback;
1247 x.user = UserContext;
1249 return EnumerateLoadedModulesW64(hProcess, enum_load_modW64_64, &x);
1252 /******************************************************************
1253 * EnumerateLoadedModules (DBGHELP.@)
1256 struct enum_load_modW64_32
1258 PENUMLOADED_MODULES_CALLBACK cb;
1259 PVOID user;
1260 char module[MAX_PATH];
1263 static BOOL CALLBACK enum_load_modW64_32(PCWSTR name, DWORD64 base, ULONG size,
1264 PVOID user)
1266 struct enum_load_modW64_32* x = user;
1267 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
1268 return x->cb(x->module, (DWORD)base, size, x->user);
1271 BOOL WINAPI EnumerateLoadedModules(HANDLE hProcess,
1272 PENUMLOADED_MODULES_CALLBACK EnumLoadedModulesCallback,
1273 PVOID UserContext)
1275 struct enum_load_modW64_32 x;
1277 x.cb = EnumLoadedModulesCallback;
1278 x.user = UserContext;
1280 return EnumerateLoadedModulesW64(hProcess, enum_load_modW64_32, &x);
1283 static unsigned int load_and_grow_modules(HANDLE process, HMODULE** hmods, unsigned start, unsigned* alloc, DWORD filter)
1285 DWORD needed;
1286 BOOL ret;
1288 while ((ret = EnumProcessModulesEx(process, *hmods + start, (*alloc - start) * sizeof(HMODULE),
1289 &needed, filter)) &&
1290 needed > (*alloc - start) * sizeof(HMODULE))
1292 HMODULE* new = HeapReAlloc(GetProcessHeap(), 0, *hmods, (*alloc) * 2 * sizeof(HMODULE));
1293 if (!new) return 0;
1294 *hmods = new;
1295 *alloc *= 2;
1297 return ret ? needed / sizeof(HMODULE) : 0;
1300 /******************************************************************
1301 * EnumerateLoadedModulesW64 (DBGHELP.@)
1304 BOOL WINAPI EnumerateLoadedModulesW64(HANDLE process,
1305 PENUMLOADED_MODULES_CALLBACKW64 enum_cb,
1306 PVOID user)
1308 HMODULE* hmods;
1309 unsigned alloc = 256, count, count32, i;
1310 USHORT pcs_machine, native_machine;
1311 BOOL with_32bit_modules;
1312 WCHAR imagenameW[MAX_PATH];
1313 MODULEINFO mi;
1314 WCHAR* sysdir = NULL;
1315 WCHAR* wowdir = NULL;
1316 size_t sysdir_len = 0, wowdir_len = 0;
1318 /* process might not be a handle to a live process */
1319 if (!IsWow64Process2(process, &pcs_machine, &native_machine)) return FALSE;
1320 with_32bit_modules = sizeof(void*) > sizeof(int) &&
1321 pcs_machine != IMAGE_FILE_MACHINE_UNKNOWN &&
1322 (dbghelp_options & SYMOPT_INCLUDE_32BIT_MODULES);
1324 if (!(hmods = HeapAlloc(GetProcessHeap(), 0, alloc * sizeof(hmods[0]))))
1325 return FALSE;
1327 /* Note:
1328 * - we report modules returned from kernelbase.EnumProcessModulesEx
1329 * - appending 32bit modules when possible and requested
1331 * When considering 32bit modules in a wow64 child process, required from
1332 * a 64bit process:
1333 * - native returns from kernelbase.EnumProcessModulesEx
1334 * redirected paths (that is in system32 directory), while
1335 * dbghelp.EnumerateLoadedModulesWine returns the effective path
1336 * (eg. syswow64 for x86_64).
1337 * - (Except for the main module, if gotten from syswow64, where kernelbase
1338 * will return the effective path)
1339 * - Wine kernelbase (and ntdll) incorrectly return these modules from
1340 * syswow64 (except for ntdll which is returned from system32).
1341 * => for these modules, always perform a system32 => syswow64 path
1342 * conversion (it'll work even if ntdll/kernelbase is fixed).
1344 if ((count = load_and_grow_modules(process, &hmods, 0, &alloc, LIST_MODULES_DEFAULT)) && with_32bit_modules)
1346 /* append 32bit modules when required */
1347 if ((count32 = load_and_grow_modules(process, &hmods, count, &alloc, LIST_MODULES_32BIT)))
1349 sysdir_len = GetSystemDirectoryW(NULL, 0);
1350 wowdir_len = GetSystemWow64Directory2W(NULL, 0, pcs_machine);
1352 if (!sysdir_len || !wowdir_len ||
1353 !(sysdir = HeapAlloc(GetProcessHeap(), 0, (sysdir_len + 1 + wowdir_len + 1) * sizeof(WCHAR))))
1355 HeapFree(GetProcessHeap(), 0, hmods);
1356 return FALSE;
1358 wowdir = sysdir + sysdir_len + 1;
1359 if (GetSystemDirectoryW(sysdir, sysdir_len) >= sysdir_len)
1360 FIXME("shouldn't happen\n");
1361 if (GetSystemWow64Directory2W(wowdir, wowdir_len, pcs_machine) >= wowdir_len)
1362 FIXME("shouldn't happen\n");
1363 wcscat(sysdir, L"\\");
1364 wcscat(wowdir, L"\\");
1367 else count32 = 0;
1369 for (i = 0; i < count + count32; i++)
1371 if (GetModuleInformation(process, hmods[i], &mi, sizeof(mi)) &&
1372 GetModuleFileNameExW(process, hmods[i], imagenameW, ARRAY_SIZE(imagenameW)))
1374 /* rewrite path in system32 into syswow64 for 32bit modules */
1375 if (i >= count)
1377 size_t len = wcslen(imagenameW);
1379 if (!wcsnicmp(imagenameW, sysdir, sysdir_len) &&
1380 (len - sysdir_len + wowdir_len) + 1 <= ARRAY_SIZE(imagenameW))
1382 memmove(&imagenameW[wowdir_len], &imagenameW[sysdir_len], (len - sysdir_len) * sizeof(WCHAR));
1383 memcpy(imagenameW, wowdir, wowdir_len * sizeof(WCHAR));
1386 if (!enum_cb(imagenameW, (DWORD_PTR)mi.lpBaseOfDll, mi.SizeOfImage, user))
1387 break;
1391 HeapFree(GetProcessHeap(), 0, hmods);
1392 HeapFree(GetProcessHeap(), 0, sysdir);
1394 return count != 0;
1397 static void dbghelp_str_WtoA(const WCHAR *src, char *dst, int dst_len)
1399 WideCharToMultiByte(CP_ACP, 0, src, -1, dst, dst_len - 1, NULL, NULL);
1400 dst[dst_len - 1] = 0;
1403 /******************************************************************
1404 * SymGetModuleInfo (DBGHELP.@)
1407 BOOL WINAPI SymGetModuleInfo(HANDLE hProcess, DWORD dwAddr,
1408 PIMAGEHLP_MODULE ModuleInfo)
1410 IMAGEHLP_MODULE mi;
1411 IMAGEHLP_MODULEW64 miw64;
1413 if (sizeof(mi) < ModuleInfo->SizeOfStruct) FIXME("Wrong size\n");
1415 miw64.SizeOfStruct = sizeof(miw64);
1416 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
1418 mi.SizeOfStruct = ModuleInfo->SizeOfStruct;
1419 mi.BaseOfImage = miw64.BaseOfImage;
1420 mi.ImageSize = miw64.ImageSize;
1421 mi.TimeDateStamp = miw64.TimeDateStamp;
1422 mi.CheckSum = miw64.CheckSum;
1423 mi.NumSyms = miw64.NumSyms;
1424 mi.SymType = miw64.SymType;
1425 dbghelp_str_WtoA(miw64.ModuleName, mi.ModuleName, sizeof(mi.ModuleName));
1426 dbghelp_str_WtoA(miw64.ImageName, mi.ImageName, sizeof(mi.ImageName));
1427 dbghelp_str_WtoA(miw64.LoadedImageName, mi.LoadedImageName, sizeof(mi.LoadedImageName));
1429 memcpy(ModuleInfo, &mi, ModuleInfo->SizeOfStruct);
1431 return TRUE;
1434 /******************************************************************
1435 * SymGetModuleInfoW (DBGHELP.@)
1438 BOOL WINAPI SymGetModuleInfoW(HANDLE hProcess, DWORD dwAddr,
1439 PIMAGEHLP_MODULEW ModuleInfo)
1441 IMAGEHLP_MODULEW64 miw64;
1442 IMAGEHLP_MODULEW miw;
1444 if (sizeof(miw) < ModuleInfo->SizeOfStruct) FIXME("Wrong size\n");
1446 miw64.SizeOfStruct = sizeof(miw64);
1447 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
1449 miw.SizeOfStruct = ModuleInfo->SizeOfStruct;
1450 miw.BaseOfImage = miw64.BaseOfImage;
1451 miw.ImageSize = miw64.ImageSize;
1452 miw.TimeDateStamp = miw64.TimeDateStamp;
1453 miw.CheckSum = miw64.CheckSum;
1454 miw.NumSyms = miw64.NumSyms;
1455 miw.SymType = miw64.SymType;
1456 lstrcpyW(miw.ModuleName, miw64.ModuleName);
1457 lstrcpyW(miw.ImageName, miw64.ImageName);
1458 lstrcpyW(miw.LoadedImageName, miw64.LoadedImageName);
1459 memcpy(ModuleInfo, &miw, ModuleInfo->SizeOfStruct);
1461 return TRUE;
1464 /******************************************************************
1465 * SymGetModuleInfo64 (DBGHELP.@)
1468 BOOL WINAPI SymGetModuleInfo64(HANDLE hProcess, DWORD64 dwAddr,
1469 PIMAGEHLP_MODULE64 ModuleInfo)
1471 IMAGEHLP_MODULE64 mi64;
1472 IMAGEHLP_MODULEW64 miw64;
1474 if (sizeof(mi64) < ModuleInfo->SizeOfStruct)
1476 SetLastError(ERROR_MOD_NOT_FOUND); /* NOTE: native returns this error */
1477 WARN("Wrong size %lu\n", ModuleInfo->SizeOfStruct);
1478 return FALSE;
1481 miw64.SizeOfStruct = sizeof(miw64);
1482 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
1484 mi64.SizeOfStruct = ModuleInfo->SizeOfStruct;
1485 mi64.BaseOfImage = miw64.BaseOfImage;
1486 mi64.ImageSize = miw64.ImageSize;
1487 mi64.TimeDateStamp = miw64.TimeDateStamp;
1488 mi64.CheckSum = miw64.CheckSum;
1489 mi64.NumSyms = miw64.NumSyms;
1490 mi64.SymType = miw64.SymType;
1491 dbghelp_str_WtoA(miw64.ModuleName, mi64.ModuleName, sizeof(mi64.ModuleName));
1492 dbghelp_str_WtoA(miw64.ImageName, mi64.ImageName, sizeof(mi64.ImageName));
1493 dbghelp_str_WtoA(miw64.LoadedImageName, mi64.LoadedImageName, sizeof(mi64.LoadedImageName));
1494 dbghelp_str_WtoA(miw64.LoadedPdbName, mi64.LoadedPdbName, sizeof(mi64.LoadedPdbName));
1496 mi64.CVSig = miw64.CVSig;
1497 dbghelp_str_WtoA(miw64.CVData, mi64.CVData, sizeof(mi64.CVData));
1498 mi64.PdbSig = miw64.PdbSig;
1499 mi64.PdbSig70 = miw64.PdbSig70;
1500 mi64.PdbAge = miw64.PdbAge;
1501 mi64.PdbUnmatched = miw64.PdbUnmatched;
1502 mi64.DbgUnmatched = miw64.DbgUnmatched;
1503 mi64.LineNumbers = miw64.LineNumbers;
1504 mi64.GlobalSymbols = miw64.GlobalSymbols;
1505 mi64.TypeInfo = miw64.TypeInfo;
1506 mi64.SourceIndexed = miw64.SourceIndexed;
1507 mi64.Publics = miw64.Publics;
1508 mi64.MachineType = miw64.MachineType;
1509 mi64.Reserved = miw64.Reserved;
1511 memcpy(ModuleInfo, &mi64, ModuleInfo->SizeOfStruct);
1513 return TRUE;
1516 /******************************************************************
1517 * SymGetModuleInfoW64 (DBGHELP.@)
1520 BOOL WINAPI SymGetModuleInfoW64(HANDLE hProcess, DWORD64 dwAddr,
1521 PIMAGEHLP_MODULEW64 ModuleInfo)
1523 struct process* pcs = process_find_by_handle(hProcess);
1524 struct module* module;
1525 IMAGEHLP_MODULEW64 miw64;
1527 TRACE("%p %I64x %p\n", hProcess, dwAddr, ModuleInfo);
1529 if (!pcs) return FALSE;
1530 if (ModuleInfo->SizeOfStruct > sizeof(*ModuleInfo)) return FALSE;
1531 module = module_find_by_addr(pcs, dwAddr);
1532 if (!module) return FALSE;
1534 miw64 = module->module;
1536 if (dbghelp_opt_real_path && module->real_path)
1537 lstrcpynW(miw64.LoadedImageName, module->real_path, ARRAY_SIZE(miw64.LoadedImageName));
1538 else if (miw64.SymType == SymDeferred)
1540 miw64.LoadedImageName[0] = '\0';
1541 miw64.TimeDateStamp = 0;
1544 /* update debug information from container if any */
1545 if (module->module.SymType == SymNone)
1547 module = module_get_container(pcs, module);
1548 if (module && module->module.SymType != SymNone)
1550 miw64.SymType = module->module.SymType;
1551 miw64.NumSyms = module->module.NumSyms;
1554 memcpy(ModuleInfo, &miw64, ModuleInfo->SizeOfStruct);
1555 return TRUE;
1558 /***********************************************************************
1559 * SymGetModuleBase (DBGHELP.@)
1561 DWORD WINAPI SymGetModuleBase(HANDLE hProcess, DWORD dwAddr)
1563 return (DWORD)SymGetModuleBase64(hProcess, dwAddr);
1566 /***********************************************************************
1567 * SymGetModuleBase64 (DBGHELP.@)
1569 DWORD64 WINAPI SymGetModuleBase64(HANDLE hProcess, DWORD64 dwAddr)
1571 struct process* pcs = process_find_by_handle(hProcess);
1572 struct module* module;
1574 if (!pcs) return 0;
1575 module = module_find_by_addr(pcs, dwAddr);
1576 if (!module) return 0;
1577 return module->module.BaseOfImage;
1580 /******************************************************************
1581 * module_reset_debug_info
1582 * Removes any debug information linked to a given module.
1584 void module_reset_debug_info(struct module* module)
1586 module->sortlist_valid = TRUE;
1587 module->sorttab_size = 0;
1588 module->addr_sorttab = NULL;
1589 module->num_sorttab = module->num_symbols = 0;
1590 hash_table_destroy(&module->ht_symbols);
1591 module->ht_symbols.num_buckets = 0;
1592 module->ht_symbols.buckets = NULL;
1593 hash_table_destroy(&module->ht_types);
1594 module->ht_types.num_buckets = 0;
1595 module->ht_types.buckets = NULL;
1596 module->vtypes.num_elts = 0;
1597 hash_table_destroy(&module->ht_symbols);
1598 module->sources_used = module->sources_alloc = 0;
1599 module->sources = NULL;
1602 /******************************************************************
1603 * SymRefreshModuleList (DBGHELP.@)
1605 BOOL WINAPI SymRefreshModuleList(HANDLE hProcess)
1607 struct process* pcs;
1609 TRACE("(%p)\n", hProcess);
1611 if (!(pcs = process_find_by_handle(hProcess))) return FALSE;
1613 return pcs->loader->synchronize_module_list(pcs);
1616 /***********************************************************************
1617 * SymFunctionTableAccess (DBGHELP.@)
1619 PVOID WINAPI SymFunctionTableAccess(HANDLE hProcess, DWORD AddrBase)
1621 return SymFunctionTableAccess64(hProcess, AddrBase);
1624 /***********************************************************************
1625 * SymFunctionTableAccess64 (DBGHELP.@)
1627 PVOID WINAPI SymFunctionTableAccess64(HANDLE hProcess, DWORD64 AddrBase)
1629 struct process* pcs = process_find_by_handle(hProcess);
1630 struct module* module;
1632 if (!pcs) return NULL;
1633 module = module_find_by_addr(pcs, AddrBase);
1634 if (!module || !module->cpu->find_runtime_function) return NULL;
1636 return module->cpu->find_runtime_function(module, AddrBase);
1639 static struct module* native_load_module(struct process* pcs, const WCHAR* name, ULONG_PTR addr)
1641 return NULL;
1644 static BOOL native_load_debug_info(struct process* process, struct module* module)
1646 module->module.SymType = SymNone;
1647 return FALSE;
1650 static BOOL native_fetch_file_info(struct process* process, const WCHAR* name, ULONG_PTR load_addr, DWORD_PTR* base,
1651 DWORD* size, DWORD* checksum)
1653 return FALSE;
1656 static BOOL noloader_synchronize_module_list(struct process* pcs)
1658 return FALSE;
1661 static BOOL noloader_enum_modules(struct process *process, enum_modules_cb cb, void* user)
1663 return FALSE;
1666 static BOOL empty_synchronize_module_list(struct process* pcs)
1668 return TRUE;
1671 static BOOL empty_enum_modules(struct process *process, enum_modules_cb cb, void* user)
1673 return TRUE;
1676 /* to be used when debuggee isn't a live target */
1677 const struct loader_ops no_loader_ops =
1679 noloader_synchronize_module_list,
1680 native_load_module,
1681 native_load_debug_info,
1682 noloader_enum_modules,
1683 native_fetch_file_info,
1686 /* to be used when debuggee is a live target, but which system information isn't available */
1687 const struct loader_ops empty_loader_ops =
1689 empty_synchronize_module_list,
1690 native_load_module,
1691 native_load_debug_info,
1692 empty_enum_modules,
1693 native_fetch_file_info,
1696 BOOL WINAPI wine_get_module_information(HANDLE proc, DWORD64 base, struct dhext_module_information* wmi, unsigned len)
1698 struct process* pcs;
1699 struct module* module;
1700 struct dhext_module_information dhmi;
1702 /* could be interpreted as a WinDbg extension */
1703 if (!dbghelp_opt_extension_api)
1705 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1706 return FALSE;
1709 TRACE("(%p %I64x %p %u\n", proc, base, wmi, len);
1711 if (!(pcs = process_find_by_handle(proc))) return FALSE;
1712 if (len > sizeof(*wmi)) return FALSE;
1714 module = module_find_by_addr(pcs, base);
1715 if (!module) return FALSE;
1717 dhmi.type = module->type;
1718 dhmi.is_virtual = module->is_virtual;
1719 dhmi.is_wine_builtin = module->is_wine_builtin;
1720 dhmi.has_file_image = module->has_file_image;
1721 dhmi.debug_format_bitmask = module->debug_format_bitmask;
1722 if ((module = module_get_container(pcs, module)))
1724 dhmi.debug_format_bitmask |= module->debug_format_bitmask;
1726 memcpy(wmi, &dhmi, len);
1728 return TRUE;