msvcrt: Use EnumSystemLocalesEx instead of directly accessing kernel32 resources.
[wine.git] / dlls / dbghelp / module.c
blobcd34b81460fcfb363b9227487e2fcb654cf03a9f
1 /*
2 * File module.c - module handling for the wine debugger
4 * Copyright (C) 1993, Eric Youngdale.
5 * 2000-2007, Eric Pouech
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <assert.h>
27 #include "dbghelp_private.h"
28 #include "image_private.h"
29 #include "psapi.h"
30 #include "winternl.h"
31 #include "wine/debug.h"
32 #include "wine/heap.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
36 #define NOTE_GNU_BUILD_ID 3
38 const WCHAR S_ElfW[] = L"<elf>";
39 const WCHAR S_WineLoaderW[] = L"<wine-loader>";
40 static const WCHAR * const ext[] = {L".acm", L".dll", L".drv", L".exe", L".ocx", L".vxd", NULL};
42 static int match_ext(const WCHAR* ptr, size_t len)
44 const WCHAR* const *e;
45 size_t l;
47 for (e = ext; *e; e++)
49 l = lstrlenW(*e);
50 if (l >= len) return 0;
51 if (wcsnicmp(&ptr[len - l], *e, l)) continue;
52 return l;
54 return 0;
57 static const WCHAR* get_filename(const WCHAR* name, const WCHAR* endptr)
59 const WCHAR* ptr;
61 if (!endptr) endptr = name + lstrlenW(name);
62 for (ptr = endptr - 1; ptr >= name; ptr--)
64 if (*ptr == '/' || *ptr == '\\') break;
66 return ++ptr;
69 static BOOL is_wine_loader(const WCHAR *module)
71 const WCHAR *filename = get_filename(module, NULL);
72 const char *ptr;
73 BOOL ret = FALSE;
74 WCHAR *buffer;
75 DWORD len;
77 if ((ptr = getenv("WINELOADER")))
79 ptr = file_nameA(ptr);
80 len = 2 + MultiByteToWideChar( CP_UNIXCP, 0, ptr, -1, NULL, 0 );
81 buffer = heap_alloc( len * sizeof(WCHAR) );
82 MultiByteToWideChar( CP_UNIXCP, 0, ptr, -1, buffer, len );
84 else
86 buffer = heap_alloc( sizeof(L"wine") + 2 * sizeof(WCHAR) );
87 lstrcpyW( buffer, L"wine" );
90 if (!wcscmp( filename, buffer ))
91 ret = TRUE;
93 lstrcatW( buffer, L"64" );
94 if (!wcscmp( filename, buffer ))
95 ret = TRUE;
97 heap_free( buffer );
98 return ret;
101 static void module_fill_module(const WCHAR* in, WCHAR* out, size_t size)
103 const WCHAR *ptr, *endptr;
104 size_t len, l;
106 endptr = in + lstrlenW(in);
107 endptr -= match_ext(in, endptr - in);
108 ptr = get_filename(in, endptr);
109 len = min(endptr - ptr, size - 1);
110 memcpy(out, ptr, len * sizeof(WCHAR));
111 out[len] = '\0';
112 if (is_wine_loader(out))
113 lstrcpynW(out, S_WineLoaderW, size);
114 else
116 if (len > 3 && !wcsicmp(&out[len - 3], L".so") &&
117 (l = match_ext(out, len - 3)))
118 lstrcpyW(&out[len - l - 3], L"<elf>");
120 while ((*out = towlower(*out))) out++;
123 void module_set_module(struct module* module, const WCHAR* name)
125 module_fill_module(name, module->module.ModuleName, ARRAY_SIZE(module->module.ModuleName));
126 module_fill_module(name, module->modulename, ARRAY_SIZE(module->modulename));
129 /* Returned string must be freed by caller */
130 WCHAR *get_wine_loader_name(struct process *pcs)
132 const WCHAR *name;
133 WCHAR* altname;
134 unsigned len;
136 name = process_getenv(pcs, L"WINELOADER");
137 if (!name) name = pcs->is_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 = SymDeferred;
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 BOOL module_load_debug(struct module* module)
355 IMAGEHLP_DEFERRED_SYMBOL_LOADW64 idslW64;
357 /* if deferred, force loading */
358 if (module->module.SymType == SymDeferred)
360 BOOL ret;
362 if (module->is_virtual) ret = FALSE;
363 else if (module->type == DMT_PE)
365 idslW64.SizeOfStruct = sizeof(idslW64);
366 idslW64.BaseOfImage = module->module.BaseOfImage;
367 idslW64.CheckSum = module->module.CheckSum;
368 idslW64.TimeDateStamp = module->module.TimeDateStamp;
369 memcpy(idslW64.FileName, module->module.ImageName,
370 sizeof(module->module.ImageName));
371 idslW64.Reparse = FALSE;
372 idslW64.hFile = INVALID_HANDLE_VALUE;
374 pcs_callback(module->process, CBA_DEFERRED_SYMBOL_LOAD_START, &idslW64);
375 ret = pe_load_debug_info(module->process, module);
376 pcs_callback(module->process,
377 ret ? CBA_DEFERRED_SYMBOL_LOAD_COMPLETE : CBA_DEFERRED_SYMBOL_LOAD_FAILURE,
378 &idslW64);
380 else ret = module->process->loader->load_debug_info(module->process, module);
382 if (!ret) module->module.SymType = SymNone;
383 assert(module->module.SymType != SymDeferred);
384 module->module.NumSyms = module->ht_symbols.num_elts;
386 return module->module.SymType != SymNone;
389 /******************************************************************
390 * module_get_debug
392 * get the debug information from a module:
393 * - if the module's type is deferred, then force loading of debug info (and return
394 * the module itself)
395 * - if the module has no debug info and has an ELF container, then return the ELF
396 * container (and also force the ELF container's debug info loading if deferred)
397 * - otherwise return the module itself if it has some debug info
399 BOOL module_get_debug(struct module_pair* pair)
401 if (!pair->requested) return FALSE;
402 /* for a PE builtin, always get info from container */
403 if (!(pair->effective = module_get_container(pair->pcs, pair->requested)))
404 pair->effective = pair->requested;
405 return module_load_debug(pair->effective);
408 /***********************************************************************
409 * module_find_by_addr
411 * either the addr where module is loaded, or any address inside the
412 * module
414 struct module* module_find_by_addr(const struct process* pcs, DWORD64 addr,
415 enum module_type type)
417 struct module* module;
419 if (type == DMT_UNKNOWN)
421 if ((module = module_find_by_addr(pcs, addr, DMT_PE)) ||
422 (module = module_find_by_addr(pcs, addr, DMT_ELF)) ||
423 (module = module_find_by_addr(pcs, addr, DMT_MACHO)))
424 return module;
426 else
428 for (module = pcs->lmodules; module; module = module->next)
430 if (type == module->type && addr >= module->module.BaseOfImage &&
431 addr < module->module.BaseOfImage + module->module.ImageSize)
432 return module;
435 SetLastError(ERROR_MOD_NOT_FOUND);
436 return module;
439 /******************************************************************
440 * module_is_container_loaded
442 * checks whether the native container, for a (supposed) PE builtin is
443 * already loaded
445 static BOOL module_is_container_loaded(const struct process* pcs,
446 const WCHAR* ImageName, DWORD64 base)
448 size_t len;
449 struct module* module;
450 PCWSTR filename, modname;
452 if (!base) return FALSE;
453 filename = get_filename(ImageName, NULL);
454 len = lstrlenW(filename);
456 for (module = pcs->lmodules; module; module = module->next)
458 if ((module->type == DMT_ELF || module->type == DMT_MACHO) &&
459 base >= module->module.BaseOfImage &&
460 base < module->module.BaseOfImage + module->module.ImageSize)
462 modname = get_filename(module->module.LoadedImageName, NULL);
463 if (!wcsnicmp(modname, filename, len) &&
464 !memcmp(modname + len, L".so", 3 * sizeof(WCHAR)))
466 return TRUE;
470 /* likely a native PE module */
471 WARN("Couldn't find container for %s\n", debugstr_w(ImageName));
472 return FALSE;
475 static BOOL image_check_debug_link_crc(const WCHAR* file, struct image_file_map* fmap, DWORD link_crc)
477 DWORD read_bytes;
478 HANDLE handle;
479 WCHAR *path;
480 WORD magic;
481 DWORD crc;
482 BOOL ret;
484 path = get_dos_file_name(file);
485 handle = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
486 heap_free(path);
487 if (handle == INVALID_HANDLE_VALUE) return FALSE;
489 crc = calc_crc32(handle);
490 if (crc != link_crc)
492 WARN("Bad CRC for file %s (got %08lx while expecting %08lx)\n", debugstr_w(file), crc, link_crc);
493 CloseHandle(handle);
494 return FALSE;
497 SetFilePointer(handle, 0, 0, FILE_BEGIN);
498 if (ReadFile(handle, &magic, sizeof(magic), &read_bytes, NULL) && magic == IMAGE_DOS_SIGNATURE)
499 ret = pe_map_file(handle, fmap, DMT_PE);
500 else
501 ret = elf_map_handle(handle, fmap);
502 CloseHandle(handle);
503 return ret;
506 static BOOL image_check_debug_link_gnu_id(const WCHAR* file, struct image_file_map* fmap, const BYTE* id, unsigned idlen)
508 struct image_section_map buildid_sect;
509 DWORD read_bytes;
510 HANDLE handle;
511 WCHAR *path;
512 WORD magic;
513 BOOL ret;
515 path = get_dos_file_name(file);
516 handle = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
517 heap_free(path);
518 if (handle == INVALID_HANDLE_VALUE) return FALSE;
520 TRACE("Located debug information file at %s\n", debugstr_w(file));
522 if (ReadFile(handle, &magic, sizeof(magic), &read_bytes, NULL) && magic == IMAGE_DOS_SIGNATURE)
523 ret = pe_map_file(handle, fmap, DMT_PE);
524 else
525 ret = elf_map_handle(handle, fmap);
526 CloseHandle(handle);
528 if (ret && image_find_section(fmap, ".note.gnu.build-id", &buildid_sect))
530 const UINT32* note;
532 note = (const UINT32*)image_map_section(&buildid_sect);
533 if (note != IMAGE_NO_MAP)
535 /* the usual ELF note structure: name-size desc-size type <name> <desc> */
536 if (note[2] == NOTE_GNU_BUILD_ID)
538 if (note[1] == idlen && !memcmp(note + 3 + ((note[0] + 3) >> 2), id, idlen))
539 return TRUE;
540 WARN("mismatch in buildid information for %s\n", wine_dbgstr_w(file));
543 image_unmap_section(&buildid_sect);
544 image_unmap_file(fmap);
546 return FALSE;
549 /******************************************************************
550 * image_locate_debug_link
552 * Locate a filename from a .gnu_debuglink section, using the same
553 * strategy as gdb:
554 * "If the full name of the directory containing the executable is
555 * execdir, and the executable has a debug link that specifies the
556 * name debugfile, then GDB will automatically search for the
557 * debugging information file in three places:
558 * - the directory containing the executable file (that is, it
559 * will look for a file named `execdir/debugfile',
560 * - a subdirectory of that directory named `.debug' (that is, the
561 * file `execdir/.debug/debugfile', and
562 * - a subdirectory of the global debug file directory that includes
563 * the executable's full path, and the name from the link (that is,
564 * the file `globaldebugdir/execdir/debugfile', where globaldebugdir
565 * is the global debug file directory, and execdir has been turned
566 * into a relative path)." (from GDB manual)
568 static BOOL image_locate_debug_link(const struct module* module, struct image_file_map* fmap, const char* filename, DWORD crc)
570 static const WCHAR globalDebugDirW[] = {'/','u','s','r','/','l','i','b','/','d','e','b','u','g','/'};
571 static const WCHAR dotDebugW[] = {'.','d','e','b','u','g','/'};
572 const size_t globalDebugDirLen = ARRAY_SIZE(globalDebugDirW);
573 size_t filename_len, path_len;
574 WCHAR* p = NULL;
575 WCHAR* slash;
576 WCHAR* slash2;
577 struct image_file_map* fmap_link = NULL;
579 fmap_link = HeapAlloc(GetProcessHeap(), 0, sizeof(*fmap_link));
580 if (!fmap_link) return FALSE;
582 filename_len = MultiByteToWideChar(CP_UNIXCP, 0, filename, -1, NULL, 0);
583 path_len = lstrlenW(module->module.LoadedImageName);
584 if (module->real_path) path_len = max(path_len, lstrlenW(module->real_path));
585 p = HeapAlloc(GetProcessHeap(), 0,
586 (globalDebugDirLen + path_len + 6 + 1 + filename_len + 1) * sizeof(WCHAR));
587 if (!p) goto found;
589 /* we prebuild the string with "execdir" */
590 lstrcpyW(p, module->module.LoadedImageName);
591 slash = p;
592 if ((slash2 = wcsrchr(slash, '/'))) slash = slash2 + 1;
593 if ((slash2 = wcsrchr(slash, '\\'))) slash = slash2 + 1;
595 /* testing execdir/filename */
596 MultiByteToWideChar(CP_UNIXCP, 0, filename, -1, slash, filename_len);
597 if (image_check_debug_link_crc(p, fmap_link, crc)) goto found;
599 /* testing execdir/.debug/filename */
600 memcpy(slash, dotDebugW, sizeof(dotDebugW));
601 MultiByteToWideChar(CP_UNIXCP, 0, filename, -1, slash + ARRAY_SIZE(dotDebugW), filename_len);
602 if (image_check_debug_link_crc(p, fmap_link, crc)) goto found;
604 if (module->real_path)
606 lstrcpyW(p, module->real_path);
607 slash = p;
608 if ((slash2 = wcsrchr(slash, '/'))) slash = slash2 + 1;
609 if ((slash2 = wcsrchr(slash, '\\'))) slash = slash2 + 1;
610 MultiByteToWideChar(CP_UNIXCP, 0, filename, -1, slash, filename_len);
611 if (image_check_debug_link_crc(p, fmap_link, crc)) goto found;
614 /* testing globaldebugdir/execdir/filename */
615 memmove(p + globalDebugDirLen, p, (slash - p) * sizeof(WCHAR));
616 memcpy(p, globalDebugDirW, globalDebugDirLen * sizeof(WCHAR));
617 slash += globalDebugDirLen;
618 MultiByteToWideChar(CP_UNIXCP, 0, filename, -1, slash, filename_len);
619 if (image_check_debug_link_crc(p, fmap_link, crc)) goto found;
621 /* finally testing filename */
622 if (image_check_debug_link_crc(slash, fmap_link, crc)) goto found;
625 WARN("Couldn't locate or map %s\n", filename);
626 HeapFree(GetProcessHeap(), 0, p);
627 HeapFree(GetProcessHeap(), 0, fmap_link);
628 return FALSE;
630 found:
631 TRACE("Located debug information file %s at %s\n", filename, debugstr_w(p));
632 HeapFree(GetProcessHeap(), 0, p);
633 fmap->alternate = fmap_link;
634 return TRUE;
637 static WCHAR* append_hex(WCHAR* dst, const BYTE* id, const BYTE* end)
639 while (id < end)
641 *dst++ = "0123456789abcdef"[*id >> 4 ];
642 *dst++ = "0123456789abcdef"[*id & 0x0F];
643 id++;
645 return dst;
648 /******************************************************************
649 * image_load_debugaltlink
651 * Handle a (potential) .gnu_debugaltlink section and the link to
652 * (another) alternate debug file.
653 * Return an heap-allocated image_file_map when the section .gnu_debugaltlink is present,
654 * and a matching debug file has been located.
656 struct image_file_map* image_load_debugaltlink(struct image_file_map* fmap, struct module* module)
658 struct image_section_map debugaltlink_sect;
659 const char* data;
660 struct image_file_map* fmap_link = NULL;
661 BOOL ret = FALSE;
663 for (; fmap; fmap = fmap->alternate)
665 if (image_find_section(fmap, ".gnu_debugaltlink", &debugaltlink_sect)) break;
667 if (!fmap)
669 TRACE("No .gnu_debugaltlink section found for %s\n", debugstr_w(module->modulename));
670 return NULL;
673 data = image_map_section(&debugaltlink_sect);
674 if (data != IMAGE_NO_MAP)
676 unsigned sect_len;
677 const BYTE* id;
678 /* The content of the section is:
679 * + a \0 terminated string
680 * + followed by the build-id
681 * We try loading the dwz_alternate, either as absolute path, or relative to the embedded build-id
683 sect_len = image_get_map_size(&debugaltlink_sect);
684 id = memchr(data, '\0', sect_len);
685 if (id++)
687 unsigned idlen = (const BYTE*)data + sect_len - id;
688 fmap_link = HeapAlloc(GetProcessHeap(), 0, sizeof(*fmap_link));
689 if (fmap_link)
691 unsigned filename_len = MultiByteToWideChar(CP_UNIXCP, 0, data, -1, NULL, 0);
692 /* Trying absolute path */
693 WCHAR* dst = HeapAlloc(GetProcessHeap(), 0, filename_len * sizeof(WCHAR));
694 if (dst)
696 MultiByteToWideChar(CP_UNIXCP, 0, data, -1, dst, filename_len);
697 ret = image_check_debug_link_gnu_id(dst, fmap_link, id, idlen);
698 HeapFree(GetProcessHeap(), 0, dst);
700 /* Trying relative path to build-id directory */
701 if (!ret)
703 dst = HeapAlloc(GetProcessHeap(), 0,
704 sizeof(L"/usr/lib/debug/.build-id/") + (3 + filename_len + idlen * 2) * sizeof(WCHAR));
705 if (dst)
707 WCHAR* p;
709 /* SIGH....
710 * some relative links are relative to /usr/lib/debug/.build-id, some others are from the directory
711 * where the alternate file is...
712 * so try both
714 p = memcpy(dst, L"/usr/lib/debug/.build-id/", sizeof(L"/usr/lib/debug/.build-id/"));
715 p += wcslen(dst);
716 MultiByteToWideChar(CP_UNIXCP, 0, data, -1, p, filename_len);
717 ret = image_check_debug_link_gnu_id(dst, fmap_link, id, idlen);
718 if (!ret)
720 p = append_hex(p, id, id + idlen);
721 *p++ = '/';
722 MultiByteToWideChar(CP_UNIXCP, 0, data, -1, p, filename_len);
723 ret = image_check_debug_link_gnu_id(dst, fmap_link, id, idlen);
725 HeapFree(GetProcessHeap(), 0, dst);
728 if (!ret)
730 HeapFree(GetProcessHeap(), 0, fmap_link);
731 WARN("Couldn't find a match for .gnu_debugaltlink section %s for %s\n", data, debugstr_w(module->modulename));
732 fmap_link = NULL;
737 image_unmap_section(&debugaltlink_sect);
738 if (fmap_link) TRACE("Found match .gnu_debugaltlink section for %s\n", debugstr_w(module->modulename));
739 return fmap_link;
742 /******************************************************************
743 * image_locate_build_id_target
745 * Try to find the .so file containing the debug info out of the build-id note information
747 static BOOL image_locate_build_id_target(struct image_file_map* fmap, const BYTE* id, unsigned idlen)
749 struct image_file_map* fmap_link = NULL;
750 WCHAR* p;
751 WCHAR* z;
753 fmap_link = HeapAlloc(GetProcessHeap(), 0, sizeof(*fmap_link));
754 if (!fmap_link) return FALSE;
756 p = malloc(sizeof(L"/usr/lib/debug/.build-id/") +
757 (idlen * 2 + 1) * sizeof(WCHAR) + sizeof(L".debug"));
758 wcscpy(p, L"/usr/lib/debug/.build-id/");
759 z = p + wcslen(p);
760 if (idlen)
762 z = append_hex(z, id, id + 1);
763 if (idlen > 1)
765 *z++ = L'/';
766 z = append_hex(z, id + 1, id + idlen);
769 memcpy(z, L".debug", sizeof(L".debug"));
770 TRACE("checking %s\n", wine_dbgstr_w(p));
772 if (image_check_debug_link_gnu_id(p, fmap_link, id, idlen))
774 free(p);
775 fmap->alternate = fmap_link;
776 return TRUE;
779 TRACE("not found\n");
780 free(p);
781 HeapFree(GetProcessHeap(), 0, fmap_link);
782 return FALSE;
785 /******************************************************************
786 * image_check_alternate
788 * Load alternate files for a given image file, looking at either .note.gnu_build-id
789 * or .gnu_debuglink sections.
791 BOOL image_check_alternate(struct image_file_map* fmap, const struct module* module)
793 BOOL ret = FALSE;
794 BOOL found = FALSE;
795 struct image_section_map buildid_sect, debuglink_sect;
797 /* if present, add the .gnu_debuglink file as an alternate to current one */
798 if (image_find_section(fmap, ".note.gnu.build-id", &buildid_sect))
800 const UINT32* note;
802 found = TRUE;
803 note = (const UINT32*)image_map_section(&buildid_sect);
804 if (note != IMAGE_NO_MAP)
806 /* the usual ELF note structure: name-size desc-size type <name> <desc> */
807 if (note[2] == NOTE_GNU_BUILD_ID)
809 ret = image_locate_build_id_target(fmap, (const BYTE*)(note + 3 + ((note[0] + 3) >> 2)), note[1]);
812 image_unmap_section(&buildid_sect);
814 /* if present, add the .gnu_debuglink file as an alternate to current one */
815 if (!ret && image_find_section(fmap, ".gnu_debuglink", &debuglink_sect))
817 const char* dbg_link;
819 found = TRUE;
820 dbg_link = image_map_section(&debuglink_sect);
821 if (dbg_link != IMAGE_NO_MAP)
823 /* The content of a debug link section is:
824 * 1/ a NULL terminated string, containing the file name for the
825 * debug info
826 * 2/ padding on 4 byte boundary
827 * 3/ CRC of the linked file
829 DWORD crc = *(const DWORD*)(dbg_link + ((DWORD_PTR)(strlen(dbg_link) + 4) & ~3));
830 ret = image_locate_debug_link(module, fmap, dbg_link, crc);
831 if (!ret)
832 WARN("Couldn't load linked debug file for %s\n",
833 debugstr_w(module->modulename));
835 image_unmap_section(&debuglink_sect);
837 return found ? ret : TRUE;
840 /***********************************************************************
841 * SymLoadModule (DBGHELP.@)
843 DWORD WINAPI SymLoadModule(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
844 PCSTR ModuleName, DWORD BaseOfDll, DWORD SizeOfDll)
846 return SymLoadModuleEx(hProcess, hFile, ImageName, ModuleName, BaseOfDll,
847 SizeOfDll, NULL, 0);
850 /***********************************************************************
851 * SymLoadModuleEx (DBGHELP.@)
853 DWORD64 WINAPI SymLoadModuleEx(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
854 PCSTR ModuleName, DWORD64 BaseOfDll, DWORD DllSize,
855 PMODLOAD_DATA Data, DWORD Flags)
857 PWSTR wImageName, wModuleName;
858 unsigned len;
859 DWORD64 ret;
861 TRACE("(%p %p %s %s %s %08lx %p %08lx)\n",
862 hProcess, hFile, debugstr_a(ImageName), debugstr_a(ModuleName),
863 wine_dbgstr_longlong(BaseOfDll), DllSize, Data, Flags);
865 if (ImageName)
867 len = MultiByteToWideChar(CP_ACP, 0, ImageName, -1, NULL, 0);
868 wImageName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
869 MultiByteToWideChar(CP_ACP, 0, ImageName, -1, wImageName, len);
871 else wImageName = NULL;
872 if (ModuleName)
874 len = MultiByteToWideChar(CP_ACP, 0, ModuleName, -1, NULL, 0);
875 wModuleName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
876 MultiByteToWideChar(CP_ACP, 0, ModuleName, -1, wModuleName, len);
878 else wModuleName = NULL;
880 ret = SymLoadModuleExW(hProcess, hFile, wImageName, wModuleName,
881 BaseOfDll, DllSize, Data, Flags);
882 HeapFree(GetProcessHeap(), 0, wImageName);
883 HeapFree(GetProcessHeap(), 0, wModuleName);
884 return ret;
887 /***********************************************************************
888 * SymLoadModuleExW (DBGHELP.@)
890 DWORD64 WINAPI SymLoadModuleExW(HANDLE hProcess, HANDLE hFile, PCWSTR wImageName,
891 PCWSTR wModuleName, DWORD64 BaseOfDll, DWORD SizeOfDll,
892 PMODLOAD_DATA Data, DWORD Flags)
894 struct process* pcs;
895 struct module* module = NULL;
896 struct module* altmodule;
898 TRACE("(%p %p %s %s %s %08lx %p %08lx)\n",
899 hProcess, hFile, debugstr_w(wImageName), debugstr_w(wModuleName),
900 wine_dbgstr_longlong(BaseOfDll), SizeOfDll, Data, Flags);
902 if (Data)
903 FIXME("Unsupported load data parameter %p for %s\n",
904 Data, debugstr_w(wImageName));
906 if (!(pcs = process_find_by_handle(hProcess))) return 0;
908 if (Flags & ~(SLMFLAG_VIRTUAL))
909 FIXME("Unsupported Flags %08lx for %s\n", Flags, debugstr_w(wImageName));
911 pcs->loader->synchronize_module_list(pcs);
913 /* this is a Wine extension to the API just to redo the synchronisation */
914 if (!wImageName && !hFile) return 0;
916 if (Flags & SLMFLAG_VIRTUAL)
918 if (!wImageName) return 0;
919 module = module_new(pcs, wImageName, DMT_PE, TRUE, BaseOfDll, SizeOfDll, 0, 0, IMAGE_FILE_MACHINE_UNKNOWN);
920 if (!module) return 0;
921 module->module.SymType = SymVirtual;
923 /* check if it's a builtin PE module with a containing ELF module */
924 else if (wImageName && module_is_container_loaded(pcs, wImageName, BaseOfDll))
926 /* force the loading of DLL as builtin */
927 module = pe_load_builtin_module(pcs, wImageName, BaseOfDll, SizeOfDll);
929 if (!module)
931 /* otherwise, try a regular PE module */
932 if (!(module = pe_load_native_module(pcs, wImageName, hFile, BaseOfDll, SizeOfDll)) &&
933 wImageName)
935 /* and finally an ELF or Mach-O module */
936 module = pcs->loader->load_module(pcs, wImageName, BaseOfDll);
939 if (!module)
941 WARN("Couldn't locate %s\n", debugstr_w(wImageName));
942 return 0;
944 /* by default module_new fills module.ModuleName from a derivation
945 * of LoadedImageName. Overwrite it, if we have better information
947 if (wModuleName)
948 module_set_module(module, wModuleName);
949 if (wImageName)
950 lstrcpynW(module->module.ImageName, wImageName, ARRAY_SIZE(module->module.ImageName));
952 for (altmodule = pcs->lmodules; altmodule; altmodule = altmodule->next)
954 if (altmodule != module && altmodule->type == module->type &&
955 module->module.BaseOfImage >= altmodule->module.BaseOfImage &&
956 module->module.BaseOfImage < altmodule->module.BaseOfImage + altmodule->module.ImageSize)
957 break;
959 if (altmodule)
961 /* we have a conflict as the new module cannot be found by its base address
962 * we need to get rid of one on the two modules
964 if (lstrcmpW(module->modulename, altmodule->modulename) != 0)
966 /* module overlaps an existing but different module... unload new module and return error */
967 WARN("%ls overlaps %ls\n", module->modulename, altmodule->modulename);
968 module_remove(pcs, module);
969 SetLastError(ERROR_INVALID_PARAMETER);
970 return 0;
972 /* loading same module at same address... don't change anything */
973 if (module->module.BaseOfImage == altmodule->module.BaseOfImage)
975 module_remove(pcs, module);
976 SetLastError(ERROR_SUCCESS);
977 return 0;
979 /* replace old module with new one, which will look like a shift of base address */
980 WARN("Shift module %ls from %I64x to %I64x\n",
981 module->modulename, altmodule->module.BaseOfImage, module->module.BaseOfImage);
982 module_remove(pcs, altmodule);
985 if ((dbghelp_options & SYMOPT_DEFERRED_LOADS) == 0 && !module_get_container(pcs, module))
986 module_load_debug(module);
987 return module->module.BaseOfImage;
990 /***********************************************************************
991 * SymLoadModule64 (DBGHELP.@)
993 DWORD64 WINAPI SymLoadModule64(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
994 PCSTR ModuleName, DWORD64 BaseOfDll, DWORD SizeOfDll)
996 return SymLoadModuleEx(hProcess, hFile, ImageName, ModuleName, BaseOfDll, SizeOfDll,
997 NULL, 0);
1000 /******************************************************************
1001 * module_remove
1004 BOOL module_remove(struct process* pcs, struct module* module)
1006 struct module_format*modfmt;
1007 struct module** p;
1008 unsigned i;
1010 TRACE("%s (%p)\n", debugstr_w(module->modulename), module);
1012 for (i = 0; i < DFI_LAST; i++)
1014 if ((modfmt = module->format_info[i]) && modfmt->remove)
1015 modfmt->remove(pcs, module->format_info[i]);
1017 hash_table_destroy(&module->ht_symbols);
1018 hash_table_destroy(&module->ht_types);
1019 HeapFree(GetProcessHeap(), 0, module->sources);
1020 HeapFree(GetProcessHeap(), 0, module->addr_sorttab);
1021 HeapFree(GetProcessHeap(), 0, module->real_path);
1022 pool_destroy(&module->pool);
1023 /* native dbghelp doesn't invoke registered callback(,CBA_SYMBOLS_UNLOADED,) here
1024 * so do we
1026 for (p = &pcs->lmodules; *p; p = &(*p)->next)
1028 if (*p == module)
1030 *p = module->next;
1031 HeapFree(GetProcessHeap(), 0, module);
1032 return TRUE;
1035 FIXME("This shouldn't happen\n");
1036 return FALSE;
1039 /******************************************************************
1040 * SymUnloadModule (DBGHELP.@)
1043 BOOL WINAPI SymUnloadModule(HANDLE hProcess, DWORD BaseOfDll)
1045 return SymUnloadModule64(hProcess, BaseOfDll);
1048 /******************************************************************
1049 * SymUnloadModule64 (DBGHELP.@)
1052 BOOL WINAPI SymUnloadModule64(HANDLE hProcess, DWORD64 BaseOfDll)
1054 struct process* pcs;
1055 struct module* module;
1057 pcs = process_find_by_handle(hProcess);
1058 if (!pcs) return FALSE;
1059 module = module_find_by_addr(pcs, BaseOfDll, DMT_UNKNOWN);
1060 if (!module) return FALSE;
1061 /* remove local scope if defined inside this module */
1062 if (pcs->localscope_pc >= module->module.BaseOfImage &&
1063 pcs->localscope_pc < module->module.BaseOfImage + module->module.ImageSize)
1065 pcs->localscope_pc = 0;
1066 pcs->localscope_symt = NULL;
1068 module_remove(pcs, module);
1069 return TRUE;
1072 /******************************************************************
1073 * SymEnumerateModules (DBGHELP.@)
1076 struct enum_modW64_32
1078 PSYM_ENUMMODULES_CALLBACK cb;
1079 PVOID user;
1080 char module[MAX_PATH];
1083 static BOOL CALLBACK enum_modW64_32(PCWSTR name, DWORD64 base, PVOID user)
1085 struct enum_modW64_32* x = user;
1087 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
1088 return x->cb(x->module, (DWORD)base, x->user);
1091 BOOL WINAPI SymEnumerateModules(HANDLE hProcess,
1092 PSYM_ENUMMODULES_CALLBACK EnumModulesCallback,
1093 PVOID UserContext)
1095 struct enum_modW64_32 x;
1097 x.cb = EnumModulesCallback;
1098 x.user = UserContext;
1100 return SymEnumerateModulesW64(hProcess, enum_modW64_32, &x);
1103 /******************************************************************
1104 * SymEnumerateModules64 (DBGHELP.@)
1107 struct enum_modW64_64
1109 PSYM_ENUMMODULES_CALLBACK64 cb;
1110 PVOID user;
1111 char module[MAX_PATH];
1114 static BOOL CALLBACK enum_modW64_64(PCWSTR name, DWORD64 base, PVOID user)
1116 struct enum_modW64_64* x = user;
1118 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
1119 return x->cb(x->module, base, x->user);
1122 BOOL WINAPI SymEnumerateModules64(HANDLE hProcess,
1123 PSYM_ENUMMODULES_CALLBACK64 EnumModulesCallback,
1124 PVOID UserContext)
1126 struct enum_modW64_64 x;
1128 x.cb = EnumModulesCallback;
1129 x.user = UserContext;
1131 return SymEnumerateModulesW64(hProcess, enum_modW64_64, &x);
1134 /******************************************************************
1135 * SymEnumerateModulesW64 (DBGHELP.@)
1138 BOOL WINAPI SymEnumerateModulesW64(HANDLE hProcess,
1139 PSYM_ENUMMODULES_CALLBACKW64 EnumModulesCallback,
1140 PVOID UserContext)
1142 struct process* pcs = process_find_by_handle(hProcess);
1143 struct module* module;
1145 if (!pcs) return FALSE;
1147 for (module = pcs->lmodules; module; module = module->next)
1149 if (!dbghelp_opt_native &&
1150 (module->type == DMT_ELF || module->type == DMT_MACHO))
1151 continue;
1152 if (!EnumModulesCallback(module->modulename,
1153 module->module.BaseOfImage, UserContext))
1154 break;
1156 return TRUE;
1159 /******************************************************************
1160 * EnumerateLoadedModules64 (DBGHELP.@)
1163 struct enum_load_modW64_64
1165 PENUMLOADED_MODULES_CALLBACK64 cb;
1166 PVOID user;
1167 char module[MAX_PATH];
1170 static BOOL CALLBACK enum_load_modW64_64(PCWSTR name, DWORD64 base, ULONG size,
1171 PVOID user)
1173 struct enum_load_modW64_64* x = user;
1175 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
1176 return x->cb(x->module, base, size, x->user);
1179 BOOL WINAPI EnumerateLoadedModules64(HANDLE hProcess,
1180 PENUMLOADED_MODULES_CALLBACK64 EnumLoadedModulesCallback,
1181 PVOID UserContext)
1183 struct enum_load_modW64_64 x;
1185 x.cb = EnumLoadedModulesCallback;
1186 x.user = UserContext;
1188 return EnumerateLoadedModulesW64(hProcess, enum_load_modW64_64, &x);
1191 /******************************************************************
1192 * EnumerateLoadedModules (DBGHELP.@)
1195 struct enum_load_modW64_32
1197 PENUMLOADED_MODULES_CALLBACK cb;
1198 PVOID user;
1199 char module[MAX_PATH];
1202 static BOOL CALLBACK enum_load_modW64_32(PCWSTR name, DWORD64 base, ULONG size,
1203 PVOID user)
1205 struct enum_load_modW64_32* x = user;
1206 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
1207 return x->cb(x->module, (DWORD)base, size, x->user);
1210 BOOL WINAPI EnumerateLoadedModules(HANDLE hProcess,
1211 PENUMLOADED_MODULES_CALLBACK EnumLoadedModulesCallback,
1212 PVOID UserContext)
1214 struct enum_load_modW64_32 x;
1216 x.cb = EnumLoadedModulesCallback;
1217 x.user = UserContext;
1219 return EnumerateLoadedModulesW64(hProcess, enum_load_modW64_32, &x);
1222 /******************************************************************
1223 * EnumerateLoadedModulesW64 (DBGHELP.@)
1226 BOOL WINAPI EnumerateLoadedModulesW64(HANDLE hProcess,
1227 PENUMLOADED_MODULES_CALLBACKW64 EnumLoadedModulesCallback,
1228 PVOID UserContext)
1230 HMODULE* hMods;
1231 WCHAR baseW[256], modW[256];
1232 DWORD i, sz;
1233 MODULEINFO mi;
1235 hMods = HeapAlloc(GetProcessHeap(), 0, 256 * sizeof(hMods[0]));
1236 if (!hMods) return FALSE;
1238 if (!EnumProcessModules(hProcess, hMods, 256 * sizeof(hMods[0]), &sz))
1240 /* hProcess should also be a valid process handle !! */
1241 HeapFree(GetProcessHeap(), 0, hMods);
1242 return FALSE;
1244 if (sz > 256 * sizeof(hMods[0]))
1246 hMods = HeapReAlloc(GetProcessHeap(), 0, hMods, sz);
1247 if (!hMods || !EnumProcessModules(hProcess, hMods, sz, &sz))
1248 return FALSE;
1250 sz /= sizeof(HMODULE);
1251 for (i = 0; i < sz; i++)
1253 if (!GetModuleInformation(hProcess, hMods[i], &mi, sizeof(mi)) ||
1254 !GetModuleBaseNameW(hProcess, hMods[i], baseW, ARRAY_SIZE(baseW)))
1255 continue;
1256 module_fill_module(baseW, modW, ARRAY_SIZE(modW));
1257 EnumLoadedModulesCallback(modW, (DWORD_PTR)mi.lpBaseOfDll, mi.SizeOfImage,
1258 UserContext);
1260 HeapFree(GetProcessHeap(), 0, hMods);
1262 return sz != 0 && i == sz;
1265 static void dbghelp_str_WtoA(const WCHAR *src, char *dst, int dst_len)
1267 WideCharToMultiByte(CP_ACP, 0, src, -1, dst, dst_len - 1, NULL, NULL);
1268 dst[dst_len - 1] = 0;
1271 /******************************************************************
1272 * SymGetModuleInfo (DBGHELP.@)
1275 BOOL WINAPI SymGetModuleInfo(HANDLE hProcess, DWORD dwAddr,
1276 PIMAGEHLP_MODULE ModuleInfo)
1278 IMAGEHLP_MODULE mi;
1279 IMAGEHLP_MODULEW64 miw64;
1281 if (sizeof(mi) < ModuleInfo->SizeOfStruct) FIXME("Wrong size\n");
1283 miw64.SizeOfStruct = sizeof(miw64);
1284 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
1286 mi.SizeOfStruct = ModuleInfo->SizeOfStruct;
1287 mi.BaseOfImage = miw64.BaseOfImage;
1288 mi.ImageSize = miw64.ImageSize;
1289 mi.TimeDateStamp = miw64.TimeDateStamp;
1290 mi.CheckSum = miw64.CheckSum;
1291 mi.NumSyms = miw64.NumSyms;
1292 mi.SymType = miw64.SymType;
1293 dbghelp_str_WtoA(miw64.ModuleName, mi.ModuleName, sizeof(mi.ModuleName));
1294 dbghelp_str_WtoA(miw64.ImageName, mi.ImageName, sizeof(mi.ImageName));
1295 dbghelp_str_WtoA(miw64.LoadedImageName, mi.LoadedImageName, sizeof(mi.LoadedImageName));
1297 memcpy(ModuleInfo, &mi, ModuleInfo->SizeOfStruct);
1299 return TRUE;
1302 /******************************************************************
1303 * SymGetModuleInfoW (DBGHELP.@)
1306 BOOL WINAPI SymGetModuleInfoW(HANDLE hProcess, DWORD dwAddr,
1307 PIMAGEHLP_MODULEW ModuleInfo)
1309 IMAGEHLP_MODULEW64 miw64;
1310 IMAGEHLP_MODULEW miw;
1312 if (sizeof(miw) < ModuleInfo->SizeOfStruct) FIXME("Wrong size\n");
1314 miw64.SizeOfStruct = sizeof(miw64);
1315 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
1317 miw.SizeOfStruct = ModuleInfo->SizeOfStruct;
1318 miw.BaseOfImage = miw64.BaseOfImage;
1319 miw.ImageSize = miw64.ImageSize;
1320 miw.TimeDateStamp = miw64.TimeDateStamp;
1321 miw.CheckSum = miw64.CheckSum;
1322 miw.NumSyms = miw64.NumSyms;
1323 miw.SymType = miw64.SymType;
1324 lstrcpyW(miw.ModuleName, miw64.ModuleName);
1325 lstrcpyW(miw.ImageName, miw64.ImageName);
1326 lstrcpyW(miw.LoadedImageName, miw64.LoadedImageName);
1327 memcpy(ModuleInfo, &miw, ModuleInfo->SizeOfStruct);
1329 return TRUE;
1332 /******************************************************************
1333 * SymGetModuleInfo64 (DBGHELP.@)
1336 BOOL WINAPI SymGetModuleInfo64(HANDLE hProcess, DWORD64 dwAddr,
1337 PIMAGEHLP_MODULE64 ModuleInfo)
1339 IMAGEHLP_MODULE64 mi64;
1340 IMAGEHLP_MODULEW64 miw64;
1342 if (sizeof(mi64) < ModuleInfo->SizeOfStruct)
1344 SetLastError(ERROR_MOD_NOT_FOUND); /* NOTE: native returns this error */
1345 WARN("Wrong size %lu\n", ModuleInfo->SizeOfStruct);
1346 return FALSE;
1349 miw64.SizeOfStruct = sizeof(miw64);
1350 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
1352 mi64.SizeOfStruct = ModuleInfo->SizeOfStruct;
1353 mi64.BaseOfImage = miw64.BaseOfImage;
1354 mi64.ImageSize = miw64.ImageSize;
1355 mi64.TimeDateStamp = miw64.TimeDateStamp;
1356 mi64.CheckSum = miw64.CheckSum;
1357 mi64.NumSyms = miw64.NumSyms;
1358 mi64.SymType = miw64.SymType;
1359 dbghelp_str_WtoA(miw64.ModuleName, mi64.ModuleName, sizeof(mi64.ModuleName));
1360 dbghelp_str_WtoA(miw64.ImageName, mi64.ImageName, sizeof(mi64.ImageName));
1361 dbghelp_str_WtoA(miw64.LoadedImageName, mi64.LoadedImageName, sizeof(mi64.LoadedImageName));
1362 dbghelp_str_WtoA(miw64.LoadedPdbName, mi64.LoadedPdbName, sizeof(mi64.LoadedPdbName));
1364 mi64.CVSig = miw64.CVSig;
1365 dbghelp_str_WtoA(miw64.CVData, mi64.CVData, sizeof(mi64.CVData));
1366 mi64.PdbSig = miw64.PdbSig;
1367 mi64.PdbSig70 = miw64.PdbSig70;
1368 mi64.PdbAge = miw64.PdbAge;
1369 mi64.PdbUnmatched = miw64.PdbUnmatched;
1370 mi64.DbgUnmatched = miw64.DbgUnmatched;
1371 mi64.LineNumbers = miw64.LineNumbers;
1372 mi64.GlobalSymbols = miw64.GlobalSymbols;
1373 mi64.TypeInfo = miw64.TypeInfo;
1374 mi64.SourceIndexed = miw64.SourceIndexed;
1375 mi64.Publics = miw64.Publics;
1376 mi64.MachineType = miw64.MachineType;
1377 mi64.Reserved = miw64.Reserved;
1379 memcpy(ModuleInfo, &mi64, ModuleInfo->SizeOfStruct);
1381 return TRUE;
1384 /******************************************************************
1385 * SymGetModuleInfoW64 (DBGHELP.@)
1388 BOOL WINAPI SymGetModuleInfoW64(HANDLE hProcess, DWORD64 dwAddr,
1389 PIMAGEHLP_MODULEW64 ModuleInfo)
1391 struct process* pcs = process_find_by_handle(hProcess);
1392 struct module* module;
1393 IMAGEHLP_MODULEW64 miw64;
1395 TRACE("%p %s %p\n", hProcess, wine_dbgstr_longlong(dwAddr), ModuleInfo);
1397 if (!pcs) return FALSE;
1398 if (ModuleInfo->SizeOfStruct > sizeof(*ModuleInfo)) return FALSE;
1399 module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
1400 if (!module) return FALSE;
1402 miw64 = module->module;
1404 /* update debug information from container if any */
1405 if (module->module.SymType == SymNone)
1407 module = module_get_container(pcs, module);
1408 if (module && module->module.SymType != SymNone)
1410 miw64.SymType = module->module.SymType;
1411 miw64.NumSyms = module->module.NumSyms;
1414 memcpy(ModuleInfo, &miw64, ModuleInfo->SizeOfStruct);
1415 return TRUE;
1418 /***********************************************************************
1419 * SymGetModuleBase (DBGHELP.@)
1421 DWORD WINAPI SymGetModuleBase(HANDLE hProcess, DWORD dwAddr)
1423 return (DWORD)SymGetModuleBase64(hProcess, dwAddr);
1426 /***********************************************************************
1427 * SymGetModuleBase64 (DBGHELP.@)
1429 DWORD64 WINAPI SymGetModuleBase64(HANDLE hProcess, DWORD64 dwAddr)
1431 struct process* pcs = process_find_by_handle(hProcess);
1432 struct module* module;
1434 if (!pcs) return 0;
1435 module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
1436 if (!module) return 0;
1437 return module->module.BaseOfImage;
1440 /******************************************************************
1441 * module_reset_debug_info
1442 * Removes any debug information linked to a given module.
1444 void module_reset_debug_info(struct module* module)
1446 module->sortlist_valid = TRUE;
1447 module->sorttab_size = 0;
1448 module->addr_sorttab = NULL;
1449 module->num_sorttab = module->num_symbols = 0;
1450 hash_table_destroy(&module->ht_symbols);
1451 module->ht_symbols.num_buckets = 0;
1452 module->ht_symbols.buckets = NULL;
1453 hash_table_destroy(&module->ht_types);
1454 module->ht_types.num_buckets = 0;
1455 module->ht_types.buckets = NULL;
1456 module->vtypes.num_elts = 0;
1457 hash_table_destroy(&module->ht_symbols);
1458 module->sources_used = module->sources_alloc = 0;
1459 module->sources = NULL;
1462 /******************************************************************
1463 * SymRefreshModuleList (DBGHELP.@)
1465 BOOL WINAPI SymRefreshModuleList(HANDLE hProcess)
1467 struct process* pcs;
1469 TRACE("(%p)\n", hProcess);
1471 if (!(pcs = process_find_by_handle(hProcess))) return FALSE;
1473 return pcs->loader->synchronize_module_list(pcs);
1476 /***********************************************************************
1477 * SymFunctionTableAccess (DBGHELP.@)
1479 PVOID WINAPI SymFunctionTableAccess(HANDLE hProcess, DWORD AddrBase)
1481 return SymFunctionTableAccess64(hProcess, AddrBase);
1484 /***********************************************************************
1485 * SymFunctionTableAccess64 (DBGHELP.@)
1487 PVOID WINAPI SymFunctionTableAccess64(HANDLE hProcess, DWORD64 AddrBase)
1489 struct process* pcs = process_find_by_handle(hProcess);
1490 struct module* module;
1492 if (!pcs) return NULL;
1493 module = module_find_by_addr(pcs, AddrBase, DMT_UNKNOWN);
1494 if (!module || !module->cpu->find_runtime_function) return NULL;
1496 return module->cpu->find_runtime_function(module, AddrBase);
1499 static BOOL native_synchronize_module_list(struct process* pcs)
1501 return FALSE;
1504 static struct module* native_load_module(struct process* pcs, const WCHAR* name, ULONG_PTR addr)
1506 return NULL;
1509 static BOOL native_load_debug_info(struct process* process, struct module* module)
1511 module->module.SymType = SymNone;
1512 return FALSE;
1515 static BOOL native_enum_modules(struct process *process, enum_modules_cb cb, void* user)
1517 return FALSE;
1520 static BOOL native_fetch_file_info(struct process* process, const WCHAR* name, ULONG_PTR load_addr, DWORD_PTR* base,
1521 DWORD* size, DWORD* checksum)
1523 return FALSE;
1526 const struct loader_ops no_loader_ops =
1528 native_synchronize_module_list,
1529 native_load_module,
1530 native_load_debug_info,
1531 native_enum_modules,
1532 native_fetch_file_info,