2 * File module.c - module handling for the wine debugger
4 * Copyright (C) 1993, Eric Youngdale.
5 * 2000-2004, 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include "dbghelp_private.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
36 static void module_fill_module(const char* in, char* out, unsigned size)
41 for (ptr = in + strlen(in) - 1;
42 *ptr != '/' && *ptr != '\\' && ptr >= in;
44 if (ptr < in || *ptr == '/' || *ptr == '\\') ptr++;
45 lstrcpynA(out, ptr, size);
48 (!strcasecmp(&out[len - 4], ".dll") || !strcasecmp(&out[len - 4], ".exe")))
50 else if (((len > 12 && out[len - 13] == '/') || len == 12) &&
51 (!strcasecmp(out + len - 12, "wine-pthread") ||
52 !strcasecmp(out + len - 12, "wine-kthread")))
53 lstrcpynA(out, "<wine-loader>",size);
57 (!strcasecmp(&out[len - 7], ".dll.so") || !strcasecmp(&out[len - 7], ".exe.so")))
58 strcpy(&out[len - 7], "<elf>");
60 out[len - 7] == '.' && !strcasecmp(&out[len - 3], ".so"))
62 if (len + 3 < size) strcpy(&out[len - 3], "<elf>");
63 else WARN("Buffer too short: %s\n", out);
66 while ((*out = tolower(*out))) out++;
69 /***********************************************************************
70 * Creates and links a new module to a process
72 struct module* module_new(struct process* pcs, const char* name,
73 enum module_type type,
74 unsigned long mod_addr, unsigned long size,
75 unsigned long stamp, unsigned long checksum)
77 struct module* module;
79 assert(type == DMT_ELF || type == DMT_PE);
80 if (!(module = HeapAlloc(GetProcessHeap(), 0, sizeof(*module))))
83 memset(module, 0, sizeof(*module));
85 module->next = pcs->lmodules;
86 pcs->lmodules = module;
88 TRACE("=> %s %08lx-%08lx %s\n",
89 type == DMT_ELF ? "ELF" : (type == DMT_PE ? "PE" : "---"),
90 mod_addr, mod_addr + size, name);
92 pool_init(&module->pool, 65536);
94 module->module.SizeOfStruct = sizeof(module->module);
95 module->module.BaseOfImage = mod_addr;
96 module->module.ImageSize = size;
97 module_fill_module(name, module->module.ModuleName,
98 sizeof(module->module.ModuleName));
99 module->module.ImageName[0] = '\0';
100 lstrcpynA(module->module.LoadedImageName, name, sizeof(module->module.LoadedImageName));
101 module->module.SymType = SymNone;
102 module->module.NumSyms = 0;
103 module->module.TimeDateStamp = stamp;
104 module->module.CheckSum = checksum;
107 module->sortlist_valid = FALSE;
108 module->addr_sorttab = NULL;
109 /* FIXME: this seems a bit too high (on a per module basis)
110 * need some statistics about this
112 hash_table_init(&module->pool, &module->ht_symbols, 4096);
113 hash_table_init(&module->pool, &module->ht_types, 4096);
114 vector_init(&module->vtypes, sizeof(struct symt*), 32);
116 module->sources_used = 0;
117 module->sources_alloc = 0;
123 /***********************************************************************
124 * module_find_by_name
127 struct module* module_find_by_name(const struct process* pcs,
128 const char* name, enum module_type type)
130 struct module* module;
132 if (type == DMT_UNKNOWN)
134 if ((module = module_find_by_name(pcs, name, DMT_PE)) ||
135 (module = module_find_by_name(pcs, name, DMT_ELF)))
140 char modname[MAX_PATH];
142 for (module = pcs->lmodules; module; module = module->next)
144 if (type == module->type &&
145 !strcasecmp(name, module->module.LoadedImageName))
148 module_fill_module(name, modname, sizeof(modname));
149 for (module = pcs->lmodules; module; module = module->next)
151 if (type == module->type &&
152 !strcasecmp(modname, module->module.ModuleName))
156 SetLastError(ERROR_INVALID_NAME);
160 /***********************************************************************
161 * module_get_container
164 struct module* module_get_container(const struct process* pcs,
165 const struct module* inner)
167 struct module* module;
169 for (module = pcs->lmodules; module; module = module->next)
171 if (module != inner &&
172 module->module.BaseOfImage <= inner->module.BaseOfImage &&
173 module->module.BaseOfImage + module->module.ImageSize >=
174 inner->module.BaseOfImage + inner->module.ImageSize)
180 /***********************************************************************
181 * module_get_containee
184 struct module* module_get_containee(const struct process* pcs,
185 const struct module* outter)
187 struct module* module;
189 for (module = pcs->lmodules; module; module = module->next)
191 if (module != outter &&
192 outter->module.BaseOfImage <= module->module.BaseOfImage &&
193 outter->module.BaseOfImage + outter->module.ImageSize >=
194 module->module.BaseOfImage + module->module.ImageSize)
200 /******************************************************************
203 * get the debug information from a module:
204 * - if the module's type is deferred, then force loading of debug info (and return
206 * - if the module has no debug info and has an ELF container, then return the ELF
207 * container (and also force the ELF container's debug info loading if deferred)
208 * - otherwise return the module itself if it has some debug info
210 struct module* module_get_debug(const struct process* pcs, struct module* module)
212 struct module* parent;
214 if (!module) return NULL;
215 /* for a PE builtin, always get info from parent */
216 if ((parent = module_get_container(pcs, module)))
218 /* if deferred, force loading */
219 if (module->module.SymType == SymDeferred)
223 switch (module->type)
225 case DMT_ELF: ret = elf_load_debug_info(module, NULL); break;
226 case DMT_PE: ret = pe_load_debug_info(pcs, module); break;
227 default: ret = FALSE; break;
229 if (!ret) module->module.SymType = SymNone;
230 assert(module->module.SymType != SymDeferred);
232 return (module && module->module.SymType != SymNone) ? module : NULL;
235 /***********************************************************************
236 * module_find_by_addr
238 * either the addr where module is loaded, or any address inside the
241 struct module* module_find_by_addr(const struct process* pcs, unsigned long addr,
242 enum module_type type)
244 struct module* module;
246 if (type == DMT_UNKNOWN)
248 if ((module = module_find_by_addr(pcs, addr, DMT_PE)) ||
249 (module = module_find_by_addr(pcs, addr, DMT_ELF)))
254 for (module = pcs->lmodules; module; module = module->next)
256 if (type == module->type && addr >= module->module.BaseOfImage &&
257 addr < module->module.BaseOfImage + module->module.ImageSize)
261 SetLastError(ERROR_INVALID_ADDRESS);
265 static BOOL module_is_elf_container_loaded(struct process* pcs, const char* ImageName,
266 const char* ModuleName)
268 char buffer[MAX_PATH];
270 struct module* module;
274 module_fill_module(ImageName, buffer, sizeof(buffer));
277 len = strlen(ModuleName);
278 for (module = pcs->lmodules; module; module = module->next)
280 if (!strncasecmp(module->module.ModuleName, ModuleName, len) &&
281 module->type == DMT_ELF &&
282 !strcmp(module->module.ModuleName + len, "<elf>"))
288 /******************************************************************
289 * module_get_type_by_name
291 * Guesses a filename type from its extension
293 enum module_type module_get_type_by_name(const char* name)
296 int len = strlen(name);
298 /* check for terminating .so or .so.[digit] */
299 ptr = strrchr(name, '.');
302 if (!strcmp(ptr, ".so") ||
303 (isdigit(ptr[1]) && !ptr[2] && ptr >= name + 3 && !memcmp(ptr - 3, ".so", 3)))
305 else if (!strcasecmp(ptr, ".pdb"))
308 /* wine-[kp]thread is also an ELF module */
309 else if (((len > 12 && name[len - 13] == '/') || len == 12) &&
310 (!strcasecmp(name + len - 12, "wine-pthread") ||
311 !strcasecmp(name + len - 12, "wine-kthread")))
318 /***********************************************************************
319 * SymLoadModule (DBGHELP.@)
321 DWORD WINAPI SymLoadModule(HANDLE hProcess, HANDLE hFile, char* ImageName,
322 char* ModuleName, DWORD BaseOfDll, DWORD SizeOfDll)
325 struct module* module = NULL;
327 TRACE("(%p %p %s %s %08lx %08lx)\n",
328 hProcess, hFile, debugstr_a(ImageName), debugstr_a(ModuleName),
329 BaseOfDll, SizeOfDll);
331 pcs = process_find_by_handle(hProcess);
332 if (!pcs) return FALSE;
334 /* force transparent ELF loading / unloading */
335 elf_synchronize_module_list(pcs);
337 /* this is a Wine extension to the API just to redo the synchronisation */
338 if (!ImageName && !hFile) return 0;
340 if (module_is_elf_container_loaded(pcs, ImageName, ModuleName))
342 /* force the loading of DLL as builtin */
343 if ((module = pe_load_module_from_pcs(pcs, ImageName, ModuleName,
344 BaseOfDll, SizeOfDll)))
346 WARN("Couldn't locate %s\n", ImageName);
349 TRACE("Assuming %s as native DLL\n", ImageName);
350 if (!(module = pe_load_module(pcs, ImageName, hFile, BaseOfDll, SizeOfDll)))
352 if (module_get_type_by_name(ImageName) == DMT_ELF &&
353 (module = elf_load_module(pcs, ImageName, BaseOfDll)))
355 FIXME("Should have successfully loaded debug information for image %s\n",
357 if ((module = pe_load_module_from_pcs(pcs, ImageName, ModuleName,
358 BaseOfDll, SizeOfDll)))
360 WARN("Couldn't locate %s\n", ImageName);
365 /* by default pe_load_module fills module.ModuleName from a derivation
366 * of ImageName. Overwrite it, if we have better information
369 lstrcpynA(module->module.ModuleName, ModuleName, sizeof(module->module.ModuleName));
370 lstrcpynA(module->module.ImageName, ImageName, sizeof(module->module.ImageName));
372 return module->module.BaseOfImage;
375 /******************************************************************
379 BOOL module_remove(struct process* pcs, struct module* module)
383 TRACE("%s (%p)\n", module->module.ModuleName, module);
384 hash_table_destroy(&module->ht_symbols);
385 hash_table_destroy(&module->ht_types);
386 HeapFree(GetProcessHeap(), 0, (char*)module->sources);
387 HeapFree(GetProcessHeap(), 0, module->addr_sorttab);
388 pool_destroy(&module->pool);
390 for (p = &pcs->lmodules; *p; p = &(*p)->next)
395 HeapFree(GetProcessHeap(), 0, module);
399 FIXME("This shouldn't happen\n");
403 /******************************************************************
404 * SymUnloadModule (DBGHELP.@)
407 BOOL WINAPI SymUnloadModule(HANDLE hProcess, DWORD BaseOfDll)
410 struct module* module;
412 pcs = process_find_by_handle(hProcess);
413 if (!pcs) return FALSE;
414 module = module_find_by_addr(pcs, BaseOfDll, DMT_UNKNOWN);
415 if (!module) return FALSE;
416 return module_remove(pcs, module);
419 /******************************************************************
420 * SymEnumerateModules (DBGHELP.@)
423 BOOL WINAPI SymEnumerateModules(HANDLE hProcess,
424 PSYM_ENUMMODULES_CALLBACK EnumModulesCallback,
427 struct process* pcs = process_find_by_handle(hProcess);
428 struct module* module;
430 if (!pcs) return FALSE;
432 for (module = pcs->lmodules; module; module = module->next)
434 if (!(dbghelp_options & SYMOPT_WINE_WITH_ELF_MODULES) && module->type != DMT_PE)
436 if (!EnumModulesCallback(module->module.ModuleName,
437 module->module.BaseOfImage, UserContext))
443 /******************************************************************
444 * EnumerateLoadedModules (DBGHELP.@)
447 BOOL WINAPI EnumerateLoadedModules(HANDLE hProcess,
448 PENUMLOADED_MODULES_CALLBACK EnumLoadedModulesCallback,
452 char base[256], mod[256];
456 hMods = HeapAlloc(GetProcessHeap(), 0, 256 * sizeof(hMods[0]));
457 if (!hMods) return FALSE;
459 if (!EnumProcessModules(hProcess, hMods, 256 * sizeof(hMods[0]), &sz))
461 /* hProcess should also be a valid process handle !! */
462 FIXME("If this happens, bump the number in mod\n");
463 HeapFree(GetProcessHeap(), 0, hMods);
466 sz /= sizeof(HMODULE);
467 for (i = 0; i < sz; i++)
469 if (!GetModuleInformation(hProcess, hMods[i], &mi, sizeof(mi)) ||
470 !GetModuleBaseNameA(hProcess, hMods[i], base, sizeof(base)))
472 module_fill_module(base, mod, sizeof(mod));
473 EnumLoadedModulesCallback(mod, (DWORD)mi.lpBaseOfDll, mi.SizeOfImage,
476 HeapFree(GetProcessHeap(), 0, hMods);
478 return sz != 0 && i == sz;
481 /******************************************************************
482 * SymGetModuleInfo (DBGHELP.@)
485 BOOL WINAPI SymGetModuleInfo(HANDLE hProcess, DWORD dwAddr,
486 PIMAGEHLP_MODULE ModuleInfo)
488 struct process* pcs = process_find_by_handle(hProcess);
489 struct module* module;
491 if (!pcs) return FALSE;
492 if (ModuleInfo->SizeOfStruct < sizeof(*ModuleInfo)) return FALSE;
493 module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
494 if (!module) return FALSE;
496 *ModuleInfo = module->module;
497 if (module->module.SymType == SymNone)
499 module = module_get_container(pcs, module);
500 if (module && module->module.SymType != SymNone)
501 ModuleInfo->SymType = module->module.SymType;
507 /***********************************************************************
508 * SymGetModuleBase (IMAGEHLP.@)
510 DWORD WINAPI SymGetModuleBase(HANDLE hProcess, DWORD dwAddr)
512 struct process* pcs = process_find_by_handle(hProcess);
513 struct module* module;
516 module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
517 if (!module) return 0;
518 return module->module.BaseOfImage;
521 /******************************************************************
522 * module_reset_debug_info
523 * Removes any debug information linked to a given module.
525 void module_reset_debug_info(struct module* module)
527 module->sortlist_valid = TRUE;
528 module->addr_sorttab = NULL;
529 hash_table_destroy(&module->ht_symbols);
530 module->ht_symbols.num_buckets = 0;
531 module->ht_symbols.buckets = NULL;
532 hash_table_destroy(&module->ht_types);
533 module->ht_types.num_buckets = 0;
534 module->ht_types.buckets = NULL;
535 module->vtypes.num_elts = 0;
536 hash_table_destroy(&module->ht_symbols);
537 module->sources_used = module->sources_alloc = 0;
538 module->sources = NULL;