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