include/mscvpdb.h: Use flexible array members for the rest of structures.
[wine.git] / dlls / dbghelp / stabs.c
blob20d5623f39151dca81bf0ca2967fe70543bbda4a
1 /*
2 * File stabs.c - read stabs information from the modules
4 * Copyright (C) 1996, Eric Youngdale.
5 * 1999-2005, Eric Pouech
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.
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.
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
22 * Maintenance Information
23 * -----------------------
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
32 #include <sys/types.h>
33 #include <fcntl.h>
34 #include <stdint.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <stdio.h>
38 #include <assert.h>
39 #include <stdarg.h>
41 #include "windef.h"
42 #include "winbase.h"
43 #include "winnls.h"
45 #include "dbghelp_private.h"
46 #include "image_private.h"
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp_stabs);
52 /* Masks for n_type field */
53 #define N_STAB 0xe0
54 #define N_PEXT 0x10
55 #define N_TYPE 0x1e
56 #define N_EXT 0x01
58 /* Values for (n_type & N_TYPE) */
59 #define N_UNDF 0x00
60 #define N_ABS 0x02
61 #define N_INDR 0x0a
62 #define N_SECT 0x0e
63 #define N_GSYM 0x20
64 #define N_FUN 0x24
65 #define N_STSYM 0x26
66 #define N_LCSYM 0x28
67 #define N_MAIN 0x2a
68 #define N_ROSYM 0x2c
69 #define N_BNSYM 0x2e
70 #define N_OPT 0x3c
71 #define N_RSYM 0x40
72 #define N_SLINE 0x44
73 #define N_ENSYM 0x4e
74 #define N_SO 0x64
75 #define N_OSO 0x66
76 #define N_LSYM 0x80
77 #define N_BINCL 0x82
78 #define N_SOL 0x84
79 #define N_PSYM 0xa0
80 #define N_EINCL 0xa2
81 #define N_LBRAC 0xc0
82 #define N_EXCL 0xc2
83 #define N_RBRAC 0xe0
85 static BOOL stab_strcpy(char* dest, int sz, const char* source)
87 char* ptr = dest;
89 * A strcpy routine that stops when we hit the ':' character.
90 * Faster than copying the whole thing, and then nuking the
91 * ':'.
92 * Takes also care of (valid) a::b constructs
94 while (*source != '\0')
96 if (source[0] != ':' && sz-- > 0) *ptr++ = *source++;
97 else if (source[1] == ':' && (sz -= 2) > 0)
99 *ptr++ = *source++;
100 *ptr++ = *source++;
102 else break;
104 *ptr-- = '\0';
105 /* GCC emits, in some cases, a .<digit>+ suffix.
106 * This is used for static variable inside functions, so
107 * that we can have several such variables with same name in
108 * the same compilation unit
109 * We simply ignore that suffix when present (we also get rid
110 * of it in ELF symtab parsing)
112 if (ptr >= dest && isdigit(*ptr))
114 while (ptr > dest && isdigit(*ptr)) ptr--;
115 if (*ptr == '.') *ptr = '\0';
117 return (sz > 0);
120 typedef struct
122 char* name;
123 ULONG_PTR value;
124 struct symt** vector;
125 int nrofentries;
126 } include_def;
128 #define MAX_INCLUDES 5120
130 static include_def* include_defs = NULL;
131 static int num_include_def = 0;
132 static int num_alloc_include_def = 0;
133 static int cu_include_stack[MAX_INCLUDES];
134 static int cu_include_stk_idx = 0;
135 static struct symt** cu_vector = NULL;
136 static int cu_nrofentries = 0;
137 static struct symt_basic* stabs_basic[36];
139 static int stabs_new_include(const char* file, ULONG_PTR val)
141 if (num_include_def == num_alloc_include_def)
143 if (!include_defs)
145 num_alloc_include_def = 256;
146 include_defs = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
147 sizeof(include_defs[0]) * num_alloc_include_def);
149 else
151 num_alloc_include_def *= 2;
152 include_defs = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, include_defs,
153 sizeof(include_defs[0]) * num_alloc_include_def);
156 include_defs[num_include_def].name = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(file) + 1), file);
157 include_defs[num_include_def].value = val;
158 include_defs[num_include_def].vector = NULL;
159 include_defs[num_include_def].nrofentries = 0;
161 return num_include_def++;
164 static int stabs_find_include(const char* file, ULONG_PTR val)
166 int i;
168 for (i = 0; i < num_include_def; i++)
170 if (val == include_defs[i].value &&
171 strcmp(file, include_defs[i].name) == 0)
172 return i;
174 return -1;
177 static int stabs_add_include(int idx)
179 if (idx < 0) return -1;
180 cu_include_stk_idx++;
182 /* if this happens, just bump MAX_INCLUDES */
183 /* we could also handle this as another dynarray */
184 assert(cu_include_stk_idx < MAX_INCLUDES);
185 cu_include_stack[cu_include_stk_idx] = idx;
186 return cu_include_stk_idx;
189 static void stabs_reset_includes(void)
192 * The struct symt:s that we would need to use are reset when
193 * we start a new file. (at least the ones in filenr == 0)
195 cu_include_stk_idx = 0;/* keep 0 as index for the .c file itself */
196 memset(cu_vector, 0, sizeof(cu_vector[0]) * cu_nrofentries);
199 static void stabs_free_includes(void)
201 int i;
203 stabs_reset_includes();
204 for (i = 0; i < num_include_def; i++)
206 HeapFree(GetProcessHeap(), 0, include_defs[i].name);
207 HeapFree(GetProcessHeap(), 0, include_defs[i].vector);
209 HeapFree(GetProcessHeap(), 0, include_defs);
210 include_defs = NULL;
211 num_include_def = 0;
212 num_alloc_include_def = 0;
213 HeapFree(GetProcessHeap(), 0, cu_vector);
214 cu_vector = NULL;
215 cu_nrofentries = 0;
218 static struct symt** stabs_find_ref(LONG_PTR filenr, LONG_PTR subnr)
220 struct symt** ret;
222 /* FIXME: I could perhaps create a dummy include_def for each compilation
223 * unit which would allow not to handle those two cases separately
225 if (filenr == 0)
227 if (cu_nrofentries <= subnr)
229 cu_nrofentries = max( cu_nrofentries * 2, subnr + 1 );
230 if (!cu_vector)
231 cu_vector = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
232 sizeof(cu_vector[0]) * cu_nrofentries);
233 else
234 cu_vector = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
235 cu_vector, sizeof(cu_vector[0]) * cu_nrofentries);
237 ret = &cu_vector[subnr];
239 else
241 include_def* idef;
243 assert(filenr <= cu_include_stk_idx);
244 idef = &include_defs[cu_include_stack[filenr]];
246 if (idef->nrofentries <= subnr)
248 idef->nrofentries = max( idef->nrofentries * 2, subnr + 1 );
249 if (!idef->vector)
250 idef->vector = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
251 sizeof(idef->vector[0]) * idef->nrofentries);
252 else
253 idef->vector = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
254 idef->vector, sizeof(idef->vector[0]) * idef->nrofentries);
256 ret = &idef->vector[subnr];
258 TRACE("(%Id,%Id) => %p (%p)\n", filenr, subnr, ret, *ret);
259 return ret;
262 static struct symt** stabs_read_type_enum(const char** x)
264 LONG_PTR filenr, subnr;
265 const char* iter;
266 char* end;
268 iter = *x;
269 if (*iter == '(')
271 ++iter; /* '(' */
272 filenr = strtol(iter, &end, 10); /* <int> */
273 iter = ++end; /* ',' */
274 subnr = strtol(iter, &end, 10); /* <int> */
275 iter = ++end; /* ')' */
277 else
279 filenr = 0;
280 subnr = strtol(iter, &end, 10); /* <int> */
281 iter = end;
283 *x = iter;
284 return stabs_find_ref(filenr, subnr);
287 #define PTS_DEBUG
288 struct ParseTypedefData
290 const char* ptr;
291 char buf[1024];
292 int idx;
293 struct module* module;
294 #ifdef PTS_DEBUG
295 struct PTS_Error
297 const char* ptr;
298 unsigned line;
299 } errors[16];
300 int err_idx;
301 #endif
304 #ifdef PTS_DEBUG
305 static void stabs_pts_push(struct ParseTypedefData* ptd, unsigned line)
307 assert(ptd->err_idx < ARRAY_SIZE(ptd->errors));
308 ptd->errors[ptd->err_idx].line = line;
309 ptd->errors[ptd->err_idx].ptr = ptd->ptr;
310 ptd->err_idx++;
312 #define PTS_ABORTIF(ptd, t) do { if (t) { stabs_pts_push((ptd), __LINE__); return -1;} } while (0)
313 #else
314 #define PTS_ABORTIF(ptd, t) do { if (t) return -1; } while (0)
315 #endif
317 static int stabs_get_basic(struct ParseTypedefData* ptd, unsigned basic, struct symt** symt)
319 PTS_ABORTIF(ptd, basic >= ARRAY_SIZE(stabs_basic));
321 if (!stabs_basic[basic])
323 switch (basic)
325 case 1: stabs_basic[basic] = symt_get_basic(btInt, 4); break; /* int */
326 case 2: stabs_basic[basic] = symt_get_basic(btChar, 1); break; /* char */
327 case 3: stabs_basic[basic] = symt_get_basic(btInt, 2); break; /* short int */
328 case 4: stabs_basic[basic] = symt_get_basic(btInt, 4); break; /* long int */
329 case 5: stabs_basic[basic] = symt_get_basic(btUInt, 1); break; /* unsigned char */
330 case 6: stabs_basic[basic] = symt_get_basic(btInt, 1); break; /* signed char */
331 case 7: stabs_basic[basic] = symt_get_basic(btUInt, 2); break; /* unsigned short int */
332 case 8: stabs_basic[basic] = symt_get_basic(btUInt, 4); break; /* unsigned int */
333 case 9: stabs_basic[basic] = symt_get_basic(btUInt, 2); break; /* unsigned */
334 case 10: stabs_basic[basic] = symt_get_basic(btUInt, 2); break; /* unsigned long int */
335 case 11: stabs_basic[basic] = symt_get_basic(btVoid, 0); break; /* void */
336 case 12: stabs_basic[basic] = symt_get_basic(btFloat, 4); break; /* float */
337 case 13: stabs_basic[basic] = symt_get_basic(btFloat, 8); break; /* double */
338 case 14: stabs_basic[basic] = symt_get_basic(btFloat, 2); break; /* long double", */
339 case 15: stabs_basic[basic] = symt_get_basic(btInt, 4); break; /* integer */
340 case 16: stabs_basic[basic] = symt_get_basic(btBool, 1); break; /* bool */
341 /* case 17: short real */
342 /* case 18: real */
343 case 25: stabs_basic[basic] = symt_get_basic(btComplex, 8); break; /* float complex */
344 case 26: stabs_basic[basic] = symt_get_basic(btComplex, 6); break; /* double complex", */
345 case 30: stabs_basic[basic] = symt_get_basic(btWChar, 2); break; /* wchar_t */
346 case 31: stabs_basic[basic] = symt_get_basic(btInt, 8); break; /* long long int */
347 case 32: stabs_basic[basic] = symt_get_basic(btUInt, 8); break; /* long long unsigned */
348 /* starting at 35 are wine extensions (especially for R implementation) */
349 case 35: stabs_basic[basic] = symt_get_basic(btComplex, 4); break; /* long double complex", */
350 default: PTS_ABORTIF(ptd, 1);
353 *symt = &stabs_basic[basic]->symt;
354 return 0;
357 static int stabs_pts_read_type_def(struct ParseTypedefData* ptd,
358 const char* typename, struct symt** dt);
360 static int stabs_pts_read_id(struct ParseTypedefData* ptd)
362 const char* first = ptd->ptr;
363 unsigned int template = 0;
364 char ch;
366 while ((ch = *ptd->ptr++) != '\0')
368 switch (ch)
370 case ':':
371 if (template == 0)
373 unsigned int len = ptd->ptr - first - 1;
374 PTS_ABORTIF(ptd, len >= sizeof(ptd->buf) - ptd->idx);
375 memcpy(ptd->buf + ptd->idx, first, len);
376 ptd->buf[ptd->idx + len] = '\0';
377 ptd->idx += len + 1;
378 return 0;
380 break;
381 case '<': template++; break;
382 case '>': PTS_ABORTIF(ptd, template == 0); template--; break;
385 return -1;
388 static int stabs_pts_read_number(struct ParseTypedefData* ptd, LONG_PTR* v)
390 char* last;
392 *v = strtol(ptd->ptr, &last, 10);
393 PTS_ABORTIF(ptd, last == ptd->ptr);
394 ptd->ptr = last;
395 return 0;
398 static int stabs_pts_read_type_reference(struct ParseTypedefData* ptd,
399 LONG_PTR* filenr, LONG_PTR* subnr)
401 if (*ptd->ptr == '(')
403 /* '(' <int> ',' <int> ')' */
404 ptd->ptr++;
405 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, filenr) == -1);
406 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
407 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, subnr) == -1);
408 PTS_ABORTIF(ptd, *ptd->ptr++ != ')');
410 else
412 *filenr = 0;
413 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, subnr) == -1);
415 return 0;
418 struct pts_range_value
420 ULONGLONG val;
421 int sign;
424 static int stabs_pts_read_range_value(struct ParseTypedefData* ptd, struct pts_range_value* prv)
426 char* last;
428 switch (*ptd->ptr)
430 case '0':
431 while (*ptd->ptr == '0') ptd->ptr++;
432 if (*ptd->ptr >= '1' && *ptd->ptr <= '7')
434 switch (ptd->ptr[1])
436 case '0':
437 PTS_ABORTIF(ptd, ptd->ptr[0] != '1');
438 prv->sign = -1;
439 prv->val = 0;
440 while (isdigit(*ptd->ptr)) prv->val = (prv->val << 3) + *ptd->ptr++ - '0';
441 break;
442 case '7':
443 prv->sign = 1;
444 prv->val = 0;
445 while (isdigit(*ptd->ptr)) prv->val = (prv->val << 3) + *ptd->ptr++ - '0';
446 break;
447 default: PTS_ABORTIF(ptd, 1); break;
449 } else prv->sign = 0;
450 break;
451 case '-':
452 prv->sign = -1;
453 prv->val = strtoull(++ptd->ptr, &last, 10);
454 ptd->ptr = last;
455 break;
456 case '+':
457 default:
458 prv->sign = 1;
459 prv->val = strtoull(ptd->ptr, &last, 10);
460 ptd->ptr = last;
461 break;
463 return 0;
466 static int stabs_pts_read_range(struct ParseTypedefData* ptd, const char* typename,
467 struct symt** dt)
469 struct symt* ref;
470 struct pts_range_value lo;
471 struct pts_range_value hi;
472 unsigned size;
473 enum BasicType bt;
474 int i;
475 ULONGLONG v;
477 /* type ';' <int> ';' <int> ';' */
478 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &ref) == -1);
479 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
480 PTS_ABORTIF(ptd, stabs_pts_read_range_value(ptd, &lo) == -1);
481 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
482 PTS_ABORTIF(ptd, stabs_pts_read_range_value(ptd, &hi) == -1);
483 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
485 /* basically, we don't use ref... in some cases, for example, float is declared
486 * as a derived type of int... which won't help us... so we guess the types
487 * from the various formats
489 if (lo.sign == 0 && hi.sign < 0)
491 bt = btUInt;
492 size = hi.val;
494 else if (lo.sign < 0 && hi.sign == 0)
496 bt = btUInt;
497 size = lo.val;
499 else if (lo.sign > 0 && hi.sign == 0)
501 bt = btFloat;
502 size = lo.val;
504 else if (lo.sign < 0 && hi.sign > 0)
506 v = 1 << 7;
507 for (i = 7; i < 64; i += 8)
509 if (lo.val == v && hi.val == v - 1)
511 bt = btInt;
512 size = (i + 1) / 8;
513 break;
515 v <<= 8;
517 PTS_ABORTIF(ptd, i >= 64);
519 else if (lo.sign == 0 && hi.sign > 0)
521 if (hi.val == 127) /* specific case for char... */
523 bt = btChar;
524 size = 1;
526 else
528 v = 1;
529 for (i = 8; i <= 64; i += 8)
531 v <<= 8;
532 if (hi.val + 1 == v)
534 bt = btUInt;
535 size = (i + 1) / 8;
536 break;
539 PTS_ABORTIF(ptd, i > 64);
542 else PTS_ABORTIF(ptd, 1);
544 *dt = &symt_get_basic(bt, size)->symt;
545 return 0;
548 static inline int stabs_pts_read_method_info(struct ParseTypedefData* ptd)
550 struct symt* dt;
551 const char* tmp;
552 char mthd;
556 /* get type of return value */
557 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
558 if (*ptd->ptr == ';') ptd->ptr++;
560 /* get types of parameters */
561 if (*ptd->ptr == ':')
563 PTS_ABORTIF(ptd, !(tmp = strchr(ptd->ptr + 1, ';')));
564 ptd->ptr = tmp + 1;
566 PTS_ABORTIF(ptd, !(*ptd->ptr >= '0' && *ptd->ptr <= '9'));
567 ptd->ptr++;
568 PTS_ABORTIF(ptd, !(ptd->ptr[0] >= 'A' && *ptd->ptr <= 'D'));
569 mthd = *++ptd->ptr;
570 PTS_ABORTIF(ptd, mthd != '.' && mthd != '?' && mthd != '*');
571 ptd->ptr++;
572 if (mthd == '*')
574 LONG_PTR ofs;
576 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &ofs) == -1);
577 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
578 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
579 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
581 } while (*ptd->ptr != ';');
582 ptd->ptr++;
584 return 0;
587 static inline int stabs_pts_read_aggregate(struct ParseTypedefData* ptd,
588 struct symt_udt* sdt)
590 LONG_PTR sz, ofs;
591 struct symt* adt;
592 struct symt* dt = NULL;
593 int idx;
594 int doadd;
596 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &sz) == -1);
598 doadd = symt_set_udt_size(ptd->module, sdt, sz);
599 if (*ptd->ptr == '!') /* C++ inheritance */
601 LONG_PTR num_classes;
603 ptd->ptr++;
604 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &num_classes) == -1);
605 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
606 while (--num_classes >= 0)
608 ptd->ptr += 2; /* skip visibility and inheritance */
609 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &ofs) == -1);
610 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
612 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &adt) == -1);
614 if (doadd && adt)
616 char tmp[256];
618 strcpy(tmp, "__inherited_class_");
619 strcat(tmp, symt_get_name(adt));
621 symt_add_udt_element(ptd->module, sdt, tmp, adt, ofs, 0, 0);
623 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
627 /* if the structure has already been filled, just redo the parsing
628 * but don't store results into the struct
629 * FIXME: there's a quite ugly memory leak in there...
632 /* Now parse the individual elements of the structure/union. */
633 while (*ptd->ptr != ';')
635 /* agg_name : type ',' <int:offset> ',' <int:size> */
636 idx = ptd->idx;
638 if (ptd->ptr[0] == '$' && ptd->ptr[1] == 'v')
640 LONG_PTR x;
642 if (ptd->ptr[2] == 'f')
644 /* C++ virtual method table */
645 ptd->ptr += 3;
646 stabs_read_type_enum(&ptd->ptr);
647 PTS_ABORTIF(ptd, *ptd->ptr++ != ':');
648 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
649 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
650 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &x) == -1);
651 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
652 ptd->idx = idx;
653 continue;
655 else if (ptd->ptr[2] == 'b')
657 ptd->ptr += 3;
658 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
659 PTS_ABORTIF(ptd, *ptd->ptr++ != ':');
660 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
661 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
662 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &x) == -1);
663 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
664 ptd->idx = idx;
665 continue;
669 PTS_ABORTIF(ptd, stabs_pts_read_id(ptd) == -1);
670 /* Ref. TSDF R2.130 Section 7.4. When the field name is a method name
671 * it is followed by two colons rather than one.
673 if (*ptd->ptr == ':')
675 ptd->ptr++;
676 stabs_pts_read_method_info(ptd);
677 ptd->idx = idx;
678 continue;
680 else
682 /* skip C++ member protection /0 /1 or /2 */
683 if (*ptd->ptr == '/') ptd->ptr += 2;
685 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &adt) == -1);
687 switch (*ptd->ptr++)
689 case ',':
690 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &ofs) == -1);
691 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
692 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &sz) == -1);
693 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
695 if (doadd) symt_add_udt_element(ptd->module, sdt, ptd->buf + idx, adt, ofs, 0, 0);
696 break;
697 case ':':
699 const char* tmp;
700 /* method parameters... terminated by ';' */
701 PTS_ABORTIF(ptd, !(tmp = strchr(ptd->ptr, ';')));
702 ptd->ptr = tmp + 1;
704 break;
705 default:
706 PTS_ABORTIF(ptd, TRUE);
708 ptd->idx = idx;
710 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
711 if (*ptd->ptr == '~')
713 ptd->ptr++;
714 PTS_ABORTIF(ptd, *ptd->ptr++ != '%');
715 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
716 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
718 return 0;
721 static inline int stabs_pts_read_enum(struct ParseTypedefData* ptd,
722 struct symt_enum* edt)
724 LONG_PTR value;
725 int idx;
727 while (*ptd->ptr != ';')
729 idx = ptd->idx;
730 PTS_ABORTIF(ptd, stabs_pts_read_id(ptd) == -1);
731 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &value) == -1);
732 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
733 symt_add_enum_element(ptd->module, edt, ptd->buf + idx, value);
734 ptd->idx = idx;
736 ptd->ptr++;
737 return 0;
740 static inline int stabs_pts_read_array(struct ParseTypedefData* ptd,
741 struct symt** adt)
743 LONG_PTR lo, hi;
744 struct symt* range_dt;
745 struct symt* base_dt;
747 /* ar<typeinfo_nodef>;<int>;<int>;<typeinfo> */
749 PTS_ABORTIF(ptd, *ptd->ptr++ != 'r');
751 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &range_dt) == -1);
752 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
753 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &lo) == -1);
754 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
755 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &hi) == -1);
756 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
758 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &base_dt) == -1);
760 *adt = &symt_new_array(ptd->module, lo, hi - lo + 1, base_dt, range_dt)->symt;
761 return 0;
764 static int stabs_pts_read_type_def(struct ParseTypedefData* ptd, const char* typename,
765 struct symt** ret_dt)
767 int idx;
768 LONG_PTR sz = -1;
769 struct symt* new_dt = NULL; /* newly created data type */
770 struct symt* ref_dt; /* referenced data type (pointer...) */
771 LONG_PTR filenr1, subnr1, tmp;
773 /* things are a bit complicated because of the way the typedefs are stored inside
774 * the file, because addresses can change when realloc is done, so we must call
775 * over and over stabs_find_ref() to keep the correct values around
777 PTS_ABORTIF(ptd, stabs_pts_read_type_reference(ptd, &filenr1, &subnr1) == -1);
779 while (*ptd->ptr == '=')
781 ptd->ptr++;
782 PTS_ABORTIF(ptd, new_dt != NULL);
784 /* first handle attribute if any */
785 switch (*ptd->ptr)
787 case '@':
788 if (*++ptd->ptr == 's')
790 ptd->ptr++;
791 if (stabs_pts_read_number(ptd, &sz) == -1)
793 ERR("Not an attribute... NIY\n");
794 ptd->ptr -= 2;
795 return -1;
797 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
799 break;
801 /* then the real definitions */
802 switch (*ptd->ptr++)
804 case '*':
805 case '&':
806 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &ref_dt) == -1);
807 new_dt = &symt_new_pointer(ptd->module, ref_dt, ptd->module->cpu->word_size)->symt;
808 break;
809 case 'k': /* 'const' modifier */
810 case 'B': /* 'volatile' modifier */
811 /* just kinda ignore the modifier, I guess -gmt */
812 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, typename, &new_dt) == -1);
813 break;
814 case '(':
815 ptd->ptr--;
816 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, typename, &new_dt) == -1);
817 break;
818 case 'a':
819 PTS_ABORTIF(ptd, stabs_pts_read_array(ptd, &new_dt) == -1);
820 break;
821 case 'r':
823 struct symt** prev_dt;
824 PTS_ABORTIF(ptd, stabs_pts_read_range(ptd, typename, &new_dt) == -1);
826 prev_dt = stabs_find_ref(filenr1, subnr1);
827 /* allow redefining with same base type */
828 if (*prev_dt && *prev_dt != new_dt) WARN("Multiple range def in %ls\n", ptd->module->module.ModuleName);
829 else *prev_dt = new_dt;
831 break;
832 case 'f':
833 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &ref_dt) == -1);
834 new_dt = &symt_new_function_signature(ptd->module, ref_dt, -1)->symt;
835 break;
836 case 'e':
837 stabs_get_basic(ptd, 1 /* int */, &ref_dt);
838 new_dt = &symt_new_enum(ptd->module, typename, ref_dt)->symt;
839 PTS_ABORTIF(ptd, stabs_pts_read_enum(ptd, (struct symt_enum*)new_dt) == -1);
840 break;
841 case 's':
842 case 'u':
844 struct symt_udt* udt;
845 enum UdtKind kind = (ptd->ptr[-1] == 's') ? UdtStruct : UdtUnion;
846 /* udt can have been already defined in a forward definition */
847 udt = (struct symt_udt*)*stabs_find_ref(filenr1, subnr1);
848 if (!udt)
850 udt = symt_new_udt(ptd->module, typename, 0, kind);
851 /* we need to set it here, because a struct can hold a pointer
852 * to itself
854 new_dt = *stabs_find_ref(filenr1, subnr1) = &udt->symt;
856 else
858 unsigned l1, l2;
859 if (udt->symt.tag != SymTagUDT)
861 ERR("Forward declaration (%p/%s) is not an aggregate (%u)\n",
862 udt, symt_get_name(&udt->symt), udt->symt.tag);
863 return -1;
865 /* FIXME: we currently don't correctly construct nested C++
866 * classes names. Therefore, we could be here with either:
867 * - typename and udt->hash_elt.name being the same string
868 * (non embedded case)
869 * - typename being foo::bar while udt->hash_elt.name being
870 * just bar
871 * So, we twist the comparison to test both occurrences. When
872 * we have proper C++ types in this file, this twist has to be
873 * removed
875 l1 = strlen(udt->hash_elt.name);
876 l2 = strlen(typename);
877 if (l1 > l2 || strcmp(udt->hash_elt.name, typename + l2 - l1))
878 ERR("Forward declaration name mismatch %s <> %s\n",
879 udt->hash_elt.name, typename);
880 new_dt = &udt->symt;
882 PTS_ABORTIF(ptd, stabs_pts_read_aggregate(ptd, udt) == -1);
884 break;
885 case 'x':
886 idx = ptd->idx;
887 tmp = *ptd->ptr++;
888 PTS_ABORTIF(ptd, stabs_pts_read_id(ptd) == -1);
889 switch (tmp)
891 case 'e':
892 stabs_get_basic(ptd, 1 /* int */, &ref_dt);
893 new_dt = &symt_new_enum(ptd->module, ptd->buf + idx, ref_dt)->symt;
894 break;
895 case 's':
896 new_dt = &symt_new_udt(ptd->module, ptd->buf + idx, 0, UdtStruct)->symt;
897 break;
898 case 'u':
899 new_dt = &symt_new_udt(ptd->module, ptd->buf + idx, 0, UdtUnion)->symt;
900 break;
901 default:
902 return -1;
904 ptd->idx = idx;
905 break;
906 case '-':
908 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &tmp) == -1);
909 PTS_ABORTIF(ptd, stabs_get_basic(ptd, tmp, &new_dt) == -1);
910 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
912 break;
913 case '#':
914 if (*ptd->ptr == '#')
916 ptd->ptr++;
917 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &ref_dt) == -1);
918 new_dt = &symt_new_function_signature(ptd->module, ref_dt, -1)->symt;
920 else
922 struct symt* cls_dt;
923 struct symt* pmt_dt;
925 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &cls_dt) == -1);
926 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
927 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &ref_dt) == -1);
928 new_dt = &symt_new_function_signature(ptd->module, ref_dt, -1)->symt;
929 while (*ptd->ptr == ',')
931 ptd->ptr++;
932 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &pmt_dt) == -1);
935 break;
936 case 'R':
938 LONG_PTR type, len, unk;
939 int basic;
941 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &type) == -1);
942 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
943 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &len) == -1);
944 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
945 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &unk) == -1);
946 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
948 switch (type) /* see stabs_get_basic for the details */
950 case 1: basic = 12; break;
951 case 2: basic = 13; break;
952 case 3: basic = 25; break;
953 case 4: basic = 26; break;
954 case 5: basic = 35; break;
955 case 6: basic = 14; break;
956 default: PTS_ABORTIF(ptd, 1);
958 PTS_ABORTIF(ptd, stabs_get_basic(ptd, basic, &new_dt) == -1);
960 break;
961 default:
962 ERR("Unknown type '%c'\n", ptd->ptr[-1]);
963 return -1;
967 if (!new_dt)
969 /* is it a forward declaration that has been filled ? */
970 new_dt = *stabs_find_ref(filenr1, subnr1);
971 /* if not, this should be void (which is defined as a ref to itself, but we
972 * don't correctly catch it)
974 if (!new_dt && typename)
976 new_dt = &symt_get_basic(btVoid, 0)->symt;
977 PTS_ABORTIF(ptd, strcmp(typename, "void"));
981 *stabs_find_ref(filenr1, subnr1) = *ret_dt = new_dt;
983 TRACE("Adding (%Id,%Id) %s\n", filenr1, subnr1, debugstr_a(typename));
985 return 0;
988 static int stabs_parse_typedef(struct module* module, const char* ptr,
989 const char* typename)
991 struct ParseTypedefData ptd;
992 struct symt* dt;
993 int ret = -1;
995 /* check for already existing definition */
997 TRACE("%s => %s\n", typename, debugstr_a(ptr));
998 ptd.module = module;
999 ptd.idx = 0;
1000 #ifdef PTS_DEBUG
1001 ptd.err_idx = 0;
1002 #endif
1003 for (ptd.ptr = ptr - 1; ;)
1005 ptd.ptr = strchr(ptd.ptr + 1, ':');
1006 if (ptd.ptr == NULL || *++ptd.ptr != ':') break;
1008 if (ptd.ptr)
1010 if (*ptd.ptr != '(') ptd.ptr++;
1011 /* most of type definitions take one char, except Tt */
1012 if (*ptd.ptr != '(') ptd.ptr++;
1013 ret = stabs_pts_read_type_def(&ptd, typename, &dt);
1016 if (ret == -1 || *ptd.ptr)
1018 #ifdef PTS_DEBUG
1019 int i;
1020 TRACE("Failure on %s\n", debugstr_a(ptr));
1021 if (ret == -1)
1023 for (i = 0; i < ptd.err_idx; i++)
1025 TRACE("[%d]: line %d => %s\n",
1026 i, ptd.errors[i].line, debugstr_a(ptd.errors[i].ptr));
1029 else
1030 TRACE("[0]: => %s\n", debugstr_a(ptd.ptr));
1032 #else
1033 ERR("Failure on %s at %s\n", debugstr_a(ptr), debugstr_a(ptd.ptr));
1034 #endif
1035 return FALSE;
1038 return TRUE;
1041 static struct symt* stabs_parse_type(const char* stab)
1043 const char* c = stab - 1;
1046 * Look through the stab definition, and figure out what struct symt
1047 * this represents. If we have something we know about, assign the
1048 * type.
1049 * According to "The \"stabs\" debug format" (Rev 2.130) the name may be
1050 * a C++ name and contain double colons e.g. foo::bar::baz:t5=*6.
1054 if ((c = strchr(c + 1, ':')) == NULL) return NULL;
1055 } while (*++c == ':');
1058 * The next characters say more about the type (i.e. data, function, etc)
1059 * of symbol. Skip them. (C++ for example may have Tt).
1060 * Actually this is a very weak description; I think Tt is the only
1061 * multiple combination we should see.
1063 while (*c && *c != '(' && !isdigit(*c))
1064 c++;
1066 * The next is either an integer or a (integer,integer).
1067 * The stabs_read_type_enum() takes care that stab_types is large enough.
1069 return *stabs_read_type_enum(&c);
1072 enum pending_obj_kind
1074 PENDING_VAR,
1075 PENDING_LINE,
1078 struct pending_loc_var
1080 char name[256];
1081 struct symt* type;
1082 enum DataKind kind;
1083 struct location loc;
1086 struct pending_line
1088 int source_idx;
1089 int line_num;
1090 ULONG_PTR offset;
1091 ULONG_PTR load_offset;
1094 struct pending_object
1096 enum pending_obj_kind tag;
1097 union {
1098 struct pending_loc_var var;
1099 struct pending_line line;
1100 } u;
1103 struct pending_list
1105 struct pending_object* objs;
1106 unsigned num;
1107 unsigned allocated;
1110 static inline void pending_make_room(struct pending_list* pending)
1112 if (pending->num == pending->allocated)
1114 if (!pending->objs)
1116 pending->allocated = 8;
1117 pending->objs = HeapAlloc(GetProcessHeap(), 0,
1118 pending->allocated * sizeof(pending->objs[0]));
1120 else
1122 pending->allocated *= 2;
1123 pending->objs = HeapReAlloc(GetProcessHeap(), 0, pending->objs,
1124 pending->allocated * sizeof(pending->objs[0]));
1129 static inline void pending_add_var(struct pending_list* pending, const char* name,
1130 enum DataKind dt, const struct location* loc)
1132 pending_make_room(pending);
1133 pending->objs[pending->num].tag = PENDING_VAR;
1134 if (!stab_strcpy(pending->objs[pending->num].u.var.name,
1135 sizeof(pending->objs[pending->num].u.var.name), name))
1137 ERR("symbol too long %s\n", debugstr_a(name));
1138 return;
1140 pending->objs[pending->num].u.var.type = stabs_parse_type(name);
1141 pending->objs[pending->num].u.var.kind = dt;
1142 pending->objs[pending->num].u.var.loc = *loc;
1143 pending->num++;
1146 static inline void pending_add_line(struct pending_list* pending, int source_idx,
1147 int line_num, ULONG_PTR offset,
1148 ULONG_PTR load_offset)
1150 pending_make_room(pending);
1151 pending->objs[pending->num].tag = PENDING_LINE;
1152 pending->objs[pending->num].u.line.source_idx = source_idx;
1153 pending->objs[pending->num].u.line.line_num = line_num;
1154 pending->objs[pending->num].u.line.offset = offset;
1155 pending->objs[pending->num].u.line.load_offset = load_offset;
1156 pending->num++;
1159 static void pending_flush(struct pending_list* pending, struct module* module,
1160 struct symt_function* func, struct symt_block* block)
1162 unsigned int i;
1164 for (i = 0; i < pending->num; i++)
1166 switch (pending->objs[i].tag)
1168 case PENDING_VAR:
1169 symt_add_func_local(module, func,
1170 pending->objs[i].u.var.kind, &pending->objs[i].u.var.loc,
1171 block, pending->objs[i].u.var.type, pending->objs[i].u.var.name);
1172 break;
1173 case PENDING_LINE:
1174 if (module->type == DMT_MACHO)
1175 pending->objs[i].u.line.offset -= func->ranges[0].low - pending->objs[i].u.line.load_offset;
1176 symt_add_func_line(module, func, pending->objs[i].u.line.source_idx,
1177 pending->objs[i].u.line.line_num, func->ranges[0].low + pending->objs[i].u.line.offset);
1178 break;
1179 default:
1180 ERR("Unknown pending object tag %u\n", (unsigned)pending->objs[i].tag);
1181 break;
1184 pending->num = 0;
1187 /******************************************************************
1188 * stabs_finalize_function
1190 * Ends function creation: mainly:
1191 * - cleans up line number information
1192 * - tries to set up a debug-start tag (FIXME: heuristic to be enhanced)
1193 * - for stabs which have absolute address in them, initializes the size of the
1194 * function (assuming that current function ends where next function starts)
1196 static void stabs_finalize_function(struct module* module, struct symt_function* func,
1197 ULONG_PTR size)
1199 IMAGEHLP_LINE64 il;
1200 struct location loc;
1201 DWORD disp;
1203 if (!func) return;
1204 /* To define the debug-start of the function, we use the second line number.
1205 * Not 100% bullet proof, but better than nothing
1207 il.SizeOfStruct = sizeof(il);
1208 if (SymGetLineFromAddr64(module->process->handle, func->ranges[0].low, &disp, &il) &&
1209 SymGetLineNext64(module->process->handle, &il))
1211 loc.kind = loc_absolute;
1212 loc.offset = il.Address - func->ranges[0].low;
1213 symt_add_function_point(module, func, SymTagFuncDebugStart,
1214 &loc, NULL);
1216 if (size) func->ranges[0].high = func->ranges[0].low + size;
1219 static inline void stabbuf_append(char **buf, unsigned *buf_size, const char *str)
1221 unsigned str_len, buf_len;
1223 str_len = strlen(str);
1224 buf_len = strlen(*buf);
1226 if(str_len+buf_len >= *buf_size) {
1227 *buf_size += buf_len + str_len;
1228 *buf = HeapReAlloc(GetProcessHeap(), 0, *buf, *buf_size);
1231 strcpy(*buf+buf_len, str);
1234 BOOL stabs_parse(struct module* module, ULONG_PTR load_offset,
1235 const char* pv_stab_ptr, size_t nstab, size_t stabsize,
1236 const char* strs, int strtablen,
1237 stabs_def_cb callback, void* user)
1239 struct symt_function* curr_func = NULL;
1240 struct symt_block* block = NULL;
1241 struct symt_compiland* compiland = NULL;
1242 char* srcpath = NULL;
1243 int i;
1244 const char* ptr;
1245 char* stabbuff;
1246 unsigned int stabbufflen;
1247 const struct stab_nlist* stab_ptr;
1248 const char* strs_end;
1249 int strtabinc;
1250 char symname[4096];
1251 unsigned incl[32];
1252 int incl_stk = -1;
1253 int source_idx = -1;
1254 struct pending_list pending_block;
1255 struct pending_list pending_func;
1256 BOOL ret = TRUE;
1257 struct location loc;
1258 unsigned char type;
1259 uint64_t n_value;
1261 strs_end = strs + strtablen;
1263 memset(stabs_basic, 0, sizeof(stabs_basic));
1264 memset(&pending_block, 0, sizeof(pending_block));
1265 memset(&pending_func, 0, sizeof(pending_func));
1268 * Allocate a buffer into which we can build stab strings for cases
1269 * where the stab is continued over multiple lines.
1271 stabbufflen = 65536;
1272 stabbuff = HeapAlloc(GetProcessHeap(), 0, stabbufflen);
1274 strtabinc = 0;
1275 stabbuff[0] = '\0';
1276 for (i = 0; i < nstab; i++)
1278 stab_ptr = (struct stab_nlist *)(pv_stab_ptr + i * stabsize);
1279 n_value = stabsize == sizeof(struct macho64_nlist) ? ((struct macho64_nlist *)stab_ptr)->n_value : stab_ptr->n_value;
1280 ptr = strs + stab_ptr->n_strx;
1281 if ((ptr > strs_end) || (ptr + strlen(ptr) > strs_end))
1283 WARN("Bad stabs string %p\n", ptr);
1284 continue;
1286 if (*ptr != '\0' && (ptr[strlen(ptr) - 1] == '\\'))
1289 * Indicates continuation. Append this to the buffer, and go onto the
1290 * next record. Repeat the process until we find a stab without the
1291 * '/' character, as this indicates we have the whole thing.
1293 stabbuf_append(&stabbuff, &stabbufflen, ptr);
1294 continue;
1296 else if (stabbuff[0] != '\0')
1298 stabbuf_append(&stabbuff, &stabbufflen, ptr);
1299 ptr = stabbuff;
1302 if (stab_ptr->n_type & N_STAB)
1303 type = stab_ptr->n_type;
1304 else
1306 type = (stab_ptr->n_type & N_TYPE);
1307 if (module->type == DMT_MACHO) type &= ~N_PEXT;
1310 /* only symbol entries contain a typedef */
1311 switch (type)
1313 case N_GSYM:
1314 case N_LCSYM:
1315 case N_STSYM:
1316 case N_RSYM:
1317 case N_LSYM:
1318 case N_ROSYM:
1319 case N_PSYM:
1320 if (strchr(ptr, '=') != NULL)
1323 * The stabs aren't in writable memory, so copy it over so we are
1324 * sure we can scribble on it.
1326 if (ptr != stabbuff)
1328 stabbuff[0] = 0;
1329 stabbuf_append(&stabbuff, &stabbufflen, ptr);
1330 ptr = stabbuff;
1332 if (!stab_strcpy(symname, sizeof(symname), ptr) ||
1333 !stabs_parse_typedef(module, ptr, symname))
1335 /* skip this definition */
1336 stabbuff[0] = '\0';
1337 continue;
1342 switch (type)
1344 case N_GSYM:
1346 * These are useless with ELF. They have no value, and you have to
1347 * read the normal symbol table to get the address. Thus we
1348 * ignore them, and when we process the normal symbol table
1349 * we should do the right thing.
1351 * With a.out or mingw, they actually do make some amount of sense.
1353 if (!stab_strcpy(symname, sizeof(symname), ptr))
1355 ERR("symbol too long: %s\n", debugstr_a(ptr));
1356 stabbuff[0] = '\0';
1357 continue;
1359 loc.kind = loc_absolute;
1360 loc.reg = 0;
1361 loc.offset = load_offset + n_value;
1362 symt_new_global_variable(module, compiland, symname, TRUE /* FIXME */,
1363 loc, 0, stabs_parse_type(ptr));
1364 break;
1365 case N_LCSYM:
1366 case N_STSYM:
1367 /* These are static symbols and BSS symbols. */
1368 if (!stab_strcpy(symname, sizeof(symname), ptr))
1370 ERR("symbol too long: %s\n", debugstr_a(ptr));
1371 stabbuff[0] = '\0';
1372 continue;
1374 loc.kind = loc_absolute;
1375 loc.reg = 0;
1376 loc.offset = load_offset + n_value;
1377 symt_new_global_variable(module, compiland, symname, TRUE /* FIXME */,
1378 loc, 0, stabs_parse_type(ptr));
1379 break;
1380 case N_LBRAC:
1381 if (curr_func)
1383 block = symt_open_func_block(module, curr_func, block, 1);
1384 block->ranges[0].low = curr_func->ranges[0].low + n_value;
1385 block->ranges[0].high = 0; /* will be set by N_RBRAC */
1386 pending_flush(&pending_block, module, curr_func, block);
1388 break;
1389 case N_RBRAC:
1390 if (curr_func)
1392 block->ranges[0].high = curr_func->ranges[0].low + n_value;
1393 block = symt_close_func_block(module, curr_func, block);
1395 break;
1396 case N_PSYM:
1397 /* These are function parameters. */
1398 if (curr_func != NULL)
1400 struct symt* param_type = stabs_parse_type(ptr);
1401 if (!stab_strcpy(symname, sizeof(symname), ptr))
1403 ERR("symbol too long: %s\n", debugstr_a(ptr));
1404 stabbuff[0] = '\0';
1405 continue;
1407 loc.kind = loc_regrel;
1408 loc.reg = module->cpu->frame_regno;
1409 loc.offset = n_value;
1410 symt_add_func_local(module, curr_func,
1411 (int)n_value >= 0 ? DataIsParam : DataIsLocal,
1412 &loc, NULL, param_type, symname);
1413 symt_add_function_signature_parameter(module,
1414 (struct symt_function_signature*)curr_func->type,
1415 param_type);
1417 break;
1418 case N_RSYM:
1419 /* These are registers (as local variables) */
1420 if (curr_func != NULL)
1422 loc.kind = loc_register;
1423 loc.offset = 0;
1425 switch (n_value)
1427 case 0: loc.reg = CV_REG_EAX; break;
1428 case 1: loc.reg = CV_REG_ECX; break;
1429 case 2: loc.reg = CV_REG_EDX; break;
1430 case 3: loc.reg = CV_REG_EBX; break;
1431 case 4: loc.reg = CV_REG_ESP; break;
1432 case 5: loc.reg = CV_REG_EBP; break;
1433 case 6: loc.reg = CV_REG_ESI; break;
1434 case 7: loc.reg = CV_REG_EDI; break;
1435 case 11:
1436 case 12:
1437 case 13:
1438 case 14:
1439 case 15:
1440 case 16:
1441 case 17:
1442 case 18:
1443 case 19: loc.reg = CV_REG_ST0 + n_value - 12; break;
1444 case 21:
1445 case 22:
1446 case 23:
1447 case 24:
1448 case 25:
1449 case 26:
1450 case 27:
1451 case 28: loc.reg = CV_REG_XMM0 + n_value - 21; break;
1452 case 29:
1453 case 30:
1454 case 31:
1455 case 32:
1456 case 33:
1457 case 34:
1458 case 35:
1459 case 36: loc.reg = CV_REG_MM0 + n_value - 29; break;
1460 default:
1461 FIXME("Unknown register value (%Iu)\n", (ULONG_PTR)n_value);
1462 loc.reg = CV_REG_NONE;
1463 break;
1465 if (!stab_strcpy(symname, sizeof(symname), ptr))
1467 ERR("symbol too long: %s\n", debugstr_a(ptr));
1468 stabbuff[0] = '\0';
1469 continue;
1471 if (ptr[strlen(symname) + 1] == 'P')
1473 struct symt* param_type = stabs_parse_type(ptr);
1474 stab_strcpy(symname, sizeof(symname), ptr);
1475 symt_add_func_local(module, curr_func, DataIsParam, &loc,
1476 NULL, param_type, symname);
1477 symt_add_function_signature_parameter(module,
1478 (struct symt_function_signature*)curr_func->type,
1479 param_type);
1481 else
1482 pending_add_var(&pending_block, ptr, DataIsLocal, &loc);
1484 break;
1485 case N_LSYM:
1486 /* These are local variables */
1487 loc.kind = loc_regrel;
1488 loc.reg = module->cpu->frame_regno;
1489 loc.offset = n_value;
1490 if (curr_func != NULL) pending_add_var(&pending_block, ptr, DataIsLocal, &loc);
1491 break;
1492 case N_SLINE:
1494 * This is a line number. These are always relative to the start
1495 * of the function (N_FUN), and this makes the lookup easier.
1497 assert(source_idx >= 0);
1498 if (curr_func != NULL)
1500 ULONG_PTR offset = n_value;
1501 if (module->type == DMT_MACHO)
1502 offset -= curr_func->ranges[0].low - load_offset;
1503 symt_add_func_line(module, curr_func, source_idx,
1504 stab_ptr->n_desc, curr_func->ranges[0].low + offset);
1506 else pending_add_line(&pending_func, source_idx, stab_ptr->n_desc,
1507 n_value, load_offset);
1508 break;
1509 case N_FUN:
1511 * For now, just declare the various functions. Later
1512 * on, we will add the line number information and the
1513 * local symbols.
1516 * Copy the string to a temp buffer so we
1517 * can kill everything after the ':'. We do
1518 * it this way because otherwise we end up dirtying
1519 * all of the pages related to the stabs, and that
1520 * sucks up swap space like crazy.
1522 if (!stab_strcpy(symname, sizeof(symname), ptr))
1524 ERR("symbol too long: %s\n", debugstr_a(ptr));
1525 stabbuff[0] = '\0';
1526 continue;
1528 if (*symname)
1530 struct symt_function_signature* func_type;
1532 if (curr_func)
1534 /* First, clean up the previous function we were working on.
1535 * Assume size of the func is the delta between current offset
1536 * and offset of last function
1538 stabs_finalize_function(module, curr_func,
1539 n_value ?
1540 (load_offset + n_value - curr_func->ranges[0].low) : 0);
1542 func_type = symt_new_function_signature(module,
1543 stabs_parse_type(ptr), -1);
1544 curr_func = symt_new_function(module, compiland, symname,
1545 load_offset + n_value, 0,
1546 &func_type->symt);
1547 pending_flush(&pending_func, module, curr_func, NULL);
1549 else
1551 /* some versions of GCC to use a N_FUN "" to mark the end of a function
1552 * and n_value contains the size of the func
1554 stabs_finalize_function(module, curr_func, n_value);
1555 curr_func = NULL;
1557 break;
1558 case N_SO:
1560 * This indicates a new source file. Append the records
1561 * together, to build the correct path name.
1563 if (*ptr == '\0') /* end of N_SO file */
1565 /* Nuke old path. */
1566 HeapFree(GetProcessHeap(), 0, srcpath);
1567 srcpath = NULL;
1568 stabs_finalize_function(module, curr_func, 0);
1569 curr_func = NULL;
1570 source_idx = -1;
1571 incl_stk = -1;
1572 assert(block == NULL);
1573 compiland = NULL;
1575 else
1577 int len = strlen(ptr);
1578 if (ptr[len-1] != '/')
1580 stabs_reset_includes();
1581 source_idx = source_new(module, srcpath, ptr);
1582 compiland = symt_new_compiland(module, source_idx);
1584 else
1586 srcpath = HeapAlloc(GetProcessHeap(), 0, len + 1);
1587 strcpy(srcpath, ptr);
1590 break;
1591 case N_SOL:
1592 source_idx = source_new(module, srcpath, ptr);
1593 break;
1594 case N_UNDF:
1595 strs += strtabinc;
1596 strtabinc = n_value;
1597 /* I'm not sure this is needed, so trace it before we obsolete it */
1598 if (curr_func)
1600 FIXME("UNDF: curr_func %s\n", curr_func->hash_elt.name);
1601 stabs_finalize_function(module, curr_func, 0); /* FIXME */
1602 curr_func = NULL;
1604 break;
1605 case N_OPT:
1606 /* Ignore this. We don't care what it points to. */
1607 break;
1608 case N_BINCL:
1609 stabs_add_include(stabs_new_include(ptr, n_value));
1610 assert(incl_stk < (int) ARRAY_SIZE(incl) - 1);
1611 incl[++incl_stk] = source_idx;
1612 source_idx = source_new(module, NULL, ptr);
1613 break;
1614 case N_EINCL:
1615 assert(incl_stk >= 0);
1616 source_idx = incl[incl_stk--];
1617 break;
1618 case N_EXCL:
1619 if (stabs_add_include(stabs_find_include(ptr, n_value)) < 0)
1621 ERR("Excluded header not found (%s,%Id)\n", ptr, (ULONG_PTR)n_value);
1622 module_reset_debug_info(module);
1623 ret = FALSE;
1624 goto done;
1626 break;
1627 case N_MAIN:
1628 /* Always ignore these. GCC doesn't even generate them. */
1629 break;
1630 case N_BNSYM:
1631 case N_ENSYM:
1632 case N_OSO:
1633 case N_INDR:
1634 /* Always ignore these, they seem to be used only on Darwin. */
1635 break;
1636 case N_ABS:
1637 case N_SECT:
1638 /* FIXME: Other definition types (N_TEXT, N_DATA, N_BSS, ...)? */
1639 if (callback)
1641 BOOL is_public = (stab_ptr->n_type & N_EXT);
1642 BOOL is_global = is_public;
1644 /* "private extern"; shared among compilation units in a shared
1645 * library, but not accessible from outside the library. */
1646 if (stab_ptr->n_type & N_PEXT)
1648 is_public = FALSE;
1649 is_global = TRUE;
1652 if (*ptr == '_') ptr++;
1653 if (!stab_strcpy(symname, sizeof(symname), ptr))
1655 ERR("symbol too long: %s\n", debugstr_a(ptr));
1656 stabbuff[0] = '\0';
1657 continue;
1660 callback(module, load_offset, symname, n_value,
1661 is_public, is_global, stab_ptr->n_other, compiland, user);
1663 break;
1664 default:
1665 ERR("Unknown stab type 0x%02x\n", type);
1666 break;
1668 stabbuff[0] = '\0';
1669 TRACE("0x%02x %Ix %s\n",
1670 stab_ptr->n_type, (ULONG_PTR)n_value, debugstr_a(strs + stab_ptr->n_strx));
1672 module->module.SymType = SymDia;
1673 module->debug_format_bitmask |= DHEXT_FORMAT_STABS;
1674 /* FIXME: we could have a finer grain here */
1675 module->module.LineNumbers = TRUE;
1676 module->module.GlobalSymbols = TRUE;
1677 module->module.TypeInfo = TRUE;
1678 module->module.SourceIndexed = TRUE;
1679 module->module.Publics = TRUE;
1680 done:
1681 HeapFree(GetProcessHeap(), 0, stabbuff);
1682 stabs_free_includes();
1683 HeapFree(GetProcessHeap(), 0, pending_block.objs);
1684 HeapFree(GetProcessHeap(), 0, pending_func.objs);
1685 HeapFree(GetProcessHeap(), 0, srcpath);
1687 return ret;