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