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