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