2 * File stabs.c - read stabs information from the modules
4 * Copyright (C) 1996, Eric Youngdale.
5 * 1999-2004, 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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
29 * available (hopefully) from http:\\sources.redhat.com\gdb\onlinedocs
34 #include <sys/types.h>
37 #ifdef HAVE_SYS_MMAN_H
48 #define PATH_MAX MAX_PATH
58 #include "dbghelp_private.h"
60 #include "wine/debug.h"
62 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp_stabs);
92 struct stab_nlist* n_next;
98 unsigned long n_value;
101 static void stab_strcpy(char* dest, int sz, const char* source)
104 * A strcpy routine that stops when we hit the ':' character.
105 * Faster than copying the whole thing, and then nuking the
108 while (*source != '\0' && *source != ':' && sz-- > 0)
111 /* GCC seems to emit, in some cases, a .<digit>+ suffix.
112 * This is used for static variable inside functions, so
113 * that we can have several such variables with same name in
114 * the same compilation unit
115 * We simply ignore that suffix when present (we also get rid
116 * of it in ELF symtab parsing)
120 while (isdigit(*dest)) dest--;
121 if (*dest == '.') *dest = '\0';
131 struct symt** vector;
135 #define MAX_INCLUDES 5120
137 static include_def* include_defs = NULL;
138 static int num_include_def = 0;
139 static int num_alloc_include_def = 0;
140 static int cu_include_stack[MAX_INCLUDES];
141 static int cu_include_stk_idx = 0;
142 static struct symt** cu_vector = NULL;
143 static int cu_nrofentries = 0;
144 static struct symt_basic* stabs_basic[36];
146 static int stabs_new_include(const char* file, unsigned long val)
148 if (num_include_def == num_alloc_include_def)
150 num_alloc_include_def += 256;
152 include_defs = HeapAlloc(GetProcessHeap(), 0,
153 sizeof(include_defs[0]) * num_alloc_include_def);
155 include_defs = HeapReAlloc(GetProcessHeap(), 0, include_defs,
156 sizeof(include_defs[0]) * num_alloc_include_def);
157 memset(include_defs + num_include_def, 0, sizeof(include_defs[0]) * 256);
159 include_defs[num_include_def].name = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(file) + 1), file);
160 include_defs[num_include_def].value = val;
161 include_defs[num_include_def].vector = NULL;
162 include_defs[num_include_def].nrofentries = 0;
164 return num_include_def++;
167 static int stabs_find_include(const char* file, unsigned long val)
171 for (i = 0; i < num_include_def; i++)
173 if (val == include_defs[i].value &&
174 strcmp(file, include_defs[i].name) == 0)
180 static int stabs_add_include(int idx)
182 if (idx < 0) return -1;
183 cu_include_stk_idx++;
185 /* if this happens, just bump MAX_INCLUDES */
186 /* we could also handle this as another dynarray */
187 assert(cu_include_stk_idx < MAX_INCLUDES);
188 cu_include_stack[cu_include_stk_idx] = idx;
189 return cu_include_stk_idx;
192 static void stabs_reset_includes(void)
195 * The struct symt:s that we would need to use are reset when
196 * we start a new file. (at least the ones in filenr == 0)
198 cu_include_stk_idx = 0;/* keep 0 as index for the .c file itself */
199 memset(cu_vector, 0, sizeof(cu_vector[0]) * cu_nrofentries);
202 static void stabs_free_includes(void)
206 stabs_reset_includes();
207 for (i = 0; i < num_include_def; i++)
209 HeapFree(GetProcessHeap(), 0, include_defs[i].name);
210 HeapFree(GetProcessHeap(), 0, include_defs[i].vector);
212 HeapFree(GetProcessHeap(), 0, include_defs);
215 num_alloc_include_def = 0;
216 HeapFree(GetProcessHeap(), 0, cu_vector);
221 static struct symt** stabs_find_ref(long filenr, long subnr)
225 /* FIXME: I could perhaps create a dummy include_def for each compilation
226 * unit which would allow not to handle those two cases separately
230 if (cu_nrofentries <= subnr)
233 cu_vector = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
234 sizeof(cu_vector[0]) * (subnr+1));
236 cu_vector = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
237 cu_vector, sizeof(cu_vector[0]) * (subnr+1));
238 cu_nrofentries = subnr + 1;
240 ret = &cu_vector[subnr];
246 assert(filenr <= cu_include_stk_idx);
247 idef = &include_defs[cu_include_stack[filenr]];
249 if (idef->nrofentries <= subnr)
252 idef->vector = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
253 sizeof(idef->vector[0]) * (subnr+1));
255 idef->vector = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
256 idef->vector, sizeof(idef->vector[0]) * (subnr+1));
257 idef->nrofentries = subnr + 1;
259 ret = &idef->vector[subnr];
261 TRACE("(%ld,%ld) => %p (%p)\n", filenr, subnr, ret, *ret);
265 static struct symt** stabs_read_type_enum(const char** x)
272 filenr = strtol(*x, (char**)x, 10); /* <int> */
274 subnr = strtol(*x, (char**)x, 10); /* <int> */
280 subnr = strtol(*x, (char**)x, 10); /* <int> */
282 return stabs_find_ref(filenr, subnr);
286 struct ParseTypedefData
291 struct module* module;
303 static void stabs_pts_push(struct ParseTypedefData* ptd, unsigned line)
305 assert(ptd->err_idx < sizeof(ptd->errors) / sizeof(ptd->errors[0]));
306 ptd->errors[ptd->err_idx].line = line;
307 ptd->errors[ptd->err_idx].ptr = ptd->ptr;
310 #define PTS_ABORTIF(ptd, t) do { if (t) { stabs_pts_push((ptd), __LINE__); return -1;} } while (0)
312 #define PTS_ABORTIF(ptd, t) do { if (t) return -1; } while (0)
315 static int stabs_get_basic(struct ParseTypedefData* ptd, unsigned basic, struct symt** symt)
317 PTS_ABORTIF(ptd, basic >= sizeof(stabs_basic) / sizeof(stabs_basic[0]));
319 if (!stabs_basic[basic])
323 case 1: stabs_basic[basic] = symt_new_basic(ptd->module, btInt, "int", 4); break;
324 case 2: stabs_basic[basic] = symt_new_basic(ptd->module, btChar, "char", 1); break;
325 case 3: stabs_basic[basic] = symt_new_basic(ptd->module, btInt, "short int", 2); break;
326 case 4: stabs_basic[basic] = symt_new_basic(ptd->module, btInt, "long int", 4); break;
327 case 5: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt, "unsigned char", 1); break;
328 case 6: stabs_basic[basic] = symt_new_basic(ptd->module, btInt, "signed char", 1); break;
329 case 7: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt, "unsigned short int", 2); break;
330 case 8: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt, "unsigned int", 4); break;
331 case 9: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt, "unsigned", 2); break;
332 case 10: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt, "unsigned long int", 2); break;
333 case 11: stabs_basic[basic] = symt_new_basic(ptd->module, btVoid, "void", 0); break;
334 case 12: stabs_basic[basic] = symt_new_basic(ptd->module, btFloat, "float", 4); break;
335 case 13: stabs_basic[basic] = symt_new_basic(ptd->module, btFloat, "double", 8); break;
336 case 14: stabs_basic[basic] = symt_new_basic(ptd->module, btFloat, "long double", 12); break;
337 case 15: stabs_basic[basic] = symt_new_basic(ptd->module, btInt, "integer", 4); break;
338 case 16: stabs_basic[basic] = symt_new_basic(ptd->module, btBool, "bool", 1); break;
339 /* case 17: short real */
341 case 25: stabs_basic[basic] = symt_new_basic(ptd->module, btComplex, "float complex", 8); break;
342 case 26: stabs_basic[basic] = symt_new_basic(ptd->module, btComplex, "double complex", 16); break;
343 case 30: stabs_basic[basic] = symt_new_basic(ptd->module, btWChar, "wchar_t", 2); break;
344 case 31: stabs_basic[basic] = symt_new_basic(ptd->module, btInt, "long long int", 8); break;
345 case 32: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt, "long long unsigned", 8); break;
346 /* starting at 35 are wine extensions (especially for R implementation) */
347 case 35: stabs_basic[basic] = symt_new_basic(ptd->module, btComplex, "long double complex", 24); break;
348 default: PTS_ABORTIF(ptd, 1);
351 *symt = &stabs_basic[basic]->symt;
355 static int stabs_pts_read_type_def(struct ParseTypedefData* ptd,
356 const char* typename, struct symt** dt);
358 static int stabs_pts_read_id(struct ParseTypedefData* ptd)
360 const char* first = ptd->ptr;
363 PTS_ABORTIF(ptd, (ptd->ptr = strchr(ptd->ptr, ':')) == NULL);
364 len = ptd->ptr - first;
365 PTS_ABORTIF(ptd, len >= sizeof(ptd->buf) - ptd->idx);
366 memcpy(ptd->buf + ptd->idx, first, len);
367 ptd->buf[ptd->idx + len] = '\0';
369 ptd->ptr++; /* ':' */
373 static int stabs_pts_read_number(struct ParseTypedefData* ptd, long* v)
377 *v = strtol(ptd->ptr, &last, 10);
378 PTS_ABORTIF(ptd, last == ptd->ptr);
383 static int stabs_pts_read_type_reference(struct ParseTypedefData* ptd,
384 long* filenr, long* subnr)
386 if (*ptd->ptr == '(')
388 /* '(' <int> ',' <int> ')' */
390 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, filenr) == -1);
391 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
392 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, subnr) == -1);
393 PTS_ABORTIF(ptd, *ptd->ptr++ != ')');
398 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, subnr) == -1);
403 struct pts_range_value
405 unsigned long long val;
409 static int stabs_pts_read_range_value(struct ParseTypedefData* ptd, struct pts_range_value* prv)
416 while (*ptd->ptr == '0') ptd->ptr++;
417 if (*ptd->ptr >= '1' && *ptd->ptr <= '7')
422 PTS_ABORTIF(ptd, ptd->ptr[0] != '1');
425 while (isdigit(*ptd->ptr)) prv->val = (prv->val << 3) + *ptd->ptr++ - '0';
430 while (isdigit(*ptd->ptr)) prv->val = (prv->val << 3) + *ptd->ptr++ - '0';
432 default: PTS_ABORTIF(ptd, 1); break;
434 } else prv->sign = 0;
438 prv->val = strtoull(++ptd->ptr, &last, 10);
444 prv->val = strtoull(ptd->ptr, &last, 10);
451 static int stabs_pts_read_range(struct ParseTypedefData* ptd, const char* typename,
455 struct pts_range_value lo;
456 struct pts_range_value hi;
460 unsigned long long v;
462 /* type ';' <int> ';' <int> ';' */
463 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &ref) == -1);
464 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
465 PTS_ABORTIF(ptd, stabs_pts_read_range_value(ptd, &lo) == -1);
466 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
467 PTS_ABORTIF(ptd, stabs_pts_read_range_value(ptd, &hi) == -1);
468 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
470 /* basically, we don't use ref... in some cases, for example, float is declared
471 * as a derivated type of int... which won't help us... so we guess the types
472 * from the various formats
474 if (lo.sign == 0 && hi.sign < 0)
479 else if (lo.sign < 0 && hi.sign == 0)
484 else if (lo.sign > 0 && hi.sign == 0)
489 else if (lo.sign < 0 && hi.sign > 0)
492 for (i = 7; i < 64; i += 8)
494 if (lo.val == v && hi.val == v - 1)
502 PTS_ABORTIF(ptd, i >= 64);
504 else if (lo.sign == 0 && hi.sign > 0)
506 if (hi.val == 127) /* specific case for char... */
514 for (i = 8; i <= 64; i += 8)
524 PTS_ABORTIF(ptd, i > 64);
527 else PTS_ABORTIF(ptd, 1);
529 *dt = &symt_new_basic(ptd->module, bt, typename, size)->symt;
533 static inline int stabs_pts_read_method_info(struct ParseTypedefData* ptd)
541 /* get type of return value */
542 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
543 if (*ptd->ptr == ';') ptd->ptr++;
545 /* get types of parameters */
546 if (*ptd->ptr == ':')
548 PTS_ABORTIF(ptd, !(tmp = strchr(ptd->ptr + 1, ';')));
551 PTS_ABORTIF(ptd, !(*ptd->ptr >= '0' && *ptd->ptr <= '9'));
553 PTS_ABORTIF(ptd, !(ptd->ptr[0] >= 'A' && *ptd->ptr <= 'D'));
555 PTS_ABORTIF(ptd, mthd != '.' && mthd != '?' && mthd != '*');
562 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &ofs) == -1);
563 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
564 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
565 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
567 } while (*ptd->ptr != ';');
573 static inline int stabs_pts_read_aggregate(struct ParseTypedefData* ptd,
574 struct symt_udt* sdt)
578 struct symt* dt = NULL;
582 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &sz) == -1);
584 doadd = symt_set_udt_size(ptd->module, sdt, sz);
585 if (*ptd->ptr == '!') /* C++ inheritence */
590 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &num_classes) == -1);
591 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
592 while (--num_classes >= 0)
594 ptd->ptr += 2; /* skip visibility and inheritence */
595 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &ofs) == -1);
596 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
598 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &adt) == -1);
606 symt_get_info(adt, TI_GET_SYMNAME, &name);
607 strcmp(tmp, "__inherited_class_");
608 WideCharToMultiByte(CP_ACP, 0, name, -1,
609 tmp + strlen(tmp), sizeof(tmp) - strlen(tmp),
611 HeapFree(GetProcessHeap(), 0, name);
612 /* FIXME: TI_GET_LENGTH will not always work, especially when adt
613 * has just been seen as a forward definition and not the real stuff
615 * As we don't use much the size of members in structs, this may not
616 * be much of a problem
618 symt_get_info(adt, TI_GET_LENGTH, &size);
619 symt_add_udt_element(ptd->module, sdt, tmp, adt, ofs, size * 8);
621 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
625 /* if the structure has already been filled, just redo the parsing
626 * but don't store results into the struct
627 * FIXME: there's a quite ugly memory leak in there...
630 /* Now parse the individual elements of the structure/union. */
631 while (*ptd->ptr != ';')
633 /* agg_name : type ',' <int:offset> ',' <int:size> */
636 if (ptd->ptr[0] == '$' && ptd->ptr[1] == 'v')
640 if (ptd->ptr[2] == 'f')
642 /* C++ virtual method table */
644 stabs_read_type_enum(&ptd->ptr);
645 PTS_ABORTIF(ptd, *ptd->ptr++ != ':');
646 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
647 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
648 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &x) == -1);
649 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
653 else if (ptd->ptr[2] == 'b')
656 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
657 PTS_ABORTIF(ptd, *ptd->ptr++ != ':');
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_number(ptd, &x) == -1);
661 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
667 PTS_ABORTIF(ptd, stabs_pts_read_id(ptd) == -1);
668 /* Ref. TSDF R2.130 Section 7.4. When the field name is a method name
669 * it is followed by two colons rather than one.
671 if (*ptd->ptr == ':')
674 stabs_pts_read_method_info(ptd);
680 /* skip C++ member protection /0 /1 or /2 */
681 if (*ptd->ptr == '/') ptd->ptr += 2;
683 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &adt) == -1);
688 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &ofs) == -1);
689 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
690 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &sz) == -1);
691 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
693 if (doadd) symt_add_udt_element(ptd->module, sdt, ptd->buf + idx, adt, ofs, sz);
698 /* method parameters... terminated by ';' */
699 PTS_ABORTIF(ptd, !(tmp = strchr(ptd->ptr, ';')));
704 PTS_ABORTIF(ptd, TRUE);
708 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
709 if (*ptd->ptr == '~')
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++ != ';');
719 static inline int stabs_pts_read_enum(struct ParseTypedefData* ptd,
720 struct symt_enum* edt)
725 while (*ptd->ptr != ';')
728 PTS_ABORTIF(ptd, stabs_pts_read_id(ptd) == -1);
729 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &value) == -1);
730 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
731 symt_add_enum_element(ptd->module, edt, ptd->buf + idx, value);
738 static inline int stabs_pts_read_array(struct ParseTypedefData* ptd,
744 /* ar<typeinfo_nodef>;<int>;<int>;<typeinfo> */
746 PTS_ABORTIF(ptd, *ptd->ptr++ != 'r');
747 /* FIXME: range type is lost, always assume int */
748 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &rdt) == -1);
749 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
750 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &lo) == -1);
751 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
752 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &hi) == -1);
753 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
755 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &rdt) == -1);
757 *adt = &symt_new_array(ptd->module, lo, hi, rdt)->symt;
761 static int stabs_pts_read_type_def(struct ParseTypedefData* ptd, const char* typename,
762 struct symt** ret_dt)
766 struct symt* new_dt = NULL; /* newly created data type */
767 struct symt* ref_dt; /* referenced data type (pointer...) */
768 long filenr1, subnr1, tmp;
770 /* things are a bit complicated because of the way the typedefs are stored inside
771 * the file, because addresses can change when realloc is done, so we must call
772 * over and over stabs_find_ref() to keep the correct values around
774 PTS_ABORTIF(ptd, stabs_pts_read_type_reference(ptd, &filenr1, &subnr1) == -1);
776 while (*ptd->ptr == '=')
779 PTS_ABORTIF(ptd, new_dt != btNoType);
781 /* first handle attribute if any */
785 if (*++ptd->ptr == 's')
788 if (stabs_pts_read_number(ptd, &sz) == -1)
790 ERR("Not an attribute... NIY\n");
794 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
798 /* then the real definitions */
803 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &ref_dt) == -1);
804 new_dt = &symt_new_pointer(ptd->module, ref_dt)->symt;
806 case 'k': /* 'const' modifier */
807 case 'B': /* 'volatile' modifier */
808 /* just kinda ignore the modifier, I guess -gmt */
809 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, typename, &new_dt) == -1);
813 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, typename, &new_dt) == -1);
816 PTS_ABORTIF(ptd, stabs_pts_read_array(ptd, &new_dt) == -1);
819 PTS_ABORTIF(ptd, stabs_pts_read_range(ptd, typename, &new_dt) == -1);
820 assert(!*stabs_find_ref(filenr1, subnr1));
821 *stabs_find_ref(filenr1, subnr1) = new_dt;
824 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &ref_dt) == -1);
825 new_dt = &symt_new_function_signature(ptd->module, ref_dt)->symt;
828 new_dt = &symt_new_enum(ptd->module, typename)->symt;
829 PTS_ABORTIF(ptd, stabs_pts_read_enum(ptd, (struct symt_enum*)new_dt) == -1);
834 struct symt_udt* udt;
835 enum UdtKind kind = (ptd->ptr[-1] == 's') ? UdtStruct : UdtUnion;
836 /* udt can have been already defined in a forward definition */
837 udt = (struct symt_udt*)*stabs_find_ref(filenr1, subnr1);
840 udt = symt_new_udt(ptd->module, typename, 0, kind);
841 /* we need to set it here, because a struct can hold a pointer
844 new_dt = *stabs_find_ref(filenr1, subnr1) = &udt->symt;
848 if (udt->symt.tag != SymTagUDT)
850 ERR("Forward declaration (%p/%s) is not an aggregate (%u)\n",
851 udt, symt_get_name(&udt->symt), udt->symt.tag);
854 if (strcmp(udt->hash_elt.name, typename))
855 ERR("Forward declaration name mismatch %s <> %s\n",
856 udt->hash_elt.name, typename);
857 /* should check typename is the same too */
860 PTS_ABORTIF(ptd, stabs_pts_read_aggregate(ptd, udt) == -1);
866 PTS_ABORTIF(ptd, stabs_pts_read_id(ptd) == -1);
870 new_dt = &symt_new_enum(ptd->module, ptd->buf + idx)->symt;
873 new_dt = &symt_new_udt(ptd->module, ptd->buf + idx, 0, UdtStruct)->symt;
876 new_dt = &symt_new_udt(ptd->module, ptd->buf + idx, 0, UdtUnion)->symt;
885 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &tmp) == -1);
886 PTS_ABORTIF(ptd, stabs_get_basic(ptd, tmp, &new_dt) == -1);
887 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
891 if (*ptd->ptr == '#')
894 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &ref_dt) == -1);
895 new_dt = &symt_new_function_signature(ptd->module, ref_dt)->symt;
902 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &cls_dt) == -1);
903 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
904 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &ref_dt) == -1);
905 new_dt = &symt_new_function_signature(ptd->module, ref_dt)->symt;
906 while (*ptd->ptr == ',')
909 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &pmt_dt) == -1);
918 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &type) == -1);
919 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
920 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &len) == -1);
921 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
922 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &unk) == -1);
923 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
925 switch (type) /* see stabs_get_basic for the details */
927 case 1: basic = 12; break;
928 case 2: basic = 13; break;
929 case 3: basic = 25; break;
930 case 4: basic = 26; break;
931 case 5: basic = 35; break;
932 case 6: basic = 14; break;
933 default: PTS_ABORTIF(ptd, 1);
935 PTS_ABORTIF(ptd, stabs_get_basic(ptd, basic, &new_dt) == -1);
939 ERR("Unknown type '%c'\n", ptd->ptr[-1]);
946 /* is it a forward declaration that has been filled ? */
947 new_dt = *stabs_find_ref(filenr1, subnr1);
948 /* if not, this should be void (which is defined as a ref to itself, but we
949 * don't correctly catch it)
951 if (!new_dt && typename)
953 new_dt = &symt_new_basic(ptd->module, btVoid, typename, 0)->symt;
954 PTS_ABORTIF(ptd, strcmp(typename, "void"));
958 *stabs_find_ref(filenr1, subnr1) = *ret_dt = new_dt;
960 TRACE("Adding (%ld,%ld) %s\n", filenr1, subnr1, typename);
965 static int stabs_parse_typedef(struct module* module, const char* ptr,
966 const char* typename)
968 struct ParseTypedefData ptd;
972 /* check for already existing definition */
974 TRACE("%s\n", debugstr_a(ptr));
980 for (ptd.ptr = ptr - 1; ;)
982 ptd.ptr = strchr(ptd.ptr + 1, ':');
983 if (ptd.ptr == NULL || *++ptd.ptr != ':') break;
987 if (*ptd.ptr != '(') ptd.ptr++;
988 /* most of type definitions take one char, except Tt */
989 if (*ptd.ptr != '(') ptd.ptr++;
990 ret = stabs_pts_read_type_def(&ptd, typename, &dt);
993 if (ret == -1 || *ptd.ptr)
997 TRACE("Failure on %s\n", debugstr_a(ptr));
1000 for (i = 0; i < ptd.err_idx; i++)
1002 TRACE("[%d]: line %d => %s\n",
1003 i, ptd.errors[i].line, debugstr_a(ptd.errors[i].ptr));
1007 TRACE("[0]: => %s\n", debugstr_a(ptd.ptr));
1010 ERR("Failure on %s at %s\n", debugstr_a(ptr), debugstr_a(ptd.ptr));
1018 static struct symt* stabs_parse_type(const char* stab)
1020 const char* c = stab - 1;
1023 * Look through the stab definition, and figure out what struct symt
1024 * this represents. If we have something we know about, assign the
1026 * According to "The \"stabs\" debug format" (Rev 2.130) the name may be
1027 * a C++ name and contain double colons e.g. foo::bar::baz:t5=*6.
1031 if ((c = strchr(c + 1, ':')) == NULL) return NULL;
1032 } while (*++c == ':');
1035 * The next characters say more about the type (i.e. data, function, etc)
1036 * of symbol. Skip them. (C++ for example may have Tt).
1037 * Actually this is a very weak description; I think Tt is the only
1038 * multiple combination we should see.
1040 while (*c && *c != '(' && !isdigit(*c))
1043 * The next is either an integer or a (integer,integer).
1044 * The stabs_read_type_enum() takes care that stab_types is large enough.
1046 return *stabs_read_type_enum(&c);
1049 struct pending_loc_var
1057 /******************************************************************
1058 * stabs_finalize_function
1060 * Ends function creation: mainly:
1061 * - cleans up line number information
1062 * - tries to set up a debug-start tag (FIXME: heuristic to be enhanced)
1064 static void stabs_finalize_function(struct module* module, struct symt_function* func)
1069 symt_normalize_function(module, func);
1070 /* To define the debug-start of the function, we use the second line number.
1071 * Not 100% bullet proof, but better than nothing
1073 if (symt_fill_func_line_info(module, func, func->address, &il) &&
1074 symt_get_func_line_next(module, &il))
1076 symt_add_function_point(module, func, SymTagFuncDebugStart,
1077 il.Address - func->address, NULL);
1081 BOOL stabs_parse(struct module* module, const char* addr,
1082 unsigned long load_offset, unsigned int staboff, int stablen,
1083 unsigned int strtaboff, int strtablen)
1085 struct symt_function* curr_func = NULL;
1086 struct symt_block* block = NULL;
1087 struct symt_compiland* compiland = NULL;
1088 char currpath[PATH_MAX]; /* path to current file */
1089 char srcpath[PATH_MAX]; /* path to directory source file is in */
1094 unsigned int stabbufflen;
1095 const struct stab_nlist* stab_ptr;
1101 int source_idx = -1;
1102 struct pending_loc_var* pending_vars = NULL;
1103 unsigned num_pending_vars = 0;
1104 unsigned num_allocated_pending_vars = 0;
1107 nstab = stablen / sizeof(struct stab_nlist);
1108 stab_ptr = (const struct stab_nlist*)(addr + staboff);
1109 strs = (const char*)(addr + strtaboff);
1111 memset(srcpath, 0, sizeof(srcpath));
1112 memset(stabs_basic, 0, sizeof(stabs_basic));
1115 * Allocate a buffer into which we can build stab strings for cases
1116 * where the stab is continued over multiple lines.
1118 stabbufflen = 65536;
1119 stabbuff = HeapAlloc(GetProcessHeap(), 0, stabbufflen);
1123 for (i = 0; i < nstab; i++, stab_ptr++)
1125 ptr = strs + stab_ptr->n_un.n_strx;
1126 if (ptr[strlen(ptr) - 1] == '\\')
1129 * Indicates continuation. Append this to the buffer, and go onto the
1130 * next record. Repeat the process until we find a stab without the
1131 * '/' character, as this indicates we have the whole thing.
1133 unsigned len = strlen(ptr);
1134 if (strlen(stabbuff) + len > stabbufflen)
1136 stabbufflen += 65536;
1137 stabbuff = HeapReAlloc(GetProcessHeap(), 0, stabbuff, stabbufflen);
1139 strncat(stabbuff, ptr, len - 1);
1142 else if (stabbuff[0] != '\0')
1144 strcat(stabbuff, ptr);
1148 if (strchr(ptr, '=') != NULL)
1151 * The stabs aren't in writable memory, so copy it over so we are
1152 * sure we can scribble on it.
1154 if (ptr != stabbuff)
1156 strcpy(stabbuff, ptr);
1159 stab_strcpy(symname, sizeof(symname), ptr);
1160 if (!stabs_parse_typedef(module, ptr, symname))
1162 /* skip this definition */
1169 const char* defs[] = {"","","","", /* 00 */
1170 "","","","", /* 08 */
1171 "","","","", /* 10 */
1172 "","","","", /* 18 */
1173 "gsym","","fun","stsym", /* 20 */
1174 "lcsym","main","rosym","", /* 28 */
1175 "","","","", /* 30 */
1176 "","","opt","", /* 38 */
1177 "rsym","","sline","", /* 40 */
1178 "","","","", /* 48 */
1179 "","","","", /* 50 */
1180 "","","","", /* 58 */
1181 "","","so","", /* 60 */
1182 "","","","", /* 68 */
1183 "","","","", /* 70 */
1184 "","","","", /* 78 */
1185 "lsym","bincl","sol","", /* 80 */
1186 "","","","", /* 88 */
1187 "","","","", /* 90 */
1188 "","","","", /* 98 */
1189 "psym","eincl","","", /* a0 */
1190 "","","","", /* a8 */
1191 "","","","", /* b0 */
1192 "","","","", /* b8 */
1193 "lbrac","excl","","", /* c0 */
1194 "","","","", /* c8 */
1195 "","","","", /* d0 */
1196 "","","","", /* d8 */
1197 "rbrac","","","", /* e0 */
1200 FIXME("Got %s<%u> %u/%lu (%s)\n",
1201 defs[stab_ptr->n_type / 2], stab_ptr->n_type, stab_ptr->n_desc, stab_ptr->n_value, debugstr_a(ptr));
1204 switch (stab_ptr->n_type)
1208 * These are useless with ELF. They have no value, and you have to
1209 * read the normal symbol table to get the address. Thus we
1210 * ignore them, and when we process the normal symbol table
1211 * we should do the right thing.
1213 * With a.out or mingw, they actually do make some amount of sense.
1215 stab_strcpy(symname, sizeof(symname), ptr);
1216 symt_new_global_variable(module, compiland, symname, TRUE /* FIXME */,
1217 load_offset + stab_ptr->n_value, 0,
1218 stabs_parse_type(ptr));
1222 /* These are static symbols and BSS symbols. */
1223 stab_strcpy(symname, sizeof(symname), ptr);
1224 symt_new_global_variable(module, compiland, symname, TRUE /* FIXME */,
1225 load_offset + stab_ptr->n_value, 0,
1226 stabs_parse_type(ptr));
1229 block = symt_open_func_block(module, curr_func, block,
1230 stab_ptr->n_value, 0);
1231 for (j = 0; j < num_pending_vars; j++)
1233 symt_add_func_local(module, curr_func, pending_vars[j].regno,
1234 pending_vars[j].offset,
1235 block, pending_vars[j].type, pending_vars[j].name);
1237 num_pending_vars = 0;
1240 block = symt_close_func_block(module, curr_func, block,
1244 /* These are function parameters. */
1245 if (curr_func != NULL)
1247 struct symt* param_type = stabs_parse_type(ptr);
1248 stab_strcpy(symname, sizeof(symname), ptr);
1249 symt_add_func_local(module, curr_func, 0, stab_ptr->n_value,
1250 NULL, param_type, symname);
1251 symt_add_function_signature_parameter(module,
1252 (struct symt_function_signature*)curr_func->type,
1257 /* These are registers (as local variables) */
1258 if (curr_func != NULL)
1262 if (num_pending_vars == num_allocated_pending_vars)
1264 num_allocated_pending_vars += 8;
1266 pending_vars = HeapAlloc(GetProcessHeap(), 0,
1267 num_allocated_pending_vars * sizeof(pending_vars[0]));
1269 pending_vars = HeapReAlloc(GetProcessHeap(), 0, pending_vars,
1270 num_allocated_pending_vars * sizeof(pending_vars[0]));
1272 switch (stab_ptr->n_value)
1274 case 0: reg = CV_REG_EAX; break;
1275 case 1: reg = CV_REG_ECX; break;
1276 case 2: reg = CV_REG_EDX; break;
1277 case 3: reg = CV_REG_EBX; break;
1278 case 4: reg = CV_REG_ESP; break;
1279 case 5: reg = CV_REG_EBP; break;
1280 case 6: reg = CV_REG_ESI; break;
1281 case 7: reg = CV_REG_EDI; break;
1290 case 19: reg = CV_REG_ST0 + stab_ptr->n_value - 12; break;
1292 FIXME("Unknown register value (%lu)\n", stab_ptr->n_value);
1297 stab_strcpy(pending_vars[num_pending_vars].name,
1298 sizeof(pending_vars[num_pending_vars].name), ptr);
1299 pending_vars[num_pending_vars].type = stabs_parse_type(ptr);
1300 pending_vars[num_pending_vars].offset = 0;
1301 pending_vars[num_pending_vars].regno = reg;
1306 /* These are local variables */
1307 if (curr_func != NULL)
1309 if (num_pending_vars == num_allocated_pending_vars)
1311 num_allocated_pending_vars += 8;
1313 pending_vars = HeapAlloc(GetProcessHeap(), 0,
1314 num_allocated_pending_vars * sizeof(pending_vars[0]));
1316 pending_vars = HeapReAlloc(GetProcessHeap(), 0, pending_vars,
1317 num_allocated_pending_vars * sizeof(pending_vars[0]));
1319 stab_strcpy(pending_vars[num_pending_vars].name,
1320 sizeof(pending_vars[num_pending_vars].name), ptr);
1321 pending_vars[num_pending_vars].type = stabs_parse_type(ptr);
1322 pending_vars[num_pending_vars].offset = stab_ptr->n_value;
1323 pending_vars[num_pending_vars].regno = 0;
1329 * This is a line number. These are always relative to the start
1330 * of the function (N_FUN), and this makes the lookup easier.
1332 if (curr_func != NULL)
1334 assert(source_idx >= 0);
1335 symt_add_func_line(module, curr_func, source_idx,
1336 stab_ptr->n_desc, stab_ptr->n_value);
1340 /* First, clean up the previous function we were working on. */
1341 stabs_finalize_function(module, curr_func);
1344 * For now, just declare the various functions. Later
1345 * on, we will add the line number information and the
1349 * Copy the string to a temp buffer so we
1350 * can kill everything after the ':'. We do
1351 * it this way because otherwise we end up dirtying
1352 * all of the pages related to the stabs, and that
1353 * sucks up swap space like crazy.
1355 stab_strcpy(symname, sizeof(symname), ptr);
1358 struct symt_function_signature* func_type;
1359 func_type = symt_new_function_signature(module,
1360 stabs_parse_type(ptr));
1361 curr_func = symt_new_function(module, compiland, symname,
1362 load_offset + stab_ptr->n_value, 0,
1367 /* some GCC seem to use a N_FUN "" to mark the end of a function */
1373 * This indicates a new source file. Append the records
1374 * together, to build the correct path name.
1376 if (*ptr == '\0') /* end of N_SO file */
1378 /* Nuke old path. */
1380 stabs_finalize_function(module, curr_func);
1384 assert(block == NULL);
1389 int len = strlen(ptr);
1390 if (ptr[len-1] != '/')
1392 strcpy(currpath, srcpath);
1393 strcat(currpath, ptr);
1394 stabs_reset_includes();
1395 compiland = symt_new_compiland(module, currpath);
1396 source_idx = source_new(module, currpath);
1399 strcpy(srcpath, ptr);
1405 strcpy(currpath, srcpath);
1406 strcat(currpath, ptr);
1409 strcpy(currpath, ptr);
1410 source_idx = source_new(module, currpath);
1414 strtabinc = stab_ptr->n_value;
1415 stabs_finalize_function(module, curr_func);
1419 /* Ignore this. We don't care what it points to. */
1422 stabs_add_include(stabs_new_include(ptr, stab_ptr->n_value));
1423 assert(incl_stk < (int)(sizeof(incl) / sizeof(incl[0])) - 1);
1424 incl[++incl_stk] = source_idx;
1425 source_idx = source_new(module, ptr);
1428 assert(incl_stk >= 0);
1429 source_idx = incl[incl_stk--];
1432 if (stabs_add_include(stabs_find_include(ptr, stab_ptr->n_value)) < 0)
1434 ERR("Excluded header not found (%s,%ld)\n", ptr, stab_ptr->n_value);
1435 module_reset_debug_info(module);
1441 /* Always ignore these. GCC doesn't even generate them. */
1444 ERR("Unknown stab type 0x%02x\n", stab_ptr->n_type);
1448 TRACE("0x%02x %lx %s\n",
1449 stab_ptr->n_type, stab_ptr->n_value, debugstr_a(strs + stab_ptr->n_un.n_strx));
1451 module->module.SymType = SymDia;
1453 HeapFree(GetProcessHeap(), 0, stabbuff);
1454 stabs_free_includes();
1455 if (pending_vars) HeapFree(GetProcessHeap(), 0, pending_vars);