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
107 * Takes also care of (valid) a::b constructs
109 while (*source != '\0')
111 if (source[0] != ':' && sz-- > 0) *dest++ = *source++;
112 else if (source[1] == ':' && (sz -= 2) > 0)
120 /* GCC seems to emit, in some cases, a .<digit>+ suffix.
121 * This is used for static variable inside functions, so
122 * that we can have several such variables with same name in
123 * the same compilation unit
124 * We simply ignore that suffix when present (we also get rid
125 * of it in ELF symtab parsing)
129 while (isdigit(*dest)) dest--;
130 if (*dest == '.') *dest = '\0';
140 struct symt** vector;
144 #define MAX_INCLUDES 5120
146 static include_def* include_defs = NULL;
147 static int num_include_def = 0;
148 static int num_alloc_include_def = 0;
149 static int cu_include_stack[MAX_INCLUDES];
150 static int cu_include_stk_idx = 0;
151 static struct symt** cu_vector = NULL;
152 static int cu_nrofentries = 0;
153 static struct symt_basic* stabs_basic[36];
155 static int stabs_new_include(const char* file, unsigned long val)
157 if (num_include_def == num_alloc_include_def)
159 num_alloc_include_def += 256;
161 include_defs = HeapAlloc(GetProcessHeap(), 0,
162 sizeof(include_defs[0]) * num_alloc_include_def);
164 include_defs = HeapReAlloc(GetProcessHeap(), 0, include_defs,
165 sizeof(include_defs[0]) * num_alloc_include_def);
166 memset(include_defs + num_include_def, 0, sizeof(include_defs[0]) * 256);
168 include_defs[num_include_def].name = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(file) + 1), file);
169 include_defs[num_include_def].value = val;
170 include_defs[num_include_def].vector = NULL;
171 include_defs[num_include_def].nrofentries = 0;
173 return num_include_def++;
176 static int stabs_find_include(const char* file, unsigned long val)
180 for (i = 0; i < num_include_def; i++)
182 if (val == include_defs[i].value &&
183 strcmp(file, include_defs[i].name) == 0)
189 static int stabs_add_include(int idx)
191 if (idx < 0) return -1;
192 cu_include_stk_idx++;
194 /* if this happens, just bump MAX_INCLUDES */
195 /* we could also handle this as another dynarray */
196 assert(cu_include_stk_idx < MAX_INCLUDES);
197 cu_include_stack[cu_include_stk_idx] = idx;
198 return cu_include_stk_idx;
201 static void stabs_reset_includes(void)
204 * The struct symt:s that we would need to use are reset when
205 * we start a new file. (at least the ones in filenr == 0)
207 cu_include_stk_idx = 0;/* keep 0 as index for the .c file itself */
208 memset(cu_vector, 0, sizeof(cu_vector[0]) * cu_nrofentries);
211 static void stabs_free_includes(void)
215 stabs_reset_includes();
216 for (i = 0; i < num_include_def; i++)
218 HeapFree(GetProcessHeap(), 0, include_defs[i].name);
219 HeapFree(GetProcessHeap(), 0, include_defs[i].vector);
221 HeapFree(GetProcessHeap(), 0, include_defs);
224 num_alloc_include_def = 0;
225 HeapFree(GetProcessHeap(), 0, cu_vector);
230 static struct symt** stabs_find_ref(long filenr, long subnr)
234 /* FIXME: I could perhaps create a dummy include_def for each compilation
235 * unit which would allow not to handle those two cases separately
239 if (cu_nrofentries <= subnr)
242 cu_vector = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
243 sizeof(cu_vector[0]) * (subnr+1));
245 cu_vector = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
246 cu_vector, sizeof(cu_vector[0]) * (subnr+1));
247 cu_nrofentries = subnr + 1;
249 ret = &cu_vector[subnr];
255 assert(filenr <= cu_include_stk_idx);
256 idef = &include_defs[cu_include_stack[filenr]];
258 if (idef->nrofentries <= subnr)
261 idef->vector = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
262 sizeof(idef->vector[0]) * (subnr+1));
264 idef->vector = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
265 idef->vector, sizeof(idef->vector[0]) * (subnr+1));
266 idef->nrofentries = subnr + 1;
268 ret = &idef->vector[subnr];
270 TRACE("(%ld,%ld) => %p (%p)\n", filenr, subnr, ret, *ret);
274 static struct symt** stabs_read_type_enum(const char** x)
281 filenr = strtol(*x, (char**)x, 10); /* <int> */
283 subnr = strtol(*x, (char**)x, 10); /* <int> */
289 subnr = strtol(*x, (char**)x, 10); /* <int> */
291 return stabs_find_ref(filenr, subnr);
295 struct ParseTypedefData
300 struct module* module;
312 static void stabs_pts_push(struct ParseTypedefData* ptd, unsigned line)
314 assert(ptd->err_idx < sizeof(ptd->errors) / sizeof(ptd->errors[0]));
315 ptd->errors[ptd->err_idx].line = line;
316 ptd->errors[ptd->err_idx].ptr = ptd->ptr;
319 #define PTS_ABORTIF(ptd, t) do { if (t) { stabs_pts_push((ptd), __LINE__); return -1;} } while (0)
321 #define PTS_ABORTIF(ptd, t) do { if (t) return -1; } while (0)
324 static int stabs_get_basic(struct ParseTypedefData* ptd, unsigned basic, struct symt** symt)
326 PTS_ABORTIF(ptd, basic >= sizeof(stabs_basic) / sizeof(stabs_basic[0]));
328 if (!stabs_basic[basic])
332 case 1: stabs_basic[basic] = symt_new_basic(ptd->module, btInt, "int", 4); break;
333 case 2: stabs_basic[basic] = symt_new_basic(ptd->module, btChar, "char", 1); break;
334 case 3: stabs_basic[basic] = symt_new_basic(ptd->module, btInt, "short int", 2); break;
335 case 4: stabs_basic[basic] = symt_new_basic(ptd->module, btInt, "long int", 4); break;
336 case 5: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt, "unsigned char", 1); break;
337 case 6: stabs_basic[basic] = symt_new_basic(ptd->module, btInt, "signed char", 1); break;
338 case 7: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt, "unsigned short int", 2); break;
339 case 8: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt, "unsigned int", 4); break;
340 case 9: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt, "unsigned", 2); break;
341 case 10: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt, "unsigned long int", 2); break;
342 case 11: stabs_basic[basic] = symt_new_basic(ptd->module, btVoid, "void", 0); break;
343 case 12: stabs_basic[basic] = symt_new_basic(ptd->module, btFloat, "float", 4); break;
344 case 13: stabs_basic[basic] = symt_new_basic(ptd->module, btFloat, "double", 8); break;
345 case 14: stabs_basic[basic] = symt_new_basic(ptd->module, btFloat, "long double", 12); break;
346 case 15: stabs_basic[basic] = symt_new_basic(ptd->module, btInt, "integer", 4); break;
347 case 16: stabs_basic[basic] = symt_new_basic(ptd->module, btBool, "bool", 1); break;
348 /* case 17: short real */
350 case 25: stabs_basic[basic] = symt_new_basic(ptd->module, btComplex, "float complex", 8); break;
351 case 26: stabs_basic[basic] = symt_new_basic(ptd->module, btComplex, "double complex", 16); break;
352 case 30: stabs_basic[basic] = symt_new_basic(ptd->module, btWChar, "wchar_t", 2); break;
353 case 31: stabs_basic[basic] = symt_new_basic(ptd->module, btInt, "long long int", 8); break;
354 case 32: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt, "long long unsigned", 8); break;
355 /* starting at 35 are wine extensions (especially for R implementation) */
356 case 35: stabs_basic[basic] = symt_new_basic(ptd->module, btComplex, "long double complex", 24); break;
357 default: PTS_ABORTIF(ptd, 1);
360 *symt = &stabs_basic[basic]->symt;
364 static int stabs_pts_read_type_def(struct ParseTypedefData* ptd,
365 const char* typename, struct symt** dt);
367 static int stabs_pts_read_id(struct ParseTypedefData* ptd)
369 const char* first = ptd->ptr;
370 unsigned int template = 0;
373 while ((ch = *ptd->ptr++) != '\0')
380 unsigned int len = ptd->ptr - first - 1;
381 PTS_ABORTIF(ptd, len >= sizeof(ptd->buf) - ptd->idx);
382 memcpy(ptd->buf + ptd->idx, first, len);
383 ptd->buf[ptd->idx + len] = '\0';
388 case '<': template++; break;
389 case '>': PTS_ABORTIF(ptd, template == 0); template--; break;
395 static int stabs_pts_read_number(struct ParseTypedefData* ptd, long* v)
399 *v = strtol(ptd->ptr, &last, 10);
400 PTS_ABORTIF(ptd, last == ptd->ptr);
405 static int stabs_pts_read_type_reference(struct ParseTypedefData* ptd,
406 long* filenr, long* subnr)
408 if (*ptd->ptr == '(')
410 /* '(' <int> ',' <int> ')' */
412 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, filenr) == -1);
413 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
414 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, subnr) == -1);
415 PTS_ABORTIF(ptd, *ptd->ptr++ != ')');
420 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, subnr) == -1);
425 struct pts_range_value
427 unsigned long long val;
431 static int stabs_pts_read_range_value(struct ParseTypedefData* ptd, struct pts_range_value* prv)
438 while (*ptd->ptr == '0') ptd->ptr++;
439 if (*ptd->ptr >= '1' && *ptd->ptr <= '7')
444 PTS_ABORTIF(ptd, ptd->ptr[0] != '1');
447 while (isdigit(*ptd->ptr)) prv->val = (prv->val << 3) + *ptd->ptr++ - '0';
452 while (isdigit(*ptd->ptr)) prv->val = (prv->val << 3) + *ptd->ptr++ - '0';
454 default: PTS_ABORTIF(ptd, 1); break;
456 } else prv->sign = 0;
460 prv->val = strtoull(++ptd->ptr, &last, 10);
466 prv->val = strtoull(ptd->ptr, &last, 10);
473 static int stabs_pts_read_range(struct ParseTypedefData* ptd, const char* typename,
477 struct pts_range_value lo;
478 struct pts_range_value hi;
482 unsigned long long v;
484 /* type ';' <int> ';' <int> ';' */
485 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &ref) == -1);
486 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
487 PTS_ABORTIF(ptd, stabs_pts_read_range_value(ptd, &lo) == -1);
488 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
489 PTS_ABORTIF(ptd, stabs_pts_read_range_value(ptd, &hi) == -1);
490 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
492 /* basically, we don't use ref... in some cases, for example, float is declared
493 * as a derivated type of int... which won't help us... so we guess the types
494 * from the various formats
496 if (lo.sign == 0 && hi.sign < 0)
501 else if (lo.sign < 0 && hi.sign == 0)
506 else if (lo.sign > 0 && hi.sign == 0)
511 else if (lo.sign < 0 && hi.sign > 0)
514 for (i = 7; i < 64; i += 8)
516 if (lo.val == v && hi.val == v - 1)
524 PTS_ABORTIF(ptd, i >= 64);
526 else if (lo.sign == 0 && hi.sign > 0)
528 if (hi.val == 127) /* specific case for char... */
536 for (i = 8; i <= 64; i += 8)
546 PTS_ABORTIF(ptd, i > 64);
549 else PTS_ABORTIF(ptd, 1);
551 *dt = &symt_new_basic(ptd->module, bt, typename, size)->symt;
555 static inline int stabs_pts_read_method_info(struct ParseTypedefData* ptd)
563 /* get type of return value */
564 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
565 if (*ptd->ptr == ';') ptd->ptr++;
567 /* get types of parameters */
568 if (*ptd->ptr == ':')
570 PTS_ABORTIF(ptd, !(tmp = strchr(ptd->ptr + 1, ';')));
573 PTS_ABORTIF(ptd, !(*ptd->ptr >= '0' && *ptd->ptr <= '9'));
575 PTS_ABORTIF(ptd, !(ptd->ptr[0] >= 'A' && *ptd->ptr <= 'D'));
577 PTS_ABORTIF(ptd, mthd != '.' && mthd != '?' && mthd != '*');
584 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &ofs) == -1);
585 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
586 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
587 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
589 } while (*ptd->ptr != ';');
595 static inline int stabs_pts_read_aggregate(struct ParseTypedefData* ptd,
596 struct symt_udt* sdt)
600 struct symt* dt = NULL;
604 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &sz) == -1);
606 doadd = symt_set_udt_size(ptd->module, sdt, sz);
607 if (*ptd->ptr == '!') /* C++ inheritence */
612 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &num_classes) == -1);
613 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
614 while (--num_classes >= 0)
616 ptd->ptr += 2; /* skip visibility and inheritence */
617 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &ofs) == -1);
618 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
620 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &adt) == -1);
628 symt_get_info(adt, TI_GET_SYMNAME, &name);
629 strcpy(tmp, "__inherited_class_");
630 WideCharToMultiByte(CP_ACP, 0, name, -1,
631 tmp + strlen(tmp), sizeof(tmp) - strlen(tmp),
633 HeapFree(GetProcessHeap(), 0, name);
634 /* FIXME: TI_GET_LENGTH will not always work, especially when adt
635 * has just been seen as a forward definition and not the real stuff
637 * As we don't use much the size of members in structs, this may not
638 * be much of a problem
640 symt_get_info(adt, TI_GET_LENGTH, &size);
641 symt_add_udt_element(ptd->module, sdt, tmp, adt, ofs, size * 8);
643 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
647 /* if the structure has already been filled, just redo the parsing
648 * but don't store results into the struct
649 * FIXME: there's a quite ugly memory leak in there...
652 /* Now parse the individual elements of the structure/union. */
653 while (*ptd->ptr != ';')
655 /* agg_name : type ',' <int:offset> ',' <int:size> */
658 if (ptd->ptr[0] == '$' && ptd->ptr[1] == 'v')
662 if (ptd->ptr[2] == 'f')
664 /* C++ virtual method table */
666 stabs_read_type_enum(&ptd->ptr);
667 PTS_ABORTIF(ptd, *ptd->ptr++ != ':');
668 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
669 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
670 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &x) == -1);
671 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
675 else if (ptd->ptr[2] == 'b')
678 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
679 PTS_ABORTIF(ptd, *ptd->ptr++ != ':');
680 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
681 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
682 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &x) == -1);
683 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
689 PTS_ABORTIF(ptd, stabs_pts_read_id(ptd) == -1);
690 /* Ref. TSDF R2.130 Section 7.4. When the field name is a method name
691 * it is followed by two colons rather than one.
693 if (*ptd->ptr == ':')
696 stabs_pts_read_method_info(ptd);
702 /* skip C++ member protection /0 /1 or /2 */
703 if (*ptd->ptr == '/') ptd->ptr += 2;
705 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &adt) == -1);
710 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &ofs) == -1);
711 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
712 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &sz) == -1);
713 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
715 if (doadd) symt_add_udt_element(ptd->module, sdt, ptd->buf + idx, adt, ofs, sz);
720 /* method parameters... terminated by ';' */
721 PTS_ABORTIF(ptd, !(tmp = strchr(ptd->ptr, ';')));
726 PTS_ABORTIF(ptd, TRUE);
730 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
731 if (*ptd->ptr == '~')
734 PTS_ABORTIF(ptd, *ptd->ptr++ != '%');
735 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
736 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
741 static inline int stabs_pts_read_enum(struct ParseTypedefData* ptd,
742 struct symt_enum* edt)
747 while (*ptd->ptr != ';')
750 PTS_ABORTIF(ptd, stabs_pts_read_id(ptd) == -1);
751 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &value) == -1);
752 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
753 symt_add_enum_element(ptd->module, edt, ptd->buf + idx, value);
760 static inline int stabs_pts_read_array(struct ParseTypedefData* ptd,
766 /* ar<typeinfo_nodef>;<int>;<int>;<typeinfo> */
768 PTS_ABORTIF(ptd, *ptd->ptr++ != 'r');
769 /* FIXME: range type is lost, always assume int */
770 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &rdt) == -1);
771 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
772 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &lo) == -1);
773 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
774 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &hi) == -1);
775 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
777 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &rdt) == -1);
779 *adt = &symt_new_array(ptd->module, lo, hi, rdt)->symt;
783 static int stabs_pts_read_type_def(struct ParseTypedefData* ptd, const char* typename,
784 struct symt** ret_dt)
788 struct symt* new_dt = NULL; /* newly created data type */
789 struct symt* ref_dt; /* referenced data type (pointer...) */
790 long filenr1, subnr1, tmp;
792 /* things are a bit complicated because of the way the typedefs are stored inside
793 * the file, because addresses can change when realloc is done, so we must call
794 * over and over stabs_find_ref() to keep the correct values around
796 PTS_ABORTIF(ptd, stabs_pts_read_type_reference(ptd, &filenr1, &subnr1) == -1);
798 while (*ptd->ptr == '=')
801 PTS_ABORTIF(ptd, new_dt != btNoType);
803 /* first handle attribute if any */
807 if (*++ptd->ptr == 's')
810 if (stabs_pts_read_number(ptd, &sz) == -1)
812 ERR("Not an attribute... NIY\n");
816 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
820 /* then the real definitions */
825 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &ref_dt) == -1);
826 new_dt = &symt_new_pointer(ptd->module, ref_dt)->symt;
828 case 'k': /* 'const' modifier */
829 case 'B': /* 'volatile' modifier */
830 /* just kinda ignore the modifier, I guess -gmt */
831 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, typename, &new_dt) == -1);
835 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, typename, &new_dt) == -1);
838 PTS_ABORTIF(ptd, stabs_pts_read_array(ptd, &new_dt) == -1);
841 PTS_ABORTIF(ptd, stabs_pts_read_range(ptd, typename, &new_dt) == -1);
842 assert(!*stabs_find_ref(filenr1, subnr1));
843 *stabs_find_ref(filenr1, subnr1) = new_dt;
846 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &ref_dt) == -1);
847 new_dt = &symt_new_function_signature(ptd->module, ref_dt)->symt;
850 new_dt = &symt_new_enum(ptd->module, typename)->symt;
851 PTS_ABORTIF(ptd, stabs_pts_read_enum(ptd, (struct symt_enum*)new_dt) == -1);
856 struct symt_udt* udt;
857 enum UdtKind kind = (ptd->ptr[-1] == 's') ? UdtStruct : UdtUnion;
858 /* udt can have been already defined in a forward definition */
859 udt = (struct symt_udt*)*stabs_find_ref(filenr1, subnr1);
862 udt = symt_new_udt(ptd->module, typename, 0, kind);
863 /* we need to set it here, because a struct can hold a pointer
866 new_dt = *stabs_find_ref(filenr1, subnr1) = &udt->symt;
871 if (udt->symt.tag != SymTagUDT)
873 ERR("Forward declaration (%p/%s) is not an aggregate (%u)\n",
874 udt, symt_get_name(&udt->symt), udt->symt.tag);
877 /* FIXME: we currently don't correctly construct nested C++
878 * classes names. Therefore, we could be here with either:
879 * - typename and udt->hash_elt.name being the same string
880 * (non embedded case)
881 * - typename being foo::bar while udt->hash_elt.name being
883 * So, we twist the comparison to test both occurrences. When
884 * we have proper C++ types in this file, this twist has to be
887 l1 = strlen(udt->hash_elt.name);
888 l2 = strlen(typename);
889 if (l1 > l2 || strcmp(udt->hash_elt.name, typename + l2 - l1))
890 ERR("Forward declaration name mismatch %s <> %s\n",
891 udt->hash_elt.name, typename);
894 PTS_ABORTIF(ptd, stabs_pts_read_aggregate(ptd, udt) == -1);
900 PTS_ABORTIF(ptd, stabs_pts_read_id(ptd) == -1);
904 new_dt = &symt_new_enum(ptd->module, ptd->buf + idx)->symt;
907 new_dt = &symt_new_udt(ptd->module, ptd->buf + idx, 0, UdtStruct)->symt;
910 new_dt = &symt_new_udt(ptd->module, ptd->buf + idx, 0, UdtUnion)->symt;
919 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &tmp) == -1);
920 PTS_ABORTIF(ptd, stabs_get_basic(ptd, tmp, &new_dt) == -1);
921 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
925 if (*ptd->ptr == '#')
928 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &ref_dt) == -1);
929 new_dt = &symt_new_function_signature(ptd->module, ref_dt)->symt;
936 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &cls_dt) == -1);
937 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
938 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &ref_dt) == -1);
939 new_dt = &symt_new_function_signature(ptd->module, ref_dt)->symt;
940 while (*ptd->ptr == ',')
943 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &pmt_dt) == -1);
952 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &type) == -1);
953 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
954 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &len) == -1);
955 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
956 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &unk) == -1);
957 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
959 switch (type) /* see stabs_get_basic for the details */
961 case 1: basic = 12; break;
962 case 2: basic = 13; break;
963 case 3: basic = 25; break;
964 case 4: basic = 26; break;
965 case 5: basic = 35; break;
966 case 6: basic = 14; break;
967 default: PTS_ABORTIF(ptd, 1);
969 PTS_ABORTIF(ptd, stabs_get_basic(ptd, basic, &new_dt) == -1);
973 ERR("Unknown type '%c'\n", ptd->ptr[-1]);
980 /* is it a forward declaration that has been filled ? */
981 new_dt = *stabs_find_ref(filenr1, subnr1);
982 /* if not, this should be void (which is defined as a ref to itself, but we
983 * don't correctly catch it)
985 if (!new_dt && typename)
987 new_dt = &symt_new_basic(ptd->module, btVoid, typename, 0)->symt;
988 PTS_ABORTIF(ptd, strcmp(typename, "void"));
992 *stabs_find_ref(filenr1, subnr1) = *ret_dt = new_dt;
994 TRACE("Adding (%ld,%ld) %s\n", filenr1, subnr1, typename);
999 static int stabs_parse_typedef(struct module* module, const char* ptr,
1000 const char* typename)
1002 struct ParseTypedefData ptd;
1006 /* check for already existing definition */
1008 TRACE("%s => %s\n", typename, debugstr_a(ptr));
1009 ptd.module = module;
1014 for (ptd.ptr = ptr - 1; ;)
1016 ptd.ptr = strchr(ptd.ptr + 1, ':');
1017 if (ptd.ptr == NULL || *++ptd.ptr != ':') break;
1021 if (*ptd.ptr != '(') ptd.ptr++;
1022 /* most of type definitions take one char, except Tt */
1023 if (*ptd.ptr != '(') ptd.ptr++;
1024 ret = stabs_pts_read_type_def(&ptd, typename, &dt);
1027 if (ret == -1 || *ptd.ptr)
1031 TRACE("Failure on %s\n", debugstr_a(ptr));
1034 for (i = 0; i < ptd.err_idx; i++)
1036 TRACE("[%d]: line %d => %s\n",
1037 i, ptd.errors[i].line, debugstr_a(ptd.errors[i].ptr));
1041 TRACE("[0]: => %s\n", debugstr_a(ptd.ptr));
1044 ERR("Failure on %s at %s\n", debugstr_a(ptr), debugstr_a(ptd.ptr));
1052 static struct symt* stabs_parse_type(const char* stab)
1054 const char* c = stab - 1;
1057 * Look through the stab definition, and figure out what struct symt
1058 * this represents. If we have something we know about, assign the
1060 * According to "The \"stabs\" debug format" (Rev 2.130) the name may be
1061 * a C++ name and contain double colons e.g. foo::bar::baz:t5=*6.
1065 if ((c = strchr(c + 1, ':')) == NULL) return NULL;
1066 } while (*++c == ':');
1069 * The next characters say more about the type (i.e. data, function, etc)
1070 * of symbol. Skip them. (C++ for example may have Tt).
1071 * Actually this is a very weak description; I think Tt is the only
1072 * multiple combination we should see.
1074 while (*c && *c != '(' && !isdigit(*c))
1077 * The next is either an integer or a (integer,integer).
1078 * The stabs_read_type_enum() takes care that stab_types is large enough.
1080 return *stabs_read_type_enum(&c);
1083 struct pending_loc_var
1091 /******************************************************************
1092 * stabs_finalize_function
1094 * Ends function creation: mainly:
1095 * - cleans up line number information
1096 * - tries to set up a debug-start tag (FIXME: heuristic to be enhanced)
1097 * - for stabs which have abolute address in them, initializes the size of the
1098 * function (assuming that current function ends where next function starts)
1100 static void stabs_finalize_function(struct module* module, struct symt_function* func,
1106 symt_normalize_function(module, func);
1107 /* To define the debug-start of the function, we use the second line number.
1108 * Not 100% bullet proof, but better than nothing
1110 if (symt_fill_func_line_info(module, func, func->address, &il) &&
1111 symt_get_func_line_next(module, &il))
1113 symt_add_function_point(module, func, SymTagFuncDebugStart,
1114 il.Address - func->address, NULL);
1116 if (end) func->size = end - func->address;
1119 BOOL stabs_parse(struct module* module, unsigned long load_offset,
1120 const void* pv_stab_ptr, int stablen,
1121 const char* strs, int strtablen)
1123 struct symt_function* curr_func = NULL;
1124 struct symt_block* block = NULL;
1125 struct symt_compiland* compiland = NULL;
1126 char currpath[PATH_MAX]; /* path to current file */
1127 char srcpath[PATH_MAX]; /* path to directory source file is in */
1132 unsigned int stabbufflen;
1133 const struct stab_nlist* stab_ptr = pv_stab_ptr;
1134 const char* strs_end;
1139 int source_idx = -1;
1140 struct pending_loc_var* pending_vars = NULL;
1141 unsigned num_pending_vars = 0;
1142 unsigned num_allocated_pending_vars = 0;
1145 nstab = stablen / sizeof(struct stab_nlist);
1146 strs_end = strs + strtablen;
1148 memset(srcpath, 0, sizeof(srcpath));
1149 memset(stabs_basic, 0, sizeof(stabs_basic));
1152 * Allocate a buffer into which we can build stab strings for cases
1153 * where the stab is continued over multiple lines.
1155 stabbufflen = 65536;
1156 stabbuff = HeapAlloc(GetProcessHeap(), 0, stabbufflen);
1160 for (i = 0; i < nstab; i++, stab_ptr++)
1162 ptr = strs + stab_ptr->n_un.n_strx;
1163 if ((ptr > strs_end) || (ptr + strlen(ptr) > strs_end))
1165 WARN("Bad stabs string %p\n", ptr);
1168 if (ptr[strlen(ptr) - 1] == '\\')
1171 * Indicates continuation. Append this to the buffer, and go onto the
1172 * next record. Repeat the process until we find a stab without the
1173 * '/' character, as this indicates we have the whole thing.
1175 unsigned len = strlen(ptr);
1176 if (strlen(stabbuff) + len > stabbufflen)
1178 stabbufflen += 65536;
1179 stabbuff = HeapReAlloc(GetProcessHeap(), 0, stabbuff, stabbufflen);
1181 strncat(stabbuff, ptr, len - 1);
1184 else if (stabbuff[0] != '\0')
1186 strcat(stabbuff, ptr);
1190 /* only symbol entries contain a typedef */
1191 switch (stab_ptr->n_type)
1199 if (strchr(ptr, '=') != NULL)
1202 * The stabs aren't in writable memory, so copy it over so we are
1203 * sure we can scribble on it.
1205 if (ptr != stabbuff)
1207 strcpy(stabbuff, ptr);
1210 stab_strcpy(symname, sizeof(symname), ptr);
1211 if (!stabs_parse_typedef(module, ptr, symname))
1213 /* skip this definition */
1221 const char* defs[] = {"","","","", /* 00 */
1222 "","","","", /* 08 */
1223 "","","","", /* 10 */
1224 "","","","", /* 18 */
1225 "gsym","","fun","stsym", /* 20 */
1226 "lcsym","main","rosym","", /* 28 */
1227 "","","","", /* 30 */
1228 "","","opt","", /* 38 */
1229 "rsym","","sline","", /* 40 */
1230 "","","","", /* 48 */
1231 "","","","", /* 50 */
1232 "","","","", /* 58 */
1233 "","","so","", /* 60 */
1234 "","","","", /* 68 */
1235 "","","","", /* 70 */
1236 "","","","", /* 78 */
1237 "lsym","bincl","sol","", /* 80 */
1238 "","","","", /* 88 */
1239 "","","","", /* 90 */
1240 "","","","", /* 98 */
1241 "psym","eincl","","", /* a0 */
1242 "","","","", /* a8 */
1243 "","","","", /* b0 */
1244 "","","","", /* b8 */
1245 "lbrac","excl","","", /* c0 */
1246 "","","","", /* c8 */
1247 "","","","", /* d0 */
1248 "","","","", /* d8 */
1249 "rbrac","","","", /* e0 */
1252 FIXME("Got %s<%u> %u/%lu (%s)\n",
1253 defs[stab_ptr->n_type / 2], stab_ptr->n_type, stab_ptr->n_desc, stab_ptr->n_value, debugstr_a(ptr));
1256 switch (stab_ptr->n_type)
1260 * These are useless with ELF. They have no value, and you have to
1261 * read the normal symbol table to get the address. Thus we
1262 * ignore them, and when we process the normal symbol table
1263 * we should do the right thing.
1265 * With a.out or mingw, they actually do make some amount of sense.
1267 stab_strcpy(symname, sizeof(symname), ptr);
1268 symt_new_global_variable(module, compiland, symname, TRUE /* FIXME */,
1269 load_offset + stab_ptr->n_value, 0,
1270 stabs_parse_type(ptr));
1274 /* These are static symbols and BSS symbols. */
1275 stab_strcpy(symname, sizeof(symname), ptr);
1276 symt_new_global_variable(module, compiland, symname, TRUE /* FIXME */,
1277 load_offset + stab_ptr->n_value, 0,
1278 stabs_parse_type(ptr));
1281 block = symt_open_func_block(module, curr_func, block,
1282 stab_ptr->n_value, 0);
1283 for (j = 0; j < num_pending_vars; j++)
1285 symt_add_func_local(module, curr_func, pending_vars[j].regno,
1286 pending_vars[j].offset,
1287 block, pending_vars[j].type, pending_vars[j].name);
1289 num_pending_vars = 0;
1292 block = symt_close_func_block(module, curr_func, block,
1296 /* These are function parameters. */
1297 if (curr_func != NULL)
1299 struct symt* param_type = stabs_parse_type(ptr);
1300 stab_strcpy(symname, sizeof(symname), ptr);
1301 symt_add_func_local(module, curr_func, 0, stab_ptr->n_value,
1302 NULL, param_type, symname);
1303 symt_add_function_signature_parameter(module,
1304 (struct symt_function_signature*)curr_func->type,
1309 /* These are registers (as local variables) */
1310 if (curr_func != NULL)
1314 if (num_pending_vars == num_allocated_pending_vars)
1316 num_allocated_pending_vars += 8;
1318 pending_vars = HeapAlloc(GetProcessHeap(), 0,
1319 num_allocated_pending_vars * sizeof(pending_vars[0]));
1321 pending_vars = HeapReAlloc(GetProcessHeap(), 0, pending_vars,
1322 num_allocated_pending_vars * sizeof(pending_vars[0]));
1324 switch (stab_ptr->n_value)
1326 case 0: reg = CV_REG_EAX; break;
1327 case 1: reg = CV_REG_ECX; break;
1328 case 2: reg = CV_REG_EDX; break;
1329 case 3: reg = CV_REG_EBX; break;
1330 case 4: reg = CV_REG_ESP; break;
1331 case 5: reg = CV_REG_EBP; break;
1332 case 6: reg = CV_REG_ESI; break;
1333 case 7: reg = CV_REG_EDI; break;
1342 case 19: reg = CV_REG_ST0 + stab_ptr->n_value - 12; break;
1344 FIXME("Unknown register value (%lu)\n", stab_ptr->n_value);
1349 stab_strcpy(pending_vars[num_pending_vars].name,
1350 sizeof(pending_vars[num_pending_vars].name), ptr);
1351 pending_vars[num_pending_vars].type = stabs_parse_type(ptr);
1352 pending_vars[num_pending_vars].offset = 0;
1353 pending_vars[num_pending_vars].regno = reg;
1358 /* These are local variables */
1359 if (curr_func != NULL)
1361 if (num_pending_vars == num_allocated_pending_vars)
1363 num_allocated_pending_vars += 8;
1365 pending_vars = HeapAlloc(GetProcessHeap(), 0,
1366 num_allocated_pending_vars * sizeof(pending_vars[0]));
1368 pending_vars = HeapReAlloc(GetProcessHeap(), 0, pending_vars,
1369 num_allocated_pending_vars * sizeof(pending_vars[0]));
1371 stab_strcpy(pending_vars[num_pending_vars].name,
1372 sizeof(pending_vars[num_pending_vars].name), ptr);
1373 pending_vars[num_pending_vars].type = stabs_parse_type(ptr);
1374 pending_vars[num_pending_vars].offset = stab_ptr->n_value;
1375 pending_vars[num_pending_vars].regno = 0;
1381 * This is a line number. These are always relative to the start
1382 * of the function (N_FUN), and this makes the lookup easier.
1384 if (curr_func != NULL)
1386 assert(source_idx >= 0);
1387 symt_add_func_line(module, curr_func, source_idx,
1388 stab_ptr->n_desc, stab_ptr->n_value);
1392 /* First, clean up the previous function we were working on. */
1393 stabs_finalize_function(module, curr_func,
1394 stab_ptr->n_value ? load_offset + stab_ptr->n_value : 0);
1397 * For now, just declare the various functions. Later
1398 * on, we will add the line number information and the
1402 * Copy the string to a temp buffer so we
1403 * can kill everything after the ':'. We do
1404 * it this way because otherwise we end up dirtying
1405 * all of the pages related to the stabs, and that
1406 * sucks up swap space like crazy.
1408 stab_strcpy(symname, sizeof(symname), ptr);
1411 struct symt_function_signature* func_type;
1412 func_type = symt_new_function_signature(module,
1413 stabs_parse_type(ptr));
1414 curr_func = symt_new_function(module, compiland, symname,
1415 load_offset + stab_ptr->n_value, 0,
1420 /* some GCC seem to use a N_FUN "" to mark the end of a function */
1426 * This indicates a new source file. Append the records
1427 * together, to build the correct path name.
1429 if (*ptr == '\0') /* end of N_SO file */
1431 /* Nuke old path. */
1433 stabs_finalize_function(module, curr_func,
1434 stab_ptr->n_value ? load_offset + stab_ptr->n_value : 0);
1438 assert(block == NULL);
1443 int len = strlen(ptr);
1444 if (ptr[len-1] != '/')
1446 strcpy(currpath, srcpath);
1447 strcat(currpath, ptr);
1448 stabs_reset_includes();
1449 compiland = symt_new_compiland(module, currpath);
1450 source_idx = source_new(module, currpath);
1453 strcpy(srcpath, ptr);
1459 strcpy(currpath, srcpath);
1460 strcat(currpath, ptr);
1463 strcpy(currpath, ptr);
1464 source_idx = source_new(module, currpath);
1468 strtabinc = stab_ptr->n_value;
1469 /* I'm not sure this is needed, so trace it before we obsolete it */
1470 if (curr_func) FIXME("UNDF: curr_func %s\n", curr_func->hash_elt.name);
1471 stabs_finalize_function(module, curr_func, 0); /* FIXME */
1475 /* Ignore this. We don't care what it points to. */
1478 stabs_add_include(stabs_new_include(ptr, stab_ptr->n_value));
1479 assert(incl_stk < (int)(sizeof(incl) / sizeof(incl[0])) - 1);
1480 incl[++incl_stk] = source_idx;
1481 source_idx = source_new(module, ptr);
1484 assert(incl_stk >= 0);
1485 source_idx = incl[incl_stk--];
1488 if (stabs_add_include(stabs_find_include(ptr, stab_ptr->n_value)) < 0)
1490 ERR("Excluded header not found (%s,%ld)\n", ptr, stab_ptr->n_value);
1491 module_reset_debug_info(module);
1497 /* Always ignore these. GCC doesn't even generate them. */
1500 ERR("Unknown stab type 0x%02x\n", stab_ptr->n_type);
1504 TRACE("0x%02x %lx %s\n",
1505 stab_ptr->n_type, stab_ptr->n_value, debugstr_a(strs + stab_ptr->n_un.n_strx));
1507 module->module.SymType = SymDia;
1509 HeapFree(GetProcessHeap(), 0, stabbuff);
1510 stabs_free_includes();
1511 HeapFree(GetProcessHeap(), 0, pending_vars);