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