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
27 #include "dbghelp_private.h"
28 #include "image_private.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
;
47 for (e
= ext
; *e
; e
++)
50 if (l
>= len
) return 0;
51 if (wcsnicmp(&ptr
[len
- l
], *e
, l
)) continue;
57 static const WCHAR
* get_filename(const WCHAR
* name
, const WCHAR
* endptr
)
61 if (!endptr
) endptr
= name
+ lstrlenW(name
);
62 for (ptr
= endptr
- 1; ptr
>= name
; ptr
--)
64 if (*ptr
== '/' || *ptr
== '\\') break;
69 static BOOL
is_wine_loader(const WCHAR
*module
)
71 const WCHAR
*filename
= get_filename(module
, NULL
);
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
);
86 buffer
= heap_alloc( sizeof(L
"wine") + 2 * sizeof(WCHAR
) );
87 lstrcpyW( buffer
, L
"wine" );
90 if (!wcscmp( filename
, buffer
))
93 lstrcatW( buffer
, L
"64" );
94 if (!wcscmp( filename
, buffer
))
101 static void module_fill_module(const WCHAR
* in
, WCHAR
* out
, size_t size
)
103 const WCHAR
*ptr
, *endptr
;
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
));
112 if (is_wine_loader(out
))
113 lstrcpynW(out
, S_WineLoaderW
, size
);
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
)
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
));
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';
156 TRACE("returning %s\n", debugstr_w(altname
));
160 static const char* get_module_type(enum module_type type
, BOOL
virtual)
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
;
182 assert(type
== DMT_ELF
|| type
== DMT_PE
|| type
== DMT_MACHO
);
183 if (!(module
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*module
))))
186 module
->next
= pcs
->lmodules
;
187 pcs
->lmodules
= module
;
189 TRACE("=> %s %I64x-%I64x %s\n",
190 get_module_type(type
, virtual), mod_addr
, mod_addr
+ size
, debugstr_w(name
));
192 pool_init(&module
->pool
, 65536);
194 module
->process
= pcs
;
195 module
->module
.SizeOfStruct
= sizeof(module
->module
);
196 module
->module
.BaseOfImage
= mod_addr
;
197 module
->module
.ImageSize
= size
;
198 module_set_module(module
, name
);
199 module
->module
.ImageName
[0] = '\0';
200 lstrcpynW(module
->module
.LoadedImageName
, name
, ARRAY_SIZE(module
->module
.LoadedImageName
));
201 module
->module
.SymType
= SymDeferred
;
202 module
->module
.NumSyms
= 0;
203 module
->module
.TimeDateStamp
= stamp
;
204 module
->module
.CheckSum
= checksum
;
206 memset(module
->module
.LoadedPdbName
, 0, sizeof(module
->module
.LoadedPdbName
));
207 module
->module
.CVSig
= 0;
208 memset(module
->module
.CVData
, 0, sizeof(module
->module
.CVData
));
209 module
->module
.PdbSig
= 0;
210 memset(&module
->module
.PdbSig70
, 0, sizeof(module
->module
.PdbSig70
));
211 module
->module
.PdbAge
= 0;
212 module
->module
.PdbUnmatched
= FALSE
;
213 module
->module
.DbgUnmatched
= FALSE
;
214 module
->module
.LineNumbers
= FALSE
;
215 module
->module
.GlobalSymbols
= FALSE
;
216 module
->module
.TypeInfo
= FALSE
;
217 module
->module
.SourceIndexed
= FALSE
;
218 module
->module
.Publics
= FALSE
;
219 module
->module
.MachineType
= machine
;
220 module
->module
.Reserved
= 0;
222 module
->reloc_delta
= 0;
224 module
->is_virtual
= virtual;
225 for (i
= 0; i
< DFI_LAST
; i
++) module
->format_info
[i
] = NULL
;
226 module
->sortlist_valid
= FALSE
;
227 module
->sorttab_size
= 0;
228 module
->addr_sorttab
= NULL
;
229 module
->num_sorttab
= 0;
230 module
->num_symbols
= 0;
231 module
->cpu
= cpu_find(machine
);
233 module
->cpu
= dbghelp_current_cpu
;
235 vector_init(&module
->vsymt
, sizeof(struct symt
*), 128);
236 vector_init(&module
->vcustom_symt
, sizeof(struct symt
*), 16);
237 /* FIXME: this seems a bit too high (on a per module basis)
238 * need some statistics about this
240 hash_table_init(&module
->pool
, &module
->ht_symbols
, 4096);
241 hash_table_init(&module
->pool
, &module
->ht_types
, 4096);
242 vector_init(&module
->vtypes
, sizeof(struct symt
*), 32);
244 module
->sources_used
= 0;
245 module
->sources_alloc
= 0;
247 wine_rb_init(&module
->sources_offsets_tree
, source_rb_compare
);
249 /* add top level symbol */
250 module
->top
= symt_new_module(module
);
255 BOOL
module_init_pair(struct module_pair
* pair
, HANDLE hProcess
, DWORD64 addr
)
257 if (!(pair
->pcs
= process_find_by_handle(hProcess
))) return FALSE
;
258 pair
->requested
= module_find_by_addr(pair
->pcs
, addr
, DMT_UNKNOWN
);
259 return module_get_debug(pair
);
262 /***********************************************************************
263 * module_find_by_nameW
266 struct module
* module_find_by_nameW(const struct process
* pcs
, const WCHAR
* name
)
268 struct module
* module
;
270 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
272 if (!wcsicmp(name
, module
->modulename
)) return module
;
274 SetLastError(ERROR_INVALID_NAME
);
278 struct module
* module_find_by_nameA(const struct process
* pcs
, const char* name
)
280 WCHAR wname
[MAX_PATH
];
282 MultiByteToWideChar(CP_ACP
, 0, name
, -1, wname
, ARRAY_SIZE(wname
));
283 return module_find_by_nameW(pcs
, wname
);
286 /***********************************************************************
287 * module_is_already_loaded
290 struct module
* module_is_already_loaded(const struct process
* pcs
, const WCHAR
* name
)
292 struct module
* module
;
293 const WCHAR
* filename
;
295 /* first compare the loaded image name... */
296 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
298 if (!wcsicmp(name
, module
->module
.LoadedImageName
))
301 /* then compare the standard filenames (without the path) ... */
302 filename
= get_filename(name
, NULL
);
303 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
305 if (!wcsicmp(filename
, get_filename(module
->module
.LoadedImageName
, NULL
)))
308 SetLastError(ERROR_INVALID_NAME
);
312 /***********************************************************************
313 * module_get_container
316 static struct module
* module_get_container(const struct process
* pcs
,
317 const struct module
* inner
)
319 struct module
* module
;
321 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
323 if (module
!= inner
&&
324 module
->module
.BaseOfImage
<= inner
->module
.BaseOfImage
&&
325 module
->module
.BaseOfImage
+ module
->module
.ImageSize
>=
326 inner
->module
.BaseOfImage
+ inner
->module
.ImageSize
)
332 /***********************************************************************
333 * module_get_containee
336 struct module
* module_get_containee(const struct process
* pcs
, const struct module
* outer
)
338 struct module
* module
;
340 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
342 if (module
!= outer
&&
343 outer
->module
.BaseOfImage
<= module
->module
.BaseOfImage
&&
344 outer
->module
.BaseOfImage
+ outer
->module
.ImageSize
>=
345 module
->module
.BaseOfImage
+ module
->module
.ImageSize
)
351 BOOL
module_load_debug(struct module
* module
)
353 IMAGEHLP_DEFERRED_SYMBOL_LOADW64 idslW64
;
355 /* if deferred, force loading */
356 if (module
->module
.SymType
== SymDeferred
)
360 if (module
->is_virtual
) ret
= FALSE
;
361 else if (module
->type
== DMT_PE
)
363 idslW64
.SizeOfStruct
= sizeof(idslW64
);
364 idslW64
.BaseOfImage
= module
->module
.BaseOfImage
;
365 idslW64
.CheckSum
= module
->module
.CheckSum
;
366 idslW64
.TimeDateStamp
= module
->module
.TimeDateStamp
;
367 memcpy(idslW64
.FileName
, module
->module
.ImageName
,
368 sizeof(module
->module
.ImageName
));
369 idslW64
.Reparse
= FALSE
;
370 idslW64
.hFile
= INVALID_HANDLE_VALUE
;
372 pcs_callback(module
->process
, CBA_DEFERRED_SYMBOL_LOAD_START
, &idslW64
);
373 ret
= pe_load_debug_info(module
->process
, module
);
374 pcs_callback(module
->process
,
375 ret
? CBA_DEFERRED_SYMBOL_LOAD_COMPLETE
: CBA_DEFERRED_SYMBOL_LOAD_FAILURE
,
378 else ret
= module
->process
->loader
->load_debug_info(module
->process
, module
);
380 if (!ret
) module
->module
.SymType
= SymNone
;
381 assert(module
->module
.SymType
!= SymDeferred
);
382 module
->module
.NumSyms
= module
->ht_symbols
.num_elts
;
384 return module
->module
.SymType
!= SymNone
;
387 /******************************************************************
390 * get the debug information from a module:
391 * - if the module's type is deferred, then force loading of debug info (and return
393 * - if the module has no debug info and has an ELF container, then return the ELF
394 * container (and also force the ELF container's debug info loading if deferred)
395 * - otherwise return the module itself if it has some debug info
397 BOOL
module_get_debug(struct module_pair
* pair
)
399 if (!pair
->requested
) return FALSE
;
400 /* for a PE builtin, always get info from container */
401 if (!(pair
->effective
= module_get_container(pair
->pcs
, pair
->requested
)))
402 pair
->effective
= pair
->requested
;
403 return module_load_debug(pair
->effective
);
406 /***********************************************************************
407 * module_find_by_addr
409 * either the addr where module is loaded, or any address inside the
412 struct module
* module_find_by_addr(const struct process
* pcs
, DWORD64 addr
,
413 enum module_type type
)
415 struct module
* module
;
417 if (type
== DMT_UNKNOWN
)
419 if ((module
= module_find_by_addr(pcs
, addr
, DMT_PE
)) ||
420 (module
= module_find_by_addr(pcs
, addr
, DMT_ELF
)) ||
421 (module
= module_find_by_addr(pcs
, addr
, DMT_MACHO
)))
426 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
428 if (type
== module
->type
&& addr
>= module
->module
.BaseOfImage
&&
429 addr
< module
->module
.BaseOfImage
+ module
->module
.ImageSize
)
433 SetLastError(ERROR_MOD_NOT_FOUND
);
437 /******************************************************************
438 * module_is_container_loaded
440 * checks whether the native container, for a (supposed) PE builtin is
443 static BOOL
module_is_container_loaded(const struct process
* pcs
,
444 const WCHAR
* ImageName
, DWORD64 base
)
447 struct module
* module
;
448 PCWSTR filename
, modname
;
450 if (!base
) return FALSE
;
451 filename
= get_filename(ImageName
, NULL
);
452 len
= lstrlenW(filename
);
454 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
456 if ((module
->type
== DMT_ELF
|| module
->type
== DMT_MACHO
) &&
457 base
>= module
->module
.BaseOfImage
&&
458 base
< module
->module
.BaseOfImage
+ module
->module
.ImageSize
)
460 modname
= get_filename(module
->module
.LoadedImageName
, NULL
);
461 if (!wcsnicmp(modname
, filename
, len
) &&
462 !memcmp(modname
+ len
, L
".so", 3 * sizeof(WCHAR
)))
468 /* likely a native PE module */
469 WARN("Couldn't find container for %s\n", debugstr_w(ImageName
));
473 static BOOL
image_check_debug_link_crc(const WCHAR
* file
, struct image_file_map
* fmap
, DWORD link_crc
)
482 path
= get_dos_file_name(file
);
483 handle
= CreateFileW(path
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, NULL
);
485 if (handle
== INVALID_HANDLE_VALUE
) return FALSE
;
487 crc
= calc_crc32(handle
);
490 WARN("Bad CRC for file %s (got %08lx while expecting %08lx)\n", debugstr_w(file
), crc
, link_crc
);
495 SetFilePointer(handle
, 0, 0, FILE_BEGIN
);
496 if (ReadFile(handle
, &magic
, sizeof(magic
), &read_bytes
, NULL
) && magic
== IMAGE_DOS_SIGNATURE
)
497 ret
= pe_map_file(handle
, fmap
, DMT_PE
);
499 ret
= elf_map_handle(handle
, fmap
);
504 static BOOL
image_check_debug_link_gnu_id(const WCHAR
* file
, struct image_file_map
* fmap
, const BYTE
* id
, unsigned idlen
)
506 struct image_section_map buildid_sect
;
513 path
= get_dos_file_name(file
);
514 handle
= CreateFileW(path
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, NULL
);
516 if (handle
== INVALID_HANDLE_VALUE
) return FALSE
;
518 TRACE("Located debug information file at %s\n", debugstr_w(file
));
520 if (ReadFile(handle
, &magic
, sizeof(magic
), &read_bytes
, NULL
) && magic
== IMAGE_DOS_SIGNATURE
)
521 ret
= pe_map_file(handle
, fmap
, DMT_PE
);
523 ret
= elf_map_handle(handle
, fmap
);
526 if (ret
&& image_find_section(fmap
, ".note.gnu.build-id", &buildid_sect
))
530 note
= (const UINT32
*)image_map_section(&buildid_sect
);
531 if (note
!= IMAGE_NO_MAP
)
533 /* the usual ELF note structure: name-size desc-size type <name> <desc> */
534 if (note
[2] == NOTE_GNU_BUILD_ID
)
536 if (note
[1] == idlen
&& !memcmp(note
+ 3 + ((note
[0] + 3) >> 2), id
, idlen
))
538 WARN("mismatch in buildid information for %s\n", wine_dbgstr_w(file
));
541 image_unmap_section(&buildid_sect
);
542 image_unmap_file(fmap
);
547 /******************************************************************
548 * image_locate_debug_link
550 * Locate a filename from a .gnu_debuglink section, using the same
552 * "If the full name of the directory containing the executable is
553 * execdir, and the executable has a debug link that specifies the
554 * name debugfile, then GDB will automatically search for the
555 * debugging information file in three places:
556 * - the directory containing the executable file (that is, it
557 * will look for a file named `execdir/debugfile',
558 * - a subdirectory of that directory named `.debug' (that is, the
559 * file `execdir/.debug/debugfile', and
560 * - a subdirectory of the global debug file directory that includes
561 * the executable's full path, and the name from the link (that is,
562 * the file `globaldebugdir/execdir/debugfile', where globaldebugdir
563 * is the global debug file directory, and execdir has been turned
564 * into a relative path)." (from GDB manual)
566 static BOOL
image_locate_debug_link(const struct module
* module
, struct image_file_map
* fmap
, const char* filename
, DWORD crc
)
568 static const WCHAR globalDebugDirW
[] = {'/','u','s','r','/','l','i','b','/','d','e','b','u','g','/'};
569 static const WCHAR dotDebugW
[] = {'.','d','e','b','u','g','/'};
570 const size_t globalDebugDirLen
= ARRAY_SIZE(globalDebugDirW
);
571 size_t filename_len
, path_len
;
575 struct image_file_map
* fmap_link
= NULL
;
577 fmap_link
= HeapAlloc(GetProcessHeap(), 0, sizeof(*fmap_link
));
578 if (!fmap_link
) return FALSE
;
580 filename_len
= MultiByteToWideChar(CP_UNIXCP
, 0, filename
, -1, NULL
, 0);
581 path_len
= lstrlenW(module
->module
.LoadedImageName
);
582 if (module
->real_path
) path_len
= max(path_len
, lstrlenW(module
->real_path
));
583 p
= HeapAlloc(GetProcessHeap(), 0,
584 (globalDebugDirLen
+ path_len
+ 6 + 1 + filename_len
+ 1) * sizeof(WCHAR
));
587 /* we prebuild the string with "execdir" */
588 lstrcpyW(p
, module
->module
.LoadedImageName
);
590 if ((slash2
= wcsrchr(slash
, '/'))) slash
= slash2
+ 1;
591 if ((slash2
= wcsrchr(slash
, '\\'))) slash
= slash2
+ 1;
593 /* testing execdir/filename */
594 MultiByteToWideChar(CP_UNIXCP
, 0, filename
, -1, slash
, filename_len
);
595 if (image_check_debug_link_crc(p
, fmap_link
, crc
)) goto found
;
597 /* testing execdir/.debug/filename */
598 memcpy(slash
, dotDebugW
, sizeof(dotDebugW
));
599 MultiByteToWideChar(CP_UNIXCP
, 0, filename
, -1, slash
+ ARRAY_SIZE(dotDebugW
), filename_len
);
600 if (image_check_debug_link_crc(p
, fmap_link
, crc
)) goto found
;
602 if (module
->real_path
)
604 lstrcpyW(p
, module
->real_path
);
606 if ((slash2
= wcsrchr(slash
, '/'))) slash
= slash2
+ 1;
607 if ((slash2
= wcsrchr(slash
, '\\'))) slash
= slash2
+ 1;
608 MultiByteToWideChar(CP_UNIXCP
, 0, filename
, -1, slash
, filename_len
);
609 if (image_check_debug_link_crc(p
, fmap_link
, crc
)) goto found
;
612 /* testing globaldebugdir/execdir/filename */
613 memmove(p
+ globalDebugDirLen
, p
, (slash
- p
) * sizeof(WCHAR
));
614 memcpy(p
, globalDebugDirW
, globalDebugDirLen
* sizeof(WCHAR
));
615 slash
+= globalDebugDirLen
;
616 MultiByteToWideChar(CP_UNIXCP
, 0, filename
, -1, slash
, filename_len
);
617 if (image_check_debug_link_crc(p
, fmap_link
, crc
)) goto found
;
619 /* finally testing filename */
620 if (image_check_debug_link_crc(slash
, fmap_link
, crc
)) goto found
;
623 WARN("Couldn't locate or map %s\n", filename
);
624 HeapFree(GetProcessHeap(), 0, p
);
625 HeapFree(GetProcessHeap(), 0, fmap_link
);
629 TRACE("Located debug information file %s at %s\n", filename
, debugstr_w(p
));
630 HeapFree(GetProcessHeap(), 0, p
);
631 fmap
->alternate
= fmap_link
;
635 static WCHAR
* append_hex(WCHAR
* dst
, const BYTE
* id
, const BYTE
* end
)
639 *dst
++ = "0123456789abcdef"[*id
>> 4 ];
640 *dst
++ = "0123456789abcdef"[*id
& 0x0F];
646 /******************************************************************
647 * image_locate_build_id_target
649 * Try to find the .so file containing the debug info out of the build-id note information
651 static BOOL
image_locate_build_id_target(struct image_file_map
* fmap
, const BYTE
* id
, unsigned idlen
)
653 struct image_file_map
* fmap_link
= NULL
;
658 fmap_link
= HeapAlloc(GetProcessHeap(), 0, sizeof(*fmap_link
));
659 if (!fmap_link
) return FALSE
;
661 p
= malloc(sizeof(L
"/usr/lib/debug/.build-id/") +
662 (idlen
* 2 + 1) * sizeof(WCHAR
) + sizeof(L
".debug"));
664 wcscpy(p
, L
"/usr/lib/debug/.build-id/");
668 z
= append_hex(z
, id
, id
+ 1);
672 z
= append_hex(z
, id
+ 1, id
+ idlen
);
675 wcscpy(z
, L
".debug");
676 TRACE("checking %s\n", wine_dbgstr_w(p
));
678 if (image_check_debug_link_gnu_id(p
, fmap_link
, id
, idlen
))
681 fmap
->alternate
= fmap_link
;
685 sz
= GetEnvironmentVariableW(L
"WINEHOMEDIR", NULL
, 0);
688 z
= realloc(p
, sz
* sizeof(WCHAR
) +
689 sizeof(L
"\\.cache\\debuginfod_client\\") +
690 idlen
* 2 * sizeof(WCHAR
) + sizeof(L
"\\debuginfo") + 500);
693 GetEnvironmentVariableW(L
"WINEHOMEDIR", p
, sz
);
695 wcscpy(z
, L
"\\.cache\\debuginfod_client\\");
697 z
= append_hex(z
, id
, id
+ idlen
);
698 wcscpy(z
, L
"\\debuginfo");
699 TRACE("checking %ls\n", p
);
700 if (image_check_debug_link_gnu_id(p
, fmap_link
, id
, idlen
))
703 fmap
->alternate
= fmap_link
;
708 TRACE("not found\n");
711 HeapFree(GetProcessHeap(), 0, fmap_link
);
715 /******************************************************************
716 * image_load_debugaltlink
718 * Handle a (potential) .gnu_debugaltlink section and the link to
719 * (another) alternate debug file.
720 * Return an heap-allocated image_file_map when the section .gnu_debugaltlink is present,
721 * and a matching debug file has been located.
723 struct image_file_map
* image_load_debugaltlink(struct image_file_map
* fmap
, struct module
* module
)
725 struct image_section_map debugaltlink_sect
;
727 struct image_file_map
* fmap_link
= NULL
;
730 for (; fmap
; fmap
= fmap
->alternate
)
732 if (image_find_section(fmap
, ".gnu_debugaltlink", &debugaltlink_sect
)) break;
736 TRACE("No .gnu_debugaltlink section found for %s\n", debugstr_w(module
->modulename
));
740 data
= image_map_section(&debugaltlink_sect
);
741 if (data
!= IMAGE_NO_MAP
)
745 /* The content of the section is:
746 * + a \0 terminated string (filename)
747 * + followed by the build-id
748 * We try loading the dwz_alternate:
749 * - from the filename: either as absolute path, or relative to the embedded build-id
750 * - from the build-id
751 * In both cases, checking that found .so file matches the requested build-id
753 sect_len
= image_get_map_size(&debugaltlink_sect
);
754 id
= memchr(data
, '\0', sect_len
);
757 unsigned idlen
= (const BYTE
*)data
+ sect_len
- id
;
758 fmap_link
= HeapAlloc(GetProcessHeap(), 0, sizeof(*fmap_link
));
761 unsigned filename_len
= MultiByteToWideChar(CP_UNIXCP
, 0, data
, -1, NULL
, 0);
762 /* Trying absolute path */
763 WCHAR
* dst
= HeapAlloc(GetProcessHeap(), 0, filename_len
* sizeof(WCHAR
));
766 MultiByteToWideChar(CP_UNIXCP
, 0, data
, -1, dst
, filename_len
);
767 ret
= image_check_debug_link_gnu_id(dst
, fmap_link
, id
, idlen
);
768 HeapFree(GetProcessHeap(), 0, dst
);
770 /* Trying relative path to build-id directory */
773 dst
= HeapAlloc(GetProcessHeap(), 0,
774 sizeof(L
"/usr/lib/debug/.build-id/") + (3 + filename_len
+ idlen
* 2) * sizeof(WCHAR
));
780 * some relative links are relative to /usr/lib/debug/.build-id, some others are from the directory
781 * where the alternate file is...
784 p
= memcpy(dst
, L
"/usr/lib/debug/.build-id/", sizeof(L
"/usr/lib/debug/.build-id/"));
786 MultiByteToWideChar(CP_UNIXCP
, 0, data
, -1, p
, filename_len
);
787 ret
= image_check_debug_link_gnu_id(dst
, fmap_link
, id
, idlen
);
790 p
= append_hex(p
, id
, id
+ idlen
);
792 MultiByteToWideChar(CP_UNIXCP
, 0, data
, -1, p
, filename_len
);
793 ret
= image_check_debug_link_gnu_id(dst
, fmap_link
, id
, idlen
);
795 HeapFree(GetProcessHeap(), 0, dst
);
800 HeapFree(GetProcessHeap(), 0, fmap_link
);
801 /* didn't work out with filename, try file lookup based on build-id */
802 ret
= image_locate_build_id_target(fmap
, id
, idlen
);
805 WARN("Couldn't find a match for .gnu_debugaltlink section %s for %s\n", data
, debugstr_w(module
->modulename
));
812 image_unmap_section(&debugaltlink_sect
);
813 if (fmap_link
) TRACE("Found match .gnu_debugaltlink section for %s\n", debugstr_w(module
->modulename
));
817 /******************************************************************
818 * image_check_alternate
820 * Load alternate files for a given image file, looking at either .note.gnu_build-id
821 * or .gnu_debuglink sections.
823 BOOL
image_check_alternate(struct image_file_map
* fmap
, const struct module
* module
)
827 struct image_section_map buildid_sect
, debuglink_sect
;
829 /* if present, add the .gnu_debuglink file as an alternate to current one */
830 if (image_find_section(fmap
, ".note.gnu.build-id", &buildid_sect
))
835 note
= (const UINT32
*)image_map_section(&buildid_sect
);
836 if (note
!= IMAGE_NO_MAP
)
838 /* the usual ELF note structure: name-size desc-size type <name> <desc> */
839 if (note
[2] == NOTE_GNU_BUILD_ID
)
841 ret
= image_locate_build_id_target(fmap
, (const BYTE
*)(note
+ 3 + ((note
[0] + 3) >> 2)), note
[1]);
844 image_unmap_section(&buildid_sect
);
846 /* if present, add the .gnu_debuglink file as an alternate to current one */
847 if (!ret
&& image_find_section(fmap
, ".gnu_debuglink", &debuglink_sect
))
849 const char* dbg_link
;
852 dbg_link
= image_map_section(&debuglink_sect
);
853 if (dbg_link
!= IMAGE_NO_MAP
)
855 /* The content of a debug link section is:
856 * 1/ a NULL terminated string, containing the file name for the
858 * 2/ padding on 4 byte boundary
859 * 3/ CRC of the linked file
861 DWORD crc
= *(const DWORD
*)(dbg_link
+ ((DWORD_PTR
)(strlen(dbg_link
) + 4) & ~3));
862 ret
= image_locate_debug_link(module
, fmap
, dbg_link
, crc
);
864 WARN("Couldn't load linked debug file for %s\n",
865 debugstr_w(module
->modulename
));
867 image_unmap_section(&debuglink_sect
);
869 return found
? ret
: TRUE
;
872 /***********************************************************************
873 * SymLoadModule (DBGHELP.@)
875 DWORD WINAPI
SymLoadModule(HANDLE hProcess
, HANDLE hFile
, PCSTR ImageName
,
876 PCSTR ModuleName
, DWORD BaseOfDll
, DWORD SizeOfDll
)
878 return SymLoadModuleEx(hProcess
, hFile
, ImageName
, ModuleName
, BaseOfDll
,
882 /***********************************************************************
883 * SymLoadModuleEx (DBGHELP.@)
885 DWORD64 WINAPI
SymLoadModuleEx(HANDLE hProcess
, HANDLE hFile
, PCSTR ImageName
,
886 PCSTR ModuleName
, DWORD64 BaseOfDll
, DWORD DllSize
,
887 PMODLOAD_DATA Data
, DWORD Flags
)
889 PWSTR wImageName
, wModuleName
;
893 TRACE("(%p %p %s %s %I64x %08lx %p %08lx)\n",
894 hProcess
, hFile
, debugstr_a(ImageName
), debugstr_a(ModuleName
),
895 BaseOfDll
, DllSize
, Data
, Flags
);
899 len
= MultiByteToWideChar(CP_ACP
, 0, ImageName
, -1, NULL
, 0);
900 wImageName
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
901 MultiByteToWideChar(CP_ACP
, 0, ImageName
, -1, wImageName
, len
);
903 else wImageName
= NULL
;
906 len
= MultiByteToWideChar(CP_ACP
, 0, ModuleName
, -1, NULL
, 0);
907 wModuleName
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
908 MultiByteToWideChar(CP_ACP
, 0, ModuleName
, -1, wModuleName
, len
);
910 else wModuleName
= NULL
;
912 ret
= SymLoadModuleExW(hProcess
, hFile
, wImageName
, wModuleName
,
913 BaseOfDll
, DllSize
, Data
, Flags
);
914 HeapFree(GetProcessHeap(), 0, wImageName
);
915 HeapFree(GetProcessHeap(), 0, wModuleName
);
919 /***********************************************************************
920 * SymLoadModuleExW (DBGHELP.@)
922 DWORD64 WINAPI
SymLoadModuleExW(HANDLE hProcess
, HANDLE hFile
, PCWSTR wImageName
,
923 PCWSTR wModuleName
, DWORD64 BaseOfDll
, DWORD SizeOfDll
,
924 PMODLOAD_DATA Data
, DWORD Flags
)
927 struct module
* module
= NULL
;
928 struct module
* altmodule
;
930 TRACE("(%p %p %s %s %I64x %08lx %p %08lx)\n",
931 hProcess
, hFile
, debugstr_w(wImageName
), debugstr_w(wModuleName
),
932 BaseOfDll
, SizeOfDll
, Data
, Flags
);
935 FIXME("Unsupported load data parameter %p for %s\n",
936 Data
, debugstr_w(wImageName
));
938 if (!(pcs
= process_find_by_handle(hProcess
))) return 0;
940 if (Flags
& ~(SLMFLAG_VIRTUAL
))
941 FIXME("Unsupported Flags %08lx for %s\n", Flags
, debugstr_w(wImageName
));
943 pcs
->loader
->synchronize_module_list(pcs
);
945 /* this is a Wine extension to the API just to redo the synchronisation */
946 if (!wImageName
&& !hFile
) return 0;
948 if (Flags
& SLMFLAG_VIRTUAL
)
950 if (!wImageName
) return 0;
951 module
= module_new(pcs
, wImageName
, DMT_PE
, TRUE
, BaseOfDll
, SizeOfDll
, 0, 0, IMAGE_FILE_MACHINE_UNKNOWN
);
952 if (!module
) return 0;
953 module
->module
.SymType
= SymVirtual
;
955 /* check if it's a builtin PE module with a containing ELF module */
956 else if (wImageName
&& module_is_container_loaded(pcs
, wImageName
, BaseOfDll
))
958 /* force the loading of DLL as builtin */
959 module
= pe_load_builtin_module(pcs
, wImageName
, BaseOfDll
, SizeOfDll
);
963 /* otherwise, try a regular PE module */
964 if (!(module
= pe_load_native_module(pcs
, wImageName
, hFile
, BaseOfDll
, SizeOfDll
)) &&
967 /* and finally an ELF or Mach-O module */
968 module
= pcs
->loader
->load_module(pcs
, wImageName
, BaseOfDll
);
973 WARN("Couldn't locate %s\n", debugstr_w(wImageName
));
976 /* by default module_new fills module.ModuleName from a derivation
977 * of LoadedImageName. Overwrite it, if we have better information
980 module_set_module(module
, wModuleName
);
982 lstrcpynW(module
->module
.ImageName
, wImageName
, ARRAY_SIZE(module
->module
.ImageName
));
984 for (altmodule
= pcs
->lmodules
; altmodule
; altmodule
= altmodule
->next
)
986 if (altmodule
!= module
&& altmodule
->type
== module
->type
&&
987 module
->module
.BaseOfImage
>= altmodule
->module
.BaseOfImage
&&
988 module
->module
.BaseOfImage
< altmodule
->module
.BaseOfImage
+ altmodule
->module
.ImageSize
)
993 /* we have a conflict as the new module cannot be found by its base address
994 * we need to get rid of one on the two modules
996 if (lstrcmpW(module
->modulename
, altmodule
->modulename
) != 0)
998 /* module overlaps an existing but different module... unload new module and return error */
999 WARN("%ls overlaps %ls\n", module
->modulename
, altmodule
->modulename
);
1000 module_remove(pcs
, module
);
1001 SetLastError(ERROR_INVALID_PARAMETER
);
1004 /* loading same module at same address... don't change anything */
1005 if (module
->module
.BaseOfImage
== altmodule
->module
.BaseOfImage
)
1007 module_remove(pcs
, module
);
1008 SetLastError(ERROR_SUCCESS
);
1011 /* replace old module with new one, which will look like a shift of base address */
1012 WARN("Shift module %ls from %I64x to %I64x\n",
1013 module
->modulename
, altmodule
->module
.BaseOfImage
, module
->module
.BaseOfImage
);
1014 module_remove(pcs
, altmodule
);
1017 if ((dbghelp_options
& SYMOPT_DEFERRED_LOADS
) == 0 && !module_get_container(pcs
, module
))
1018 module_load_debug(module
);
1019 return module
->module
.BaseOfImage
;
1022 /***********************************************************************
1023 * SymLoadModule64 (DBGHELP.@)
1025 DWORD64 WINAPI
SymLoadModule64(HANDLE hProcess
, HANDLE hFile
, PCSTR ImageName
,
1026 PCSTR ModuleName
, DWORD64 BaseOfDll
, DWORD SizeOfDll
)
1028 return SymLoadModuleEx(hProcess
, hFile
, ImageName
, ModuleName
, BaseOfDll
, SizeOfDll
,
1032 /******************************************************************
1036 BOOL
module_remove(struct process
* pcs
, struct module
* module
)
1038 struct module_format
*modfmt
;
1042 TRACE("%s (%p)\n", debugstr_w(module
->modulename
), module
);
1044 /* remove local scope if symbol is from this module */
1045 if (pcs
->localscope_symt
)
1047 struct symt
* locsym
= pcs
->localscope_symt
;
1048 if (symt_check_tag(locsym
, SymTagInlineSite
))
1049 locsym
= &symt_get_function_from_inlined((struct symt_function
*)locsym
)->symt
;
1050 if (symt_check_tag(locsym
, SymTagFunction
))
1052 locsym
= ((struct symt_function
*)locsym
)->container
;
1053 if (symt_check_tag(locsym
, SymTagCompiland
) &&
1054 module
== ((struct symt_compiland
*)locsym
)->container
->module
)
1056 pcs
->localscope_pc
= 0;
1057 pcs
->localscope_symt
= NULL
;
1061 for (i
= 0; i
< DFI_LAST
; i
++)
1063 if ((modfmt
= module
->format_info
[i
]) && modfmt
->remove
)
1064 modfmt
->remove(pcs
, module
->format_info
[i
]);
1066 hash_table_destroy(&module
->ht_symbols
);
1067 hash_table_destroy(&module
->ht_types
);
1068 HeapFree(GetProcessHeap(), 0, module
->sources
);
1069 HeapFree(GetProcessHeap(), 0, module
->addr_sorttab
);
1070 HeapFree(GetProcessHeap(), 0, module
->real_path
);
1071 pool_destroy(&module
->pool
);
1072 /* native dbghelp doesn't invoke registered callback(,CBA_SYMBOLS_UNLOADED,) here
1075 for (p
= &pcs
->lmodules
; *p
; p
= &(*p
)->next
)
1080 HeapFree(GetProcessHeap(), 0, module
);
1084 FIXME("This shouldn't happen\n");
1088 /******************************************************************
1089 * SymUnloadModule (DBGHELP.@)
1092 BOOL WINAPI
SymUnloadModule(HANDLE hProcess
, DWORD BaseOfDll
)
1094 return SymUnloadModule64(hProcess
, BaseOfDll
);
1097 /******************************************************************
1098 * SymUnloadModule64 (DBGHELP.@)
1101 BOOL WINAPI
SymUnloadModule64(HANDLE hProcess
, DWORD64 BaseOfDll
)
1103 struct process
* pcs
;
1104 struct module
* module
;
1106 pcs
= process_find_by_handle(hProcess
);
1107 if (!pcs
) return FALSE
;
1108 module
= module_find_by_addr(pcs
, BaseOfDll
, DMT_UNKNOWN
);
1109 if (!module
) return FALSE
;
1110 module_remove(pcs
, module
);
1114 /******************************************************************
1115 * SymEnumerateModules (DBGHELP.@)
1118 struct enum_modW64_32
1120 PSYM_ENUMMODULES_CALLBACK cb
;
1122 char module
[MAX_PATH
];
1125 static BOOL CALLBACK
enum_modW64_32(PCWSTR name
, DWORD64 base
, PVOID user
)
1127 struct enum_modW64_32
* x
= user
;
1129 WideCharToMultiByte(CP_ACP
, 0, name
, -1, x
->module
, sizeof(x
->module
), NULL
, NULL
);
1130 return x
->cb(x
->module
, (DWORD
)base
, x
->user
);
1133 BOOL WINAPI
SymEnumerateModules(HANDLE hProcess
,
1134 PSYM_ENUMMODULES_CALLBACK EnumModulesCallback
,
1137 struct enum_modW64_32 x
;
1139 x
.cb
= EnumModulesCallback
;
1140 x
.user
= UserContext
;
1142 return SymEnumerateModulesW64(hProcess
, enum_modW64_32
, &x
);
1145 /******************************************************************
1146 * SymEnumerateModules64 (DBGHELP.@)
1149 struct enum_modW64_64
1151 PSYM_ENUMMODULES_CALLBACK64 cb
;
1153 char module
[MAX_PATH
];
1156 static BOOL CALLBACK
enum_modW64_64(PCWSTR name
, DWORD64 base
, PVOID user
)
1158 struct enum_modW64_64
* x
= user
;
1160 WideCharToMultiByte(CP_ACP
, 0, name
, -1, x
->module
, sizeof(x
->module
), NULL
, NULL
);
1161 return x
->cb(x
->module
, base
, x
->user
);
1164 BOOL WINAPI
SymEnumerateModules64(HANDLE hProcess
,
1165 PSYM_ENUMMODULES_CALLBACK64 EnumModulesCallback
,
1168 struct enum_modW64_64 x
;
1170 x
.cb
= EnumModulesCallback
;
1171 x
.user
= UserContext
;
1173 return SymEnumerateModulesW64(hProcess
, enum_modW64_64
, &x
);
1176 /******************************************************************
1177 * SymEnumerateModulesW64 (DBGHELP.@)
1180 BOOL WINAPI
SymEnumerateModulesW64(HANDLE hProcess
,
1181 PSYM_ENUMMODULES_CALLBACKW64 EnumModulesCallback
,
1184 struct process
* pcs
= process_find_by_handle(hProcess
);
1185 struct module
* module
;
1187 if (!pcs
) return FALSE
;
1189 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
1191 if (!dbghelp_opt_native
&&
1192 (module
->type
== DMT_ELF
|| module
->type
== DMT_MACHO
))
1194 if (!EnumModulesCallback(module
->modulename
,
1195 module
->module
.BaseOfImage
, UserContext
))
1201 /******************************************************************
1202 * EnumerateLoadedModules64 (DBGHELP.@)
1205 struct enum_load_modW64_64
1207 PENUMLOADED_MODULES_CALLBACK64 cb
;
1209 char module
[MAX_PATH
];
1212 static BOOL CALLBACK
enum_load_modW64_64(PCWSTR name
, DWORD64 base
, ULONG size
,
1215 struct enum_load_modW64_64
* x
= user
;
1217 WideCharToMultiByte(CP_ACP
, 0, name
, -1, x
->module
, sizeof(x
->module
), NULL
, NULL
);
1218 return x
->cb(x
->module
, base
, size
, x
->user
);
1221 BOOL WINAPI
EnumerateLoadedModules64(HANDLE hProcess
,
1222 PENUMLOADED_MODULES_CALLBACK64 EnumLoadedModulesCallback
,
1225 struct enum_load_modW64_64 x
;
1227 x
.cb
= EnumLoadedModulesCallback
;
1228 x
.user
= UserContext
;
1230 return EnumerateLoadedModulesW64(hProcess
, enum_load_modW64_64
, &x
);
1233 /******************************************************************
1234 * EnumerateLoadedModules (DBGHELP.@)
1237 struct enum_load_modW64_32
1239 PENUMLOADED_MODULES_CALLBACK cb
;
1241 char module
[MAX_PATH
];
1244 static BOOL CALLBACK
enum_load_modW64_32(PCWSTR name
, DWORD64 base
, ULONG size
,
1247 struct enum_load_modW64_32
* x
= user
;
1248 WideCharToMultiByte(CP_ACP
, 0, name
, -1, x
->module
, sizeof(x
->module
), NULL
, NULL
);
1249 return x
->cb(x
->module
, (DWORD
)base
, size
, x
->user
);
1252 BOOL WINAPI
EnumerateLoadedModules(HANDLE hProcess
,
1253 PENUMLOADED_MODULES_CALLBACK EnumLoadedModulesCallback
,
1256 struct enum_load_modW64_32 x
;
1258 x
.cb
= EnumLoadedModulesCallback
;
1259 x
.user
= UserContext
;
1261 return EnumerateLoadedModulesW64(hProcess
, enum_load_modW64_32
, &x
);
1264 /******************************************************************
1265 * EnumerateLoadedModulesW64 (DBGHELP.@)
1268 BOOL WINAPI
EnumerateLoadedModulesW64(HANDLE hProcess
,
1269 PENUMLOADED_MODULES_CALLBACKW64 EnumLoadedModulesCallback
,
1273 WCHAR baseW
[256], modW
[256];
1277 DWORD filter
= LIST_MODULES_DEFAULT
;
1279 hMods
= HeapAlloc(GetProcessHeap(), 0, 256 * sizeof(hMods
[0]));
1280 if (!hMods
) return FALSE
;
1282 if (sizeof(void*) > sizeof(int) &&
1283 IsWow64Process(hProcess
, &wow64
) &&
1285 filter
= LIST_MODULES_32BIT
;
1287 if (!EnumProcessModulesEx(hProcess
, hMods
, 256 * sizeof(hMods
[0]), &sz
, filter
))
1289 /* hProcess should also be a valid process handle !! */
1290 HeapFree(GetProcessHeap(), 0, hMods
);
1293 if (sz
> 256 * sizeof(hMods
[0]))
1295 hMods
= HeapReAlloc(GetProcessHeap(), 0, hMods
, sz
);
1296 if (!hMods
|| !EnumProcessModulesEx(hProcess
, hMods
, sz
, &sz
, filter
))
1299 sz
/= sizeof(HMODULE
);
1300 for (i
= 0; i
< sz
; i
++)
1302 if (!GetModuleInformation(hProcess
, hMods
[i
], &mi
, sizeof(mi
)) ||
1303 !GetModuleBaseNameW(hProcess
, hMods
[i
], baseW
, ARRAY_SIZE(baseW
)))
1305 module_fill_module(baseW
, modW
, ARRAY_SIZE(modW
));
1306 EnumLoadedModulesCallback(modW
, (DWORD_PTR
)mi
.lpBaseOfDll
, mi
.SizeOfImage
,
1309 HeapFree(GetProcessHeap(), 0, hMods
);
1311 return sz
!= 0 && i
== sz
;
1314 static void dbghelp_str_WtoA(const WCHAR
*src
, char *dst
, int dst_len
)
1316 WideCharToMultiByte(CP_ACP
, 0, src
, -1, dst
, dst_len
- 1, NULL
, NULL
);
1317 dst
[dst_len
- 1] = 0;
1320 /******************************************************************
1321 * SymGetModuleInfo (DBGHELP.@)
1324 BOOL WINAPI
SymGetModuleInfo(HANDLE hProcess
, DWORD dwAddr
,
1325 PIMAGEHLP_MODULE ModuleInfo
)
1328 IMAGEHLP_MODULEW64 miw64
;
1330 if (sizeof(mi
) < ModuleInfo
->SizeOfStruct
) FIXME("Wrong size\n");
1332 miw64
.SizeOfStruct
= sizeof(miw64
);
1333 if (!SymGetModuleInfoW64(hProcess
, dwAddr
, &miw64
)) return FALSE
;
1335 mi
.SizeOfStruct
= ModuleInfo
->SizeOfStruct
;
1336 mi
.BaseOfImage
= miw64
.BaseOfImage
;
1337 mi
.ImageSize
= miw64
.ImageSize
;
1338 mi
.TimeDateStamp
= miw64
.TimeDateStamp
;
1339 mi
.CheckSum
= miw64
.CheckSum
;
1340 mi
.NumSyms
= miw64
.NumSyms
;
1341 mi
.SymType
= miw64
.SymType
;
1342 dbghelp_str_WtoA(miw64
.ModuleName
, mi
.ModuleName
, sizeof(mi
.ModuleName
));
1343 dbghelp_str_WtoA(miw64
.ImageName
, mi
.ImageName
, sizeof(mi
.ImageName
));
1344 dbghelp_str_WtoA(miw64
.LoadedImageName
, mi
.LoadedImageName
, sizeof(mi
.LoadedImageName
));
1346 memcpy(ModuleInfo
, &mi
, ModuleInfo
->SizeOfStruct
);
1351 /******************************************************************
1352 * SymGetModuleInfoW (DBGHELP.@)
1355 BOOL WINAPI
SymGetModuleInfoW(HANDLE hProcess
, DWORD dwAddr
,
1356 PIMAGEHLP_MODULEW ModuleInfo
)
1358 IMAGEHLP_MODULEW64 miw64
;
1359 IMAGEHLP_MODULEW miw
;
1361 if (sizeof(miw
) < ModuleInfo
->SizeOfStruct
) FIXME("Wrong size\n");
1363 miw64
.SizeOfStruct
= sizeof(miw64
);
1364 if (!SymGetModuleInfoW64(hProcess
, dwAddr
, &miw64
)) return FALSE
;
1366 miw
.SizeOfStruct
= ModuleInfo
->SizeOfStruct
;
1367 miw
.BaseOfImage
= miw64
.BaseOfImage
;
1368 miw
.ImageSize
= miw64
.ImageSize
;
1369 miw
.TimeDateStamp
= miw64
.TimeDateStamp
;
1370 miw
.CheckSum
= miw64
.CheckSum
;
1371 miw
.NumSyms
= miw64
.NumSyms
;
1372 miw
.SymType
= miw64
.SymType
;
1373 lstrcpyW(miw
.ModuleName
, miw64
.ModuleName
);
1374 lstrcpyW(miw
.ImageName
, miw64
.ImageName
);
1375 lstrcpyW(miw
.LoadedImageName
, miw64
.LoadedImageName
);
1376 memcpy(ModuleInfo
, &miw
, ModuleInfo
->SizeOfStruct
);
1381 /******************************************************************
1382 * SymGetModuleInfo64 (DBGHELP.@)
1385 BOOL WINAPI
SymGetModuleInfo64(HANDLE hProcess
, DWORD64 dwAddr
,
1386 PIMAGEHLP_MODULE64 ModuleInfo
)
1388 IMAGEHLP_MODULE64 mi64
;
1389 IMAGEHLP_MODULEW64 miw64
;
1391 if (sizeof(mi64
) < ModuleInfo
->SizeOfStruct
)
1393 SetLastError(ERROR_MOD_NOT_FOUND
); /* NOTE: native returns this error */
1394 WARN("Wrong size %lu\n", ModuleInfo
->SizeOfStruct
);
1398 miw64
.SizeOfStruct
= sizeof(miw64
);
1399 if (!SymGetModuleInfoW64(hProcess
, dwAddr
, &miw64
)) return FALSE
;
1401 mi64
.SizeOfStruct
= ModuleInfo
->SizeOfStruct
;
1402 mi64
.BaseOfImage
= miw64
.BaseOfImage
;
1403 mi64
.ImageSize
= miw64
.ImageSize
;
1404 mi64
.TimeDateStamp
= miw64
.TimeDateStamp
;
1405 mi64
.CheckSum
= miw64
.CheckSum
;
1406 mi64
.NumSyms
= miw64
.NumSyms
;
1407 mi64
.SymType
= miw64
.SymType
;
1408 dbghelp_str_WtoA(miw64
.ModuleName
, mi64
.ModuleName
, sizeof(mi64
.ModuleName
));
1409 dbghelp_str_WtoA(miw64
.ImageName
, mi64
.ImageName
, sizeof(mi64
.ImageName
));
1410 dbghelp_str_WtoA(miw64
.LoadedImageName
, mi64
.LoadedImageName
, sizeof(mi64
.LoadedImageName
));
1411 dbghelp_str_WtoA(miw64
.LoadedPdbName
, mi64
.LoadedPdbName
, sizeof(mi64
.LoadedPdbName
));
1413 mi64
.CVSig
= miw64
.CVSig
;
1414 dbghelp_str_WtoA(miw64
.CVData
, mi64
.CVData
, sizeof(mi64
.CVData
));
1415 mi64
.PdbSig
= miw64
.PdbSig
;
1416 mi64
.PdbSig70
= miw64
.PdbSig70
;
1417 mi64
.PdbAge
= miw64
.PdbAge
;
1418 mi64
.PdbUnmatched
= miw64
.PdbUnmatched
;
1419 mi64
.DbgUnmatched
= miw64
.DbgUnmatched
;
1420 mi64
.LineNumbers
= miw64
.LineNumbers
;
1421 mi64
.GlobalSymbols
= miw64
.GlobalSymbols
;
1422 mi64
.TypeInfo
= miw64
.TypeInfo
;
1423 mi64
.SourceIndexed
= miw64
.SourceIndexed
;
1424 mi64
.Publics
= miw64
.Publics
;
1425 mi64
.MachineType
= miw64
.MachineType
;
1426 mi64
.Reserved
= miw64
.Reserved
;
1428 memcpy(ModuleInfo
, &mi64
, ModuleInfo
->SizeOfStruct
);
1433 /******************************************************************
1434 * SymGetModuleInfoW64 (DBGHELP.@)
1437 BOOL WINAPI
SymGetModuleInfoW64(HANDLE hProcess
, DWORD64 dwAddr
,
1438 PIMAGEHLP_MODULEW64 ModuleInfo
)
1440 struct process
* pcs
= process_find_by_handle(hProcess
);
1441 struct module
* module
;
1442 IMAGEHLP_MODULEW64 miw64
;
1444 TRACE("%p %I64x %p\n", hProcess
, dwAddr
, ModuleInfo
);
1446 if (!pcs
) return FALSE
;
1447 if (ModuleInfo
->SizeOfStruct
> sizeof(*ModuleInfo
)) return FALSE
;
1448 module
= module_find_by_addr(pcs
, dwAddr
, DMT_UNKNOWN
);
1449 if (!module
) return FALSE
;
1451 miw64
= module
->module
;
1453 if (dbghelp_opt_real_path
&& module
->real_path
)
1454 lstrcpynW(miw64
.LoadedImageName
, module
->real_path
, ARRAY_SIZE(miw64
.LoadedImageName
));
1456 /* update debug information from container if any */
1457 if (module
->module
.SymType
== SymNone
)
1459 module
= module_get_container(pcs
, module
);
1460 if (module
&& module
->module
.SymType
!= SymNone
)
1462 miw64
.SymType
= module
->module
.SymType
;
1463 miw64
.NumSyms
= module
->module
.NumSyms
;
1466 memcpy(ModuleInfo
, &miw64
, ModuleInfo
->SizeOfStruct
);
1470 /***********************************************************************
1471 * SymGetModuleBase (DBGHELP.@)
1473 DWORD WINAPI
SymGetModuleBase(HANDLE hProcess
, DWORD dwAddr
)
1475 return (DWORD
)SymGetModuleBase64(hProcess
, dwAddr
);
1478 /***********************************************************************
1479 * SymGetModuleBase64 (DBGHELP.@)
1481 DWORD64 WINAPI
SymGetModuleBase64(HANDLE hProcess
, DWORD64 dwAddr
)
1483 struct process
* pcs
= process_find_by_handle(hProcess
);
1484 struct module
* module
;
1487 module
= module_find_by_addr(pcs
, dwAddr
, DMT_UNKNOWN
);
1488 if (!module
) return 0;
1489 return module
->module
.BaseOfImage
;
1492 /******************************************************************
1493 * module_reset_debug_info
1494 * Removes any debug information linked to a given module.
1496 void module_reset_debug_info(struct module
* module
)
1498 module
->sortlist_valid
= TRUE
;
1499 module
->sorttab_size
= 0;
1500 module
->addr_sorttab
= NULL
;
1501 module
->num_sorttab
= module
->num_symbols
= 0;
1502 hash_table_destroy(&module
->ht_symbols
);
1503 module
->ht_symbols
.num_buckets
= 0;
1504 module
->ht_symbols
.buckets
= NULL
;
1505 hash_table_destroy(&module
->ht_types
);
1506 module
->ht_types
.num_buckets
= 0;
1507 module
->ht_types
.buckets
= NULL
;
1508 module
->vtypes
.num_elts
= 0;
1509 hash_table_destroy(&module
->ht_symbols
);
1510 module
->sources_used
= module
->sources_alloc
= 0;
1511 module
->sources
= NULL
;
1514 /******************************************************************
1515 * SymRefreshModuleList (DBGHELP.@)
1517 BOOL WINAPI
SymRefreshModuleList(HANDLE hProcess
)
1519 struct process
* pcs
;
1521 TRACE("(%p)\n", hProcess
);
1523 if (!(pcs
= process_find_by_handle(hProcess
))) return FALSE
;
1525 return pcs
->loader
->synchronize_module_list(pcs
);
1528 /***********************************************************************
1529 * SymFunctionTableAccess (DBGHELP.@)
1531 PVOID WINAPI
SymFunctionTableAccess(HANDLE hProcess
, DWORD AddrBase
)
1533 return SymFunctionTableAccess64(hProcess
, AddrBase
);
1536 /***********************************************************************
1537 * SymFunctionTableAccess64 (DBGHELP.@)
1539 PVOID WINAPI
SymFunctionTableAccess64(HANDLE hProcess
, DWORD64 AddrBase
)
1541 struct process
* pcs
= process_find_by_handle(hProcess
);
1542 struct module
* module
;
1544 if (!pcs
) return NULL
;
1545 module
= module_find_by_addr(pcs
, AddrBase
, DMT_UNKNOWN
);
1546 if (!module
|| !module
->cpu
->find_runtime_function
) return NULL
;
1548 return module
->cpu
->find_runtime_function(module
, AddrBase
);
1551 static BOOL
native_synchronize_module_list(struct process
* pcs
)
1556 static struct module
* native_load_module(struct process
* pcs
, const WCHAR
* name
, ULONG_PTR addr
)
1561 static BOOL
native_load_debug_info(struct process
* process
, struct module
* module
)
1563 module
->module
.SymType
= SymNone
;
1567 static BOOL
native_enum_modules(struct process
*process
, enum_modules_cb cb
, void* user
)
1572 static BOOL
native_fetch_file_info(struct process
* process
, const WCHAR
* name
, ULONG_PTR load_addr
, DWORD_PTR
* base
,
1573 DWORD
* size
, DWORD
* checksum
)
1578 const struct loader_ops no_loader_ops
=
1580 native_synchronize_module_list
,
1582 native_load_debug_info
,
1583 native_enum_modules
,
1584 native_fetch_file_info
,