makefiles: Recurse into all directories for install-lib and install-dev too.
[wine] / dlls / dbghelp / coff.c
1 /*
2  * Read VC++ debug information from COFF and eventually
3  * from PDB files.
4  *
5  * Copyright (C) 1996,      Eric Youngdale.
6  * Copyright (C) 1999-2000, Ulrich Weigand.
7  * Copyright (C) 2004,      Eric Pouech.
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23
24 /*
25  * Note - this handles reading debug information for 32 bit applications
26  * that run under Windows-NT for example.  I doubt that this would work well
27  * for 16 bit applications, but I don't think it really matters since the
28  * file format is different, and we should never get in here in such cases.
29  *
30  * TODO:
31  *      Get 16 bit CV stuff working.
32  *      Add symbol size to internal symbol table.
33  */
34
35 #include "config.h"
36 #include "wine/port.h"
37
38 #include <assert.h>
39 #include <stdlib.h>
40
41 #include <string.h>
42 #ifdef HAVE_UNISTD_H
43 # include <unistd.h>
44 #endif
45 #ifndef PATH_MAX
46 #define PATH_MAX MAX_PATH
47 #endif
48 #include <stdarg.h>
49 #include "windef.h"
50 #include "winbase.h"
51 #include "winternl.h"
52
53 #include "wine/exception.h"
54 #include "wine/debug.h"
55 #include "dbghelp_private.h"
56 #include "wine/mscvpdb.h"
57
58 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp_coff);
59
60 /*========================================================================
61  * Process COFF debug information.
62  */
63
64 struct CoffFile
65 {
66     unsigned int                startaddr;
67     unsigned int                endaddr;
68     struct symt_compiland*      compiland;
69     int                         linetab_offset;
70     int                         linecnt;
71     struct symt**               entries;
72     int                         neps;
73     int                         neps_alloc;
74 };
75
76 struct CoffFileSet
77 {
78     struct CoffFile*    files;
79     int                 nfiles;
80     int                 nfiles_alloc;
81 };
82
83 static const char*      coff_get_name(const IMAGE_SYMBOL* coff_sym, 
84                                       const char* coff_strtab)
85 {
86     static      char    namebuff[9];
87     const char*         nampnt;
88
89     if (coff_sym->N.Name.Short)
90     {
91         memcpy(namebuff, coff_sym->N.ShortName, 8);
92         namebuff[8] = '\0';
93         nampnt = &namebuff[0];
94     }
95     else
96     {
97         nampnt = coff_strtab + coff_sym->N.Name.Long;
98     }
99
100     if (nampnt[0] == '_') nampnt++;
101     return nampnt;
102 }
103
104 static int coff_add_file(struct CoffFileSet* coff_files, struct module* module,
105                          const char* filename)
106 {
107     struct CoffFile* file;
108
109     if (coff_files->nfiles + 1 >= coff_files->nfiles_alloc)
110     {
111         coff_files->nfiles_alloc += 10;
112         coff_files->files = (coff_files->files) ?
113             HeapReAlloc(GetProcessHeap(), 0, coff_files->files,
114                         coff_files->nfiles_alloc * sizeof(struct CoffFile)) :
115             HeapAlloc(GetProcessHeap(), 0,
116                       coff_files->nfiles_alloc * sizeof(struct CoffFile));
117     }
118     file = coff_files->files + coff_files->nfiles;
119     file->startaddr = 0xffffffff;
120     file->endaddr   = 0;
121     file->compiland = symt_new_compiland(module, 0,
122                                          source_new(module, NULL, filename));
123     file->linetab_offset = -1;
124     file->linecnt = 0;
125     file->entries = NULL;
126     file->neps = file->neps_alloc = 0;
127
128     return coff_files->nfiles++;
129 }
130
131 static void coff_add_symbol(struct CoffFile* coff_file, struct symt* sym)
132 {
133     if (coff_file->neps + 1 >= coff_file->neps_alloc)
134     {
135         coff_file->neps_alloc += 10;
136         coff_file->entries = (coff_file->entries) ?
137             HeapReAlloc(GetProcessHeap(), 0, coff_file->entries,
138                         coff_file->neps_alloc * sizeof(struct symt*)) :
139             HeapAlloc(GetProcessHeap(), 0, 
140                       coff_file->neps_alloc * sizeof(struct symt*));
141     }
142     coff_file->entries[coff_file->neps++] = sym;
143 }
144
145 BOOL coff_process_info(const struct msc_debug_info* msc_dbg)
146 {
147     const IMAGE_AUX_SYMBOL*             aux;
148     const IMAGE_COFF_SYMBOLS_HEADER*    coff;
149     const IMAGE_LINENUMBER*             coff_linetab;
150     const IMAGE_LINENUMBER*             linepnt;
151     const char*                         coff_strtab;
152     const IMAGE_SYMBOL*                 coff_sym;
153     const IMAGE_SYMBOL*                 coff_symbols;
154     struct CoffFileSet                  coff_files;
155     int                                 curr_file_idx = -1;
156     unsigned int                        i;
157     int                                 j;
158     int                                 k;
159     int                                 l;
160     int                                 linetab_indx;
161     const char*                         nampnt;
162     int                                 naux;
163     BOOL                                ret = FALSE;
164     DWORD                               addr;
165
166     TRACE("Processing COFF symbols...\n");
167
168     assert(sizeof(IMAGE_SYMBOL) == IMAGE_SIZEOF_SYMBOL);
169     assert(sizeof(IMAGE_LINENUMBER) == IMAGE_SIZEOF_LINENUMBER);
170
171     coff_files.files = NULL;
172     coff_files.nfiles = coff_files.nfiles_alloc = 0;
173
174     coff = (const IMAGE_COFF_SYMBOLS_HEADER*)msc_dbg->root;
175
176     coff_symbols = (const IMAGE_SYMBOL*)((const char *)coff + coff->LvaToFirstSymbol);
177     coff_linetab = (const IMAGE_LINENUMBER*)((const char *)coff + coff->LvaToFirstLinenumber);
178     coff_strtab = (const char*)(coff_symbols + coff->NumberOfSymbols);
179
180     linetab_indx = 0;
181
182     for (i = 0; i < coff->NumberOfSymbols; i++)
183     {
184         coff_sym = coff_symbols + i;
185         naux = coff_sym->NumberOfAuxSymbols;
186
187         if (coff_sym->StorageClass == IMAGE_SYM_CLASS_FILE)
188         {
189             curr_file_idx = coff_add_file(&coff_files, msc_dbg->module, 
190                                           (const char*)(coff_sym + 1));
191             TRACE("New file %s\n", (const char*)(coff_sym + 1));
192             i += naux;
193             continue;
194         }
195
196         if (curr_file_idx < 0)
197         {
198             assert(coff_files.nfiles == 0 && coff_files.nfiles_alloc == 0);
199             curr_file_idx = coff_add_file(&coff_files, msc_dbg->module, "<none>");
200             TRACE("New file <none>\n");
201         }
202
203         /*
204          * This guy marks the size and location of the text section
205          * for the current file.  We need to keep track of this so
206          * we can figure out what file the different global functions
207          * go with.
208          */
209         if (coff_sym->StorageClass == IMAGE_SYM_CLASS_STATIC &&
210             naux != 0 && coff_sym->Type == 0 && coff_sym->SectionNumber == 1)
211         {
212             aux = (const IMAGE_AUX_SYMBOL*) (coff_sym + 1);
213
214             if (coff_files.files[curr_file_idx].linetab_offset != -1)
215             {
216                 /*
217                  * Save this so we can still get the old name.
218                  */
219                 const char* fn;
220
221                 fn = source_get(msc_dbg->module,
222                                 coff_files.files[curr_file_idx].compiland->source);
223
224                 TRACE("Duplicating sect from %s: %x %x %x %d %d\n",
225                       fn, aux->Section.Length,
226                       aux->Section.NumberOfRelocations,
227                       aux->Section.NumberOfLinenumbers,
228                       aux->Section.Number, aux->Section.Selection);
229                 TRACE("More sect %d %s %08x %d %d %d\n",
230                       coff_sym->SectionNumber,
231                       coff_get_name(coff_sym, coff_strtab),
232                       coff_sym->Value, coff_sym->Type,
233                       coff_sym->StorageClass, coff_sym->NumberOfAuxSymbols);
234
235                 /*
236                  * Duplicate the file entry.  We have no way to describe
237                  * multiple text sections in our current way of handling things.
238                  */
239                 coff_add_file(&coff_files, msc_dbg->module, fn);
240             }
241             else
242             {
243                 TRACE("New text sect from %s: %x %x %x %d %d\n",
244                       source_get(msc_dbg->module, coff_files.files[curr_file_idx].compiland->source),
245                       aux->Section.Length,
246                       aux->Section.NumberOfRelocations,
247                       aux->Section.NumberOfLinenumbers,
248                       aux->Section.Number, aux->Section.Selection);
249             }
250
251             if (coff_files.files[curr_file_idx].startaddr > coff_sym->Value)
252             {
253                 coff_files.files[curr_file_idx].startaddr = coff_sym->Value;
254             }
255
256             if (coff_files.files[curr_file_idx].endaddr < coff_sym->Value + aux->Section.Length)
257             {
258                 coff_files.files[curr_file_idx].endaddr = coff_sym->Value + aux->Section.Length;
259             }
260
261             coff_files.files[curr_file_idx].linetab_offset = linetab_indx;
262             coff_files.files[curr_file_idx].linecnt = aux->Section.NumberOfLinenumbers;
263             linetab_indx += aux->Section.NumberOfLinenumbers;
264             i += naux;
265             continue;
266         }
267
268         if (coff_sym->StorageClass == IMAGE_SYM_CLASS_STATIC && naux == 0 && 
269             coff_sym->SectionNumber == 1)
270         {
271             DWORD base = msc_dbg->sectp[coff_sym->SectionNumber - 1].VirtualAddress;
272             /*
273              * This is a normal static function when naux == 0.
274              * Just register it.  The current file is the correct
275              * one in this instance.
276              */
277             nampnt = coff_get_name(coff_sym, coff_strtab);
278
279             TRACE("\tAdding static symbol %s\n", nampnt);
280
281             /* FIXME: was adding symbol to this_file ??? */
282             coff_add_symbol(&coff_files.files[curr_file_idx],
283                             &symt_new_function(msc_dbg->module, 
284                                                coff_files.files[curr_file_idx].compiland, 
285                                                nampnt,
286                                                msc_dbg->module->module.BaseOfImage + base + coff_sym->Value,
287                                                0 /* FIXME */,
288                                                NULL /* FIXME */)->symt);
289             i += naux;
290             continue;
291         }
292
293         if (coff_sym->StorageClass == IMAGE_SYM_CLASS_EXTERNAL &&
294             ISFCN(coff_sym->Type) && coff_sym->SectionNumber > 0)
295         {
296             struct symt_compiland* compiland = NULL;
297             DWORD base = msc_dbg->sectp[coff_sym->SectionNumber - 1].VirtualAddress;
298             nampnt = coff_get_name(coff_sym, coff_strtab);
299
300             TRACE("%d: %s %s\n",
301                   i, wine_dbgstr_longlong(msc_dbg->module->module.BaseOfImage + base + coff_sym->Value),
302                   nampnt);
303             TRACE("\tAdding global symbol %s (sect=%s)\n",
304                   nampnt, msc_dbg->sectp[coff_sym->SectionNumber - 1].Name);
305
306             /*
307              * Now we need to figure out which file this guy belongs to.
308              */
309             for (j = 0; j < coff_files.nfiles; j++)
310             {
311                 if (coff_files.files[j].startaddr <= base + coff_sym->Value
312                      && coff_files.files[j].endaddr > base + coff_sym->Value)
313                 {
314                     compiland = coff_files.files[j].compiland;
315                     break;
316                 }
317             }
318             if (j < coff_files.nfiles)
319             {
320                 coff_add_symbol(&coff_files.files[j],
321                                 &symt_new_function(msc_dbg->module, compiland, nampnt, 
322                                                    msc_dbg->module->module.BaseOfImage + base + coff_sym->Value,
323                                                    0 /* FIXME */, NULL /* FIXME */)->symt);
324             } 
325             else 
326             {
327                 symt_new_function(msc_dbg->module, NULL, nampnt, 
328                                   msc_dbg->module->module.BaseOfImage + base + coff_sym->Value,
329                                   0 /* FIXME */, NULL /* FIXME */);
330             }
331             i += naux;
332             continue;
333         }
334
335         if (coff_sym->StorageClass == IMAGE_SYM_CLASS_EXTERNAL &&
336             coff_sym->SectionNumber > 0)
337         {
338             DWORD base = msc_dbg->sectp[coff_sym->SectionNumber - 1].VirtualAddress;
339             /*
340              * Similar to above, but for the case of data symbols.
341              * These aren't treated as entrypoints.
342              */
343             nampnt = coff_get_name(coff_sym, coff_strtab);
344
345             TRACE("%d: %s %s\n",
346                   i, wine_dbgstr_longlong(msc_dbg->module->module.BaseOfImage + base + coff_sym->Value),
347                   nampnt);
348             TRACE("\tAdding global data symbol %s\n", nampnt);
349
350             /*
351              * Now we need to figure out which file this guy belongs to.
352              */
353             symt_new_global_variable(msc_dbg->module, NULL, nampnt, TRUE /* FIXME */,
354                                      msc_dbg->module->module.BaseOfImage + base + coff_sym->Value,
355                                      0 /* FIXME */, NULL /* FIXME */);
356             i += naux;
357             continue;
358         }
359
360         if (coff_sym->StorageClass == IMAGE_SYM_CLASS_STATIC && naux == 0)
361         {
362             /*
363              * Ignore these.  They don't have anything to do with
364              * reality.
365              */
366             i += naux;
367             continue;
368         }
369
370         TRACE("Skipping unknown entry '%s' %d %d %d\n",
371               coff_get_name(coff_sym, coff_strtab),
372               coff_sym->StorageClass, coff_sym->SectionNumber, naux);
373         
374         /*
375          * For now, skip past the aux entries.
376          */
377         i += naux;
378     }
379
380     if (coff_files.files != NULL)
381     {
382         /*
383          * OK, we now should have a list of files, and we should have a list
384          * of entrypoints.  We need to sort the entrypoints so that we are
385          * able to tie the line numbers with the given functions within the
386          * file.
387          */
388         for (j = 0; j < coff_files.nfiles; j++)
389         {
390             if (coff_files.files[j].entries != NULL)
391             {
392                 qsort(coff_files.files[j].entries, coff_files.files[j].neps,
393                       sizeof(struct symt*), symt_cmp_addr);
394             }
395         }
396
397         /*
398          * Now pick apart the line number tables, and attach the entries
399          * to the given functions.
400          */
401         for (j = 0; j < coff_files.nfiles; j++)
402         {
403             l = 0;
404             if (coff_files.files[j].neps != 0)
405             {
406                 for (k = 0; k < coff_files.files[j].linecnt; k++)
407                 {
408                     linepnt = coff_linetab + coff_files.files[j].linetab_offset + k;
409                     /*
410                      * If we have spilled onto the next entrypoint, then
411                      * bump the counter..
412                      */
413                     for (;;)
414                     {
415                         if (l+1 >= coff_files.files[j].neps) break;
416                         symt_get_info(coff_files.files[j].entries[l+1], TI_GET_ADDRESS, &addr);
417                         if (((msc_dbg->module->module.BaseOfImage + linepnt->Type.VirtualAddress) < addr))
418                             break;
419                         l++;
420                     }
421
422                     if (coff_files.files[j].entries[l+1]->tag == SymTagFunction)
423                     {
424                         /*
425                          * Add the line number.  This is always relative to the
426                          * start of the function, so we need to subtract that offset
427                          * first.
428                          */
429                         symt_get_info(coff_files.files[j].entries[l+1], TI_GET_ADDRESS, &addr);
430                         symt_add_func_line(msc_dbg->module, (struct symt_function*)coff_files.files[j].entries[l+1], 
431                                            coff_files.files[j].compiland->source, linepnt->Linenumber,
432                                            msc_dbg->module->module.BaseOfImage + linepnt->Type.VirtualAddress - addr);
433                     }
434                 }
435             }
436         }
437
438         for (j = 0; j < coff_files.nfiles; j++)
439         {
440             HeapFree(GetProcessHeap(), 0, coff_files.files[j].entries);
441         }
442         HeapFree(GetProcessHeap(), 0, coff_files.files);
443         msc_dbg->module->module.SymType = SymCoff;
444         /* FIXME: we could have a finer grain here */
445         msc_dbg->module->module.LineNumbers = TRUE;
446         msc_dbg->module->module.GlobalSymbols = TRUE;
447         msc_dbg->module->module.TypeInfo = FALSE;
448         msc_dbg->module->module.SourceIndexed = TRUE;
449         msc_dbg->module->module.Publics = TRUE;
450         ret = TRUE;
451     }
452
453     return ret;
454 }