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 #if defined(__svr4__) || defined(__sun)
64 #include "wine/debug.h"
66 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp_stabs);
96 struct stab_nlist* n_next;
102 unsigned long n_value;
105 static void stab_strcpy(char* dest, int sz, const char* source)
108 * A strcpy routine that stops when we hit the ':' character.
109 * Faster than copying the whole thing, and then nuking the
112 while (*source != '\0' && *source != ':' && sz-- > 0)
122 struct symt** vector;
126 #define MAX_INCLUDES 5120
128 static include_def* include_defs = NULL;
129 static int num_include_def = 0;
130 static int num_alloc_include_def = 0;
131 static int cu_include_stack[MAX_INCLUDES];
132 static int cu_include_stk_idx = 0;
133 static struct symt** cu_vector = NULL;
134 static int cu_nrofentries = 0;
135 static struct symt_basic* stabs_basic[36];
137 static int stabs_new_include(const char* file, unsigned long val)
139 if (num_include_def == num_alloc_include_def)
141 num_alloc_include_def += 256;
143 include_defs = HeapAlloc(GetProcessHeap(), 0,
144 sizeof(include_defs[0]) * num_alloc_include_def);
146 include_defs = HeapReAlloc(GetProcessHeap(), 0, include_defs,
147 sizeof(include_defs[0]) * num_alloc_include_def);
148 memset(include_defs + num_include_def, 0, sizeof(include_defs[0]) * 256);
150 include_defs[num_include_def].name = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(file) + 1), file);
151 include_defs[num_include_def].value = val;
152 include_defs[num_include_def].vector = NULL;
153 include_defs[num_include_def].nrofentries = 0;
155 return num_include_def++;
158 static int stabs_find_include(const char* file, unsigned long val)
162 for (i = 0; i < num_include_def; i++)
164 if (val == include_defs[i].value &&
165 strcmp(file, include_defs[i].name) == 0)
171 static int stabs_add_include(int idx)
174 cu_include_stk_idx++;
176 /* if this happens, just bump MAX_INCLUDES */
177 /* we could also handle this as another dynarray */
178 assert(cu_include_stk_idx < MAX_INCLUDES);
179 cu_include_stack[cu_include_stk_idx] = idx;
180 return cu_include_stk_idx;
183 static void stabs_reset_includes(void)
186 * The struct symt:s that we would need to use are reset when
187 * we start a new file. (at least the ones in filenr == 0)
189 cu_include_stk_idx = 0;/* keep 0 as index for the .c file itself */
190 memset(cu_vector, 0, sizeof(cu_vector[0]) * cu_nrofentries);
193 static void stabs_free_includes(void)
197 stabs_reset_includes();
198 for (i = 0; i < num_include_def; i++)
200 HeapFree(GetProcessHeap(), 0, include_defs[i].name);
201 HeapFree(GetProcessHeap(), 0, include_defs[i].vector);
203 HeapFree(GetProcessHeap(), 0, include_defs);
206 num_alloc_include_def = 0;
207 HeapFree(GetProcessHeap(), 0, cu_vector);
212 static struct symt** stabs_find_ref(long filenr, long subnr)
216 /* FIXME: I could perhaps create a dummy include_def for each compilation
217 * unit which would allow not to handle those two cases separately
221 if (cu_nrofentries <= subnr)
224 cu_vector = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
225 sizeof(cu_vector[0]) * (subnr+1));
227 cu_vector = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
228 cu_vector, sizeof(cu_vector[0]) * (subnr+1));
229 cu_nrofentries = subnr + 1;
231 ret = &cu_vector[subnr];
237 assert(filenr <= cu_include_stk_idx);
238 idef = &include_defs[cu_include_stack[filenr]];
240 if (idef->nrofentries <= subnr)
243 idef->vector = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
244 sizeof(idef->vector[0]) * (subnr+1));
246 idef->vector = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
247 idef->vector, sizeof(idef->vector[0]) * (subnr+1));
248 idef->nrofentries = subnr + 1;
250 ret = &idef->vector[subnr];
252 TRACE("(%ld,%ld) => %p (%p)\n", filenr, subnr, ret, *ret);
256 static struct symt** stabs_read_type_enum(const char** x)
263 filenr = strtol(*x, (char**)x, 10); /* <int> */
265 subnr = strtol(*x, (char**)x, 10); /* <int> */
271 subnr = strtol(*x, (char**)x, 10); /* <int> */
273 return stabs_find_ref(filenr, subnr);
277 struct ParseTypedefData
282 struct module* module;
294 static void stabs_pts_push(struct ParseTypedefData* ptd, unsigned line)
296 assert(ptd->err_idx < sizeof(ptd->errors) / sizeof(ptd->errors[0]));
297 ptd->errors[ptd->err_idx].line = line;
298 ptd->errors[ptd->err_idx].ptr = ptd->ptr;
301 #define PTS_ABORTIF(ptd, t) do { if (t) { stabs_pts_push((ptd), __LINE__); return -1;} } while (0)
303 #define PTS_ABORTIF(ptd, t) do { if (t) return -1; } while (0)
306 static int stabs_get_basic(struct ParseTypedefData* ptd, unsigned basic, struct symt** symt)
308 PTS_ABORTIF(ptd, basic >= sizeof(stabs_basic) / sizeof(stabs_basic[0]));
310 if (!stabs_basic[basic])
314 case 1: stabs_basic[basic] = symt_new_basic(ptd->module, btInt, "int", 4); break;
315 case 2: stabs_basic[basic] = symt_new_basic(ptd->module, btChar, "char", 1); break;
316 case 3: stabs_basic[basic] = symt_new_basic(ptd->module, btInt, "short int", 2); break;
317 case 4: stabs_basic[basic] = symt_new_basic(ptd->module, btInt, "long int", 4); break;
318 case 5: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt, "unsigned char", 1); break;
319 case 6: stabs_basic[basic] = symt_new_basic(ptd->module, btInt, "signed char", 1); break;
320 case 7: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt, "unsigned short int", 2); break;
321 case 8: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt, "unsigned int", 4); break;
322 case 9: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt, "unsigned", 2); break;
323 case 10: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt, "unsigned long int", 2); break;
324 case 11: stabs_basic[basic] = symt_new_basic(ptd->module, btVoid, "void", 0); break;
325 case 12: stabs_basic[basic] = symt_new_basic(ptd->module, btFloat, "float", 4); break;
326 case 13: stabs_basic[basic] = symt_new_basic(ptd->module, btFloat, "double", 8); break;
327 case 14: stabs_basic[basic] = symt_new_basic(ptd->module, btFloat, "long double", 12); break;
328 case 15: stabs_basic[basic] = symt_new_basic(ptd->module, btInt, "integer", 4); break;
329 case 16: stabs_basic[basic] = symt_new_basic(ptd->module, btBool, "bool", 1); break;
330 /* case 17: short real */
332 case 25: stabs_basic[basic] = symt_new_basic(ptd->module, btComplex, "float complex", 8); break;
333 case 26: stabs_basic[basic] = symt_new_basic(ptd->module, btComplex, "double complex", 16); break;
334 case 30: stabs_basic[basic] = symt_new_basic(ptd->module, btWChar, "wchar_t", 2); break;
335 case 31: stabs_basic[basic] = symt_new_basic(ptd->module, btInt, "long long int", 8); break;
336 case 32: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt, "long long unsigned", 8); break;
337 /* starting at 35 are wine extensions (especially for R implementation) */
338 case 35: stabs_basic[basic] = symt_new_basic(ptd->module, btComplex, "long double complex", 24); break;
339 default: PTS_ABORTIF(ptd, 1);
342 *symt = &stabs_basic[basic]->symt;
346 static int stabs_pts_read_type_def(struct ParseTypedefData* ptd,
347 const char* typename, struct symt** dt);
349 static int stabs_pts_read_id(struct ParseTypedefData* ptd)
351 const char* first = ptd->ptr;
354 PTS_ABORTIF(ptd, (ptd->ptr = strchr(ptd->ptr, ':')) == NULL);
355 len = ptd->ptr - first;
356 PTS_ABORTIF(ptd, len >= sizeof(ptd->buf) - ptd->idx);
357 memcpy(ptd->buf + ptd->idx, first, len);
358 ptd->buf[ptd->idx + len] = '\0';
360 ptd->ptr++; /* ':' */
364 static int stabs_pts_read_number(struct ParseTypedefData* ptd, long* v)
368 *v = strtol(ptd->ptr, &last, 10);
369 PTS_ABORTIF(ptd, last == ptd->ptr);
374 static int stabs_pts_read_type_reference(struct ParseTypedefData* ptd,
375 long* filenr, long* subnr)
377 if (*ptd->ptr == '(')
379 /* '(' <int> ',' <int> ')' */
381 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, filenr) == -1);
382 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
383 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, subnr) == -1);
384 PTS_ABORTIF(ptd, *ptd->ptr++ != ')');
389 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, subnr) == -1);
394 struct pts_range_value
396 unsigned long long val;
400 static int stabs_pts_read_range_value(struct ParseTypedefData* ptd, struct pts_range_value* prv)
407 while (*ptd->ptr == '0') ptd->ptr++;
408 if (*ptd->ptr >= '1' && *ptd->ptr <= '7')
413 PTS_ABORTIF(ptd, ptd->ptr[0] != '1');
416 while (isdigit(*ptd->ptr)) prv->val = (prv->val << 3) + *ptd->ptr++ - '0';
421 while (isdigit(*ptd->ptr)) prv->val = (prv->val << 3) + *ptd->ptr++ - '0';
423 default: PTS_ABORTIF(ptd, 1); break;
425 } else prv->sign = 0;
429 prv->val = strtoull(++ptd->ptr, &last, 10);
435 prv->val = strtoull(ptd->ptr, &last, 10);
442 static int stabs_pts_read_range(struct ParseTypedefData* ptd, const char* typename,
446 struct pts_range_value lo;
447 struct pts_range_value hi;
451 unsigned long long v;
453 /* type ';' <int> ';' <int> ';' */
454 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &ref) == -1);
455 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
456 PTS_ABORTIF(ptd, stabs_pts_read_range_value(ptd, &lo) == -1);
457 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
458 PTS_ABORTIF(ptd, stabs_pts_read_range_value(ptd, &hi) == -1);
459 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
461 /* basically, we don't use ref... in some cases, for example, float is declared
462 * as a derivated type of int... which won't help us... so we guess the types
463 * from the various formats
465 if (lo.sign == 0 && hi.sign < 0)
470 else if (lo.sign < 0 && hi.sign == 0)
475 else if (lo.sign > 0 && hi.sign == 0)
480 else if (lo.sign < 0 && hi.sign > 0)
483 for (i = 7; i < 64; i += 8)
485 if (lo.val == v && hi.val == v - 1)
493 PTS_ABORTIF(ptd, i >= 64);
495 else if (lo.sign == 0 && hi.sign > 0)
497 if (hi.val == 127) /* specific case for char... */
505 for (i = 8; i <= 64; i += 8)
515 PTS_ABORTIF(ptd, i > 64);
518 else PTS_ABORTIF(ptd, 1);
520 *dt = &symt_new_basic(ptd->module, bt, typename, size)->symt;
524 static inline int stabs_pts_read_method_info(struct ParseTypedefData* ptd)
532 /* get type of return value */
533 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
534 if (*ptd->ptr == ';') ptd->ptr++;
536 /* get types of parameters */
537 if (*ptd->ptr == ':')
539 PTS_ABORTIF(ptd, !(tmp = strchr(ptd->ptr + 1, ';')));
542 PTS_ABORTIF(ptd, !(*ptd->ptr >= '0' && *ptd->ptr <= '9'));
544 PTS_ABORTIF(ptd, !(ptd->ptr[0] >= 'A' && *ptd->ptr <= 'D'));
546 PTS_ABORTIF(ptd, mthd != '.' && mthd != '?' && mthd != '*');
553 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &ofs) == -1);
554 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
555 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
556 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
558 } while (*ptd->ptr != ';');
564 static inline int stabs_pts_read_aggregate(struct ParseTypedefData* ptd,
565 struct symt_udt* sdt)
569 struct symt* dt = NULL;
573 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &sz) == -1);
575 doadd = symt_set_udt_size(ptd->module, sdt, sz);
576 if (*ptd->ptr == '!') /* C++ inheritence */
581 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &num_classes) == -1);
582 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
583 while (--num_classes >= 0)
585 ptd->ptr += 2; /* skip visibility and inheritence */
586 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &ofs) == -1);
587 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
589 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &adt) == -1);
597 symt_get_info(adt, TI_GET_SYMNAME, &name);
598 strcmp(tmp, "__inherited_class_");
599 WideCharToMultiByte(CP_ACP, 0, name, -1,
600 tmp + strlen(tmp), sizeof(tmp) - strlen(tmp),
602 HeapFree(GetProcessHeap(), 0, name);
603 /* FIXME: TI_GET_LENGTH will not always work, especially when adt
604 * has just been seen as a forward definition and not the real stuff
606 * As we don't use much the size of members in structs, this may not
607 * be much of a problem
609 symt_get_info(adt, TI_GET_LENGTH, &size);
610 symt_add_udt_element(ptd->module, sdt, tmp, adt, ofs, size * 8);
612 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
616 /* if the structure has already been filled, just redo the parsing
617 * but don't store results into the struct
618 * FIXME: there's a quite ugly memory leak in there...
621 /* Now parse the individual elements of the structure/union. */
622 while (*ptd->ptr != ';')
624 /* agg_name : type ',' <int:offset> ',' <int:size> */
627 if (ptd->ptr[0] == '$' && ptd->ptr[1] == 'v')
631 if (ptd->ptr[2] == 'f')
633 /* C++ virtual method table */
635 stabs_read_type_enum(&ptd->ptr);
636 PTS_ABORTIF(ptd, *ptd->ptr++ != ':');
637 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
638 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
639 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &x) == -1);
640 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
644 else if (ptd->ptr[2] == 'b')
647 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
648 PTS_ABORTIF(ptd, *ptd->ptr++ != ':');
649 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
650 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
651 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &x) == -1);
652 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
658 PTS_ABORTIF(ptd, stabs_pts_read_id(ptd) == -1);
659 /* Ref. TSDF R2.130 Section 7.4. When the field name is a method name
660 * it is followed by two colons rather than one.
662 if (*ptd->ptr == ':')
665 stabs_pts_read_method_info(ptd);
671 /* skip C++ member protection /0 /1 or /2 */
672 if (*ptd->ptr == '/') ptd->ptr += 2;
674 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &adt) == -1);
679 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &ofs) == -1);
680 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
681 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &sz) == -1);
682 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
684 if (doadd) symt_add_udt_element(ptd->module, sdt, ptd->buf + idx, adt, ofs, sz);
689 /* method parameters... terminated by ';' */
690 PTS_ABORTIF(ptd, !(tmp = strchr(ptd->ptr, ';')));
695 PTS_ABORTIF(ptd, TRUE);
699 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
700 if (*ptd->ptr == '~')
703 PTS_ABORTIF(ptd, *ptd->ptr++ != '%');
704 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
705 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
710 static inline int stabs_pts_read_enum(struct ParseTypedefData* ptd,
711 struct symt_enum* edt)
716 while (*ptd->ptr != ';')
719 PTS_ABORTIF(ptd, stabs_pts_read_id(ptd) == -1);
720 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &value) == -1);
721 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
722 symt_add_enum_element(ptd->module, edt, ptd->buf + idx, value);
729 static inline int stabs_pts_read_array(struct ParseTypedefData* ptd,
735 /* ar<typeinfo_nodef>;<int>;<int>;<typeinfo> */
737 PTS_ABORTIF(ptd, *ptd->ptr++ != 'r');
738 /* FIXME: range type is lost, always assume int */
739 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &rdt) == -1);
740 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
741 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &lo) == -1);
742 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
743 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &hi) == -1);
744 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
746 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &rdt) == -1);
748 *adt = &symt_new_array(ptd->module, lo, hi, rdt)->symt;
752 static int stabs_pts_read_type_def(struct ParseTypedefData* ptd, const char* typename,
753 struct symt** ret_dt)
757 struct symt* new_dt = NULL; /* newly created data type */
758 struct symt* ref_dt; /* referenced data type (pointer...) */
759 long filenr1, subnr1, tmp;
761 /* things are a bit complicated because of the way the typedefs are stored inside
762 * the file, because addresses can change when realloc is done, so we must call
763 * over and over stabs_find_ref() to keep the correct values around
765 PTS_ABORTIF(ptd, stabs_pts_read_type_reference(ptd, &filenr1, &subnr1) == -1);
767 while (*ptd->ptr == '=')
770 PTS_ABORTIF(ptd, new_dt != btNoType);
772 /* first handle attribute if any */
776 if (*++ptd->ptr == 's')
779 if (stabs_pts_read_number(ptd, &sz) == -1)
781 ERR("Not an attribute... NIY\n");
785 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
789 /* then the real definitions */
794 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &ref_dt) == -1);
795 new_dt = &symt_new_pointer(ptd->module, ref_dt)->symt;
797 case 'k': /* 'const' modifier */
798 case 'B': /* 'volatile' modifier */
799 /* just kinda ignore the modifier, I guess -gmt */
800 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, typename, &new_dt) == -1);
804 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, typename, &new_dt) == -1);
807 PTS_ABORTIF(ptd, stabs_pts_read_array(ptd, &new_dt) == -1);
810 PTS_ABORTIF(ptd, stabs_pts_read_range(ptd, typename, &new_dt) == -1);
811 assert(!*stabs_find_ref(filenr1, subnr1));
812 *stabs_find_ref(filenr1, subnr1) = new_dt;
815 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &ref_dt) == -1);
816 new_dt = &symt_new_function_signature(ptd->module, ref_dt)->symt;
819 new_dt = &symt_new_enum(ptd->module, typename)->symt;
820 PTS_ABORTIF(ptd, stabs_pts_read_enum(ptd, (struct symt_enum*)new_dt) == -1);
825 struct symt_udt* udt;
826 enum UdtKind kind = (ptd->ptr[-1] == 's') ? UdtStruct : UdtUnion;
827 /* udt can have been already defined in a forward definition */
828 udt = (struct symt_udt*)*stabs_find_ref(filenr1, subnr1);
831 udt = symt_new_udt(ptd->module, typename, 0, kind);
832 /* we need to set it here, because a struct can hold a pointer
835 new_dt = *stabs_find_ref(filenr1, subnr1) = &udt->symt;
839 if (udt->symt.tag != SymTagUDT)
841 ERR("Forward declaration (%p/%s) is not an aggregate (%u)\n",
842 udt, symt_get_name(&udt->symt), udt->symt.tag);
845 /* should check typename is the same too */
848 PTS_ABORTIF(ptd, stabs_pts_read_aggregate(ptd, udt) == -1);
854 PTS_ABORTIF(ptd, stabs_pts_read_id(ptd) == -1);
858 new_dt = &symt_new_enum(ptd->module, ptd->buf + idx)->symt;
861 new_dt = &symt_new_udt(ptd->module, ptd->buf + idx, 0, UdtStruct)->symt;
864 new_dt = &symt_new_udt(ptd->module, ptd->buf + idx, 0, UdtUnion)->symt;
873 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &tmp) == -1);
874 PTS_ABORTIF(ptd, stabs_get_basic(ptd, tmp, &new_dt) == -1);
875 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
879 if (*ptd->ptr == '#')
882 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &ref_dt) == -1);
883 new_dt = &symt_new_function_signature(ptd->module, ref_dt)->symt;
890 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &cls_dt) == -1);
891 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
892 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &ref_dt) == -1);
893 new_dt = &symt_new_function_signature(ptd->module, ref_dt)->symt;
894 while (*ptd->ptr == ',')
897 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &pmt_dt) == -1);
906 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &type) == -1);
907 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
908 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &len) == -1);
909 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
910 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &unk) == -1);
911 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
913 switch (type) /* see stabs_get_basic for the details */
915 case 1: basic = 12; break;
916 case 2: basic = 13; break;
917 case 3: basic = 25; break;
918 case 4: basic = 26; break;
919 case 5: basic = 35; break;
920 case 6: basic = 14; break;
921 default: PTS_ABORTIF(ptd, 1);
923 PTS_ABORTIF(ptd, stabs_get_basic(ptd, basic, &new_dt) == -1);
927 ERR("Unknown type '%c'\n", ptd->ptr[-1]);
934 /* is it a forward declaration that has been filled ? */
935 new_dt = *stabs_find_ref(filenr1, subnr1);
936 /* if not, this should be void (which is defined as a ref to itself, but we
937 * don't correctly catch it)
939 if (!new_dt && typename)
941 new_dt = &symt_new_basic(ptd->module, btVoid, typename, 0)->symt;
942 PTS_ABORTIF(ptd, strcmp(typename, "void"));
946 *stabs_find_ref(filenr1, subnr1) = *ret_dt = new_dt;
948 TRACE("Adding (%ld,%ld) %s\n", filenr1, subnr1, typename);
953 static int stabs_parse_typedef(struct module* module, const char* ptr,
954 const char* typename)
956 struct ParseTypedefData ptd;
960 /* check for already existing definition */
968 for (ptd.ptr = ptr - 1; ;)
970 ptd.ptr = strchr(ptd.ptr + 1, ':');
971 if (ptd.ptr == NULL || *++ptd.ptr != ':') break;
975 if (*ptd.ptr != '(') ptd.ptr++;
976 /* most of type definitions take one char, except Tt */
977 if (*ptd.ptr != '(') ptd.ptr++;
978 ret = stabs_pts_read_type_def(&ptd, typename, &dt);
981 if (ret == -1 || *ptd.ptr)
985 TRACE("Failure on %s\n", ptr);
988 for (i = 0; i < ptd.err_idx; i++)
990 TRACE("[%d]: line %d => %s\n",
991 i, ptd.errors[i].line, ptd.errors[i].ptr);
995 TRACE("[0]: => %s\n", ptd.ptr);
998 ERR("Failure on %s at %s\n", ptr, ptd.ptr);
1006 static struct symt* stabs_parse_type(const char* stab)
1008 const char* c = stab - 1;
1011 * Look through the stab definition, and figure out what struct symt
1012 * this represents. If we have something we know about, assign the
1014 * According to "The \"stabs\" debug format" (Rev 2.130) the name may be
1015 * a C++ name and contain double colons e.g. foo::bar::baz:t5=*6.
1019 if ((c = strchr(c + 1, ':')) == NULL) return NULL;
1020 } while (*++c == ':');
1023 * The next characters say more about the type (i.e. data, function, etc)
1024 * of symbol. Skip them. (C++ for example may have Tt).
1025 * Actually this is a very weak description; I think Tt is the only
1026 * multiple combination we should see.
1028 while (*c && *c != '(' && !isdigit(*c))
1031 * The next is either an integer or a (integer,integer).
1032 * The stabs_read_type_enum() takes care that stab_types is large enough.
1034 return *stabs_read_type_enum(&c);
1037 struct pending_loc_var
1046 struct symt_public* lookup_public(const struct module* module,
1047 const struct symt_compiland* compiland,
1051 struct symt_public* found = NULL;
1052 struct symt_public* xfound = NULL;
1053 struct symt_public* sym;
1057 struct hash_table_iter hti;
1059 const char* out_src;
1061 if (compiland && compiland->symt.tag == SymTagCompiland)
1062 in_src = source_get(module, compiland->source);
1065 hash_table_iter_init(&module->ht_symbols, &hti, name);
1066 while ((ptr = hash_table_iter_up(&hti)))
1068 sym = GET_ENTRY(ptr, struct symt_public, hash_elt);
1069 if (sym->symt.tag == SymTagPublicSymbol)
1071 xname = symt_get_name(&sym->symt);
1072 if (!xname || strcmp(xname, name)) continue;
1074 if (sym->container &&
1075 sym->container->tag == SymTagCompiland)
1076 out_src = source_get(module, ((struct symt_compiland*)sym->container)->source);
1077 else out_src = NULL;
1080 if ((in_src && !out_src) || (!in_src && out_src)) continue;
1084 if (strcmp(in_src, out_src) || (tmp = strrchr(in_src, '/')) == NULL ||
1085 strcmp(tmp + 1, out_src))
1089 /* we continue once found to insure uniqueness of public symbol's name */
1092 FIXME("More than one public symbol (%s) in %s: [%u] %p {%lx,%lx} in %s\n",
1093 name, in_src, nfind, sym, sym->address, sym->size, out_src);
1100 if (xfound) found = xfound;
1101 else FIXME("Couldn't locate %s in public symbols\n", name);
1105 if (found->container &&
1106 found->container->tag == SymTagCompiland)
1107 out_src = source_get(module, ((struct symt_compiland*)found->container)->source);
1108 else out_src = NULL;
1109 TRACE("Found for %s in %s: %p {%lx,%lx} in %s\n",
1110 name, in_src, found, found->address, found->size, out_src);
1115 SYM_TYPE stabs_parse(struct module* module, const char* addr,
1116 unsigned long load_offset, unsigned int staboff, int stablen,
1117 unsigned int strtaboff, int strtablen)
1119 struct symt_function* curr_func = NULL;
1120 struct symt_block* block = NULL;
1121 struct symt_public* public;
1122 struct symt_compiland* compiland = NULL;
1123 char currpath[PATH_MAX];
1128 unsigned int stabbufflen;
1129 const struct stab_nlist* stab_ptr;
1135 int source_idx = -1;
1136 struct pending_loc_var* pending_vars = NULL;
1137 unsigned num_pending_vars = 0;
1138 unsigned num_allocated_pending_vars = 0;
1140 nstab = stablen / sizeof(struct stab_nlist);
1141 stab_ptr = (struct stab_nlist*)(addr + staboff);
1142 strs = (char*)(addr + strtaboff);
1144 memset(currpath, 0, sizeof(currpath));
1145 memset(stabs_basic, 0, sizeof(stabs_basic));
1148 * Allocate a buffer into which we can build stab strings for cases
1149 * where the stab is continued over multiple lines.
1151 stabbufflen = 65536;
1152 stabbuff = HeapAlloc(GetProcessHeap(), 0, stabbufflen);
1156 for (i = 0; i < nstab; i++, stab_ptr++)
1158 ptr = strs + stab_ptr->n_un.n_strx;
1159 if (ptr[strlen(ptr) - 1] == '\\')
1162 * Indicates continuation. Append this to the buffer, and go onto the
1163 * next record. Repeat the process until we find a stab without the
1164 * '/' character, as this indicates we have the whole thing.
1166 unsigned len = strlen(ptr);
1167 if (strlen(stabbuff) + len > stabbufflen)
1169 stabbufflen += 65536;
1170 stabbuff = HeapReAlloc(GetProcessHeap(), 0, stabbuff, stabbufflen);
1172 strncat(stabbuff, ptr, len - 1);
1175 else if (stabbuff[0] != '\0')
1177 strcat(stabbuff, ptr);
1181 if (strchr(ptr, '=') != NULL)
1184 * The stabs aren't in writable memory, so copy it over so we are
1185 * sure we can scribble on it.
1187 if (ptr != stabbuff)
1189 strcpy(stabbuff, ptr);
1192 stab_strcpy(symname, sizeof(symname), ptr);
1193 if (!stabs_parse_typedef(module, ptr, symname))
1195 /* skip this definition */
1202 const char* defs[] = {"","","","", /* 00 */
1203 "","","","", /* 08 */
1204 "","","","", /* 10 */
1205 "","","","", /* 18 */
1206 "gsym","","fun","stsym", /* 20 */
1207 "lcsym","main","rosym","", /* 28 */
1208 "","","","", /* 30 */
1209 "","","opt","", /* 38 */
1210 "rsym","","sline","", /* 40 */
1211 "","","","", /* 48 */
1212 "","","","", /* 50 */
1213 "","","","", /* 58 */
1214 "","","so","", /* 60 */
1215 "","","","", /* 68 */
1216 "","","","", /* 70 */
1217 "","","","", /* 78 */
1218 "lsym","bincl","sol","", /* 80 */
1219 "","","","", /* 88 */
1220 "","","","", /* 90 */
1221 "","","","", /* 98 */
1222 "psym","eincl","","", /* a0 */
1223 "","","","", /* a8 */
1224 "","","","", /* b0 */
1225 "","","","", /* b8 */
1226 "lbrac","excl","","", /* c0 */
1227 "","","","", /* c8 */
1228 "","","","", /* d0 */
1229 "","","","", /* d8 */
1230 "rbrac","","","", /* e0 */
1233 FIXME("Got %s<%u> %u/%lu (%s)\n",
1234 defs[stab_ptr->n_type / 2], stab_ptr->n_type, stab_ptr->n_desc, stab_ptr->n_value, debugstr_a(ptr));
1237 switch (stab_ptr->n_type)
1241 * These are useless with ELF. They have no value, and you have to
1242 * read the normal symbol table to get the address. Thus we
1243 * ignore them, and when we process the normal symbol table
1244 * we should do the right thing.
1246 * With a.out or mingw, they actually do make some amount of sense.
1248 stab_strcpy(symname, sizeof(symname), ptr);
1250 if ((public = lookup_public(module, compiland, symname)))
1251 symt_new_global_variable(module, compiland, symname, TRUE /* FIXME */,
1252 public->address, public->size,
1253 stabs_parse_type(ptr));
1255 symt_new_global_variable(module, symname, TRUE /* FIXME */,
1256 load_offset + stab_ptr->n_value, 0,
1257 stabs_parse_type(ptr));
1262 /* These are static symbols and BSS symbols. */
1263 stab_strcpy(symname, sizeof(symname), ptr);
1264 symt_new_global_variable(module, compiland, symname, TRUE /* FIXME */,
1265 load_offset + stab_ptr->n_value, 0,
1266 stabs_parse_type(ptr));
1269 block = symt_open_func_block(module, curr_func, block,
1271 for (j = 0; j < num_pending_vars; j++)
1273 symt_add_func_local(module, curr_func, pending_vars[j].regno,
1274 pending_vars[j].offset,
1275 block, pending_vars[j].type, pending_vars[j].name);
1277 num_pending_vars = 0;
1280 block = symt_close_func_block(module, curr_func, block,
1284 /* These are function parameters. */
1285 if (curr_func != NULL)
1287 stab_strcpy(symname, sizeof(symname), ptr);
1288 symt_add_func_local(module, curr_func, 0, stab_ptr->n_value,
1289 NULL, stabs_parse_type(ptr), symname);
1293 /* These are registers (as local variables) */
1294 if (curr_func != NULL)
1298 if (num_pending_vars == num_allocated_pending_vars)
1300 num_allocated_pending_vars += 8;
1302 pending_vars = HeapAlloc(GetProcessHeap(), 0,
1303 num_allocated_pending_vars * sizeof(pending_vars[0]));
1305 pending_vars = HeapReAlloc(GetProcessHeap(), 0, pending_vars,
1306 num_allocated_pending_vars * sizeof(pending_vars[0]));
1308 switch (stab_ptr->n_value)
1310 case 0: reg = CV_REG_EAX; break;
1311 case 1: reg = CV_REG_ECX; break;
1312 case 2: reg = CV_REG_EDX; break;
1313 case 3: reg = CV_REG_EBX; break;
1314 case 4: reg = CV_REG_ESP; break;
1315 case 5: reg = CV_REG_EBP; break;
1316 case 6: reg = CV_REG_ESI; break;
1317 case 7: reg = CV_REG_EDI; break;
1326 case 19: reg = CV_REG_ST0 + stab_ptr->n_value - 12; break;
1328 FIXME("Unknown register value (%lu)\n", stab_ptr->n_value);
1333 stab_strcpy(pending_vars[num_pending_vars].name,
1334 sizeof(pending_vars[num_pending_vars].name), ptr);
1335 pending_vars[num_pending_vars].type = stabs_parse_type(ptr);
1336 pending_vars[num_pending_vars].offset = 0;
1337 pending_vars[num_pending_vars].regno = reg;
1342 /* These are local variables */
1343 if (curr_func != NULL)
1345 if (num_pending_vars == num_allocated_pending_vars)
1347 num_allocated_pending_vars += 8;
1349 pending_vars = HeapAlloc(GetProcessHeap(), 0,
1350 num_allocated_pending_vars * sizeof(pending_vars[0]));
1352 pending_vars = HeapReAlloc(GetProcessHeap(), 0, pending_vars,
1353 num_allocated_pending_vars * sizeof(pending_vars[0]));
1355 stab_strcpy(pending_vars[num_pending_vars].name,
1356 sizeof(pending_vars[num_pending_vars].name), ptr);
1357 pending_vars[num_pending_vars].type = stabs_parse_type(ptr);
1358 pending_vars[num_pending_vars].offset = stab_ptr->n_value;
1359 pending_vars[num_pending_vars].regno = 0;
1365 * This is a line number. These are always relative to the start
1366 * of the function (N_FUN), and this makes the lookup easier.
1368 if (curr_func != NULL)
1370 assert(source_idx >= 0);
1372 symt_add_func_line(module, curr_func, source_idx,
1373 stab_ptr->n_desc, stab_ptr->n_value);
1376 * This isn't right. The order of the stabs is different under
1377 * a.out, and as a result we would end up attaching the line
1378 * number to the wrong function.
1380 symt_add_func_line(module, curr_func, source_idx,
1382 stab_ptr->n_value - curr_func->addr.off);
1387 /* First, clean up the previous function we were working on. */
1388 symt_normalize_function(module, curr_func);
1391 * For now, just declare the various functions. Later
1392 * on, we will add the line number information and the
1396 * Copy the string to a temp buffer so we
1397 * can kill everything after the ':'. We do
1398 * it this way because otherwise we end up dirtying
1399 * all of the pages related to the stabs, and that
1400 * sucks up swap space like crazy.
1402 stab_strcpy(symname, sizeof(symname), ptr);
1405 struct symt_function_signature* func_type;
1406 func_type = symt_new_function_signature(module,
1407 stabs_parse_type(ptr));
1409 if ((public = lookup_public(module, compiland, symname)))
1410 curr_func = symt_new_function(module, compiland, symname,
1411 public->address, public->size,
1412 stabs_parse_type(ptr));
1414 curr_func = symt_new_function(module, compiland, symname,
1415 load_offset + stab_ptr->n_value, 0,
1421 /* some GCC seem to use a N_FUN "" to mark the end of a function */
1427 * This indicates a new source file. Append the records
1428 * together, to build the correct path name.
1430 if (*ptr == '\0') /* end of N_SO file */
1432 /* Nuke old path. */
1434 symt_normalize_function(module, curr_func);
1438 assert(block == NULL);
1444 strcat(currpath, ptr);
1446 strcpy(currpath, ptr);
1447 stabs_reset_includes();
1448 compiland = symt_new_compiland(module, currpath);
1449 source_idx = source_new(module, currpath);
1453 strcpy(currpath, ptr);
1454 source_idx = source_new(module, currpath);
1458 strtabinc = stab_ptr->n_value;
1459 symt_normalize_function(module, curr_func);
1463 /* Ignore this. We don't care what it points to. */
1466 stabs_add_include(stabs_new_include(ptr, stab_ptr->n_value));
1467 assert(incl_stk < (int)(sizeof(incl) / sizeof(incl[0])) - 1);
1468 source_idx = incl[++incl_stk] = source_new(module, ptr);
1471 assert(incl_stk > 0);
1472 source_idx = incl[--incl_stk];
1475 stabs_add_include(stabs_find_include(ptr, stab_ptr->n_value));
1478 /* Always ignore these. GCC doesn't even generate them. */
1481 ERR("Unknown stab type 0x%02x\n", stab_ptr->n_type);
1485 TRACE("0x%02x %lx %s\n",
1486 stab_ptr->n_type, stab_ptr->n_value, strs + stab_ptr->n_un.n_strx);
1489 HeapFree(GetProcessHeap(), 0, stabbuff);
1490 stabs_free_includes();
1491 if (pending_vars) HeapFree(GetProcessHeap(), 0, pending_vars);