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