urlmon: Added FIXME about unsupported pmkToLeft to BindToStorage.
[wine] / dlls / dbghelp / pe_module.c
1 /*
2  * File pe_module.c - handle PE module information
3  *
4  * Copyright (C) 1996,      Eric Youngdale.
5  * Copyright (C) 1999-2000, Ulrich Weigand.
6  * Copyright (C) 2004-2007, Eric Pouech.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  *
22  */
23
24 #include "config.h"
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <assert.h>
29
30 #include "dbghelp_private.h"
31 #include "winternl.h"
32 #include "wine/debug.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
35
36 /******************************************************************
37  *              pe_load_stabs
38  *
39  * look for stabs information in PE header (it's how the mingw compiler provides 
40  * its debugging information)
41  */
42 static BOOL pe_load_stabs(const struct process* pcs, struct module* module, 
43                           const void* mapping, IMAGE_NT_HEADERS* nth)
44 {
45     IMAGE_SECTION_HEADER*       section;
46     int                         i, stabsize = 0, stabstrsize = 0;
47     unsigned int                stabs = 0, stabstr = 0;
48     BOOL                        ret = FALSE;
49
50     section = (IMAGE_SECTION_HEADER*)
51         ((char*)&nth->OptionalHeader + nth->FileHeader.SizeOfOptionalHeader);
52     for (i = 0; i < nth->FileHeader.NumberOfSections; i++, section++)
53     {
54         if (!strcasecmp((const char*)section->Name, ".stab"))
55         {
56             stabs = section->VirtualAddress;
57             stabsize = section->SizeOfRawData;
58         }
59         else if (!strncasecmp((const char*)section->Name, ".stabstr", 8))
60         {
61             stabstr = section->VirtualAddress;
62             stabstrsize = section->SizeOfRawData;
63         }
64     }
65
66     if (stabstrsize && stabsize)
67     {
68         ret = stabs_parse(module, 
69                           module->module.BaseOfImage - nth->OptionalHeader.ImageBase, 
70                           RtlImageRvaToVa(nth, (void*)mapping, stabs, NULL),
71                           stabsize,
72                           RtlImageRvaToVa(nth, (void*)mapping, stabstr, NULL),
73                           stabstrsize);
74     }
75     return ret;
76 }
77
78 static BOOL CALLBACK dbg_match(const char* file, void* user)
79 {
80     /* accept first file */
81     return FALSE;
82 }
83
84 /******************************************************************
85  *              pe_load_dbg_file
86  *
87  * loads a .dbg file
88  */
89 static BOOL pe_load_dbg_file(const struct process* pcs, struct module* module,
90                              const char* dbg_name, DWORD timestamp)
91 {
92     char                                tmp[MAX_PATH];
93     HANDLE                              hFile = INVALID_HANDLE_VALUE, hMap = 0;
94     const BYTE*                         dbg_mapping = NULL;
95     const IMAGE_SEPARATE_DEBUG_HEADER*  hdr;
96     const IMAGE_DEBUG_DIRECTORY*        dbg;
97     BOOL                                ret = FALSE;
98
99     WINE_TRACE("Processing DBG file %s\n", dbg_name);
100
101     if (SymFindFileInPath(pcs->handle, NULL, dbg_name, NULL, 0, 0, 0, tmp, dbg_match, NULL) &&
102         (hFile = CreateFileA(tmp, GENERIC_READ, FILE_SHARE_READ, NULL,
103                              OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) != INVALID_HANDLE_VALUE &&
104         ((hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) != 0) &&
105         ((dbg_mapping = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) != NULL))
106     {
107         hdr = (const IMAGE_SEPARATE_DEBUG_HEADER*)dbg_mapping;
108         if (hdr->TimeDateStamp != timestamp)
109         {
110             WINE_ERR("Warning - %s has incorrect internal timestamp\n",
111                      dbg_name);
112             /*
113              * Well, sometimes this happens to DBG files which ARE REALLY the
114              * right .DBG files but nonetheless this check fails. Anyway,
115              * WINDBG (debugger for Windows by Microsoft) loads debug symbols
116              * which have incorrect timestamps.
117              */
118         }
119         if (hdr->Signature == IMAGE_SEPARATE_DEBUG_SIGNATURE)
120         {
121             /* section headers come immediately after debug header */
122             const IMAGE_SECTION_HEADER *sectp =
123                 (const IMAGE_SECTION_HEADER*)(hdr + 1);
124             /* and after that and the exported names comes the debug directory */
125             dbg = (const IMAGE_DEBUG_DIRECTORY*) 
126                 (dbg_mapping + sizeof(*hdr) + 
127                  hdr->NumberOfSections * sizeof(IMAGE_SECTION_HEADER) +
128                  hdr->ExportedNamesSize);
129
130
131             ret = pe_load_debug_directory(pcs, module, dbg_mapping, sectp,
132                                           hdr->NumberOfSections, dbg,
133                                           hdr->DebugDirectorySize / sizeof(*dbg));
134         }
135         else
136             ERR("Wrong signature in .DBG file %s\n", debugstr_a(tmp));
137     }
138     else
139         WINE_ERR("-Unable to peruse .DBG file %s (%s)\n", dbg_name, debugstr_a(tmp));
140
141     if (dbg_mapping) UnmapViewOfFile(dbg_mapping);
142     if (hMap) CloseHandle(hMap);
143     if (hFile != INVALID_HANDLE_VALUE) CloseHandle(hFile);
144     return ret;
145 }
146
147 /******************************************************************
148  *              pe_load_msc_debug_info
149  *
150  * Process MSC debug information in PE file.
151  */
152 static BOOL pe_load_msc_debug_info(const struct process* pcs, 
153                                    struct module* module,
154                                    const void* mapping, IMAGE_NT_HEADERS* nth)
155 {
156     BOOL                        ret = FALSE;
157     const IMAGE_DATA_DIRECTORY* dir;
158     const IMAGE_DEBUG_DIRECTORY*dbg = NULL;
159     int                         nDbg;
160
161     /* Read in debug directory */
162     dir = nth->OptionalHeader.DataDirectory + IMAGE_DIRECTORY_ENTRY_DEBUG;
163     nDbg = dir->Size / sizeof(IMAGE_DEBUG_DIRECTORY);
164     if (!nDbg) return FALSE;
165
166     dbg = RtlImageRvaToVa(nth, (void*)mapping, dir->VirtualAddress, NULL);
167
168     /* Parse debug directory */
169     if (nth->FileHeader.Characteristics & IMAGE_FILE_DEBUG_STRIPPED)
170     {
171         /* Debug info is stripped to .DBG file */
172         const IMAGE_DEBUG_MISC* misc = (const IMAGE_DEBUG_MISC*)
173             ((const char*)mapping + dbg->PointerToRawData);
174
175         if (nDbg != 1 || dbg->Type != IMAGE_DEBUG_TYPE_MISC ||
176             misc->DataType != IMAGE_DEBUG_MISC_EXENAME)
177         {
178             WINE_ERR("-Debug info stripped, but no .DBG file in module %s\n",
179                      debugstr_w(module->module.ModuleName));
180         }
181         else
182         {
183             ret = pe_load_dbg_file(pcs, module, (const char*)misc->Data, nth->FileHeader.TimeDateStamp);
184         }
185     }
186     else
187     {
188         const IMAGE_SECTION_HEADER *sectp = (const IMAGE_SECTION_HEADER*)((const char*)&nth->OptionalHeader + nth->FileHeader.SizeOfOptionalHeader);
189         /* Debug info is embedded into PE module */
190         ret = pe_load_debug_directory(pcs, module, mapping, sectp,
191             nth->FileHeader.NumberOfSections, dbg, nDbg);
192     }
193
194     return ret;
195 }
196
197 /***********************************************************************
198  *                      pe_load_export_debug_info
199  */
200 static BOOL pe_load_export_debug_info(const struct process* pcs, 
201                                       struct module* module, 
202                                       const void* mapping, IMAGE_NT_HEADERS* nth)
203 {
204     unsigned int                        i;
205     const IMAGE_EXPORT_DIRECTORY*       exports;
206     DWORD                               base = module->module.BaseOfImage;
207     DWORD                               size;
208
209     if (dbghelp_options & SYMOPT_NO_PUBLICS) return TRUE;
210
211 #if 0
212     /* Add start of DLL (better use the (yet unimplemented) Exe SymTag for this) */
213     /* FIXME: module.ModuleName isn't correctly set yet if it's passed in SymLoadModule */
214     symt_new_public(module, NULL, module->module.ModuleName, base, 1,
215                     TRUE /* FIXME */, TRUE /* FIXME */);
216 #endif
217     
218     /* Add entry point */
219     symt_new_public(module, NULL, "EntryPoint", 
220                     base + nth->OptionalHeader.AddressOfEntryPoint, 1,
221                     TRUE, TRUE);
222 #if 0
223     /* FIXME: we'd better store addresses linked to sections rather than 
224        absolute values */
225     IMAGE_SECTION_HEADER*       section;
226     /* Add start of sections */
227     section = (IMAGE_SECTION_HEADER*)
228         ((char*)&nth->OptionalHeader + nth->FileHeader.SizeOfOptionalHeader);
229     for (i = 0; i < nth->FileHeader.NumberOfSections; i++, section++) 
230     {
231         symt_new_public(module, NULL, section->Name, 
232                         RtlImageRvaToVa(nth, (void*)mapping, section->VirtualAddress, NULL), 
233                         1, TRUE /* FIXME */, TRUE /* FIXME */);
234     }
235 #endif
236
237     /* Add exported functions */
238     if ((exports = RtlImageDirectoryEntryToData((void*)mapping, FALSE,
239                                                 IMAGE_DIRECTORY_ENTRY_EXPORT, &size)))
240     {
241         const WORD*             ordinals = NULL;
242         const DWORD_PTR*        functions = NULL;
243         const DWORD*            names = NULL;
244         unsigned int            j;
245         char                    buffer[16];
246
247         functions = RtlImageRvaToVa(nth, (void*)mapping, exports->AddressOfFunctions, NULL);
248         ordinals  = RtlImageRvaToVa(nth, (void*)mapping, exports->AddressOfNameOrdinals, NULL);
249         names     = RtlImageRvaToVa(nth, (void*)mapping, exports->AddressOfNames, NULL);
250
251         for (i = 0; i < exports->NumberOfNames; i++) 
252         {
253             if (!names[i]) continue;
254             symt_new_public(module, NULL, 
255                             RtlImageRvaToVa(nth, (void*)mapping, names[i], NULL),
256                             base + functions[ordinals[i]], 
257                             1, TRUE /* FIXME */, TRUE /* FIXME */);
258         }
259             
260         for (i = 0; i < exports->NumberOfFunctions; i++) 
261         {
262             if (!functions[i]) continue;
263             /* Check if we already added it with a name */
264             for (j = 0; j < exports->NumberOfNames; j++)
265                 if ((ordinals[j] == i) && names[j]) break;
266             if (j < exports->NumberOfNames) continue;
267             snprintf(buffer, sizeof(buffer), "%d", i + exports->Base);
268             symt_new_public(module, NULL, buffer, base + (DWORD)functions[i], 1,
269                             TRUE /* FIXME */, TRUE /* FIXME */);
270         }
271     }
272     /* no real debug info, only entry points */
273     if (module->module.SymType == SymDeferred)
274         module->module.SymType = SymExport;
275     return TRUE;
276 }
277
278 /******************************************************************
279  *              pe_load_debug_info
280  *
281  */
282 BOOL pe_load_debug_info(const struct process* pcs, struct module* module)
283 {
284     BOOL                ret = FALSE;
285     HANDLE              hFile;
286     HANDLE              hMap;
287     void*               mapping;
288     IMAGE_NT_HEADERS*   nth;
289
290     hFile = CreateFileW(module->module.LoadedImageName, GENERIC_READ, FILE_SHARE_READ,
291                         NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
292     if (hFile == INVALID_HANDLE_VALUE) return ret;
293     if ((hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) != 0)
294     {
295         if ((mapping = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) != NULL)
296         {
297             nth = RtlImageNtHeader(mapping);
298
299             if (!(dbghelp_options & SYMOPT_PUBLICS_ONLY))
300             {
301                 ret = pe_load_stabs(pcs, module, mapping, nth) ||
302                     pe_load_msc_debug_info(pcs, module, mapping, nth);
303                 /* if we still have no debug info (we could only get SymExport at this
304                  * point), then do the SymExport except if we have an ELF container, 
305                  * in which case we'll rely on the export's on the ELF side
306                  */
307             }
308 /* FIXME shouldn't we check that? if (!module_get_debug(pcs, module))l */
309             if (pe_load_export_debug_info(pcs, module, mapping, nth) && !ret)
310                 ret = TRUE;
311             UnmapViewOfFile(mapping);
312         }
313         CloseHandle(hMap);
314     }
315     CloseHandle(hFile);
316
317     return ret;
318 }
319
320 /******************************************************************
321  *              pe_load_native_module
322  *
323  */
324 struct module* pe_load_native_module(struct process* pcs, const WCHAR* name,
325                                      HANDLE hFile, DWORD base, DWORD size)
326 {
327     struct module*      module = NULL;
328     BOOL                opened = FALSE;
329     HANDLE              hMap;
330     WCHAR               loaded_name[MAX_PATH];
331
332     loaded_name[0] = '\0';
333     if (!hFile)
334     {
335
336         assert(name);
337
338         if ((hFile = FindExecutableImageExW(name, pcs->search_path, loaded_name, NULL, NULL)) == NULL)
339             return NULL;
340         opened = TRUE;
341     }
342     else if (name) strcpyW(loaded_name, name);
343     else if (dbghelp_options & SYMOPT_DEFERRED_LOADS)
344         FIXME("Trouble ahead (no module name passed in deferred mode)\n");
345
346     if ((hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) != NULL)
347     {
348         void*   mapping;
349
350         if ((mapping = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) != NULL)
351         {
352             IMAGE_NT_HEADERS*   nth = RtlImageNtHeader(mapping);
353
354             if (nth)
355             {
356                 if (!base) base = nth->OptionalHeader.ImageBase;
357                 if (!size) size = nth->OptionalHeader.SizeOfImage;
358
359                 module = module_new(pcs, loaded_name, DMT_PE, FALSE, base, size,
360                                     nth->FileHeader.TimeDateStamp,
361                                     nth->OptionalHeader.CheckSum);
362                 if (module)
363                 {
364                     if (dbghelp_options & SYMOPT_DEFERRED_LOADS)
365                         module->module.SymType = SymDeferred;
366                     else
367                         pe_load_debug_info(pcs, module);
368                 }
369             }
370             UnmapViewOfFile(mapping);
371         }
372         CloseHandle(hMap);
373     }
374     if (opened) CloseHandle(hFile);
375
376     return module;
377 }
378
379 /******************************************************************
380  *              pe_load_nt_header
381  *
382  */
383 BOOL pe_load_nt_header(HANDLE hProc, DWORD base, IMAGE_NT_HEADERS* nth)
384 {
385     IMAGE_DOS_HEADER    dos;
386
387     return ReadProcessMemory(hProc, (char*)base, &dos, sizeof(dos), NULL) &&
388         dos.e_magic == IMAGE_DOS_SIGNATURE &&
389         ReadProcessMemory(hProc, (char*)(base + dos.e_lfanew), 
390                           nth, sizeof(*nth), NULL) &&
391         nth->Signature == IMAGE_NT_SIGNATURE;
392 }
393
394 /******************************************************************
395  *              pe_load_builtin_module
396  *
397  */
398 struct module* pe_load_builtin_module(struct process* pcs, const WCHAR* name,
399                                       DWORD base, DWORD size)
400 {
401     struct module*      module = NULL;
402
403     if (base && pcs->dbg_hdr_addr)
404     {
405         IMAGE_NT_HEADERS    nth;
406
407         if (pe_load_nt_header(pcs->handle, base, &nth))
408         {
409             if (!size) size = nth.OptionalHeader.SizeOfImage;
410             module = module_new(pcs, name, DMT_PE, FALSE, base, size,
411                                 nth.FileHeader.TimeDateStamp,
412                                 nth.OptionalHeader.CheckSum);
413         }
414     }
415     return module;
416 }