msi: Fix a few test failures on win9x.
[wine] / dlls / dbghelp / stabs.c
1 /*
2  * File stabs.c - read stabs information from the modules
3  *
4  * Copyright (C) 1996,      Eric Youngdale.
5  *               1999-2005, Eric Pouech
6  *
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.
11  *
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.
16  *
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
20  *
21  *
22  * Maintenance Information
23  * -----------------------
24  *
25  * For documentation on the stabs format see for example
26  *   The "stabs" debug format
27  *     by Julia Menapace, Jim Kingdon, David Mackenzie
28  *     of Cygnus Support
29  *     available (hopefully) from http:\\sources.redhat.com\gdb\onlinedocs
30  */
31
32 #include "config.h"
33
34 #include <sys/types.h>
35 #include <fcntl.h>
36 #ifdef HAVE_SYS_STAT_H
37 # include <sys/stat.h>
38 #endif
39 #ifdef HAVE_SYS_MMAN_H
40 #include <sys/mman.h>
41 #endif
42 #include <limits.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #ifdef HAVE_UNISTD_H
46 # include <unistd.h>
47 #endif
48 #include <stdio.h>
49 #ifndef PATH_MAX
50 #define PATH_MAX MAX_PATH
51 #endif
52 #include <assert.h>
53 #include <stdarg.h>
54
55 #include "windef.h"
56 #include "winbase.h"
57 #include "winnls.h"
58
59 #include "dbghelp_private.h"
60
61 #include "wine/debug.h"
62
63 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp_stabs);
64
65 #ifndef N_UNDF
66 #define N_UNDF          0x00
67 #endif
68
69 #define N_GSYM          0x20
70 #define N_FUN           0x24
71 #define N_STSYM         0x26
72 #define N_LCSYM         0x28
73 #define N_MAIN          0x2a
74 #define N_ROSYM         0x2c
75 #define N_BNSYM         0x2e
76 #define N_OPT           0x3c
77 #define N_RSYM          0x40
78 #define N_SLINE         0x44
79 #define N_ENSYM         0x4e
80 #define N_SO            0x64
81 #define N_LSYM          0x80
82 #define N_BINCL         0x82
83 #define N_SOL           0x84
84 #define N_PSYM          0xa0
85 #define N_EINCL         0xa2
86 #define N_LBRAC         0xc0
87 #define N_EXCL          0xc2
88 #define N_RBRAC         0xe0
89
90 struct stab_nlist
91 {
92     union
93     {
94         char*                   n_name;
95         struct stab_nlist*      n_next;
96         long                    n_strx;
97     } n_un;
98     unsigned char       n_type;
99     char                n_other;
100     short               n_desc;
101     unsigned long       n_value;
102 };
103
104 static void stab_strcpy(char* dest, int sz, const char* source)
105 {
106     char*       ptr = dest;
107     /*
108      * A strcpy routine that stops when we hit the ':' character.
109      * Faster than copying the whole thing, and then nuking the
110      * ':'.
111      * Takes also care of (valid) a::b constructs
112      */
113     while (*source != '\0')
114     {
115         if (source[0] != ':' && sz-- > 0) *ptr++ = *source++;
116         else if (source[1] == ':' && (sz -= 2) > 0)
117         {
118             *ptr++ = *source++;
119             *ptr++ = *source++;
120         }
121         else break;
122     }
123     *ptr-- = '\0';
124     /* GCC emits, in some cases, a .<digit>+ suffix.
125      * This is used for static variable inside functions, so
126      * that we can have several such variables with same name in
127      * the same compilation unit
128      * We simply ignore that suffix when present (we also get rid
129      * of it in ELF symtab parsing)
130      */
131     if (ptr >= dest && isdigit(*ptr))
132     {
133         while (ptr > dest && isdigit(*ptr)) ptr--;
134         if (*ptr == '.') *ptr = '\0';
135     }
136     assert(sz > 0);
137 }
138
139 typedef struct
140 {
141    char*                name;
142    unsigned long        value;
143    struct symt**        vector;
144    int                  nrofentries;
145 } include_def;
146
147 #define MAX_INCLUDES    5120
148
149 static include_def*             include_defs = NULL;
150 static int                      num_include_def = 0;
151 static int                      num_alloc_include_def = 0;
152 static int                      cu_include_stack[MAX_INCLUDES];
153 static int                      cu_include_stk_idx = 0;
154 static struct symt**            cu_vector = NULL;
155 static int                      cu_nrofentries = 0;
156 static struct symt_basic*       stabs_basic[36];
157
158 static int stabs_new_include(const char* file, unsigned long val)
159 {
160     if (num_include_def == num_alloc_include_def)
161     {
162         num_alloc_include_def += 256;
163         if (!include_defs)
164             include_defs = HeapAlloc(GetProcessHeap(), 0, 
165                                      sizeof(include_defs[0]) * num_alloc_include_def);
166         else
167             include_defs = HeapReAlloc(GetProcessHeap(), 0, include_defs,
168                                        sizeof(include_defs[0]) * num_alloc_include_def);
169         memset(include_defs + num_include_def, 0, sizeof(include_defs[0]) * 256);
170     }
171     include_defs[num_include_def].name = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(file) + 1), file);
172     include_defs[num_include_def].value = val;
173     include_defs[num_include_def].vector = NULL;
174     include_defs[num_include_def].nrofentries = 0;
175
176     return num_include_def++;
177 }
178
179 static int stabs_find_include(const char* file, unsigned long val)
180 {
181     int         i;
182
183     for (i = 0; i < num_include_def; i++)
184     {
185         if (val == include_defs[i].value &&
186             strcmp(file, include_defs[i].name) == 0)
187             return i;
188     }
189     return -1;
190 }
191
192 static int stabs_add_include(int idx)
193 {
194     if (idx < 0) return -1;
195     cu_include_stk_idx++;
196
197     /* if this happens, just bump MAX_INCLUDES */
198     /* we could also handle this as another dynarray */
199     assert(cu_include_stk_idx < MAX_INCLUDES);
200     cu_include_stack[cu_include_stk_idx] = idx;
201     return cu_include_stk_idx;
202 }
203
204 static void stabs_reset_includes(void)
205 {
206     /*
207      * The struct symt:s that we would need to use are reset when
208      * we start a new file. (at least the ones in filenr == 0)
209      */
210     cu_include_stk_idx = 0;/* keep 0 as index for the .c file itself */
211     memset(cu_vector, 0, sizeof(cu_vector[0]) * cu_nrofentries);
212 }
213
214 static void stabs_free_includes(void)
215 {
216     int i;
217
218     stabs_reset_includes();
219     for (i = 0; i < num_include_def; i++)
220     {
221         HeapFree(GetProcessHeap(), 0, include_defs[i].name);
222         HeapFree(GetProcessHeap(), 0, include_defs[i].vector);
223     }
224     HeapFree(GetProcessHeap(), 0, include_defs);
225     include_defs = NULL;
226     num_include_def = 0;
227     num_alloc_include_def = 0;
228     HeapFree(GetProcessHeap(), 0, cu_vector);
229     cu_vector = NULL;
230     cu_nrofentries = 0;
231 }
232
233 static struct symt** stabs_find_ref(long filenr, long subnr)
234 {
235     struct symt**       ret;
236
237     /* FIXME: I could perhaps create a dummy include_def for each compilation
238      * unit which would allow not to handle those two cases separately
239      */
240     if (filenr == 0)
241     {
242         if (cu_nrofentries <= subnr)
243         {
244             if (!cu_vector)
245                 cu_vector = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 
246                                       sizeof(cu_vector[0]) * (subnr+1));
247             else
248                 cu_vector = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 
249                                         cu_vector, sizeof(cu_vector[0]) * (subnr+1));
250             cu_nrofentries = subnr + 1;
251         }
252         ret = &cu_vector[subnr];
253     }
254     else
255     {
256         include_def*    idef;
257
258         assert(filenr <= cu_include_stk_idx);
259         idef = &include_defs[cu_include_stack[filenr]];
260
261         if (idef->nrofentries <= subnr)
262         {
263             if (!idef->vector)
264                 idef->vector = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 
265                                          sizeof(idef->vector[0]) * (subnr+1));
266             else
267                 idef->vector = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 
268                                            idef->vector, sizeof(idef->vector[0]) * (subnr+1));
269             idef->nrofentries = subnr + 1;
270         }
271         ret = &idef->vector[subnr];
272     }
273     TRACE("(%ld,%ld) => %p (%p)\n", filenr, subnr, ret, *ret);
274     return ret;
275 }
276
277 static struct symt** stabs_read_type_enum(const char** x)
278 {
279     long        filenr, subnr;
280     const char* iter;
281     char*       end;
282
283     iter = *x;
284     if (*iter == '(')
285     {
286         ++iter;                             /* '('   */
287         filenr = strtol(iter, &end, 10);    /* <int> */
288         iter = ++end;                       /* ','   */
289         subnr = strtol(iter, &end, 10);     /* <int> */
290         iter = ++end;                       /* ')'   */
291     }
292     else
293     {
294         filenr = 0;
295         subnr = strtol(iter, &end, 10);     /* <int> */
296         iter = end;
297     }
298     *x = iter;
299     return stabs_find_ref(filenr, subnr);
300 }
301
302 #define PTS_DEBUG
303 struct ParseTypedefData
304 {
305     const char*         ptr;
306     char                buf[1024];
307     int                 idx;
308     struct module*      module;
309 #ifdef PTS_DEBUG
310     struct PTS_Error 
311     {
312         const char*         ptr;
313         unsigned            line;
314     } errors[16];
315     int                 err_idx;
316 #endif
317 };
318
319 #ifdef PTS_DEBUG
320 static void stabs_pts_push(struct ParseTypedefData* ptd, unsigned line)
321 {
322     assert(ptd->err_idx < sizeof(ptd->errors) / sizeof(ptd->errors[0]));
323     ptd->errors[ptd->err_idx].line = line;
324     ptd->errors[ptd->err_idx].ptr = ptd->ptr;
325     ptd->err_idx++;
326 }
327 #define PTS_ABORTIF(ptd, t) do { if (t) { stabs_pts_push((ptd), __LINE__); return -1;} } while (0)
328 #else
329 #define PTS_ABORTIF(ptd, t) do { if (t) return -1; } while (0)
330 #endif
331
332 static int stabs_get_basic(struct ParseTypedefData* ptd, unsigned basic, struct symt** symt)
333 {
334     PTS_ABORTIF(ptd, basic >= sizeof(stabs_basic) / sizeof(stabs_basic[0]));
335
336     if (!stabs_basic[basic])
337     {
338         switch (basic)
339         {
340         case  1: stabs_basic[basic] = symt_new_basic(ptd->module, btInt,     "int", 4); break;
341         case  2: stabs_basic[basic] = symt_new_basic(ptd->module, btChar,    "char", 1); break;
342         case  3: stabs_basic[basic] = symt_new_basic(ptd->module, btInt,     "short int", 2); break;
343         case  4: stabs_basic[basic] = symt_new_basic(ptd->module, btInt,     "long int", 4); break;
344         case  5: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt,    "unsigned char", 1); break;
345         case  6: stabs_basic[basic] = symt_new_basic(ptd->module, btInt,     "signed char", 1); break;
346         case  7: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt,    "unsigned short int", 2); break;
347         case  8: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt,    "unsigned int", 4); break;
348         case  9: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt,    "unsigned", 2); break;
349         case 10: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt,    "unsigned long int", 2); break;
350         case 11: stabs_basic[basic] = symt_new_basic(ptd->module, btVoid,    "void", 0); break;
351         case 12: stabs_basic[basic] = symt_new_basic(ptd->module, btFloat,   "float", 4); break;
352         case 13: stabs_basic[basic] = symt_new_basic(ptd->module, btFloat,   "double", 8); break;
353         case 14: stabs_basic[basic] = symt_new_basic(ptd->module, btFloat,   "long double", 12); break;
354         case 15: stabs_basic[basic] = symt_new_basic(ptd->module, btInt,     "integer", 4); break;
355         case 16: stabs_basic[basic] = symt_new_basic(ptd->module, btBool,    "bool", 1); break;
356         /*    case 17: short real */
357         /*    case 18: real */
358         case 25: stabs_basic[basic] = symt_new_basic(ptd->module, btComplex, "float complex", 8); break;
359         case 26: stabs_basic[basic] = symt_new_basic(ptd->module, btComplex, "double complex", 16); break;
360         case 30: stabs_basic[basic] = symt_new_basic(ptd->module, btWChar,   "wchar_t", 2); break;
361         case 31: stabs_basic[basic] = symt_new_basic(ptd->module, btInt,     "long long int", 8); break;
362         case 32: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt,    "long long unsigned", 8); break;
363             /* starting at 35 are wine extensions (especially for R implementation) */
364         case 35: stabs_basic[basic] = symt_new_basic(ptd->module, btComplex, "long double complex", 24); break;
365         default: PTS_ABORTIF(ptd, 1);
366         }
367     }   
368     *symt = &stabs_basic[basic]->symt;
369     return 0;
370 }
371
372 static int stabs_pts_read_type_def(struct ParseTypedefData* ptd, 
373                                    const char* typename, struct symt** dt);
374
375 static int stabs_pts_read_id(struct ParseTypedefData* ptd)
376 {
377     const char*         first = ptd->ptr;
378     unsigned int        template = 0;
379     char                ch;
380
381     while ((ch = *ptd->ptr++) != '\0')
382     {
383         switch (ch)
384         {
385         case ':':
386             if (template == 0)
387             {
388                 unsigned int len = ptd->ptr - first - 1;
389                 PTS_ABORTIF(ptd, len >= sizeof(ptd->buf) - ptd->idx);
390                 memcpy(ptd->buf + ptd->idx, first, len);
391                 ptd->buf[ptd->idx + len] = '\0';
392                 ptd->idx += len + 1;
393                 return 0;
394             }
395             break;
396         case '<': template++; break;
397         case '>': PTS_ABORTIF(ptd, template == 0); template--; break;
398         }
399     }
400     return -1;
401 }
402
403 static int stabs_pts_read_number(struct ParseTypedefData* ptd, long* v)
404 {
405     char*       last;
406
407     *v = strtol(ptd->ptr, &last, 10);
408     PTS_ABORTIF(ptd, last == ptd->ptr);
409     ptd->ptr = last;
410     return 0;
411 }
412
413 static int stabs_pts_read_type_reference(struct ParseTypedefData* ptd,
414                                          long* filenr, long* subnr)
415 {
416     if (*ptd->ptr == '(')
417     {
418         /* '(' <int> ',' <int> ')' */
419         ptd->ptr++;
420         PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, filenr) == -1);
421         PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
422         PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, subnr) == -1);
423         PTS_ABORTIF(ptd, *ptd->ptr++ != ')');
424     }
425     else
426     {
427         *filenr = 0;
428         PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, subnr) == -1);
429     }
430     return 0;
431 }
432
433 struct pts_range_value
434 {
435     ULONGLONG           val;
436     int                 sign;
437 };
438
439 static int stabs_pts_read_range_value(struct ParseTypedefData* ptd, struct pts_range_value* prv)
440 {
441     char*       last;
442
443     switch (*ptd->ptr)
444     {
445     case '0':
446         while (*ptd->ptr == '0') ptd->ptr++;
447         if (*ptd->ptr >= '1' && *ptd->ptr <= '7')
448         {
449             switch (ptd->ptr[1])
450             {
451             case '0': 
452                 PTS_ABORTIF(ptd, ptd->ptr[0] != '1');
453                 prv->sign = -1;
454                 prv->val = 0;
455                 while (isdigit(*ptd->ptr)) prv->val = (prv->val << 3) + *ptd->ptr++ - '0';
456                 break;
457             case '7':
458                 prv->sign = 1;
459                 prv->val = 0;
460                 while (isdigit(*ptd->ptr)) prv->val = (prv->val << 3) + *ptd->ptr++ - '0';
461                 break;
462             default: PTS_ABORTIF(ptd, 1); break;
463             }
464         } else prv->sign = 0;
465         break;
466     case '-':
467         prv->sign = -1;
468         prv->val = strtoull(++ptd->ptr, &last, 10);
469         ptd->ptr = last;
470         break;
471     case '+':
472     default:    
473         prv->sign = 1;
474         prv->val = strtoull(ptd->ptr, &last, 10);
475         ptd->ptr = last;
476         break;
477     }
478     return 0;
479 }
480
481 static int stabs_pts_read_range(struct ParseTypedefData* ptd, const char* typename,
482                                 struct symt** dt)
483 {
484     struct symt*                ref;
485     struct pts_range_value      lo;
486     struct pts_range_value      hi;
487     unsigned                    size;
488     enum BasicType              bt;
489     int                         i;
490     ULONGLONG                   v;
491
492     /* type ';' <int> ';' <int> ';' */
493     PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &ref) == -1);
494     PTS_ABORTIF(ptd, *ptd->ptr++ != ';');       /* ';' */
495     PTS_ABORTIF(ptd, stabs_pts_read_range_value(ptd, &lo) == -1);
496     PTS_ABORTIF(ptd, *ptd->ptr++ != ';');       /* ';' */
497     PTS_ABORTIF(ptd, stabs_pts_read_range_value(ptd, &hi) == -1);
498     PTS_ABORTIF(ptd, *ptd->ptr++ != ';');       /* ';' */
499
500     /* basically, we don't use ref... in some cases, for example, float is declared
501      * as a derived type of int... which won't help us... so we guess the types
502      * from the various formats
503      */
504     if (lo.sign == 0 && hi.sign < 0)
505     {
506         bt = btUInt;
507         size = hi.val;
508     }
509     else if (lo.sign < 0 && hi.sign == 0)
510     {
511         bt = btUInt;
512         size = lo.val;
513     }
514     else if (lo.sign > 0 && hi.sign == 0)
515     {
516         bt = btFloat;
517         size = lo.val;
518     }
519     else if (lo.sign < 0 && hi.sign > 0)
520     {
521         v = 1 << 7;
522         for (i = 7; i < 64; i += 8)
523         {
524             if (lo.val == v && hi.val == v - 1)
525             {
526                 bt = btInt;
527                 size = (i + 1) / 8;
528                 break;
529             }
530             v <<= 8;
531         }
532         PTS_ABORTIF(ptd, i >= 64);
533     }
534     else if (lo.sign == 0 && hi.sign > 0)
535     {
536         if (hi.val == 127) /* specific case for char... */
537         {
538             bt = btChar;
539             size = 1;
540         }
541         else
542         {
543             v = 1;
544             for (i = 8; i <= 64; i += 8)
545             {
546                 v <<= 8;
547                 if (hi.val + 1 == v)
548                 {
549                     bt = btUInt;
550                     size = (i + 1) / 8;
551                     break;
552                 }
553             }
554             PTS_ABORTIF(ptd, i > 64);
555         }
556     }
557     else PTS_ABORTIF(ptd, 1);
558
559     *dt = &symt_new_basic(ptd->module, bt, typename, size)->symt;
560     return 0;
561 }
562
563 static inline int stabs_pts_read_method_info(struct ParseTypedefData* ptd)
564 {
565     struct symt*        dt;
566     char*               tmp;
567     char                mthd;
568
569     do
570     {
571         /* get type of return value */
572         PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
573         if (*ptd->ptr == ';') ptd->ptr++;
574
575         /* get types of parameters */
576         if (*ptd->ptr == ':')
577         {
578             PTS_ABORTIF(ptd, !(tmp = strchr(ptd->ptr + 1, ';')));
579             ptd->ptr = tmp + 1;
580         }
581         PTS_ABORTIF(ptd, !(*ptd->ptr >= '0' && *ptd->ptr <= '9'));
582         ptd->ptr++;
583         PTS_ABORTIF(ptd, !(ptd->ptr[0] >= 'A' && *ptd->ptr <= 'D'));
584         mthd = *++ptd->ptr;
585         PTS_ABORTIF(ptd, mthd != '.' && mthd != '?' && mthd != '*');
586         ptd->ptr++;
587         if (mthd == '*')
588         {
589             long int            ofs;
590             struct symt*        dt;
591
592             PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &ofs) == -1);
593             PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
594             PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
595             PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
596         }
597     } while (*ptd->ptr != ';');
598     ptd->ptr++;
599
600     return 0;
601 }
602
603 static inline int stabs_pts_read_aggregate(struct ParseTypedefData* ptd, 
604                                            struct symt_udt* sdt)
605 {
606     long                sz, ofs;
607     struct symt*        adt;
608     struct symt*        dt = NULL;
609     int                 idx;
610     int                 doadd;
611
612     PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &sz) == -1);
613
614     doadd = symt_set_udt_size(ptd->module, sdt, sz);
615     if (*ptd->ptr == '!') /* C++ inheritence */
616     {
617         long     num_classes;
618
619         ptd->ptr++;
620         PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &num_classes) == -1);
621         PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
622         while (--num_classes >= 0)
623         {
624             ptd->ptr += 2; /* skip visibility and inheritence */
625             PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &ofs) == -1);
626             PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
627
628             PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &adt) == -1);
629
630             if (doadd)
631             {
632                 char    tmp[256];
633                 WCHAR*  name;
634                 DWORD64 size;
635
636                 symt_get_info(adt, TI_GET_SYMNAME, &name);
637                 strcpy(tmp, "__inherited_class_");
638                 WideCharToMultiByte(CP_ACP, 0, name, -1, 
639                                     tmp + strlen(tmp), sizeof(tmp) - strlen(tmp),
640                                     NULL, NULL);
641                 HeapFree(GetProcessHeap(), 0, name);
642                 /* FIXME: TI_GET_LENGTH will not always work, especially when adt
643                  * has just been seen as a forward definition and not the real stuff
644                  * yet.
645                  * As we don't use much the size of members in structs, this may not
646                  * be much of a problem
647                  */
648                 symt_get_info(adt, TI_GET_LENGTH, &size);
649                 symt_add_udt_element(ptd->module, sdt, tmp, adt, ofs, (DWORD)size * 8);
650             }
651             PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
652         }
653         
654     }
655     /* if the structure has already been filled, just redo the parsing
656      * but don't store results into the struct
657      * FIXME: there's a quite ugly memory leak in there...
658      */
659
660     /* Now parse the individual elements of the structure/union. */
661     while (*ptd->ptr != ';') 
662     {
663         /* agg_name : type ',' <int:offset> ',' <int:size> */
664         idx = ptd->idx;
665
666         if (ptd->ptr[0] == '$' && ptd->ptr[1] == 'v')
667         {
668             long        x;
669
670             if (ptd->ptr[2] == 'f')
671             {
672                 /* C++ virtual method table */
673                 ptd->ptr += 3;
674                 stabs_read_type_enum(&ptd->ptr);
675                 PTS_ABORTIF(ptd, *ptd->ptr++ != ':');
676                 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
677                 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
678                 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &x) == -1);
679                 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
680                 ptd->idx = idx;
681                 continue;
682             }
683             else if (ptd->ptr[2] == 'b')
684             {
685                 ptd->ptr += 3;
686                 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
687                 PTS_ABORTIF(ptd, *ptd->ptr++ != ':');
688                 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
689                 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
690                 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &x) == -1);
691                 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
692                 ptd->idx = idx;
693                 continue;
694             }
695         }
696
697         PTS_ABORTIF(ptd, stabs_pts_read_id(ptd) == -1);
698         /* Ref. TSDF R2.130 Section 7.4.  When the field name is a method name
699          * it is followed by two colons rather than one.
700          */
701         if (*ptd->ptr == ':')
702         {
703             ptd->ptr++; 
704             stabs_pts_read_method_info(ptd);
705             ptd->idx = idx;
706             continue;
707         }
708         else
709         {
710             /* skip C++ member protection /0 /1 or /2 */
711             if (*ptd->ptr == '/') ptd->ptr += 2;
712         }
713         PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &adt) == -1);
714
715         switch (*ptd->ptr++)
716         {
717         case ',':
718             PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &ofs) == -1);
719             PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
720             PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &sz) == -1);
721             PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
722
723             if (doadd) symt_add_udt_element(ptd->module, sdt, ptd->buf + idx, adt, ofs, sz);
724             break;
725         case ':':
726             {
727                 char* tmp;
728                 /* method parameters... terminated by ';' */
729                 PTS_ABORTIF(ptd, !(tmp = strchr(ptd->ptr, ';')));
730                 ptd->ptr = tmp + 1;
731             }
732             break;
733         default:
734             PTS_ABORTIF(ptd, TRUE);
735         }
736         ptd->idx = idx;
737     }
738     PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
739     if (*ptd->ptr == '~')
740     {
741         ptd->ptr++;
742         PTS_ABORTIF(ptd, *ptd->ptr++ != '%');
743         PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
744         PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
745     }
746     return 0;
747 }
748
749 static inline int stabs_pts_read_enum(struct ParseTypedefData* ptd, 
750                                       struct symt_enum* edt)
751 {
752     long        value;
753     int         idx;
754
755     while (*ptd->ptr != ';')
756     {
757         idx = ptd->idx;
758         PTS_ABORTIF(ptd, stabs_pts_read_id(ptd) == -1);
759         PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &value) == -1);
760         PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
761         symt_add_enum_element(ptd->module, edt, ptd->buf + idx, value);
762         ptd->idx = idx;
763     }
764     ptd->ptr++;
765     return 0;
766 }
767
768 static inline int stabs_pts_read_array(struct ParseTypedefData* ptd,
769                                        struct symt** adt)
770 {
771     long                lo, hi;
772     struct symt*        range_dt;
773     struct symt*        base_dt;
774
775     /* ar<typeinfo_nodef>;<int>;<int>;<typeinfo> */
776
777     PTS_ABORTIF(ptd, *ptd->ptr++ != 'r');
778
779     PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &range_dt) == -1);
780     PTS_ABORTIF(ptd, *ptd->ptr++ != ';');       /* ';' */
781     PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &lo) == -1);
782     PTS_ABORTIF(ptd, *ptd->ptr++ != ';');       /* ';' */
783     PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &hi) == -1);
784     PTS_ABORTIF(ptd, *ptd->ptr++ != ';');       /* ';' */
785
786     PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &base_dt) == -1);
787
788     *adt = &symt_new_array(ptd->module, lo, hi, base_dt, range_dt)->symt;
789     return 0;
790 }
791
792 static int stabs_pts_read_type_def(struct ParseTypedefData* ptd, const char* typename,
793                                    struct symt** ret_dt)
794 {
795     int                 idx;
796     long                sz = -1;
797     struct symt*        new_dt = NULL; /* newly created data type */
798     struct symt*        ref_dt;            /* referenced data type (pointer...) */
799     long                filenr1, subnr1, tmp;
800
801     /* things are a bit complicated because of the way the typedefs are stored inside
802      * the file, because addresses can change when realloc is done, so we must call
803      * over and over stabs_find_ref() to keep the correct values around
804      */
805     PTS_ABORTIF(ptd, stabs_pts_read_type_reference(ptd, &filenr1, &subnr1) == -1);
806
807     while (*ptd->ptr == '=')
808     {
809         ptd->ptr++;
810         PTS_ABORTIF(ptd, new_dt != btNoType);
811
812         /* first handle attribute if any */
813         switch (*ptd->ptr)      
814         {
815         case '@':
816             if (*++ptd->ptr == 's')
817             {
818                 ptd->ptr++;
819                 if (stabs_pts_read_number(ptd, &sz) == -1)
820                 {
821                     ERR("Not an attribute... NIY\n");
822                     ptd->ptr -= 2;
823                     return -1;
824                 }
825                 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
826             }
827             break;
828         }
829         /* then the real definitions */
830         switch (*ptd->ptr++)
831         {
832         case '*':
833         case '&':
834             PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &ref_dt) == -1);
835             new_dt = &symt_new_pointer(ptd->module, ref_dt)->symt;
836            break;
837         case 'k': /* 'const' modifier */
838         case 'B': /* 'volatile' modifier */
839             /* just kinda ignore the modifier, I guess -gmt */
840             PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, typename, &new_dt) == -1);
841             break;
842         case '(':
843             ptd->ptr--;
844             PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, typename, &new_dt) == -1);
845             break;
846         case 'a':
847             PTS_ABORTIF(ptd, stabs_pts_read_array(ptd, &new_dt) == -1);
848             break;
849         case 'r':
850             PTS_ABORTIF(ptd, stabs_pts_read_range(ptd, typename, &new_dt) == -1);
851             assert(!*stabs_find_ref(filenr1, subnr1));
852             *stabs_find_ref(filenr1, subnr1) = new_dt;
853             break;
854         case 'f':
855             PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &ref_dt) == -1);
856             new_dt = &symt_new_function_signature(ptd->module, ref_dt, -1)->symt;
857             break;
858         case 'e':
859             stabs_get_basic(ptd, 1 /* int */, &ref_dt);
860             new_dt = &symt_new_enum(ptd->module, typename, ref_dt)->symt;
861             PTS_ABORTIF(ptd, stabs_pts_read_enum(ptd, (struct symt_enum*)new_dt) == -1);
862             break;
863         case 's':
864         case 'u':
865             {
866                 struct symt_udt*    udt;
867                 enum UdtKind kind = (ptd->ptr[-1] == 's') ? UdtStruct : UdtUnion;
868                 /* udt can have been already defined in a forward definition */
869                 udt = (struct symt_udt*)*stabs_find_ref(filenr1, subnr1);
870                 if (!udt)
871                 {
872                     udt = symt_new_udt(ptd->module, typename, 0, kind);
873                     /* we need to set it here, because a struct can hold a pointer
874                      * to itself
875                      */
876                     new_dt = *stabs_find_ref(filenr1, subnr1) = &udt->symt;
877                 }
878                 else
879                 {
880                     unsigned l1, l2;
881                     if (udt->symt.tag != SymTagUDT)
882                     {
883                         ERR("Forward declaration (%p/%s) is not an aggregate (%u)\n",
884                             udt, symt_get_name(&udt->symt), udt->symt.tag);
885                         return -1;
886                     }
887                     /* FIXME: we currently don't correctly construct nested C++
888                      * classes names. Therefore, we could be here with either:
889                      * - typename and udt->hash_elt.name being the same string
890                      *   (non embedded case)
891                      * - typename being foo::bar while udt->hash_elt.name being 
892                      *   just bar
893                      * So, we twist the comparison to test both occurrences. When
894                      * we have proper C++ types in this file, this twist has to be
895                      * removed
896                      */
897                     l1 = strlen(udt->hash_elt.name);
898                     l2 = strlen(typename);
899                     if (l1 > l2 || strcmp(udt->hash_elt.name, typename + l2 - l1))
900                         ERR("Forward declaration name mismatch %s <> %s\n",
901                             udt->hash_elt.name, typename);
902                     new_dt = &udt->symt;
903                 }
904                 PTS_ABORTIF(ptd, stabs_pts_read_aggregate(ptd, udt) == -1);
905             }
906             break;
907         case 'x':
908             idx = ptd->idx;
909             tmp = *ptd->ptr++;
910             PTS_ABORTIF(ptd, stabs_pts_read_id(ptd) == -1);
911             switch (tmp)
912             {
913             case 'e':
914                 stabs_get_basic(ptd, 1 /* int */, &ref_dt);
915                 new_dt = &symt_new_enum(ptd->module, ptd->buf + idx, ref_dt)->symt;
916                 break;
917             case 's':
918                 new_dt = &symt_new_udt(ptd->module, ptd->buf + idx, 0, UdtStruct)->symt;
919                 break;
920             case 'u':
921                 new_dt = &symt_new_udt(ptd->module, ptd->buf + idx, 0, UdtUnion)->symt;
922                 break;
923             default:
924                 return -1;
925             }
926             ptd->idx = idx;
927             break;
928         case '-':
929             {
930                 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &tmp) == -1);
931                 PTS_ABORTIF(ptd, stabs_get_basic(ptd, tmp, &new_dt) == -1);
932                 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
933             }
934             break;
935         case '#':
936             if (*ptd->ptr == '#')
937             {
938                 ptd->ptr++;
939                 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &ref_dt) == -1);
940                 new_dt = &symt_new_function_signature(ptd->module, ref_dt, -1)->symt;
941             }
942             else
943             {
944                 struct symt*    cls_dt;
945                 struct symt*    pmt_dt;
946
947                 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &cls_dt) == -1);
948                 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
949                 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &ref_dt) == -1);
950                 new_dt = &symt_new_function_signature(ptd->module, ref_dt, -1)->symt;
951                 while (*ptd->ptr == ',')
952                 {
953                     ptd->ptr++;
954                     PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &pmt_dt) == -1);
955                 }
956             }
957             break;
958         case 'R':
959             {
960                 long    type, len, unk;
961                 int     basic;
962                 
963                 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &type) == -1);
964                 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');   /* ';' */
965                 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &len) == -1);
966                 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');   /* ';' */
967                 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &unk) == -1);
968                 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');   /* ';' */
969
970                 switch (type) /* see stabs_get_basic for the details */
971                 {
972                 case 1: basic = 12; break;
973                 case 2: basic = 13; break;
974                 case 3: basic = 25; break;
975                 case 4: basic = 26; break;
976                 case 5: basic = 35; break;
977                 case 6: basic = 14; break;
978                 default: PTS_ABORTIF(ptd, 1);
979                 }
980                 PTS_ABORTIF(ptd, stabs_get_basic(ptd, basic, &new_dt) == -1);
981             }
982             break;
983         default:
984             ERR("Unknown type '%c'\n", ptd->ptr[-1]);
985             return -1;
986         }
987     }
988
989     if (!new_dt)
990     {
991         /* is it a forward declaration that has been filled ? */
992         new_dt = *stabs_find_ref(filenr1, subnr1);
993         /* if not, this should be void (which is defined as a ref to itself, but we
994          * don't correctly catch it)
995          */
996         if (!new_dt && typename)
997         {
998             new_dt = &symt_new_basic(ptd->module, btVoid, typename, 0)->symt;
999             PTS_ABORTIF(ptd, strcmp(typename, "void"));
1000         }
1001     }            
1002
1003     *stabs_find_ref(filenr1, subnr1) = *ret_dt = new_dt;
1004
1005     TRACE("Adding (%ld,%ld) %s\n", filenr1, subnr1, typename);
1006
1007     return 0;
1008 }
1009
1010 static int stabs_parse_typedef(struct module* module, const char* ptr, 
1011                                const char* typename)
1012 {
1013     struct ParseTypedefData     ptd;
1014     struct symt*                dt;
1015     int                         ret = -1;
1016
1017     /* check for already existing definition */
1018
1019     TRACE("%s => %s\n", typename, debugstr_a(ptr));
1020     ptd.module = module;
1021     ptd.idx = 0;
1022 #ifdef PTS_DEBUG
1023     ptd.err_idx = 0;
1024 #endif
1025     for (ptd.ptr = ptr - 1; ;)
1026     {
1027         ptd.ptr = strchr(ptd.ptr + 1, ':');
1028         if (ptd.ptr == NULL || *++ptd.ptr != ':') break;
1029     }
1030     if (ptd.ptr)
1031     {
1032         if (*ptd.ptr != '(') ptd.ptr++;
1033         /* most of type definitions take one char, except Tt */
1034         if (*ptd.ptr != '(') ptd.ptr++;
1035         ret = stabs_pts_read_type_def(&ptd, typename, &dt);
1036     }
1037
1038     if (ret == -1 || *ptd.ptr) 
1039     {
1040 #ifdef PTS_DEBUG
1041         int     i;
1042         TRACE("Failure on %s\n", debugstr_a(ptr));
1043         if (ret == -1)
1044         {
1045             for (i = 0; i < ptd.err_idx; i++)
1046             {
1047                 TRACE("[%d]: line %d => %s\n", 
1048                       i, ptd.errors[i].line, debugstr_a(ptd.errors[i].ptr));
1049             }
1050         }
1051         else
1052             TRACE("[0]: => %s\n", debugstr_a(ptd.ptr));
1053             
1054 #else
1055         ERR("Failure on %s at %s\n", debugstr_a(ptr), debugstr_a(ptd.ptr));
1056 #endif
1057         return FALSE;
1058     }
1059
1060     return TRUE;
1061 }
1062
1063 static struct symt* stabs_parse_type(const char* stab)
1064 {
1065     const char* c = stab - 1;
1066
1067     /*
1068      * Look through the stab definition, and figure out what struct symt
1069      * this represents.  If we have something we know about, assign the
1070      * type.
1071      * According to "The \"stabs\" debug format" (Rev 2.130) the name may be
1072      * a C++ name and contain double colons e.g. foo::bar::baz:t5=*6.
1073      */
1074     do
1075     {
1076         if ((c = strchr(c + 1, ':')) == NULL) return NULL;
1077     } while (*++c == ':');
1078
1079     /*
1080      * The next characters say more about the type (i.e. data, function, etc)
1081      * of symbol.  Skip them.  (C++ for example may have Tt).
1082      * Actually this is a very weak description; I think Tt is the only
1083      * multiple combination we should see.
1084      */
1085     while (*c && *c != '(' && !isdigit(*c))
1086         c++;
1087     /*
1088      * The next is either an integer or a (integer,integer).
1089      * The stabs_read_type_enum() takes care that stab_types is large enough.
1090      */
1091     return *stabs_read_type_enum(&c);
1092 }
1093
1094 struct pending_loc_var
1095 {
1096     char                name[256];
1097     struct symt*        type;
1098     enum DataKind       kind;
1099     struct location     loc;
1100 };
1101
1102 struct pending_block
1103 {
1104     struct pending_loc_var*     vars;
1105     unsigned                    num;
1106     unsigned                    allocated;
1107 };
1108
1109 static inline void pending_add(struct pending_block* pending, const char* name,
1110                                enum DataKind dt, const struct location* loc)
1111 {
1112     if (pending->num == pending->allocated)
1113     {
1114         pending->allocated += 8;
1115         if (!pending->vars)
1116             pending->vars = HeapAlloc(GetProcessHeap(), 0, 
1117                                      pending->allocated * sizeof(pending->vars[0]));
1118         else    
1119             pending->vars = HeapReAlloc(GetProcessHeap(), 0, pending->vars,
1120                                        pending->allocated * sizeof(pending->vars[0]));
1121     }
1122     stab_strcpy(pending->vars[pending->num].name, 
1123                 sizeof(pending->vars[pending->num].name), name);
1124     pending->vars[pending->num].type   = stabs_parse_type(name);
1125     pending->vars[pending->num].kind   = dt;
1126     pending->vars[pending->num].loc    = *loc;
1127     pending->num++;
1128 }
1129
1130 static void pending_flush(struct pending_block* pending, struct module* module, 
1131                           struct symt_function* func, struct symt_block* block)
1132 {
1133     int i;
1134
1135     for (i = 0; i < pending->num; i++)
1136     {
1137         symt_add_func_local(module, func, 
1138                             pending->vars[i].kind, &pending->vars[i].loc,
1139                             block, pending->vars[i].type, pending->vars[i].name);
1140     }
1141     pending->num = 0;
1142 }
1143
1144 /******************************************************************
1145  *              stabs_finalize_function
1146  *
1147  * Ends function creation: mainly:
1148  * - cleans up line number information
1149  * - tries to set up a debug-start tag (FIXME: heuristic to be enhanced)
1150  * - for stabs which have abolute address in them, initializes the size of the 
1151  *   function (assuming that current function ends where next function starts)
1152  */
1153 static void stabs_finalize_function(struct module* module, struct symt_function* func,
1154                                     unsigned long size)
1155 {
1156     IMAGEHLP_LINE       il;
1157     struct location     loc;
1158
1159     if (!func) return;
1160     symt_normalize_function(module, func);
1161     /* To define the debug-start of the function, we use the second line number.
1162      * Not 100% bullet proof, but better than nothing
1163      */
1164     if (symt_fill_func_line_info(module, func, func->address, &il) &&
1165         symt_get_func_line_next(module, &il))
1166     {
1167         loc.kind = loc_absolute;
1168         loc.offset = il.Address - func->address;
1169         symt_add_function_point(module, func, SymTagFuncDebugStart, 
1170                                 &loc, NULL);
1171     }
1172     if (size) func->size = size;
1173 }
1174
1175 BOOL stabs_parse(struct module* module, unsigned long load_offset, 
1176                  const void* pv_stab_ptr, int stablen,
1177                  const char* strs, int strtablen)
1178 {
1179     struct symt_function*       curr_func = NULL;
1180     struct symt_block*          block = NULL;
1181     struct symt_compiland*      compiland = NULL;
1182     char                        srcpath[PATH_MAX]; /* path to directory source file is in */
1183     int                         i;
1184     int                         nstab;
1185     const char*                 ptr;
1186     char*                       stabbuff;
1187     unsigned int                stabbufflen;
1188     const struct stab_nlist*    stab_ptr = pv_stab_ptr;
1189     const char*                 strs_end;
1190     int                         strtabinc;
1191     char                        symname[4096];
1192     unsigned                    incl[32];
1193     int                         incl_stk = -1;
1194     int                         source_idx = -1;
1195     struct pending_block        pending;
1196     BOOL                        ret = TRUE;
1197     struct location             loc;
1198
1199     nstab = stablen / sizeof(struct stab_nlist);
1200     strs_end = strs + strtablen;
1201
1202     memset(srcpath, 0, sizeof(srcpath));
1203     memset(stabs_basic, 0, sizeof(stabs_basic));
1204     memset(&pending, 0, sizeof(pending));
1205
1206     /*
1207      * Allocate a buffer into which we can build stab strings for cases
1208      * where the stab is continued over multiple lines.
1209      */
1210     stabbufflen = 65536;
1211     stabbuff = HeapAlloc(GetProcessHeap(), 0, stabbufflen);
1212
1213     strtabinc = 0;
1214     stabbuff[0] = '\0';
1215     for (i = 0; i < nstab; i++, stab_ptr++)
1216     {
1217         ptr = strs + stab_ptr->n_un.n_strx;
1218         if ((ptr > strs_end) || (ptr + strlen(ptr) > strs_end))
1219         {
1220             WARN("Bad stabs string %p\n", ptr);
1221             continue;
1222         }
1223         if (*ptr != '\0' && (ptr[strlen(ptr) - 1] == '\\'))
1224         {
1225             /*
1226              * Indicates continuation.  Append this to the buffer, and go onto the
1227              * next record.  Repeat the process until we find a stab without the
1228              * '/' character, as this indicates we have the whole thing.
1229              */
1230             unsigned    len = strlen(ptr);
1231             if (strlen(stabbuff) + len > stabbufflen)
1232             {
1233                 stabbufflen += 65536;
1234                 stabbuff = HeapReAlloc(GetProcessHeap(), 0, stabbuff, stabbufflen);
1235             }
1236             strncat(stabbuff, ptr, len - 1);
1237             continue;
1238         }
1239         else if (stabbuff[0] != '\0')
1240         {
1241             strcat(stabbuff, ptr);
1242             ptr = stabbuff;
1243         }
1244
1245         /* only symbol entries contain a typedef */
1246         switch (stab_ptr->n_type)
1247         {
1248         case N_GSYM:
1249         case N_LCSYM:
1250         case N_STSYM:
1251         case N_RSYM:
1252         case N_LSYM:
1253         case N_ROSYM:
1254         case N_PSYM:
1255             if (strchr(ptr, '=') != NULL)
1256             {
1257                 /*
1258                  * The stabs aren't in writable memory, so copy it over so we are
1259                  * sure we can scribble on it.
1260                  */
1261                 if (ptr != stabbuff)
1262                 {
1263                     strcpy(stabbuff, ptr);
1264                     ptr = stabbuff;
1265                 }
1266                 stab_strcpy(symname, sizeof(symname), ptr);
1267                 if (!stabs_parse_typedef(module, ptr, symname))
1268                 {
1269                     /* skip this definition */
1270                     stabbuff[0] = '\0';
1271                     continue;
1272                 }
1273             }
1274         }
1275
1276         switch (stab_ptr->n_type)
1277         {
1278         case N_GSYM:
1279             /*
1280              * These are useless with ELF.  They have no value, and you have to
1281              * read the normal symbol table to get the address.  Thus we
1282              * ignore them, and when we process the normal symbol table
1283              * we should do the right thing.
1284              *
1285              * With a.out or mingw, they actually do make some amount of sense.
1286              */
1287             stab_strcpy(symname, sizeof(symname), ptr);
1288             symt_new_global_variable(module, compiland, symname, TRUE /* FIXME */,
1289                                      load_offset + stab_ptr->n_value, 0,
1290                                      stabs_parse_type(ptr));
1291             break;
1292         case N_LCSYM:
1293         case N_STSYM:
1294             /* These are static symbols and BSS symbols. */
1295             stab_strcpy(symname, sizeof(symname), ptr);
1296             symt_new_global_variable(module, compiland, symname, TRUE /* FIXME */,
1297                                      load_offset + stab_ptr->n_value, 0,
1298                                      stabs_parse_type(ptr));
1299             break;
1300         case N_LBRAC:
1301             if (curr_func)
1302             {
1303                 block = symt_open_func_block(module, curr_func, block,
1304                                              stab_ptr->n_value, 0);
1305                 pending_flush(&pending, module, curr_func, block);
1306             }
1307             break;
1308         case N_RBRAC:
1309             if (curr_func)
1310                 block = symt_close_func_block(module, curr_func, block,
1311                                               stab_ptr->n_value);
1312             break;
1313         case N_PSYM:
1314             /* These are function parameters. */
1315             if (curr_func != NULL)
1316             {
1317                 struct symt*    param_type = stabs_parse_type(ptr);
1318                 stab_strcpy(symname, sizeof(symname), ptr);
1319                 loc.kind = loc_regrel;
1320                 loc.reg = 0; /* FIXME */
1321                 loc.offset = stab_ptr->n_value;
1322                 symt_add_func_local(module, curr_func,
1323                                     (long)stab_ptr->n_value >= 0 ? DataIsParam : DataIsLocal,
1324                                     &loc, NULL, param_type, symname);
1325                 symt_add_function_signature_parameter(module, 
1326                                                       (struct symt_function_signature*)curr_func->type, 
1327                                                       param_type);
1328             }
1329             break;
1330         case N_RSYM:
1331             /* These are registers (as local variables) */
1332             if (curr_func != NULL)
1333             {
1334                 loc.kind = loc_register;
1335                 loc.offset = 0;
1336
1337                 switch (stab_ptr->n_value)
1338                 {
1339                 case  0: loc.reg = CV_REG_EAX; break;
1340                 case  1: loc.reg = CV_REG_ECX; break;
1341                 case  2: loc.reg = CV_REG_EDX; break;
1342                 case  3: loc.reg = CV_REG_EBX; break;
1343                 case  4: loc.reg = CV_REG_ESP; break;
1344                 case  5: loc.reg = CV_REG_EBP; break;
1345                 case  6: loc.reg = CV_REG_ESI; break;
1346                 case  7: loc.reg = CV_REG_EDI; break;
1347                 case 11:
1348                 case 12:
1349                 case 13:
1350                 case 14:
1351                 case 15:
1352                 case 16:
1353                 case 17:
1354                 case 18:
1355                 case 19: loc.reg = CV_REG_ST0 + stab_ptr->n_value - 12; break;
1356                 default:
1357                     FIXME("Unknown register value (%lu)\n", stab_ptr->n_value);
1358                     loc.reg = CV_REG_NONE;
1359                     break;
1360                 }
1361                 stab_strcpy(symname, sizeof(symname), ptr);
1362                 if (ptr[strlen(symname) + 1] == 'P')
1363                 {
1364                     struct symt*    param_type = stabs_parse_type(ptr);
1365                     stab_strcpy(symname, sizeof(symname), ptr);
1366                     symt_add_func_local(module, curr_func, DataIsParam, &loc,
1367                                         NULL, param_type, symname);
1368                     symt_add_function_signature_parameter(module, 
1369                                                           (struct symt_function_signature*)curr_func->type, 
1370                                                           param_type);
1371                 }
1372                 else
1373                     pending_add(&pending, ptr, DataIsLocal, &loc);
1374             }
1375             break;
1376         case N_LSYM:
1377             /* These are local variables */
1378             loc.kind = loc_regrel;
1379             loc.reg = 0; /* FIXME */
1380             loc.offset = stab_ptr->n_value;
1381             if (curr_func != NULL) pending_add(&pending, ptr, DataIsLocal, &loc);
1382             break;
1383         case N_SLINE:
1384             /*
1385              * This is a line number.  These are always relative to the start
1386              * of the function (N_FUN), and this makes the lookup easier.
1387              */
1388             if (curr_func != NULL)
1389             {
1390                 assert(source_idx >= 0);
1391                 symt_add_func_line(module, curr_func, source_idx, 
1392                                    stab_ptr->n_desc, stab_ptr->n_value);
1393             }
1394             break;
1395         case N_FUN:
1396             /*
1397              * For now, just declare the various functions.  Later
1398              * on, we will add the line number information and the
1399              * local symbols.
1400              */
1401             /*
1402              * Copy the string to a temp buffer so we
1403              * can kill everything after the ':'.  We do
1404              * it this way because otherwise we end up dirtying
1405              * all of the pages related to the stabs, and that
1406              * sucks up swap space like crazy.
1407              */
1408             stab_strcpy(symname, sizeof(symname), ptr);
1409             if (*symname)
1410             {
1411                 struct symt_function_signature* func_type;
1412
1413                 if (curr_func)
1414                 {
1415                     /* First, clean up the previous function we were working on.
1416                      * Assume size of the func is the delta between current offset
1417                      * and offset of last function
1418                      */
1419                     stabs_finalize_function(module, curr_func, 
1420                                             stab_ptr->n_value ?
1421                                                 (load_offset + stab_ptr->n_value - curr_func->address) : 0);
1422                 }
1423                 func_type = symt_new_function_signature(module, 
1424                                                         stabs_parse_type(ptr), -1);
1425                 curr_func = symt_new_function(module, compiland, symname, 
1426                                               load_offset + stab_ptr->n_value, 0,
1427                                               &func_type->symt);
1428             }
1429             else
1430             {
1431                 /* some versions of GCC to use a N_FUN "" to mark the end of a function
1432                  * and n_value contains the size of the func
1433                  */
1434                 stabs_finalize_function(module, curr_func, stab_ptr->n_value);
1435                 curr_func = NULL;
1436             }
1437             break;
1438         case N_SO:
1439             /*
1440              * This indicates a new source file.  Append the records
1441              * together, to build the correct path name.
1442              */
1443             if (*ptr == '\0') /* end of N_SO file */
1444             {
1445                 /* Nuke old path. */
1446                 srcpath[0] = '\0';
1447                 stabs_finalize_function(module, curr_func, 0);
1448                 curr_func = NULL;
1449                 source_idx = -1;
1450                 incl_stk = -1;
1451                 assert(block == NULL);
1452                 compiland = NULL;
1453             }
1454             else
1455             {
1456                 int len = strlen(ptr);
1457                 if (ptr[len-1] != '/')
1458                 {
1459                     stabs_reset_includes();
1460                     source_idx = source_new(module, srcpath, ptr);
1461                     compiland = symt_new_compiland(module, 0 /* FIXME */, source_idx);
1462                 }
1463                 else
1464                     strcpy(srcpath, ptr);
1465             }
1466             break;
1467         case N_SOL:
1468             source_idx = source_new(module, srcpath, ptr);
1469             break;
1470         case N_UNDF:
1471             strs += strtabinc;
1472             strtabinc = stab_ptr->n_value;
1473             /* I'm not sure this is needed, so trace it before we obsolete it */
1474             if (curr_func)
1475             {
1476                 FIXME("UNDF: curr_func %s\n", curr_func->hash_elt.name);
1477                 stabs_finalize_function(module, curr_func, 0); /* FIXME */
1478                 curr_func = NULL;
1479             }
1480             break;
1481         case N_OPT:
1482             /* Ignore this. We don't care what it points to. */
1483             break;
1484         case N_BINCL:
1485             stabs_add_include(stabs_new_include(ptr, stab_ptr->n_value));
1486             assert(incl_stk < (int)(sizeof(incl) / sizeof(incl[0])) - 1);
1487             incl[++incl_stk] = source_idx;
1488             source_idx = source_new(module, NULL, ptr);
1489             break;
1490         case N_EINCL:
1491             assert(incl_stk >= 0);
1492             source_idx = incl[incl_stk--];
1493             break;
1494         case N_EXCL:
1495             if (stabs_add_include(stabs_find_include(ptr, stab_ptr->n_value)) < 0)
1496             {
1497                 ERR("Excluded header not found (%s,%ld)\n", ptr, stab_ptr->n_value);
1498                 module_reset_debug_info(module);
1499                 ret = FALSE;
1500                 goto done;
1501             }
1502             break;
1503         case N_MAIN:
1504             /* Always ignore these. GCC doesn't even generate them. */
1505             break;
1506         case N_BNSYM:
1507         case N_ENSYM:
1508             /* Always ignore these, they seem to be used only on Darwin. */
1509             break;
1510         default:
1511             ERR("Unknown stab type 0x%02x\n", stab_ptr->n_type);
1512             break;
1513         }
1514         stabbuff[0] = '\0';
1515         TRACE("0x%02x %lx %s\n", 
1516               stab_ptr->n_type, stab_ptr->n_value, debugstr_a(strs + stab_ptr->n_un.n_strx));
1517     }
1518     module->module.SymType = SymDia;
1519     module->module.CVSig = 'S' | ('T' << 8) | ('A' << 16) | ('B' << 24);
1520     /* FIXME: we could have a finer grain here */
1521     module->module.LineNumbers = TRUE;
1522     module->module.GlobalSymbols = TRUE;
1523     module->module.TypeInfo = TRUE;
1524     module->module.SourceIndexed = TRUE;
1525     module->module.Publics = TRUE;
1526 done:
1527     HeapFree(GetProcessHeap(), 0, stabbuff);
1528     stabs_free_includes();
1529     HeapFree(GetProcessHeap(), 0, pending.vars);
1530
1531     return ret;
1532 }