dbghelp: Attach a struct cpu* to every module.
[wine.git] / dlls / dbghelp / module.c
blob65bdbb38fdafa59962017adf8bd109867f012c5b
1 /*
2 * File module.c - module handling for the wine debugger
4 * Copyright (C) 1993, Eric Youngdale.
5 * 2000-2007, Eric Pouech
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <assert.h>
27 #include "dbghelp_private.h"
28 #include "image_private.h"
29 #include "psapi.h"
30 #include "winternl.h"
31 #include "wine/debug.h"
32 #include "wine/heap.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
36 #define NOTE_GNU_BUILD_ID 3
38 const WCHAR S_ElfW[] = L"<elf>";
39 const WCHAR S_WineLoaderW[] = L"<wine-loader>";
40 static const WCHAR * const ext[] = {L".acm", L".dll", L".drv", L".exe", L".ocx", L".vxd", NULL};
42 static int match_ext(const WCHAR* ptr, size_t len)
44 const WCHAR* const *e;
45 size_t l;
47 for (e = ext; *e; e++)
49 l = lstrlenW(*e);
50 if (l >= len) return 0;
51 if (wcsnicmp(&ptr[len - l], *e, l)) continue;
52 return l;
54 return 0;
57 static const WCHAR* get_filename(const WCHAR* name, const WCHAR* endptr)
59 const WCHAR* ptr;
61 if (!endptr) endptr = name + lstrlenW(name);
62 for (ptr = endptr - 1; ptr >= name; ptr--)
64 if (*ptr == '/' || *ptr == '\\') break;
66 return ++ptr;
69 static BOOL is_wine_loader(const WCHAR *module)
71 const WCHAR *filename = get_filename(module, NULL);
72 const char *ptr;
73 BOOL ret = FALSE;
74 WCHAR *buffer;
75 DWORD len;
77 if ((ptr = getenv("WINELOADER")))
79 ptr = file_nameA(ptr);
80 len = 2 + MultiByteToWideChar( CP_UNIXCP, 0, ptr, -1, NULL, 0 );
81 buffer = heap_alloc( len * sizeof(WCHAR) );
82 MultiByteToWideChar( CP_UNIXCP, 0, ptr, -1, buffer, len );
84 else
86 buffer = heap_alloc( sizeof(L"wine") + 2 * sizeof(WCHAR) );
87 lstrcpyW( buffer, L"wine" );
90 if (!wcscmp( filename, buffer ))
91 ret = TRUE;
93 lstrcatW( buffer, L"64" );
94 if (!wcscmp( filename, buffer ))
95 ret = TRUE;
97 heap_free( buffer );
98 return ret;
101 static void module_fill_module(const WCHAR* in, WCHAR* out, size_t size)
103 const WCHAR *ptr, *endptr;
104 size_t len, l;
106 ptr = get_filename(in, endptr = in + lstrlenW(in));
107 len = min(endptr - ptr, size - 1);
108 memcpy(out, ptr, len * sizeof(WCHAR));
109 out[len] = '\0';
110 if (len > 4 && (l = match_ext(out, len)))
111 out[len - l] = '\0';
112 else if (is_wine_loader(out))
113 lstrcpynW(out, S_WineLoaderW, size);
114 else
116 if (len > 3 && !wcsicmp(&out[len - 3], L".so") &&
117 (l = match_ext(out, len - 3)))
118 lstrcpyW(&out[len - l - 3], L"<elf>");
120 while ((*out = towlower(*out))) out++;
123 void module_set_module(struct module* module, const WCHAR* name)
125 module_fill_module(name, module->module.ModuleName, ARRAY_SIZE(module->module.ModuleName));
126 module_fill_module(name, module->modulename, ARRAY_SIZE(module->modulename));
129 /* Returned string must be freed by caller */
130 WCHAR *get_wine_loader_name(struct process *pcs)
132 const WCHAR *name;
133 WCHAR* altname;
134 unsigned len;
136 name = process_getenv(pcs, L"WINELOADER");
137 if (!name) name = pcs->is_64bit ? L"wine64" : L"wine";
138 len = lstrlenW(name);
140 /* WINELOADER isn't properly updated in Wow64 process calling inside Windows env block
141 * (it's updated in ELF env block though)
142 * So do the adaptation ourselves.
144 altname = HeapAlloc(GetProcessHeap(), 0, (len + 2 + 1) * sizeof(WCHAR));
145 if (altname)
147 memcpy(altname, name, len * sizeof(WCHAR));
148 if (pcs->is_64bit && len >= 2 && memcmp(name + len - 2, L"64", 2 * sizeof(WCHAR)) != 0)
149 lstrcpyW(altname + len, L"64");
150 else if (!pcs->is_64bit && len >= 2 && !memcmp(name + len - 2, L"64", 2 * sizeof(WCHAR)))
151 altname[len - 2] = '\0';
152 else
153 altname[len] = '\0';
156 TRACE("returning %s\n", debugstr_w(altname));
157 return altname;
160 static const char* get_module_type(enum module_type type, BOOL virtual)
162 switch (type)
164 case DMT_ELF: return virtual ? "Virtual ELF" : "ELF";
165 case DMT_PE: return virtual ? "Virtual PE" : "PE";
166 case DMT_MACHO: return virtual ? "Virtual Mach-O" : "Mach-O";
167 default: return "---";
171 /***********************************************************************
172 * Creates and links a new module to a process
174 struct module* module_new(struct process* pcs, const WCHAR* name,
175 enum module_type type, BOOL virtual,
176 DWORD64 mod_addr, DWORD64 size,
177 ULONG_PTR stamp, ULONG_PTR checksum, WORD machine)
179 struct module* module;
180 unsigned i;
182 assert(type == DMT_ELF || type == DMT_PE || type == DMT_MACHO);
183 if (!(module = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*module))))
184 return NULL;
186 module->next = pcs->lmodules;
187 pcs->lmodules = module;
189 TRACE("=> %s %s-%s %s\n",
190 get_module_type(type, virtual),
191 wine_dbgstr_longlong(mod_addr), wine_dbgstr_longlong(mod_addr + size),
192 debugstr_w(name));
194 pool_init(&module->pool, 65536);
196 module->process = pcs;
197 module->module.SizeOfStruct = sizeof(module->module);
198 module->module.BaseOfImage = mod_addr;
199 module->module.ImageSize = size;
200 module_set_module(module, name);
201 module->module.ImageName[0] = '\0';
202 lstrcpynW(module->module.LoadedImageName, name, ARRAY_SIZE(module->module.LoadedImageName));
203 module->module.SymType = SymNone;
204 module->module.NumSyms = 0;
205 module->module.TimeDateStamp = stamp;
206 module->module.CheckSum = checksum;
208 memset(module->module.LoadedPdbName, 0, sizeof(module->module.LoadedPdbName));
209 module->module.CVSig = 0;
210 memset(module->module.CVData, 0, sizeof(module->module.CVData));
211 module->module.PdbSig = 0;
212 memset(&module->module.PdbSig70, 0, sizeof(module->module.PdbSig70));
213 module->module.PdbAge = 0;
214 module->module.PdbUnmatched = FALSE;
215 module->module.DbgUnmatched = FALSE;
216 module->module.LineNumbers = FALSE;
217 module->module.GlobalSymbols = FALSE;
218 module->module.TypeInfo = FALSE;
219 module->module.SourceIndexed = FALSE;
220 module->module.Publics = FALSE;
221 module->module.MachineType = machine;
222 module->module.Reserved = 0;
224 module->reloc_delta = 0;
225 module->type = type;
226 module->is_virtual = virtual;
227 for (i = 0; i < DFI_LAST; i++) module->format_info[i] = NULL;
228 module->sortlist_valid = FALSE;
229 module->sorttab_size = 0;
230 module->addr_sorttab = NULL;
231 module->num_sorttab = 0;
232 module->num_symbols = 0;
233 module->cpu = cpu_find(machine);
234 if (!module->cpu)
235 module->cpu = dbghelp_current_cpu;
237 vector_init(&module->vsymt, sizeof(struct symt*), 128);
238 vector_init(&module->vcustom_symt, sizeof(struct symt*), 16);
239 /* FIXME: this seems a bit too high (on a per module basis)
240 * need some statistics about this
242 hash_table_init(&module->pool, &module->ht_symbols, 4096);
243 hash_table_init(&module->pool, &module->ht_types, 4096);
244 vector_init(&module->vtypes, sizeof(struct symt*), 32);
246 module->sources_used = 0;
247 module->sources_alloc = 0;
248 module->sources = 0;
249 wine_rb_init(&module->sources_offsets_tree, source_rb_compare);
251 /* add top level symbol */
252 module->top = symt_new_module(module);
254 return module;
257 BOOL module_init_pair(struct module_pair* pair, HANDLE hProcess, DWORD64 addr)
259 if (!(pair->pcs = process_find_by_handle(hProcess))) return FALSE;
260 pair->requested = module_find_by_addr(pair->pcs, addr, DMT_UNKNOWN);
261 return module_get_debug(pair);
264 /***********************************************************************
265 * module_find_by_nameW
268 struct module* module_find_by_nameW(const struct process* pcs, const WCHAR* name)
270 struct module* module;
272 for (module = pcs->lmodules; module; module = module->next)
274 if (!wcsicmp(name, module->modulename)) return module;
276 SetLastError(ERROR_INVALID_NAME);
277 return NULL;
280 struct module* module_find_by_nameA(const struct process* pcs, const char* name)
282 WCHAR wname[MAX_PATH];
284 MultiByteToWideChar(CP_ACP, 0, name, -1, wname, ARRAY_SIZE(wname));
285 return module_find_by_nameW(pcs, wname);
288 /***********************************************************************
289 * module_is_already_loaded
292 struct module* module_is_already_loaded(const struct process* pcs, const WCHAR* name)
294 struct module* module;
295 const WCHAR* filename;
297 /* first compare the loaded image name... */
298 for (module = pcs->lmodules; module; module = module->next)
300 if (!wcsicmp(name, module->module.LoadedImageName))
301 return module;
303 /* then compare the standard filenames (without the path) ... */
304 filename = get_filename(name, NULL);
305 for (module = pcs->lmodules; module; module = module->next)
307 if (!wcsicmp(filename, get_filename(module->module.LoadedImageName, NULL)))
308 return module;
310 SetLastError(ERROR_INVALID_NAME);
311 return NULL;
314 /***********************************************************************
315 * module_get_container
318 static struct module* module_get_container(const struct process* pcs,
319 const struct module* inner)
321 struct module* module;
323 for (module = pcs->lmodules; module; module = module->next)
325 if (module != inner &&
326 module->module.BaseOfImage <= inner->module.BaseOfImage &&
327 module->module.BaseOfImage + module->module.ImageSize >=
328 inner->module.BaseOfImage + inner->module.ImageSize)
329 return module;
331 return NULL;
334 /***********************************************************************
335 * module_get_containee
338 struct module* module_get_containee(const struct process* pcs, const struct module* outer)
340 struct module* module;
342 for (module = pcs->lmodules; module; module = module->next)
344 if (module != outer &&
345 outer->module.BaseOfImage <= module->module.BaseOfImage &&
346 outer->module.BaseOfImage + outer->module.ImageSize >=
347 module->module.BaseOfImage + module->module.ImageSize)
348 return module;
350 return NULL;
353 /******************************************************************
354 * module_get_debug
356 * get the debug information from a module:
357 * - if the module's type is deferred, then force loading of debug info (and return
358 * the module itself)
359 * - if the module has no debug info and has an ELF container, then return the ELF
360 * container (and also force the ELF container's debug info loading if deferred)
361 * - otherwise return the module itself if it has some debug info
363 BOOL module_get_debug(struct module_pair* pair)
365 IMAGEHLP_DEFERRED_SYMBOL_LOADW64 idslW64;
367 if (!pair->requested) return FALSE;
368 /* for a PE builtin, always get info from container */
369 if (!(pair->effective = module_get_container(pair->pcs, pair->requested)))
370 pair->effective = pair->requested;
371 /* if deferred, force loading */
372 if (pair->effective->module.SymType == SymDeferred)
374 BOOL ret;
376 if (pair->effective->is_virtual) ret = FALSE;
377 else if (pair->effective->type == DMT_PE)
379 idslW64.SizeOfStruct = sizeof(idslW64);
380 idslW64.BaseOfImage = pair->effective->module.BaseOfImage;
381 idslW64.CheckSum = pair->effective->module.CheckSum;
382 idslW64.TimeDateStamp = pair->effective->module.TimeDateStamp;
383 memcpy(idslW64.FileName, pair->effective->module.ImageName,
384 sizeof(pair->effective->module.ImageName));
385 idslW64.Reparse = FALSE;
386 idslW64.hFile = INVALID_HANDLE_VALUE;
388 pcs_callback(pair->pcs, CBA_DEFERRED_SYMBOL_LOAD_START, &idslW64);
389 ret = pe_load_debug_info(pair->pcs, pair->effective);
390 pcs_callback(pair->pcs,
391 ret ? CBA_DEFERRED_SYMBOL_LOAD_COMPLETE : CBA_DEFERRED_SYMBOL_LOAD_FAILURE,
392 &idslW64);
394 else ret = pair->pcs->loader->load_debug_info(pair->pcs, pair->effective);
396 if (!ret) pair->effective->module.SymType = SymNone;
397 assert(pair->effective->module.SymType != SymDeferred);
398 pair->effective->module.NumSyms = pair->effective->ht_symbols.num_elts;
400 return pair->effective->module.SymType != SymNone;
403 /***********************************************************************
404 * module_find_by_addr
406 * either the addr where module is loaded, or any address inside the
407 * module
409 struct module* module_find_by_addr(const struct process* pcs, DWORD64 addr,
410 enum module_type type)
412 struct module* module;
414 if (type == DMT_UNKNOWN)
416 if ((module = module_find_by_addr(pcs, addr, DMT_PE)) ||
417 (module = module_find_by_addr(pcs, addr, DMT_ELF)) ||
418 (module = module_find_by_addr(pcs, addr, DMT_MACHO)))
419 return module;
421 else
423 for (module = pcs->lmodules; module; module = module->next)
425 if (type == module->type && addr >= module->module.BaseOfImage &&
426 addr < module->module.BaseOfImage + module->module.ImageSize)
427 return module;
430 SetLastError(ERROR_MOD_NOT_FOUND);
431 return module;
434 /******************************************************************
435 * module_is_container_loaded
437 * checks whether the native container, for a (supposed) PE builtin is
438 * already loaded
440 static BOOL module_is_container_loaded(const struct process* pcs,
441 const WCHAR* ImageName, DWORD64 base)
443 size_t len;
444 struct module* module;
445 PCWSTR filename, modname;
447 if (!base) return FALSE;
448 filename = get_filename(ImageName, NULL);
449 len = lstrlenW(filename);
451 for (module = pcs->lmodules; module; module = module->next)
453 if ((module->type == DMT_ELF || module->type == DMT_MACHO) &&
454 base >= module->module.BaseOfImage &&
455 base < module->module.BaseOfImage + module->module.ImageSize)
457 modname = get_filename(module->module.LoadedImageName, NULL);
458 if (!wcsnicmp(modname, filename, len) &&
459 !memcmp(modname + len, L".so", 3 * sizeof(WCHAR)))
461 return TRUE;
465 /* likely a native PE module */
466 WARN("Couldn't find container for %s\n", debugstr_w(ImageName));
467 return FALSE;
470 static BOOL image_check_debug_link_crc(const WCHAR* file, struct image_file_map* fmap, DWORD link_crc)
472 DWORD read_bytes;
473 HANDLE handle;
474 WCHAR *path;
475 WORD magic;
476 DWORD crc;
477 BOOL ret;
479 path = get_dos_file_name(file);
480 handle = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
481 heap_free(path);
482 if (handle == INVALID_HANDLE_VALUE) return FALSE;
484 crc = calc_crc32(handle);
485 if (crc != link_crc)
487 WARN("Bad CRC for file %s (got %08x while expecting %08x)\n", debugstr_w(file), crc, link_crc);
488 CloseHandle(handle);
489 return FALSE;
492 SetFilePointer(handle, 0, 0, FILE_BEGIN);
493 if (ReadFile(handle, &magic, sizeof(magic), &read_bytes, NULL) && magic == IMAGE_DOS_SIGNATURE)
494 ret = pe_map_file(handle, fmap, DMT_PE);
495 else
496 ret = elf_map_handle(handle, fmap);
497 CloseHandle(handle);
498 return ret;
501 static BOOL image_check_debug_link_gnu_id(const WCHAR* file, struct image_file_map* fmap, const BYTE* id, unsigned idlen)
503 struct image_section_map buildid_sect;
504 DWORD read_bytes;
505 HANDLE handle;
506 WCHAR *path;
507 WORD magic;
508 BOOL ret;
510 path = get_dos_file_name(file);
511 handle = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
512 heap_free(path);
513 if (handle == INVALID_HANDLE_VALUE) return FALSE;
515 TRACE("Located debug information file at %s\n", debugstr_w(file));
517 if (ReadFile(handle, &magic, sizeof(magic), &read_bytes, NULL) && magic == IMAGE_DOS_SIGNATURE)
518 ret = pe_map_file(handle, fmap, DMT_PE);
519 else
520 ret = elf_map_handle(handle, fmap);
521 CloseHandle(handle);
523 if (ret && image_find_section(fmap, ".note.gnu.build-id", &buildid_sect))
525 const UINT32* note;
527 note = (const UINT32*)image_map_section(&buildid_sect);
528 if (note != IMAGE_NO_MAP)
530 /* the usual ELF note structure: name-size desc-size type <name> <desc> */
531 if (note[2] == NOTE_GNU_BUILD_ID)
533 if (note[1] == idlen && !memcmp(note + 3 + ((note[0] + 3) >> 2), id, idlen))
534 return TRUE;
535 WARN("mismatch in buildid information for %s\n", wine_dbgstr_w(file));
538 image_unmap_section(&buildid_sect);
539 image_unmap_file(fmap);
541 return FALSE;
544 /******************************************************************
545 * image_locate_debug_link
547 * Locate a filename from a .gnu_debuglink section, using the same
548 * strategy as gdb:
549 * "If the full name of the directory containing the executable is
550 * execdir, and the executable has a debug link that specifies the
551 * name debugfile, then GDB will automatically search for the
552 * debugging information file in three places:
553 * - the directory containing the executable file (that is, it
554 * will look for a file named `execdir/debugfile',
555 * - a subdirectory of that directory named `.debug' (that is, the
556 * file `execdir/.debug/debugfile', and
557 * - a subdirectory of the global debug file directory that includes
558 * the executable's full path, and the name from the link (that is,
559 * the file `globaldebugdir/execdir/debugfile', where globaldebugdir
560 * is the global debug file directory, and execdir has been turned
561 * into a relative path)." (from GDB manual)
563 static BOOL image_locate_debug_link(const struct module* module, struct image_file_map* fmap, const char* filename, DWORD crc)
565 static const WCHAR globalDebugDirW[] = {'/','u','s','r','/','l','i','b','/','d','e','b','u','g','/'};
566 static const WCHAR dotDebugW[] = {'.','d','e','b','u','g','/'};
567 const size_t globalDebugDirLen = ARRAY_SIZE(globalDebugDirW);
568 size_t filename_len, path_len;
569 WCHAR* p = NULL;
570 WCHAR* slash;
571 WCHAR* slash2;
572 struct image_file_map* fmap_link = NULL;
574 fmap_link = HeapAlloc(GetProcessHeap(), 0, sizeof(*fmap_link));
575 if (!fmap_link) return FALSE;
577 filename_len = MultiByteToWideChar(CP_UNIXCP, 0, filename, -1, NULL, 0);
578 path_len = lstrlenW(module->module.LoadedImageName);
579 if (module->real_path) path_len = max(path_len, lstrlenW(module->real_path));
580 p = HeapAlloc(GetProcessHeap(), 0,
581 (globalDebugDirLen + path_len + 6 + 1 + filename_len + 1) * sizeof(WCHAR));
582 if (!p) goto found;
584 /* we prebuild the string with "execdir" */
585 lstrcpyW(p, module->module.LoadedImageName);
586 slash = p;
587 if ((slash2 = wcsrchr(slash, '/'))) slash = slash2 + 1;
588 if ((slash2 = wcsrchr(slash, '\\'))) slash = slash2 + 1;
590 /* testing execdir/filename */
591 MultiByteToWideChar(CP_UNIXCP, 0, filename, -1, slash, filename_len);
592 if (image_check_debug_link_crc(p, fmap_link, crc)) goto found;
594 /* testing execdir/.debug/filename */
595 memcpy(slash, dotDebugW, sizeof(dotDebugW));
596 MultiByteToWideChar(CP_UNIXCP, 0, filename, -1, slash + ARRAY_SIZE(dotDebugW), filename_len);
597 if (image_check_debug_link_crc(p, fmap_link, crc)) goto found;
599 if (module->real_path)
601 lstrcpyW(p, module->real_path);
602 slash = p;
603 if ((slash2 = wcsrchr(slash, '/'))) slash = slash2 + 1;
604 if ((slash2 = wcsrchr(slash, '\\'))) slash = slash2 + 1;
605 MultiByteToWideChar(CP_UNIXCP, 0, filename, -1, slash, filename_len);
606 if (image_check_debug_link_crc(p, fmap_link, crc)) goto found;
609 /* testing globaldebugdir/execdir/filename */
610 memmove(p + globalDebugDirLen, p, (slash - p) * sizeof(WCHAR));
611 memcpy(p, globalDebugDirW, globalDebugDirLen * sizeof(WCHAR));
612 slash += globalDebugDirLen;
613 MultiByteToWideChar(CP_UNIXCP, 0, filename, -1, slash, filename_len);
614 if (image_check_debug_link_crc(p, fmap_link, crc)) goto found;
616 /* finally testing filename */
617 if (image_check_debug_link_crc(slash, fmap_link, crc)) goto found;
620 WARN("Couldn't locate or map %s\n", filename);
621 HeapFree(GetProcessHeap(), 0, p);
622 HeapFree(GetProcessHeap(), 0, fmap_link);
623 return FALSE;
625 found:
626 TRACE("Located debug information file %s at %s\n", filename, debugstr_w(p));
627 HeapFree(GetProcessHeap(), 0, p);
628 fmap->alternate = fmap_link;
629 return TRUE;
632 /******************************************************************
633 * image_load_debugaltlink
635 * Handle a (potential) .gnu_debugaltlink section and the link to
636 * (another) alternate debug file.
637 * Return an heap-allocated image_file_map when the section .gnu_debugaltlink is present,
638 * and a matching debug file has been located.
640 struct image_file_map* image_load_debugaltlink(struct image_file_map* fmap, struct module* module)
642 struct image_section_map debugaltlink_sect;
643 const char* data;
644 struct image_file_map* fmap_link = NULL;
645 BOOL ret = FALSE;
647 for (; fmap; fmap = fmap->alternate)
649 if (image_find_section(fmap, ".gnu_debugaltlink", &debugaltlink_sect)) break;
651 if (!fmap)
653 TRACE("No .gnu_debugaltlink section found for %s\n", debugstr_w(module->modulename));
654 return NULL;
657 data = image_map_section(&debugaltlink_sect);
658 if (data != IMAGE_NO_MAP)
660 unsigned sect_len;
661 const BYTE* id;
662 /* The content of the section is:
663 * + a \0 terminated string
664 * + followed by the build-id
665 * We try loading the dwz_alternate, either as absolute path, or relative to the embedded build-id
667 sect_len = image_get_map_size(&debugaltlink_sect);
668 id = memchr(data, '\0', sect_len);
669 if (id)
671 id++;
672 fmap_link = HeapAlloc(GetProcessHeap(), 0, sizeof(*fmap_link));
673 if (fmap_link)
675 unsigned filename_len = MultiByteToWideChar(CP_UNIXCP, 0, data, -1, NULL, 0);
676 /* Trying absolute path */
677 WCHAR* dst = HeapAlloc(GetProcessHeap(), 0, filename_len * sizeof(WCHAR));
678 if (dst)
680 MultiByteToWideChar(CP_UNIXCP, 0, data, -1, dst, filename_len);
681 ret = image_check_debug_link_gnu_id(dst, fmap_link, id, data + sect_len - (const char*)id);
682 HeapFree(GetProcessHeap(), 0, dst);
684 /* Trying relative path to build-id directory */
685 if (!ret)
687 static const WCHAR globalDebugDirW[] =
688 {'/','u','s','r','/','l','i','b','/','d','e','b','u','g','/','.','b','u','i','l','d','-','i','d','/'};
689 dst = HeapAlloc(GetProcessHeap(), 0, sizeof(globalDebugDirW) + (3 + filename_len) * sizeof(WCHAR));
690 if (dst)
692 WCHAR* p;
694 /* SIGH....
695 * some relative links are relative to /usr/lib/debug/.build-id, some others are from the directory
696 * where the alternate file is...
697 * so try both
699 p = memcpy(dst, globalDebugDirW, sizeof(globalDebugDirW));
700 p += ARRAY_SIZE(globalDebugDirW);
701 MultiByteToWideChar(CP_UNIXCP, 0, data, -1, p, filename_len);
702 ret = image_check_debug_link_gnu_id(dst, fmap_link, id, data + sect_len - (const char*)id);
703 if (!ret)
705 p = dst + ARRAY_SIZE(globalDebugDirW);
706 if ((const char*)id < data + sect_len)
708 *p++ = "0123456789abcdef"[*id >> 4 ];
709 *p++ = "0123456789abcdef"[*id & 0x0F];
711 *p++ = '/';
712 MultiByteToWideChar(CP_UNIXCP, 0, data, -1, p, filename_len);
713 ret = image_check_debug_link_gnu_id(dst, fmap_link, id, data + sect_len - (const char*)id);
715 HeapFree(GetProcessHeap(), 0, dst);
718 if (!ret)
720 HeapFree(GetProcessHeap(), 0, fmap_link);
721 WARN("Couldn't find a match for .gnu_debugaltlink section %s for %s\n", data, debugstr_w(module->modulename));
722 fmap_link = NULL;
727 image_unmap_section(&debugaltlink_sect);
728 if (fmap_link) TRACE("Found match .gnu_debugaltlink section for %s\n", debugstr_w(module->modulename));
729 return fmap_link;
732 /******************************************************************
733 * image_locate_build_id_target
735 * Try to find the .so file containing the debug info out of the build-id note information
737 static BOOL image_locate_build_id_target(struct image_file_map* fmap, const BYTE* id, unsigned idlen)
739 static const WCHAR globalDebugDirW[] = {'/','u','s','r','/','l','i','b','/','d','e','b','u','g','/'};
740 static const WCHAR buildidW[] = {'.','b','u','i','l','d','-','i','d','/'};
741 struct image_file_map* fmap_link = NULL;
742 WCHAR* p;
743 WCHAR* z;
744 const BYTE* idend = id + idlen;
746 fmap_link = HeapAlloc(GetProcessHeap(), 0, sizeof(*fmap_link));
747 if (!fmap_link) return FALSE;
749 p = HeapAlloc(GetProcessHeap(), 0,
750 sizeof(globalDebugDirW) + sizeof(buildidW) +
751 (idlen * 2 + 1) * sizeof(WCHAR) + sizeof(L".debug"));
752 z = p;
753 memcpy(z, globalDebugDirW, sizeof(globalDebugDirW));
754 z += ARRAY_SIZE(globalDebugDirW);
755 memcpy(z, buildidW, sizeof(buildidW));
756 z += ARRAY_SIZE(buildidW);
758 if (id < idend)
760 *z++ = "0123456789abcdef"[*id >> 4 ];
761 *z++ = "0123456789abcdef"[*id & 0x0F];
762 id++;
764 if (id < idend)
765 *z++ = '/';
766 while (id < idend)
768 *z++ = "0123456789abcdef"[*id >> 4 ];
769 *z++ = "0123456789abcdef"[*id & 0x0F];
770 id++;
772 memcpy(z, L".debug", sizeof(L".debug"));
773 TRACE("checking %s\n", wine_dbgstr_w(p));
775 if (image_check_debug_link_gnu_id(p, fmap_link, idend - idlen, idlen))
777 HeapFree(GetProcessHeap(), 0, p);
778 fmap->alternate = fmap_link;
779 return TRUE;
782 TRACE("not found\n");
783 HeapFree(GetProcessHeap(), 0, p);
784 HeapFree(GetProcessHeap(), 0, fmap_link);
785 return FALSE;
788 /******************************************************************
789 * image_check_alternate
791 * Load alternate files for a given image file, looking at either .note.gnu_build-id
792 * or .gnu_debuglink sections.
794 BOOL image_check_alternate(struct image_file_map* fmap, const struct module* module)
796 BOOL ret = FALSE;
797 BOOL found = FALSE;
798 struct image_section_map buildid_sect, debuglink_sect;
800 /* if present, add the .gnu_debuglink file as an alternate to current one */
801 if (image_find_section(fmap, ".note.gnu.build-id", &buildid_sect))
803 const UINT32* note;
805 found = TRUE;
806 note = (const UINT32*)image_map_section(&buildid_sect);
807 if (note != IMAGE_NO_MAP)
809 /* the usual ELF note structure: name-size desc-size type <name> <desc> */
810 if (note[2] == NOTE_GNU_BUILD_ID)
812 ret = image_locate_build_id_target(fmap, (const BYTE*)(note + 3 + ((note[0] + 3) >> 2)), note[1]);
815 image_unmap_section(&buildid_sect);
817 /* if present, add the .gnu_debuglink file as an alternate to current one */
818 if (!ret && image_find_section(fmap, ".gnu_debuglink", &debuglink_sect))
820 const char* dbg_link;
822 found = TRUE;
823 dbg_link = image_map_section(&debuglink_sect);
824 if (dbg_link != IMAGE_NO_MAP)
826 /* The content of a debug link section is:
827 * 1/ a NULL terminated string, containing the file name for the
828 * debug info
829 * 2/ padding on 4 byte boundary
830 * 3/ CRC of the linked file
832 DWORD crc = *(const DWORD*)(dbg_link + ((DWORD_PTR)(strlen(dbg_link) + 4) & ~3));
833 ret = image_locate_debug_link(module, fmap, dbg_link, crc);
834 if (!ret)
835 WARN("Couldn't load linked debug file for %s\n",
836 debugstr_w(module->modulename));
838 image_unmap_section(&debuglink_sect);
840 return found ? ret : TRUE;
843 /***********************************************************************
844 * SymLoadModule (DBGHELP.@)
846 DWORD WINAPI SymLoadModule(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
847 PCSTR ModuleName, DWORD BaseOfDll, DWORD SizeOfDll)
849 return SymLoadModuleEx(hProcess, hFile, ImageName, ModuleName, BaseOfDll,
850 SizeOfDll, NULL, 0);
853 /***********************************************************************
854 * SymLoadModuleEx (DBGHELP.@)
856 DWORD64 WINAPI SymLoadModuleEx(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
857 PCSTR ModuleName, DWORD64 BaseOfDll, DWORD DllSize,
858 PMODLOAD_DATA Data, DWORD Flags)
860 PWSTR wImageName, wModuleName;
861 unsigned len;
862 DWORD64 ret;
864 TRACE("(%p %p %s %s %s %08x %p %08x)\n",
865 hProcess, hFile, debugstr_a(ImageName), debugstr_a(ModuleName),
866 wine_dbgstr_longlong(BaseOfDll), DllSize, Data, Flags);
868 if (ImageName)
870 len = MultiByteToWideChar(CP_ACP, 0, ImageName, -1, NULL, 0);
871 wImageName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
872 MultiByteToWideChar(CP_ACP, 0, ImageName, -1, wImageName, len);
874 else wImageName = NULL;
875 if (ModuleName)
877 len = MultiByteToWideChar(CP_ACP, 0, ModuleName, -1, NULL, 0);
878 wModuleName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
879 MultiByteToWideChar(CP_ACP, 0, ModuleName, -1, wModuleName, len);
881 else wModuleName = NULL;
883 ret = SymLoadModuleExW(hProcess, hFile, wImageName, wModuleName,
884 BaseOfDll, DllSize, Data, Flags);
885 HeapFree(GetProcessHeap(), 0, wImageName);
886 HeapFree(GetProcessHeap(), 0, wModuleName);
887 return ret;
890 /***********************************************************************
891 * SymLoadModuleExW (DBGHELP.@)
893 DWORD64 WINAPI SymLoadModuleExW(HANDLE hProcess, HANDLE hFile, PCWSTR wImageName,
894 PCWSTR wModuleName, DWORD64 BaseOfDll, DWORD SizeOfDll,
895 PMODLOAD_DATA Data, DWORD Flags)
897 struct process* pcs;
898 struct module* module = NULL;
900 TRACE("(%p %p %s %s %s %08x %p %08x)\n",
901 hProcess, hFile, debugstr_w(wImageName), debugstr_w(wModuleName),
902 wine_dbgstr_longlong(BaseOfDll), SizeOfDll, Data, Flags);
904 if (Data)
905 FIXME("Unsupported load data parameter %p for %s\n",
906 Data, debugstr_w(wImageName));
908 if (!(pcs = process_find_by_handle(hProcess))) return 0;
910 if (Flags & SLMFLAG_VIRTUAL)
912 if (!wImageName) return 0;
913 module = module_new(pcs, wImageName, DMT_PE, TRUE, BaseOfDll, SizeOfDll, 0, 0, IMAGE_FILE_MACHINE_UNKNOWN);
914 if (!module) return 0;
915 if (wModuleName) module_set_module(module, wModuleName);
916 module->module.SymType = SymVirtual;
918 return module->module.BaseOfImage;
920 if (Flags & ~(SLMFLAG_VIRTUAL))
921 FIXME("Unsupported Flags %08x for %s\n", Flags, debugstr_w(wImageName));
923 if (!validate_addr64(BaseOfDll)) return 0;
925 pcs->loader->synchronize_module_list(pcs);
927 /* this is a Wine extension to the API just to redo the synchronisation */
928 if (!wImageName && !hFile) return 0;
930 /* check if the module is already loaded, or if it's a builtin PE module with
931 * an containing ELF module
933 if (wImageName)
935 module = module_is_already_loaded(pcs, wImageName);
936 if (module)
938 if (module->module.BaseOfImage == BaseOfDll)
939 SetLastError(ERROR_SUCCESS);
940 else
942 /* native allows to load the same module at different addresses
943 * we don't support this for now
945 SetLastError(ERROR_INVALID_PARAMETER);
946 FIXME("Reloading %s at different base address isn't supported\n", debugstr_w(module->modulename));
948 return 0;
950 if (!module && module_is_container_loaded(pcs, wImageName, BaseOfDll))
952 /* force the loading of DLL as builtin */
953 module = pe_load_builtin_module(pcs, wImageName, BaseOfDll, SizeOfDll);
956 if (!module)
958 /* otherwise, try a regular PE module */
959 if (!(module = pe_load_native_module(pcs, wImageName, hFile, BaseOfDll, SizeOfDll)) &&
960 wImageName)
962 /* and finally an ELF or Mach-O module */
963 module = pcs->loader->load_module(pcs, wImageName, BaseOfDll);
966 if (!module)
968 WARN("Couldn't locate %s\n", debugstr_w(wImageName));
969 return 0;
971 module->module.NumSyms = module->ht_symbols.num_elts;
972 /* by default module_new fills module.ModuleName from a derivation
973 * of LoadedImageName. Overwrite it, if we have better information
975 if (wModuleName)
976 module_set_module(module, wModuleName);
977 if (wImageName)
978 lstrcpynW(module->module.ImageName, wImageName, ARRAY_SIZE(module->module.ImageName));
980 return module->module.BaseOfImage;
983 /***********************************************************************
984 * SymLoadModule64 (DBGHELP.@)
986 DWORD64 WINAPI SymLoadModule64(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
987 PCSTR ModuleName, DWORD64 BaseOfDll, DWORD SizeOfDll)
989 return SymLoadModuleEx(hProcess, hFile, ImageName, ModuleName, BaseOfDll, SizeOfDll,
990 NULL, 0);
993 /******************************************************************
994 * module_remove
997 BOOL module_remove(struct process* pcs, struct module* module)
999 struct module_format*modfmt;
1000 struct module** p;
1001 unsigned i;
1003 TRACE("%s (%p)\n", debugstr_w(module->modulename), module);
1005 for (i = 0; i < DFI_LAST; i++)
1007 if ((modfmt = module->format_info[i]) && modfmt->remove)
1008 modfmt->remove(pcs, module->format_info[i]);
1010 hash_table_destroy(&module->ht_symbols);
1011 hash_table_destroy(&module->ht_types);
1012 HeapFree(GetProcessHeap(), 0, module->sources);
1013 HeapFree(GetProcessHeap(), 0, module->addr_sorttab);
1014 HeapFree(GetProcessHeap(), 0, module->real_path);
1015 pool_destroy(&module->pool);
1016 /* native dbghelp doesn't invoke registered callback(,CBA_SYMBOLS_UNLOADED,) here
1017 * so do we
1019 for (p = &pcs->lmodules; *p; p = &(*p)->next)
1021 if (*p == module)
1023 *p = module->next;
1024 HeapFree(GetProcessHeap(), 0, module);
1025 return TRUE;
1028 FIXME("This shouldn't happen\n");
1029 return FALSE;
1032 /******************************************************************
1033 * SymUnloadModule (DBGHELP.@)
1036 BOOL WINAPI SymUnloadModule(HANDLE hProcess, DWORD BaseOfDll)
1038 return SymUnloadModule64(hProcess, BaseOfDll);
1041 /******************************************************************
1042 * SymUnloadModule64 (DBGHELP.@)
1045 BOOL WINAPI SymUnloadModule64(HANDLE hProcess, DWORD64 BaseOfDll)
1047 struct process* pcs;
1048 struct module* module;
1050 pcs = process_find_by_handle(hProcess);
1051 if (!pcs) return FALSE;
1052 module = module_find_by_addr(pcs, BaseOfDll, DMT_UNKNOWN);
1053 if (!module) return FALSE;
1054 if (!module_remove(pcs, module)) return FALSE;
1055 /* remove local scope if defined inside this module */
1056 if (pcs->localscope_pc >= module->module.BaseOfImage &&
1057 pcs->localscope_pc < module->module.BaseOfImage + module->module.ImageSize)
1059 pcs->localscope_pc = 0;
1060 pcs->localscope_symt = NULL;
1062 return TRUE;
1065 /******************************************************************
1066 * SymEnumerateModules (DBGHELP.@)
1069 struct enum_modW64_32
1071 PSYM_ENUMMODULES_CALLBACK cb;
1072 PVOID user;
1073 char module[MAX_PATH];
1076 static BOOL CALLBACK enum_modW64_32(PCWSTR name, DWORD64 base, PVOID user)
1078 struct enum_modW64_32* x = user;
1080 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
1081 return x->cb(x->module, (DWORD)base, x->user);
1084 BOOL WINAPI SymEnumerateModules(HANDLE hProcess,
1085 PSYM_ENUMMODULES_CALLBACK EnumModulesCallback,
1086 PVOID UserContext)
1088 struct enum_modW64_32 x;
1090 x.cb = EnumModulesCallback;
1091 x.user = UserContext;
1093 return SymEnumerateModulesW64(hProcess, enum_modW64_32, &x);
1096 /******************************************************************
1097 * SymEnumerateModules64 (DBGHELP.@)
1100 struct enum_modW64_64
1102 PSYM_ENUMMODULES_CALLBACK64 cb;
1103 PVOID user;
1104 char module[MAX_PATH];
1107 static BOOL CALLBACK enum_modW64_64(PCWSTR name, DWORD64 base, PVOID user)
1109 struct enum_modW64_64* x = user;
1111 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
1112 return x->cb(x->module, base, x->user);
1115 BOOL WINAPI SymEnumerateModules64(HANDLE hProcess,
1116 PSYM_ENUMMODULES_CALLBACK64 EnumModulesCallback,
1117 PVOID UserContext)
1119 struct enum_modW64_64 x;
1121 x.cb = EnumModulesCallback;
1122 x.user = UserContext;
1124 return SymEnumerateModulesW64(hProcess, enum_modW64_64, &x);
1127 /******************************************************************
1128 * SymEnumerateModulesW64 (DBGHELP.@)
1131 BOOL WINAPI SymEnumerateModulesW64(HANDLE hProcess,
1132 PSYM_ENUMMODULES_CALLBACKW64 EnumModulesCallback,
1133 PVOID UserContext)
1135 struct process* pcs = process_find_by_handle(hProcess);
1136 struct module* module;
1138 if (!pcs) return FALSE;
1140 for (module = pcs->lmodules; module; module = module->next)
1142 if (!dbghelp_opt_native &&
1143 (module->type == DMT_ELF || module->type == DMT_MACHO))
1144 continue;
1145 if (!EnumModulesCallback(module->modulename,
1146 module->module.BaseOfImage, UserContext))
1147 break;
1149 return TRUE;
1152 /******************************************************************
1153 * EnumerateLoadedModules64 (DBGHELP.@)
1156 struct enum_load_modW64_64
1158 PENUMLOADED_MODULES_CALLBACK64 cb;
1159 PVOID user;
1160 char module[MAX_PATH];
1163 static BOOL CALLBACK enum_load_modW64_64(PCWSTR name, DWORD64 base, ULONG size,
1164 PVOID user)
1166 struct enum_load_modW64_64* x = user;
1168 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
1169 return x->cb(x->module, base, size, x->user);
1172 BOOL WINAPI EnumerateLoadedModules64(HANDLE hProcess,
1173 PENUMLOADED_MODULES_CALLBACK64 EnumLoadedModulesCallback,
1174 PVOID UserContext)
1176 struct enum_load_modW64_64 x;
1178 x.cb = EnumLoadedModulesCallback;
1179 x.user = UserContext;
1181 return EnumerateLoadedModulesW64(hProcess, enum_load_modW64_64, &x);
1184 /******************************************************************
1185 * EnumerateLoadedModules (DBGHELP.@)
1188 struct enum_load_modW64_32
1190 PENUMLOADED_MODULES_CALLBACK cb;
1191 PVOID user;
1192 char module[MAX_PATH];
1195 static BOOL CALLBACK enum_load_modW64_32(PCWSTR name, DWORD64 base, ULONG size,
1196 PVOID user)
1198 struct enum_load_modW64_32* x = user;
1199 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
1200 return x->cb(x->module, (DWORD)base, size, x->user);
1203 BOOL WINAPI EnumerateLoadedModules(HANDLE hProcess,
1204 PENUMLOADED_MODULES_CALLBACK EnumLoadedModulesCallback,
1205 PVOID UserContext)
1207 struct enum_load_modW64_32 x;
1209 x.cb = EnumLoadedModulesCallback;
1210 x.user = UserContext;
1212 return EnumerateLoadedModulesW64(hProcess, enum_load_modW64_32, &x);
1215 /******************************************************************
1216 * EnumerateLoadedModulesW64 (DBGHELP.@)
1219 BOOL WINAPI EnumerateLoadedModulesW64(HANDLE hProcess,
1220 PENUMLOADED_MODULES_CALLBACKW64 EnumLoadedModulesCallback,
1221 PVOID UserContext)
1223 HMODULE* hMods;
1224 WCHAR baseW[256], modW[256];
1225 DWORD i, sz;
1226 MODULEINFO mi;
1228 hMods = HeapAlloc(GetProcessHeap(), 0, 256 * sizeof(hMods[0]));
1229 if (!hMods) return FALSE;
1231 if (!EnumProcessModules(hProcess, hMods, 256 * sizeof(hMods[0]), &sz))
1233 /* hProcess should also be a valid process handle !! */
1234 FIXME("If this happens, bump the number in mod\n");
1235 HeapFree(GetProcessHeap(), 0, hMods);
1236 return FALSE;
1238 sz /= sizeof(HMODULE);
1239 for (i = 0; i < sz; i++)
1241 if (!GetModuleInformation(hProcess, hMods[i], &mi, sizeof(mi)) ||
1242 !GetModuleBaseNameW(hProcess, hMods[i], baseW, ARRAY_SIZE(baseW)))
1243 continue;
1244 module_fill_module(baseW, modW, ARRAY_SIZE(modW));
1245 EnumLoadedModulesCallback(modW, (DWORD_PTR)mi.lpBaseOfDll, mi.SizeOfImage,
1246 UserContext);
1248 HeapFree(GetProcessHeap(), 0, hMods);
1250 return sz != 0 && i == sz;
1253 static void dbghelp_str_WtoA(const WCHAR *src, char *dst, int dst_len)
1255 WideCharToMultiByte(CP_ACP, 0, src, -1, dst, dst_len - 1, NULL, NULL);
1256 dst[dst_len - 1] = 0;
1259 /******************************************************************
1260 * SymGetModuleInfo (DBGHELP.@)
1263 BOOL WINAPI SymGetModuleInfo(HANDLE hProcess, DWORD dwAddr,
1264 PIMAGEHLP_MODULE ModuleInfo)
1266 IMAGEHLP_MODULE mi;
1267 IMAGEHLP_MODULEW64 miw64;
1269 if (sizeof(mi) < ModuleInfo->SizeOfStruct) FIXME("Wrong size\n");
1271 miw64.SizeOfStruct = sizeof(miw64);
1272 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
1274 mi.SizeOfStruct = ModuleInfo->SizeOfStruct;
1275 mi.BaseOfImage = miw64.BaseOfImage;
1276 mi.ImageSize = miw64.ImageSize;
1277 mi.TimeDateStamp = miw64.TimeDateStamp;
1278 mi.CheckSum = miw64.CheckSum;
1279 mi.NumSyms = miw64.NumSyms;
1280 mi.SymType = miw64.SymType;
1281 dbghelp_str_WtoA(miw64.ModuleName, mi.ModuleName, sizeof(mi.ModuleName));
1282 dbghelp_str_WtoA(miw64.ImageName, mi.ImageName, sizeof(mi.ImageName));
1283 dbghelp_str_WtoA(miw64.LoadedImageName, mi.LoadedImageName, sizeof(mi.LoadedImageName));
1285 memcpy(ModuleInfo, &mi, ModuleInfo->SizeOfStruct);
1287 return TRUE;
1290 /******************************************************************
1291 * SymGetModuleInfoW (DBGHELP.@)
1294 BOOL WINAPI SymGetModuleInfoW(HANDLE hProcess, DWORD dwAddr,
1295 PIMAGEHLP_MODULEW ModuleInfo)
1297 IMAGEHLP_MODULEW64 miw64;
1298 IMAGEHLP_MODULEW miw;
1300 if (sizeof(miw) < ModuleInfo->SizeOfStruct) FIXME("Wrong size\n");
1302 miw64.SizeOfStruct = sizeof(miw64);
1303 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
1305 miw.SizeOfStruct = ModuleInfo->SizeOfStruct;
1306 miw.BaseOfImage = miw64.BaseOfImage;
1307 miw.ImageSize = miw64.ImageSize;
1308 miw.TimeDateStamp = miw64.TimeDateStamp;
1309 miw.CheckSum = miw64.CheckSum;
1310 miw.NumSyms = miw64.NumSyms;
1311 miw.SymType = miw64.SymType;
1312 lstrcpyW(miw.ModuleName, miw64.ModuleName);
1313 lstrcpyW(miw.ImageName, miw64.ImageName);
1314 lstrcpyW(miw.LoadedImageName, miw64.LoadedImageName);
1315 memcpy(ModuleInfo, &miw, ModuleInfo->SizeOfStruct);
1317 return TRUE;
1320 /******************************************************************
1321 * SymGetModuleInfo64 (DBGHELP.@)
1324 BOOL WINAPI SymGetModuleInfo64(HANDLE hProcess, DWORD64 dwAddr,
1325 PIMAGEHLP_MODULE64 ModuleInfo)
1327 IMAGEHLP_MODULE64 mi64;
1328 IMAGEHLP_MODULEW64 miw64;
1330 if (sizeof(mi64) < ModuleInfo->SizeOfStruct)
1332 SetLastError(ERROR_MOD_NOT_FOUND); /* NOTE: native returns this error */
1333 WARN("Wrong size %u\n", ModuleInfo->SizeOfStruct);
1334 return FALSE;
1337 miw64.SizeOfStruct = sizeof(miw64);
1338 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
1340 mi64.SizeOfStruct = ModuleInfo->SizeOfStruct;
1341 mi64.BaseOfImage = miw64.BaseOfImage;
1342 mi64.ImageSize = miw64.ImageSize;
1343 mi64.TimeDateStamp = miw64.TimeDateStamp;
1344 mi64.CheckSum = miw64.CheckSum;
1345 mi64.NumSyms = miw64.NumSyms;
1346 mi64.SymType = miw64.SymType;
1347 dbghelp_str_WtoA(miw64.ModuleName, mi64.ModuleName, sizeof(mi64.ModuleName));
1348 dbghelp_str_WtoA(miw64.ImageName, mi64.ImageName, sizeof(mi64.ImageName));
1349 dbghelp_str_WtoA(miw64.LoadedImageName, mi64.LoadedImageName, sizeof(mi64.LoadedImageName));
1350 dbghelp_str_WtoA(miw64.LoadedPdbName, mi64.LoadedPdbName, sizeof(mi64.LoadedPdbName));
1352 mi64.CVSig = miw64.CVSig;
1353 dbghelp_str_WtoA(miw64.CVData, mi64.CVData, sizeof(mi64.CVData));
1354 mi64.PdbSig = miw64.PdbSig;
1355 mi64.PdbSig70 = miw64.PdbSig70;
1356 mi64.PdbAge = miw64.PdbAge;
1357 mi64.PdbUnmatched = miw64.PdbUnmatched;
1358 mi64.DbgUnmatched = miw64.DbgUnmatched;
1359 mi64.LineNumbers = miw64.LineNumbers;
1360 mi64.GlobalSymbols = miw64.GlobalSymbols;
1361 mi64.TypeInfo = miw64.TypeInfo;
1362 mi64.SourceIndexed = miw64.SourceIndexed;
1363 mi64.Publics = miw64.Publics;
1364 mi64.MachineType = miw64.MachineType;
1365 mi64.Reserved = miw64.Reserved;
1367 memcpy(ModuleInfo, &mi64, ModuleInfo->SizeOfStruct);
1369 return TRUE;
1372 /******************************************************************
1373 * SymGetModuleInfoW64 (DBGHELP.@)
1376 BOOL WINAPI SymGetModuleInfoW64(HANDLE hProcess, DWORD64 dwAddr,
1377 PIMAGEHLP_MODULEW64 ModuleInfo)
1379 struct process* pcs = process_find_by_handle(hProcess);
1380 struct module* module;
1381 IMAGEHLP_MODULEW64 miw64;
1383 TRACE("%p %s %p\n", hProcess, wine_dbgstr_longlong(dwAddr), ModuleInfo);
1385 if (!pcs) return FALSE;
1386 if (ModuleInfo->SizeOfStruct > sizeof(*ModuleInfo)) return FALSE;
1387 module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
1388 if (!module) return FALSE;
1390 miw64 = module->module;
1392 /* update debug information from container if any */
1393 if (module->module.SymType == SymNone)
1395 module = module_get_container(pcs, module);
1396 if (module && module->module.SymType != SymNone)
1398 miw64.SymType = module->module.SymType;
1399 miw64.NumSyms = module->module.NumSyms;
1402 memcpy(ModuleInfo, &miw64, ModuleInfo->SizeOfStruct);
1403 return TRUE;
1406 /***********************************************************************
1407 * SymGetModuleBase (DBGHELP.@)
1409 DWORD WINAPI SymGetModuleBase(HANDLE hProcess, DWORD dwAddr)
1411 DWORD64 ret;
1413 ret = SymGetModuleBase64(hProcess, dwAddr);
1414 return validate_addr64(ret) ? ret : 0;
1417 /***********************************************************************
1418 * SymGetModuleBase64 (DBGHELP.@)
1420 DWORD64 WINAPI SymGetModuleBase64(HANDLE hProcess, DWORD64 dwAddr)
1422 struct process* pcs = process_find_by_handle(hProcess);
1423 struct module* module;
1425 if (!pcs) return 0;
1426 module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
1427 if (!module) return 0;
1428 return module->module.BaseOfImage;
1431 /******************************************************************
1432 * module_reset_debug_info
1433 * Removes any debug information linked to a given module.
1435 void module_reset_debug_info(struct module* module)
1437 module->sortlist_valid = TRUE;
1438 module->sorttab_size = 0;
1439 module->addr_sorttab = NULL;
1440 module->num_sorttab = module->num_symbols = 0;
1441 hash_table_destroy(&module->ht_symbols);
1442 module->ht_symbols.num_buckets = 0;
1443 module->ht_symbols.buckets = NULL;
1444 hash_table_destroy(&module->ht_types);
1445 module->ht_types.num_buckets = 0;
1446 module->ht_types.buckets = NULL;
1447 module->vtypes.num_elts = 0;
1448 hash_table_destroy(&module->ht_symbols);
1449 module->sources_used = module->sources_alloc = 0;
1450 module->sources = NULL;
1453 /******************************************************************
1454 * SymRefreshModuleList (DBGHELP.@)
1456 BOOL WINAPI SymRefreshModuleList(HANDLE hProcess)
1458 struct process* pcs;
1460 TRACE("(%p)\n", hProcess);
1462 if (!(pcs = process_find_by_handle(hProcess))) return FALSE;
1464 return pcs->loader->synchronize_module_list(pcs);
1467 /***********************************************************************
1468 * SymFunctionTableAccess (DBGHELP.@)
1470 PVOID WINAPI SymFunctionTableAccess(HANDLE hProcess, DWORD AddrBase)
1472 return SymFunctionTableAccess64(hProcess, AddrBase);
1475 /***********************************************************************
1476 * SymFunctionTableAccess64 (DBGHELP.@)
1478 PVOID WINAPI SymFunctionTableAccess64(HANDLE hProcess, DWORD64 AddrBase)
1480 struct process* pcs = process_find_by_handle(hProcess);
1481 struct module* module;
1483 if (!pcs || !dbghelp_current_cpu->find_runtime_function) return NULL;
1484 module = module_find_by_addr(pcs, AddrBase, DMT_UNKNOWN);
1485 if (!module) return NULL;
1487 return dbghelp_current_cpu->find_runtime_function(module, AddrBase);
1490 static BOOL native_synchronize_module_list(struct process* pcs)
1492 return FALSE;
1495 static struct module* native_load_module(struct process* pcs, const WCHAR* name, ULONG_PTR addr)
1497 return NULL;
1500 static BOOL native_load_debug_info(struct process* process, struct module* module)
1502 return FALSE;
1505 static BOOL native_enum_modules(struct process *process, enum_modules_cb cb, void* user)
1507 return FALSE;
1510 static BOOL native_fetch_file_info(struct process* process, const WCHAR* name, ULONG_PTR load_addr, DWORD_PTR* base,
1511 DWORD* size, DWORD* checksum)
1513 return FALSE;
1516 const struct loader_ops no_loader_ops =
1518 native_synchronize_module_list,
1519 native_load_module,
1520 native_load_debug_info,
1521 native_enum_modules,
1522 native_fetch_file_info,