2 * File stabs.c - read stabs information from the modules
4 * Copyright (C) 1996, Eric Youngdale.
5 * 1999-2005, Eric Pouech
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 * Maintenance Information
23 * -----------------------
25 * For documentation on the stabs format see for example
26 * The "stabs" debug format
27 * by Julia Menapace, Jim Kingdon, David Mackenzie
29 * available (hopefully) from http://sources.redhat.com/gdb/onlinedocs
33 #include "wine/port.h"
35 #include <sys/types.h>
37 #ifdef HAVE_SYS_STAT_H
38 # include <sys/stat.h>
40 #ifdef HAVE_SYS_MMAN_H
53 #ifdef HAVE_MACH_O_NLIST_H
54 # include <mach-o/nlist.h>
61 #include "dbghelp_private.h"
63 #include "wine/debug.h"
65 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp_stabs);
67 /* Masks for n_type field */
78 /* Values for (n_type & N_TYPE) */
111 unsigned char n_type;
117 static void stab_strcpy(char* dest, int sz, const char* source)
121 * A strcpy routine that stops when we hit the ':' character.
122 * Faster than copying the whole thing, and then nuking the
124 * Takes also care of (valid) a::b constructs
126 while (*source != '\0')
128 if (source[0] != ':' && sz-- > 0) *ptr++ = *source++;
129 else if (source[1] == ':' && (sz -= 2) > 0)
137 /* GCC emits, in some cases, a .<digit>+ suffix.
138 * This is used for static variable inside functions, so
139 * that we can have several such variables with same name in
140 * the same compilation unit
141 * We simply ignore that suffix when present (we also get rid
142 * of it in ELF symtab parsing)
144 if (ptr >= dest && isdigit(*ptr))
146 while (ptr > dest && isdigit(*ptr)) ptr--;
147 if (*ptr == '.') *ptr = '\0';
156 struct symt** vector;
160 #define MAX_INCLUDES 5120
162 static include_def* include_defs = NULL;
163 static int num_include_def = 0;
164 static int num_alloc_include_def = 0;
165 static int cu_include_stack[MAX_INCLUDES];
166 static int cu_include_stk_idx = 0;
167 static struct symt** cu_vector = NULL;
168 static int cu_nrofentries = 0;
169 static struct symt_basic* stabs_basic[36];
171 static int stabs_new_include(const char* file, unsigned long val)
173 if (num_include_def == num_alloc_include_def)
177 num_alloc_include_def = 256;
178 include_defs = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
179 sizeof(include_defs[0]) * num_alloc_include_def);
183 num_alloc_include_def *= 2;
184 include_defs = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, include_defs,
185 sizeof(include_defs[0]) * num_alloc_include_def);
188 include_defs[num_include_def].name = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(file) + 1), file);
189 include_defs[num_include_def].value = val;
190 include_defs[num_include_def].vector = NULL;
191 include_defs[num_include_def].nrofentries = 0;
193 return num_include_def++;
196 static int stabs_find_include(const char* file, unsigned long val)
200 for (i = 0; i < num_include_def; i++)
202 if (val == include_defs[i].value &&
203 strcmp(file, include_defs[i].name) == 0)
209 static int stabs_add_include(int idx)
211 if (idx < 0) return -1;
212 cu_include_stk_idx++;
214 /* if this happens, just bump MAX_INCLUDES */
215 /* we could also handle this as another dynarray */
216 assert(cu_include_stk_idx < MAX_INCLUDES);
217 cu_include_stack[cu_include_stk_idx] = idx;
218 return cu_include_stk_idx;
221 static void stabs_reset_includes(void)
224 * The struct symt:s that we would need to use are reset when
225 * we start a new file. (at least the ones in filenr == 0)
227 cu_include_stk_idx = 0;/* keep 0 as index for the .c file itself */
228 memset(cu_vector, 0, sizeof(cu_vector[0]) * cu_nrofentries);
231 static void stabs_free_includes(void)
235 stabs_reset_includes();
236 for (i = 0; i < num_include_def; i++)
238 HeapFree(GetProcessHeap(), 0, include_defs[i].name);
239 HeapFree(GetProcessHeap(), 0, include_defs[i].vector);
241 HeapFree(GetProcessHeap(), 0, include_defs);
244 num_alloc_include_def = 0;
245 HeapFree(GetProcessHeap(), 0, cu_vector);
250 static struct symt** stabs_find_ref(long filenr, long subnr)
254 /* FIXME: I could perhaps create a dummy include_def for each compilation
255 * unit which would allow not to handle those two cases separately
259 if (cu_nrofentries <= subnr)
261 cu_nrofentries = max( cu_nrofentries * 2, subnr + 1 );
263 cu_vector = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
264 sizeof(cu_vector[0]) * cu_nrofentries);
266 cu_vector = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
267 cu_vector, sizeof(cu_vector[0]) * cu_nrofentries);
269 ret = &cu_vector[subnr];
275 assert(filenr <= cu_include_stk_idx);
276 idef = &include_defs[cu_include_stack[filenr]];
278 if (idef->nrofentries <= subnr)
280 idef->nrofentries = max( idef->nrofentries * 2, subnr + 1 );
282 idef->vector = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
283 sizeof(idef->vector[0]) * idef->nrofentries);
285 idef->vector = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
286 idef->vector, sizeof(idef->vector[0]) * idef->nrofentries);
288 ret = &idef->vector[subnr];
290 TRACE("(%ld,%ld) => %p (%p)\n", filenr, subnr, ret, *ret);
294 static struct symt** stabs_read_type_enum(const char** x)
304 filenr = strtol(iter, &end, 10); /* <int> */
305 iter = ++end; /* ',' */
306 subnr = strtol(iter, &end, 10); /* <int> */
307 iter = ++end; /* ')' */
312 subnr = strtol(iter, &end, 10); /* <int> */
316 return stabs_find_ref(filenr, subnr);
320 struct ParseTypedefData
325 struct module* module;
337 static void stabs_pts_push(struct ParseTypedefData* ptd, unsigned line)
339 assert(ptd->err_idx < sizeof(ptd->errors) / sizeof(ptd->errors[0]));
340 ptd->errors[ptd->err_idx].line = line;
341 ptd->errors[ptd->err_idx].ptr = ptd->ptr;
344 #define PTS_ABORTIF(ptd, t) do { if (t) { stabs_pts_push((ptd), __LINE__); return -1;} } while (0)
346 #define PTS_ABORTIF(ptd, t) do { if (t) return -1; } while (0)
349 static int stabs_get_basic(struct ParseTypedefData* ptd, unsigned basic, struct symt** symt)
351 PTS_ABORTIF(ptd, basic >= sizeof(stabs_basic) / sizeof(stabs_basic[0]));
353 if (!stabs_basic[basic])
357 case 1: stabs_basic[basic] = symt_new_basic(ptd->module, btInt, "int", 4); break;
358 case 2: stabs_basic[basic] = symt_new_basic(ptd->module, btChar, "char", 1); break;
359 case 3: stabs_basic[basic] = symt_new_basic(ptd->module, btInt, "short int", 2); break;
360 case 4: stabs_basic[basic] = symt_new_basic(ptd->module, btInt, "long int", 4); break;
361 case 5: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt, "unsigned char", 1); break;
362 case 6: stabs_basic[basic] = symt_new_basic(ptd->module, btInt, "signed char", 1); break;
363 case 7: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt, "unsigned short int", 2); break;
364 case 8: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt, "unsigned int", 4); break;
365 case 9: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt, "unsigned", 2); break;
366 case 10: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt, "unsigned long int", 2); break;
367 case 11: stabs_basic[basic] = symt_new_basic(ptd->module, btVoid, "void", 0); break;
368 case 12: stabs_basic[basic] = symt_new_basic(ptd->module, btFloat, "float", 4); break;
369 case 13: stabs_basic[basic] = symt_new_basic(ptd->module, btFloat, "double", 8); break;
370 case 14: stabs_basic[basic] = symt_new_basic(ptd->module, btFloat, "long double", 12); break;
371 case 15: stabs_basic[basic] = symt_new_basic(ptd->module, btInt, "integer", 4); break;
372 case 16: stabs_basic[basic] = symt_new_basic(ptd->module, btBool, "bool", 1); break;
373 /* case 17: short real */
375 case 25: stabs_basic[basic] = symt_new_basic(ptd->module, btComplex, "float complex", 8); break;
376 case 26: stabs_basic[basic] = symt_new_basic(ptd->module, btComplex, "double complex", 16); break;
377 case 30: stabs_basic[basic] = symt_new_basic(ptd->module, btWChar, "wchar_t", 2); break;
378 case 31: stabs_basic[basic] = symt_new_basic(ptd->module, btInt, "long long int", 8); break;
379 case 32: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt, "long long unsigned", 8); break;
380 /* starting at 35 are wine extensions (especially for R implementation) */
381 case 35: stabs_basic[basic] = symt_new_basic(ptd->module, btComplex, "long double complex", 24); break;
382 default: PTS_ABORTIF(ptd, 1);
385 *symt = &stabs_basic[basic]->symt;
389 static int stabs_pts_read_type_def(struct ParseTypedefData* ptd,
390 const char* typename, struct symt** dt);
392 static int stabs_pts_read_id(struct ParseTypedefData* ptd)
394 const char* first = ptd->ptr;
395 unsigned int template = 0;
398 while ((ch = *ptd->ptr++) != '\0')
405 unsigned int len = ptd->ptr - first - 1;
406 PTS_ABORTIF(ptd, len >= sizeof(ptd->buf) - ptd->idx);
407 memcpy(ptd->buf + ptd->idx, first, len);
408 ptd->buf[ptd->idx + len] = '\0';
413 case '<': template++; break;
414 case '>': PTS_ABORTIF(ptd, template == 0); template--; break;
420 static int stabs_pts_read_number(struct ParseTypedefData* ptd, long* v)
424 *v = strtol(ptd->ptr, &last, 10);
425 PTS_ABORTIF(ptd, last == ptd->ptr);
430 static int stabs_pts_read_type_reference(struct ParseTypedefData* ptd,
431 long* filenr, long* subnr)
433 if (*ptd->ptr == '(')
435 /* '(' <int> ',' <int> ')' */
437 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, filenr) == -1);
438 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
439 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, subnr) == -1);
440 PTS_ABORTIF(ptd, *ptd->ptr++ != ')');
445 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, subnr) == -1);
450 struct pts_range_value
456 static int stabs_pts_read_range_value(struct ParseTypedefData* ptd, struct pts_range_value* prv)
463 while (*ptd->ptr == '0') ptd->ptr++;
464 if (*ptd->ptr >= '1' && *ptd->ptr <= '7')
469 PTS_ABORTIF(ptd, ptd->ptr[0] != '1');
472 while (isdigit(*ptd->ptr)) prv->val = (prv->val << 3) + *ptd->ptr++ - '0';
477 while (isdigit(*ptd->ptr)) prv->val = (prv->val << 3) + *ptd->ptr++ - '0';
479 default: PTS_ABORTIF(ptd, 1); break;
481 } else prv->sign = 0;
485 prv->val = strtoull(++ptd->ptr, &last, 10);
491 prv->val = strtoull(ptd->ptr, &last, 10);
498 static int stabs_pts_read_range(struct ParseTypedefData* ptd, const char* typename,
502 struct pts_range_value lo;
503 struct pts_range_value hi;
509 /* type ';' <int> ';' <int> ';' */
510 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &ref) == -1);
511 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
512 PTS_ABORTIF(ptd, stabs_pts_read_range_value(ptd, &lo) == -1);
513 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
514 PTS_ABORTIF(ptd, stabs_pts_read_range_value(ptd, &hi) == -1);
515 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
517 /* basically, we don't use ref... in some cases, for example, float is declared
518 * as a derived type of int... which won't help us... so we guess the types
519 * from the various formats
521 if (lo.sign == 0 && hi.sign < 0)
526 else if (lo.sign < 0 && hi.sign == 0)
531 else if (lo.sign > 0 && hi.sign == 0)
536 else if (lo.sign < 0 && hi.sign > 0)
539 for (i = 7; i < 64; i += 8)
541 if (lo.val == v && hi.val == v - 1)
549 PTS_ABORTIF(ptd, i >= 64);
551 else if (lo.sign == 0 && hi.sign > 0)
553 if (hi.val == 127) /* specific case for char... */
561 for (i = 8; i <= 64; i += 8)
571 PTS_ABORTIF(ptd, i > 64);
574 else PTS_ABORTIF(ptd, 1);
576 *dt = &symt_new_basic(ptd->module, bt, typename, size)->symt;
580 static inline int stabs_pts_read_method_info(struct ParseTypedefData* ptd)
588 /* get type of return value */
589 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
590 if (*ptd->ptr == ';') ptd->ptr++;
592 /* get types of parameters */
593 if (*ptd->ptr == ':')
595 PTS_ABORTIF(ptd, !(tmp = strchr(ptd->ptr + 1, ';')));
598 PTS_ABORTIF(ptd, !(*ptd->ptr >= '0' && *ptd->ptr <= '9'));
600 PTS_ABORTIF(ptd, !(ptd->ptr[0] >= 'A' && *ptd->ptr <= 'D'));
602 PTS_ABORTIF(ptd, mthd != '.' && mthd != '?' && mthd != '*');
608 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &ofs) == -1);
609 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
610 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
611 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
613 } while (*ptd->ptr != ';');
619 static inline int stabs_pts_read_aggregate(struct ParseTypedefData* ptd,
620 struct symt_udt* sdt)
624 struct symt* dt = NULL;
628 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &sz) == -1);
630 doadd = symt_set_udt_size(ptd->module, sdt, sz);
631 if (*ptd->ptr == '!') /* C++ inheritance */
636 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &num_classes) == -1);
637 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
638 while (--num_classes >= 0)
640 ptd->ptr += 2; /* skip visibility and inheritance */
641 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &ofs) == -1);
642 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
644 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &adt) == -1);
651 strcpy(tmp, "__inherited_class_");
652 strcat(tmp, symt_get_name(adt));
654 /* FIXME: TI_GET_LENGTH will not always work, especially when adt
655 * has just been seen as a forward definition and not the real stuff
657 * As we don't use much the size of members in structs, this may not
658 * be much of a problem
660 symt_get_info(ptd->module, adt, TI_GET_LENGTH, &size);
661 symt_add_udt_element(ptd->module, sdt, tmp, adt, ofs, (DWORD)size * 8);
663 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
667 /* if the structure has already been filled, just redo the parsing
668 * but don't store results into the struct
669 * FIXME: there's a quite ugly memory leak in there...
672 /* Now parse the individual elements of the structure/union. */
673 while (*ptd->ptr != ';')
675 /* agg_name : type ',' <int:offset> ',' <int:size> */
678 if (ptd->ptr[0] == '$' && ptd->ptr[1] == 'v')
682 if (ptd->ptr[2] == 'f')
684 /* C++ virtual method table */
686 stabs_read_type_enum(&ptd->ptr);
687 PTS_ABORTIF(ptd, *ptd->ptr++ != ':');
688 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
689 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
690 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &x) == -1);
691 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
695 else if (ptd->ptr[2] == 'b')
698 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
699 PTS_ABORTIF(ptd, *ptd->ptr++ != ':');
700 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
701 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
702 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &x) == -1);
703 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
709 PTS_ABORTIF(ptd, stabs_pts_read_id(ptd) == -1);
710 /* Ref. TSDF R2.130 Section 7.4. When the field name is a method name
711 * it is followed by two colons rather than one.
713 if (*ptd->ptr == ':')
716 stabs_pts_read_method_info(ptd);
722 /* skip C++ member protection /0 /1 or /2 */
723 if (*ptd->ptr == '/') ptd->ptr += 2;
725 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &adt) == -1);
730 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &ofs) == -1);
731 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
732 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &sz) == -1);
733 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
735 if (doadd) symt_add_udt_element(ptd->module, sdt, ptd->buf + idx, adt, ofs, sz);
740 /* method parameters... terminated by ';' */
741 PTS_ABORTIF(ptd, !(tmp = strchr(ptd->ptr, ';')));
746 PTS_ABORTIF(ptd, TRUE);
750 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
751 if (*ptd->ptr == '~')
754 PTS_ABORTIF(ptd, *ptd->ptr++ != '%');
755 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
756 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
761 static inline int stabs_pts_read_enum(struct ParseTypedefData* ptd,
762 struct symt_enum* edt)
767 while (*ptd->ptr != ';')
770 PTS_ABORTIF(ptd, stabs_pts_read_id(ptd) == -1);
771 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &value) == -1);
772 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
773 symt_add_enum_element(ptd->module, edt, ptd->buf + idx, value);
780 static inline int stabs_pts_read_array(struct ParseTypedefData* ptd,
784 struct symt* range_dt;
785 struct symt* base_dt;
787 /* ar<typeinfo_nodef>;<int>;<int>;<typeinfo> */
789 PTS_ABORTIF(ptd, *ptd->ptr++ != 'r');
791 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &range_dt) == -1);
792 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
793 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &lo) == -1);
794 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
795 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &hi) == -1);
796 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
798 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &base_dt) == -1);
800 *adt = &symt_new_array(ptd->module, lo, hi, base_dt, range_dt)->symt;
804 static int stabs_pts_read_type_def(struct ParseTypedefData* ptd, const char* typename,
805 struct symt** ret_dt)
809 struct symt* new_dt = NULL; /* newly created data type */
810 struct symt* ref_dt; /* referenced data type (pointer...) */
811 long filenr1, subnr1, tmp;
813 /* things are a bit complicated because of the way the typedefs are stored inside
814 * the file, because addresses can change when realloc is done, so we must call
815 * over and over stabs_find_ref() to keep the correct values around
817 PTS_ABORTIF(ptd, stabs_pts_read_type_reference(ptd, &filenr1, &subnr1) == -1);
819 while (*ptd->ptr == '=')
822 PTS_ABORTIF(ptd, new_dt != NULL);
824 /* first handle attribute if any */
828 if (*++ptd->ptr == 's')
831 if (stabs_pts_read_number(ptd, &sz) == -1)
833 ERR("Not an attribute... NIY\n");
837 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
841 /* then the real definitions */
846 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &ref_dt) == -1);
847 new_dt = &symt_new_pointer(ptd->module, ref_dt, sizeof(void*))->symt;
849 case 'k': /* 'const' modifier */
850 case 'B': /* 'volatile' modifier */
851 /* just kinda ignore the modifier, I guess -gmt */
852 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, typename, &new_dt) == -1);
856 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, typename, &new_dt) == -1);
859 PTS_ABORTIF(ptd, stabs_pts_read_array(ptd, &new_dt) == -1);
862 PTS_ABORTIF(ptd, stabs_pts_read_range(ptd, typename, &new_dt) == -1);
863 assert(!*stabs_find_ref(filenr1, subnr1));
864 *stabs_find_ref(filenr1, subnr1) = new_dt;
867 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &ref_dt) == -1);
868 new_dt = &symt_new_function_signature(ptd->module, ref_dt, -1)->symt;
871 stabs_get_basic(ptd, 1 /* int */, &ref_dt);
872 new_dt = &symt_new_enum(ptd->module, typename, ref_dt)->symt;
873 PTS_ABORTIF(ptd, stabs_pts_read_enum(ptd, (struct symt_enum*)new_dt) == -1);
878 struct symt_udt* udt;
879 enum UdtKind kind = (ptd->ptr[-1] == 's') ? UdtStruct : UdtUnion;
880 /* udt can have been already defined in a forward definition */
881 udt = (struct symt_udt*)*stabs_find_ref(filenr1, subnr1);
884 udt = symt_new_udt(ptd->module, typename, 0, kind);
885 /* we need to set it here, because a struct can hold a pointer
888 new_dt = *stabs_find_ref(filenr1, subnr1) = &udt->symt;
893 if (udt->symt.tag != SymTagUDT)
895 ERR("Forward declaration (%p/%s) is not an aggregate (%u)\n",
896 udt, symt_get_name(&udt->symt), udt->symt.tag);
899 /* FIXME: we currently don't correctly construct nested C++
900 * classes names. Therefore, we could be here with either:
901 * - typename and udt->hash_elt.name being the same string
902 * (non embedded case)
903 * - typename being foo::bar while udt->hash_elt.name being
905 * So, we twist the comparison to test both occurrences. When
906 * we have proper C++ types in this file, this twist has to be
909 l1 = strlen(udt->hash_elt.name);
910 l2 = strlen(typename);
911 if (l1 > l2 || strcmp(udt->hash_elt.name, typename + l2 - l1))
912 ERR("Forward declaration name mismatch %s <> %s\n",
913 udt->hash_elt.name, typename);
916 PTS_ABORTIF(ptd, stabs_pts_read_aggregate(ptd, udt) == -1);
922 PTS_ABORTIF(ptd, stabs_pts_read_id(ptd) == -1);
926 stabs_get_basic(ptd, 1 /* int */, &ref_dt);
927 new_dt = &symt_new_enum(ptd->module, ptd->buf + idx, ref_dt)->symt;
930 new_dt = &symt_new_udt(ptd->module, ptd->buf + idx, 0, UdtStruct)->symt;
933 new_dt = &symt_new_udt(ptd->module, ptd->buf + idx, 0, UdtUnion)->symt;
942 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &tmp) == -1);
943 PTS_ABORTIF(ptd, stabs_get_basic(ptd, tmp, &new_dt) == -1);
944 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
948 if (*ptd->ptr == '#')
951 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &ref_dt) == -1);
952 new_dt = &symt_new_function_signature(ptd->module, ref_dt, -1)->symt;
959 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &cls_dt) == -1);
960 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
961 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &ref_dt) == -1);
962 new_dt = &symt_new_function_signature(ptd->module, ref_dt, -1)->symt;
963 while (*ptd->ptr == ',')
966 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &pmt_dt) == -1);
975 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &type) == -1);
976 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
977 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &len) == -1);
978 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
979 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &unk) == -1);
980 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
982 switch (type) /* see stabs_get_basic for the details */
984 case 1: basic = 12; break;
985 case 2: basic = 13; break;
986 case 3: basic = 25; break;
987 case 4: basic = 26; break;
988 case 5: basic = 35; break;
989 case 6: basic = 14; break;
990 default: PTS_ABORTIF(ptd, 1);
992 PTS_ABORTIF(ptd, stabs_get_basic(ptd, basic, &new_dt) == -1);
996 ERR("Unknown type '%c'\n", ptd->ptr[-1]);
1003 /* is it a forward declaration that has been filled ? */
1004 new_dt = *stabs_find_ref(filenr1, subnr1);
1005 /* if not, this should be void (which is defined as a ref to itself, but we
1006 * don't correctly catch it)
1008 if (!new_dt && typename)
1010 new_dt = &symt_new_basic(ptd->module, btVoid, typename, 0)->symt;
1011 PTS_ABORTIF(ptd, strcmp(typename, "void"));
1015 *stabs_find_ref(filenr1, subnr1) = *ret_dt = new_dt;
1017 TRACE("Adding (%ld,%ld) %s\n", filenr1, subnr1, debugstr_a(typename));
1022 static int stabs_parse_typedef(struct module* module, const char* ptr,
1023 const char* typename)
1025 struct ParseTypedefData ptd;
1029 /* check for already existing definition */
1031 TRACE("%s => %s\n", typename, debugstr_a(ptr));
1032 ptd.module = module;
1037 for (ptd.ptr = ptr - 1; ;)
1039 ptd.ptr = strchr(ptd.ptr + 1, ':');
1040 if (ptd.ptr == NULL || *++ptd.ptr != ':') break;
1044 if (*ptd.ptr != '(') ptd.ptr++;
1045 /* most of type definitions take one char, except Tt */
1046 if (*ptd.ptr != '(') ptd.ptr++;
1047 ret = stabs_pts_read_type_def(&ptd, typename, &dt);
1050 if (ret == -1 || *ptd.ptr)
1054 TRACE("Failure on %s\n", debugstr_a(ptr));
1057 for (i = 0; i < ptd.err_idx; i++)
1059 TRACE("[%d]: line %d => %s\n",
1060 i, ptd.errors[i].line, debugstr_a(ptd.errors[i].ptr));
1064 TRACE("[0]: => %s\n", debugstr_a(ptd.ptr));
1067 ERR("Failure on %s at %s\n", debugstr_a(ptr), debugstr_a(ptd.ptr));
1075 static struct symt* stabs_parse_type(const char* stab)
1077 const char* c = stab - 1;
1080 * Look through the stab definition, and figure out what struct symt
1081 * this represents. If we have something we know about, assign the
1083 * According to "The \"stabs\" debug format" (Rev 2.130) the name may be
1084 * a C++ name and contain double colons e.g. foo::bar::baz:t5=*6.
1088 if ((c = strchr(c + 1, ':')) == NULL) return NULL;
1089 } while (*++c == ':');
1092 * The next characters say more about the type (i.e. data, function, etc)
1093 * of symbol. Skip them. (C++ for example may have Tt).
1094 * Actually this is a very weak description; I think Tt is the only
1095 * multiple combination we should see.
1097 while (*c && *c != '(' && !isdigit(*c))
1100 * The next is either an integer or a (integer,integer).
1101 * The stabs_read_type_enum() takes care that stab_types is large enough.
1103 return *stabs_read_type_enum(&c);
1106 enum pending_obj_kind
1112 struct pending_loc_var
1117 struct location loc;
1124 unsigned long offset;
1125 unsigned long load_offset;
1128 struct pending_object
1130 enum pending_obj_kind tag;
1132 struct pending_loc_var var;
1133 struct pending_line line;
1139 struct pending_object* objs;
1144 static inline void pending_make_room(struct pending_list* pending)
1146 if (pending->num == pending->allocated)
1150 pending->allocated = 8;
1151 pending->objs = HeapAlloc(GetProcessHeap(), 0,
1152 pending->allocated * sizeof(pending->objs[0]));
1156 pending->allocated *= 2;
1157 pending->objs = HeapReAlloc(GetProcessHeap(), 0, pending->objs,
1158 pending->allocated * sizeof(pending->objs[0]));
1163 static inline void pending_add_var(struct pending_list* pending, const char* name,
1164 enum DataKind dt, const struct location* loc)
1166 pending_make_room(pending);
1167 pending->objs[pending->num].tag = PENDING_VAR;
1168 stab_strcpy(pending->objs[pending->num].u.var.name,
1169 sizeof(pending->objs[pending->num].u.var.name), name);
1170 pending->objs[pending->num].u.var.type = stabs_parse_type(name);
1171 pending->objs[pending->num].u.var.kind = dt;
1172 pending->objs[pending->num].u.var.loc = *loc;
1176 static inline void pending_add_line(struct pending_list* pending, int source_idx,
1177 int line_num, unsigned long offset,
1178 unsigned long load_offset)
1180 pending_make_room(pending);
1181 pending->objs[pending->num].tag = PENDING_LINE;
1182 pending->objs[pending->num].u.line.source_idx = source_idx;
1183 pending->objs[pending->num].u.line.line_num = line_num;
1184 pending->objs[pending->num].u.line.offset = offset;
1185 pending->objs[pending->num].u.line.load_offset = load_offset;
1189 static void pending_flush(struct pending_list* pending, struct module* module,
1190 struct symt_function* func, struct symt_block* block)
1194 for (i = 0; i < pending->num; i++)
1196 switch (pending->objs[i].tag)
1199 symt_add_func_local(module, func,
1200 pending->objs[i].u.var.kind, &pending->objs[i].u.var.loc,
1201 block, pending->objs[i].u.var.type, pending->objs[i].u.var.name);
1204 if (module->type == DMT_MACHO)
1205 pending->objs[i].u.line.offset -= func->address - pending->objs[i].u.line.load_offset;
1206 symt_add_func_line(module, func, pending->objs[i].u.line.source_idx,
1207 pending->objs[i].u.line.line_num, pending->objs[i].u.line.offset);
1210 ERR("Unknown pending object tag %u\n", (unsigned)pending->objs[i].tag);
1217 /******************************************************************
1218 * stabs_finalize_function
1220 * Ends function creation: mainly:
1221 * - cleans up line number information
1222 * - tries to set up a debug-start tag (FIXME: heuristic to be enhanced)
1223 * - for stabs which have absolute address in them, initializes the size of the
1224 * function (assuming that current function ends where next function starts)
1226 static void stabs_finalize_function(struct module* module, struct symt_function* func,
1230 struct location loc;
1233 symt_normalize_function(module, func);
1234 /* To define the debug-start of the function, we use the second line number.
1235 * Not 100% bullet proof, but better than nothing
1237 if (symt_fill_func_line_info(module, func, func->address, &il) &&
1238 symt_get_func_line_next(module, &il))
1240 loc.kind = loc_absolute;
1241 loc.offset = il.Address - func->address;
1242 symt_add_function_point(module, func, SymTagFuncDebugStart,
1245 if (size) func->size = size;
1248 static inline void stabbuf_append(char **buf, unsigned *buf_size, const char *str)
1250 unsigned str_len, buf_len;
1252 str_len = strlen(str);
1253 buf_len = strlen(*buf);
1255 if(str_len+buf_len >= *buf_size) {
1256 *buf_size += buf_len + str_len;
1257 *buf = HeapReAlloc(GetProcessHeap(), 0, *buf, *buf_size);
1260 strcpy(*buf+buf_len, str);
1263 BOOL stabs_parse(struct module* module, unsigned long load_offset,
1264 const void* pv_stab_ptr, int stablen,
1265 const char* strs, int strtablen,
1266 stabs_def_cb callback, void* user)
1268 struct symt_function* curr_func = NULL;
1269 struct symt_block* block = NULL;
1270 struct symt_compiland* compiland = NULL;
1271 char* srcpath = NULL;
1276 unsigned int stabbufflen;
1277 const struct stab_nlist* stab_ptr = pv_stab_ptr;
1278 const char* strs_end;
1283 int source_idx = -1;
1284 struct pending_list pending_block;
1285 struct pending_list pending_func;
1287 struct location loc;
1290 nstab = stablen / sizeof(struct stab_nlist);
1291 strs_end = strs + strtablen;
1293 memset(stabs_basic, 0, sizeof(stabs_basic));
1294 memset(&pending_block, 0, sizeof(pending_block));
1295 memset(&pending_func, 0, sizeof(pending_func));
1298 * Allocate a buffer into which we can build stab strings for cases
1299 * where the stab is continued over multiple lines.
1301 stabbufflen = 65536;
1302 stabbuff = HeapAlloc(GetProcessHeap(), 0, stabbufflen);
1306 for (i = 0; i < nstab; i++, stab_ptr++)
1308 ptr = strs + stab_ptr->n_strx;
1309 if ((ptr > strs_end) || (ptr + strlen(ptr) > strs_end))
1311 WARN("Bad stabs string %p\n", ptr);
1314 if (*ptr != '\0' && (ptr[strlen(ptr) - 1] == '\\'))
1317 * Indicates continuation. Append this to the buffer, and go onto the
1318 * next record. Repeat the process until we find a stab without the
1319 * '/' character, as this indicates we have the whole thing.
1321 stabbuf_append(&stabbuff, &stabbufflen, ptr);
1324 else if (stabbuff[0] != '\0')
1326 stabbuf_append(&stabbuff, &stabbufflen, ptr);
1330 if (stab_ptr->n_type & N_STAB)
1331 type = stab_ptr->n_type;
1333 type = (stab_ptr->n_type & N_TYPE);
1335 /* only symbol entries contain a typedef */
1345 if (strchr(ptr, '=') != NULL)
1348 * The stabs aren't in writable memory, so copy it over so we are
1349 * sure we can scribble on it.
1351 if (ptr != stabbuff)
1354 stabbuf_append(&stabbuff, &stabbufflen, ptr);
1357 stab_strcpy(symname, sizeof(symname), ptr);
1358 if (!stabs_parse_typedef(module, ptr, symname))
1360 /* skip this definition */
1371 * These are useless with ELF. They have no value, and you have to
1372 * read the normal symbol table to get the address. Thus we
1373 * ignore them, and when we process the normal symbol table
1374 * we should do the right thing.
1376 * With a.out or mingw, they actually do make some amount of sense.
1378 stab_strcpy(symname, sizeof(symname), ptr);
1379 loc.kind = loc_absolute;
1381 loc.offset = load_offset + stab_ptr->n_value;
1382 symt_new_global_variable(module, compiland, symname, TRUE /* FIXME */,
1383 loc, 0, stabs_parse_type(ptr));
1387 /* These are static symbols and BSS symbols. */
1388 stab_strcpy(symname, sizeof(symname), ptr);
1389 loc.kind = loc_absolute;
1391 loc.offset = load_offset + stab_ptr->n_value;
1392 symt_new_global_variable(module, compiland, symname, TRUE /* FIXME */,
1393 loc, 0, stabs_parse_type(ptr));
1398 block = symt_open_func_block(module, curr_func, block,
1399 stab_ptr->n_value, 0);
1400 pending_flush(&pending_block, module, curr_func, block);
1405 block = symt_close_func_block(module, curr_func, block,
1409 /* These are function parameters. */
1410 if (curr_func != NULL)
1412 struct symt* param_type = stabs_parse_type(ptr);
1413 stab_strcpy(symname, sizeof(symname), ptr);
1414 loc.kind = loc_regrel;
1415 loc.reg = dbghelp_current_cpu->frame_regno;
1416 loc.offset = stab_ptr->n_value;
1417 symt_add_func_local(module, curr_func,
1418 (int)stab_ptr->n_value >= 0 ? DataIsParam : DataIsLocal,
1419 &loc, NULL, param_type, symname);
1420 symt_add_function_signature_parameter(module,
1421 (struct symt_function_signature*)curr_func->type,
1426 /* These are registers (as local variables) */
1427 if (curr_func != NULL)
1429 loc.kind = loc_register;
1432 switch (stab_ptr->n_value)
1434 case 0: loc.reg = CV_REG_EAX; break;
1435 case 1: loc.reg = CV_REG_ECX; break;
1436 case 2: loc.reg = CV_REG_EDX; break;
1437 case 3: loc.reg = CV_REG_EBX; break;
1438 case 4: loc.reg = CV_REG_ESP; break;
1439 case 5: loc.reg = CV_REG_EBP; break;
1440 case 6: loc.reg = CV_REG_ESI; break;
1441 case 7: loc.reg = CV_REG_EDI; break;
1450 case 19: loc.reg = CV_REG_ST0 + stab_ptr->n_value - 12; break;
1458 case 28: loc.reg = CV_REG_XMM0 + stab_ptr->n_value - 21; break;
1466 case 36: loc.reg = CV_REG_MM0 + stab_ptr->n_value - 29; break;
1468 FIXME("Unknown register value (%u)\n", stab_ptr->n_value);
1469 loc.reg = CV_REG_NONE;
1472 stab_strcpy(symname, sizeof(symname), ptr);
1473 if (ptr[strlen(symname) + 1] == 'P')
1475 struct symt* param_type = stabs_parse_type(ptr);
1476 stab_strcpy(symname, sizeof(symname), ptr);
1477 symt_add_func_local(module, curr_func, DataIsParam, &loc,
1478 NULL, param_type, symname);
1479 symt_add_function_signature_parameter(module,
1480 (struct symt_function_signature*)curr_func->type,
1484 pending_add_var(&pending_block, ptr, DataIsLocal, &loc);
1488 /* These are local variables */
1489 loc.kind = loc_regrel;
1490 loc.reg = dbghelp_current_cpu->frame_regno;
1491 loc.offset = stab_ptr->n_value;
1492 if (curr_func != NULL) pending_add_var(&pending_block, ptr, DataIsLocal, &loc);
1496 * This is a line number. These are always relative to the start
1497 * of the function (N_FUN), and this makes the lookup easier.
1499 assert(source_idx >= 0);
1500 if (curr_func != NULL)
1502 unsigned long offset = stab_ptr->n_value;
1503 if (module->type == DMT_MACHO)
1504 offset -= curr_func->address - load_offset;
1505 symt_add_func_line(module, curr_func, source_idx,
1506 stab_ptr->n_desc, offset);
1508 else pending_add_line(&pending_func, source_idx, stab_ptr->n_desc,
1509 stab_ptr->n_value, load_offset);
1513 * For now, just declare the various functions. Later
1514 * on, we will add the line number information and the
1518 * Copy the string to a temp buffer so we
1519 * can kill everything after the ':'. We do
1520 * it this way because otherwise we end up dirtying
1521 * all of the pages related to the stabs, and that
1522 * sucks up swap space like crazy.
1524 stab_strcpy(symname, sizeof(symname), ptr);
1527 struct symt_function_signature* func_type;
1531 /* First, clean up the previous function we were working on.
1532 * Assume size of the func is the delta between current offset
1533 * and offset of last function
1535 stabs_finalize_function(module, curr_func,
1537 (load_offset + stab_ptr->n_value - curr_func->address) : 0);
1539 func_type = symt_new_function_signature(module,
1540 stabs_parse_type(ptr), -1);
1541 curr_func = symt_new_function(module, compiland, symname,
1542 load_offset + stab_ptr->n_value, 0,
1544 pending_flush(&pending_func, module, curr_func, NULL);
1548 /* some versions of GCC to use a N_FUN "" to mark the end of a function
1549 * and n_value contains the size of the func
1551 stabs_finalize_function(module, curr_func, stab_ptr->n_value);
1557 * This indicates a new source file. Append the records
1558 * together, to build the correct path name.
1560 if (*ptr == '\0') /* end of N_SO file */
1562 /* Nuke old path. */
1563 HeapFree(GetProcessHeap(), 0, srcpath);
1565 stabs_finalize_function(module, curr_func, 0);
1569 assert(block == NULL);
1574 int len = strlen(ptr);
1575 if (ptr[len-1] != '/')
1577 stabs_reset_includes();
1578 source_idx = source_new(module, srcpath, ptr);
1579 compiland = symt_new_compiland(module, 0 /* FIXME */, source_idx);
1583 srcpath = HeapAlloc(GetProcessHeap(), 0, len + 1);
1584 strcpy(srcpath, ptr);
1589 source_idx = source_new(module, srcpath, ptr);
1593 strtabinc = stab_ptr->n_value;
1594 /* I'm not sure this is needed, so trace it before we obsolete it */
1597 FIXME("UNDF: curr_func %s\n", curr_func->hash_elt.name);
1598 stabs_finalize_function(module, curr_func, 0); /* FIXME */
1603 /* Ignore this. We don't care what it points to. */
1606 stabs_add_include(stabs_new_include(ptr, stab_ptr->n_value));
1607 assert(incl_stk < (int)(sizeof(incl) / sizeof(incl[0])) - 1);
1608 incl[++incl_stk] = source_idx;
1609 source_idx = source_new(module, NULL, ptr);
1612 assert(incl_stk >= 0);
1613 source_idx = incl[incl_stk--];
1616 if (stabs_add_include(stabs_find_include(ptr, stab_ptr->n_value)) < 0)
1618 ERR("Excluded header not found (%s,%d)\n", ptr, stab_ptr->n_value);
1619 module_reset_debug_info(module);
1625 /* Always ignore these. GCC doesn't even generate them. */
1630 /* Always ignore these, they seem to be used only on Darwin. */
1636 /* FIXME: Other definition types (N_TEXT, N_DATA, N_BSS, ...)? */
1639 BOOL is_public = (stab_ptr->n_type & N_EXT);
1640 BOOL is_global = is_public;
1643 /* "private extern"; shared among compilation units in a shared
1644 * library, but not accessible from outside the library. */
1645 if (stab_ptr->n_type & N_PEXT)
1652 if (*ptr == '_') ptr++;
1653 stab_strcpy(symname, sizeof(symname), ptr);
1655 callback(module, load_offset, symname, stab_ptr->n_value,
1656 is_public, is_global, stab_ptr->n_other, compiland, user);
1660 ERR("Unknown stab type 0x%02x\n", type);
1664 TRACE("0x%02x %x %s\n",
1665 stab_ptr->n_type, stab_ptr->n_value, debugstr_a(strs + stab_ptr->n_strx));
1667 module->module.SymType = SymDia;
1668 module->module.CVSig = 'S' | ('T' << 8) | ('A' << 16) | ('B' << 24);
1669 /* FIXME: we could have a finer grain here */
1670 module->module.LineNumbers = TRUE;
1671 module->module.GlobalSymbols = TRUE;
1672 module->module.TypeInfo = TRUE;
1673 module->module.SourceIndexed = TRUE;
1674 module->module.Publics = TRUE;
1676 HeapFree(GetProcessHeap(), 0, stabbuff);
1677 stabs_free_includes();
1678 HeapFree(GetProcessHeap(), 0, pending_block.objs);
1679 HeapFree(GetProcessHeap(), 0, pending_func.objs);