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