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
34 #include <sys/types.h>
36 #ifdef HAVE_SYS_STAT_H
37 # include <sys/stat.h>
39 #ifdef HAVE_SYS_MMAN_H
50 #define PATH_MAX MAX_PATH
60 #include "dbghelp_private.h"
62 #include "wine/debug.h"
64 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)
109 * A strcpy routine that stops when we hit the ':' character.
110 * Faster than copying the whole thing, and then nuking the
112 * Takes also care of (valid) a::b constructs
114 while (*source != '\0')
116 if (source[0] != ':' && sz-- > 0) *ptr++ = *source++;
117 else if (source[1] == ':' && (sz -= 2) > 0)
125 /* GCC emits, in some cases, a .<digit>+ suffix.
126 * This is used for static variable inside functions, so
127 * that we can have several such variables with same name in
128 * the same compilation unit
129 * We simply ignore that suffix when present (we also get rid
130 * of it in ELF symtab parsing)
132 if (ptr >= dest && isdigit(*ptr))
134 while (ptr > dest && isdigit(*ptr)) ptr--;
135 if (*ptr == '.') *ptr = '\0';
144 struct symt** vector;
148 #define MAX_INCLUDES 5120
150 static include_def* include_defs = NULL;
151 static int num_include_def = 0;
152 static int num_alloc_include_def = 0;
153 static int cu_include_stack[MAX_INCLUDES];
154 static int cu_include_stk_idx = 0;
155 static struct symt** cu_vector = NULL;
156 static int cu_nrofentries = 0;
157 static struct symt_basic* stabs_basic[36];
159 static int stabs_new_include(const char* file, unsigned long val)
161 if (num_include_def == num_alloc_include_def)
163 num_alloc_include_def += 256;
165 include_defs = HeapAlloc(GetProcessHeap(), 0,
166 sizeof(include_defs[0]) * num_alloc_include_def);
168 include_defs = HeapReAlloc(GetProcessHeap(), 0, include_defs,
169 sizeof(include_defs[0]) * num_alloc_include_def);
170 memset(include_defs + num_include_def, 0, sizeof(include_defs[0]) * 256);
172 include_defs[num_include_def].name = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(file) + 1), file);
173 include_defs[num_include_def].value = val;
174 include_defs[num_include_def].vector = NULL;
175 include_defs[num_include_def].nrofentries = 0;
177 return num_include_def++;
180 static int stabs_find_include(const char* file, unsigned long val)
184 for (i = 0; i < num_include_def; i++)
186 if (val == include_defs[i].value &&
187 strcmp(file, include_defs[i].name) == 0)
193 static int stabs_add_include(int idx)
195 if (idx < 0) return -1;
196 cu_include_stk_idx++;
198 /* if this happens, just bump MAX_INCLUDES */
199 /* we could also handle this as another dynarray */
200 assert(cu_include_stk_idx < MAX_INCLUDES);
201 cu_include_stack[cu_include_stk_idx] = idx;
202 return cu_include_stk_idx;
205 static void stabs_reset_includes(void)
208 * The struct symt:s that we would need to use are reset when
209 * we start a new file. (at least the ones in filenr == 0)
211 cu_include_stk_idx = 0;/* keep 0 as index for the .c file itself */
212 memset(cu_vector, 0, sizeof(cu_vector[0]) * cu_nrofentries);
215 static void stabs_free_includes(void)
219 stabs_reset_includes();
220 for (i = 0; i < num_include_def; i++)
222 HeapFree(GetProcessHeap(), 0, include_defs[i].name);
223 HeapFree(GetProcessHeap(), 0, include_defs[i].vector);
225 HeapFree(GetProcessHeap(), 0, include_defs);
228 num_alloc_include_def = 0;
229 HeapFree(GetProcessHeap(), 0, cu_vector);
234 static struct symt** stabs_find_ref(long filenr, long subnr)
238 /* FIXME: I could perhaps create a dummy include_def for each compilation
239 * unit which would allow not to handle those two cases separately
243 if (cu_nrofentries <= subnr)
246 cu_vector = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
247 sizeof(cu_vector[0]) * (subnr+1));
249 cu_vector = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
250 cu_vector, sizeof(cu_vector[0]) * (subnr+1));
251 cu_nrofentries = subnr + 1;
253 ret = &cu_vector[subnr];
259 assert(filenr <= cu_include_stk_idx);
260 idef = &include_defs[cu_include_stack[filenr]];
262 if (idef->nrofentries <= subnr)
265 idef->vector = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
266 sizeof(idef->vector[0]) * (subnr+1));
268 idef->vector = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
269 idef->vector, sizeof(idef->vector[0]) * (subnr+1));
270 idef->nrofentries = subnr + 1;
272 ret = &idef->vector[subnr];
274 TRACE("(%ld,%ld) => %p (%p)\n", filenr, subnr, ret, *ret);
278 static struct symt** stabs_read_type_enum(const char** x)
288 filenr = strtol(iter, &end, 10); /* <int> */
289 iter = ++end; /* ',' */
290 subnr = strtol(iter, &end, 10); /* <int> */
291 iter = ++end; /* ')' */
296 subnr = strtol(iter, &end, 10); /* <int> */
300 return stabs_find_ref(filenr, subnr);
304 struct ParseTypedefData
309 struct module* module;
321 static void stabs_pts_push(struct ParseTypedefData* ptd, unsigned line)
323 assert(ptd->err_idx < sizeof(ptd->errors) / sizeof(ptd->errors[0]));
324 ptd->errors[ptd->err_idx].line = line;
325 ptd->errors[ptd->err_idx].ptr = ptd->ptr;
328 #define PTS_ABORTIF(ptd, t) do { if (t) { stabs_pts_push((ptd), __LINE__); return -1;} } while (0)
330 #define PTS_ABORTIF(ptd, t) do { if (t) return -1; } while (0)
333 static int stabs_get_basic(struct ParseTypedefData* ptd, unsigned basic, struct symt** symt)
335 PTS_ABORTIF(ptd, basic >= sizeof(stabs_basic) / sizeof(stabs_basic[0]));
337 if (!stabs_basic[basic])
341 case 1: stabs_basic[basic] = symt_new_basic(ptd->module, btInt, "int", 4); break;
342 case 2: stabs_basic[basic] = symt_new_basic(ptd->module, btChar, "char", 1); break;
343 case 3: stabs_basic[basic] = symt_new_basic(ptd->module, btInt, "short int", 2); break;
344 case 4: stabs_basic[basic] = symt_new_basic(ptd->module, btInt, "long int", 4); break;
345 case 5: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt, "unsigned char", 1); break;
346 case 6: stabs_basic[basic] = symt_new_basic(ptd->module, btInt, "signed char", 1); break;
347 case 7: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt, "unsigned short int", 2); break;
348 case 8: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt, "unsigned int", 4); break;
349 case 9: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt, "unsigned", 2); break;
350 case 10: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt, "unsigned long int", 2); break;
351 case 11: stabs_basic[basic] = symt_new_basic(ptd->module, btVoid, "void", 0); break;
352 case 12: stabs_basic[basic] = symt_new_basic(ptd->module, btFloat, "float", 4); break;
353 case 13: stabs_basic[basic] = symt_new_basic(ptd->module, btFloat, "double", 8); break;
354 case 14: stabs_basic[basic] = symt_new_basic(ptd->module, btFloat, "long double", 12); break;
355 case 15: stabs_basic[basic] = symt_new_basic(ptd->module, btInt, "integer", 4); break;
356 case 16: stabs_basic[basic] = symt_new_basic(ptd->module, btBool, "bool", 1); break;
357 /* case 17: short real */
359 case 25: stabs_basic[basic] = symt_new_basic(ptd->module, btComplex, "float complex", 8); break;
360 case 26: stabs_basic[basic] = symt_new_basic(ptd->module, btComplex, "double complex", 16); break;
361 case 30: stabs_basic[basic] = symt_new_basic(ptd->module, btWChar, "wchar_t", 2); break;
362 case 31: stabs_basic[basic] = symt_new_basic(ptd->module, btInt, "long long int", 8); break;
363 case 32: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt, "long long unsigned", 8); break;
364 /* starting at 35 are wine extensions (especially for R implementation) */
365 case 35: stabs_basic[basic] = symt_new_basic(ptd->module, btComplex, "long double complex", 24); break;
366 default: PTS_ABORTIF(ptd, 1);
369 *symt = &stabs_basic[basic]->symt;
373 static int stabs_pts_read_type_def(struct ParseTypedefData* ptd,
374 const char* typename, struct symt** dt);
376 static int stabs_pts_read_id(struct ParseTypedefData* ptd)
378 const char* first = ptd->ptr;
379 unsigned int template = 0;
382 while ((ch = *ptd->ptr++) != '\0')
389 unsigned int len = ptd->ptr - first - 1;
390 PTS_ABORTIF(ptd, len >= sizeof(ptd->buf) - ptd->idx);
391 memcpy(ptd->buf + ptd->idx, first, len);
392 ptd->buf[ptd->idx + len] = '\0';
397 case '<': template++; break;
398 case '>': PTS_ABORTIF(ptd, template == 0); template--; break;
404 static int stabs_pts_read_number(struct ParseTypedefData* ptd, long* v)
408 *v = strtol(ptd->ptr, &last, 10);
409 PTS_ABORTIF(ptd, last == ptd->ptr);
414 static int stabs_pts_read_type_reference(struct ParseTypedefData* ptd,
415 long* filenr, long* subnr)
417 if (*ptd->ptr == '(')
419 /* '(' <int> ',' <int> ')' */
421 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, filenr) == -1);
422 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
423 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, subnr) == -1);
424 PTS_ABORTIF(ptd, *ptd->ptr++ != ')');
429 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, subnr) == -1);
434 struct pts_range_value
440 static int stabs_pts_read_range_value(struct ParseTypedefData* ptd, struct pts_range_value* prv)
447 while (*ptd->ptr == '0') ptd->ptr++;
448 if (*ptd->ptr >= '1' && *ptd->ptr <= '7')
453 PTS_ABORTIF(ptd, ptd->ptr[0] != '1');
456 while (isdigit(*ptd->ptr)) prv->val = (prv->val << 3) + *ptd->ptr++ - '0';
461 while (isdigit(*ptd->ptr)) prv->val = (prv->val << 3) + *ptd->ptr++ - '0';
463 default: PTS_ABORTIF(ptd, 1); break;
465 } else prv->sign = 0;
469 prv->val = strtoull(++ptd->ptr, &last, 10);
475 prv->val = strtoull(ptd->ptr, &last, 10);
482 static int stabs_pts_read_range(struct ParseTypedefData* ptd, const char* typename,
486 struct pts_range_value lo;
487 struct pts_range_value hi;
493 /* type ';' <int> ';' <int> ';' */
494 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &ref) == -1);
495 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
496 PTS_ABORTIF(ptd, stabs_pts_read_range_value(ptd, &lo) == -1);
497 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
498 PTS_ABORTIF(ptd, stabs_pts_read_range_value(ptd, &hi) == -1);
499 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
501 /* basically, we don't use ref... in some cases, for example, float is declared
502 * as a derivated type of int... which won't help us... so we guess the types
503 * from the various formats
505 if (lo.sign == 0 && hi.sign < 0)
510 else if (lo.sign < 0 && hi.sign == 0)
515 else if (lo.sign > 0 && hi.sign == 0)
520 else if (lo.sign < 0 && hi.sign > 0)
523 for (i = 7; i < 64; i += 8)
525 if (lo.val == v && hi.val == v - 1)
533 PTS_ABORTIF(ptd, i >= 64);
535 else if (lo.sign == 0 && hi.sign > 0)
537 if (hi.val == 127) /* specific case for char... */
545 for (i = 8; i <= 64; i += 8)
555 PTS_ABORTIF(ptd, i > 64);
558 else PTS_ABORTIF(ptd, 1);
560 *dt = &symt_new_basic(ptd->module, bt, typename, size)->symt;
564 static inline int stabs_pts_read_method_info(struct ParseTypedefData* ptd)
572 /* get type of return value */
573 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
574 if (*ptd->ptr == ';') ptd->ptr++;
576 /* get types of parameters */
577 if (*ptd->ptr == ':')
579 PTS_ABORTIF(ptd, !(tmp = strchr(ptd->ptr + 1, ';')));
582 PTS_ABORTIF(ptd, !(*ptd->ptr >= '0' && *ptd->ptr <= '9'));
584 PTS_ABORTIF(ptd, !(ptd->ptr[0] >= 'A' && *ptd->ptr <= 'D'));
586 PTS_ABORTIF(ptd, mthd != '.' && mthd != '?' && mthd != '*');
593 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &ofs) == -1);
594 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
595 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
596 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
598 } while (*ptd->ptr != ';');
604 static inline int stabs_pts_read_aggregate(struct ParseTypedefData* ptd,
605 struct symt_udt* sdt)
609 struct symt* dt = NULL;
613 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &sz) == -1);
615 doadd = symt_set_udt_size(ptd->module, sdt, sz);
616 if (*ptd->ptr == '!') /* C++ inheritence */
621 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &num_classes) == -1);
622 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
623 while (--num_classes >= 0)
625 ptd->ptr += 2; /* skip visibility and inheritence */
626 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &ofs) == -1);
627 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
629 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &adt) == -1);
637 symt_get_info(adt, TI_GET_SYMNAME, &name);
638 strcpy(tmp, "__inherited_class_");
639 WideCharToMultiByte(CP_ACP, 0, name, -1,
640 tmp + strlen(tmp), sizeof(tmp) - strlen(tmp),
642 HeapFree(GetProcessHeap(), 0, name);
643 /* FIXME: TI_GET_LENGTH will not always work, especially when adt
644 * has just been seen as a forward definition and not the real stuff
646 * As we don't use much the size of members in structs, this may not
647 * be much of a problem
649 symt_get_info(adt, TI_GET_LENGTH, &size);
650 symt_add_udt_element(ptd->module, sdt, tmp, adt, ofs, (DWORD)size * 8);
652 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
656 /* if the structure has already been filled, just redo the parsing
657 * but don't store results into the struct
658 * FIXME: there's a quite ugly memory leak in there...
661 /* Now parse the individual elements of the structure/union. */
662 while (*ptd->ptr != ';')
664 /* agg_name : type ',' <int:offset> ',' <int:size> */
667 if (ptd->ptr[0] == '$' && ptd->ptr[1] == 'v')
671 if (ptd->ptr[2] == 'f')
673 /* C++ virtual method table */
675 stabs_read_type_enum(&ptd->ptr);
676 PTS_ABORTIF(ptd, *ptd->ptr++ != ':');
677 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
678 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
679 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &x) == -1);
680 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
684 else if (ptd->ptr[2] == 'b')
687 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
688 PTS_ABORTIF(ptd, *ptd->ptr++ != ':');
689 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
690 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
691 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &x) == -1);
692 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
698 PTS_ABORTIF(ptd, stabs_pts_read_id(ptd) == -1);
699 /* Ref. TSDF R2.130 Section 7.4. When the field name is a method name
700 * it is followed by two colons rather than one.
702 if (*ptd->ptr == ':')
705 stabs_pts_read_method_info(ptd);
711 /* skip C++ member protection /0 /1 or /2 */
712 if (*ptd->ptr == '/') ptd->ptr += 2;
714 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &adt) == -1);
719 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &ofs) == -1);
720 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
721 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &sz) == -1);
722 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
724 if (doadd) symt_add_udt_element(ptd->module, sdt, ptd->buf + idx, adt, ofs, sz);
729 /* method parameters... terminated by ';' */
730 PTS_ABORTIF(ptd, !(tmp = strchr(ptd->ptr, ';')));
735 PTS_ABORTIF(ptd, TRUE);
739 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
740 if (*ptd->ptr == '~')
743 PTS_ABORTIF(ptd, *ptd->ptr++ != '%');
744 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
745 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
750 static inline int stabs_pts_read_enum(struct ParseTypedefData* ptd,
751 struct symt_enum* edt)
756 while (*ptd->ptr != ';')
759 PTS_ABORTIF(ptd, stabs_pts_read_id(ptd) == -1);
760 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &value) == -1);
761 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
762 symt_add_enum_element(ptd->module, edt, ptd->buf + idx, value);
769 static inline int stabs_pts_read_array(struct ParseTypedefData* ptd,
773 struct symt* range_dt;
774 struct symt* base_dt;
776 /* ar<typeinfo_nodef>;<int>;<int>;<typeinfo> */
778 PTS_ABORTIF(ptd, *ptd->ptr++ != 'r');
780 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &range_dt) == -1);
781 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
782 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &lo) == -1);
783 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
784 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &hi) == -1);
785 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
787 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &base_dt) == -1);
789 *adt = &symt_new_array(ptd->module, lo, hi, base_dt, range_dt)->symt;
793 static int stabs_pts_read_type_def(struct ParseTypedefData* ptd, const char* typename,
794 struct symt** ret_dt)
798 struct symt* new_dt = NULL; /* newly created data type */
799 struct symt* ref_dt; /* referenced data type (pointer...) */
800 long filenr1, subnr1, tmp;
802 /* things are a bit complicated because of the way the typedefs are stored inside
803 * the file, because addresses can change when realloc is done, so we must call
804 * over and over stabs_find_ref() to keep the correct values around
806 PTS_ABORTIF(ptd, stabs_pts_read_type_reference(ptd, &filenr1, &subnr1) == -1);
808 while (*ptd->ptr == '=')
811 PTS_ABORTIF(ptd, new_dt != btNoType);
813 /* first handle attribute if any */
817 if (*++ptd->ptr == 's')
820 if (stabs_pts_read_number(ptd, &sz) == -1)
822 ERR("Not an attribute... NIY\n");
826 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
830 /* then the real definitions */
835 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &ref_dt) == -1);
836 new_dt = &symt_new_pointer(ptd->module, ref_dt)->symt;
838 case 'k': /* 'const' modifier */
839 case 'B': /* 'volatile' modifier */
840 /* just kinda ignore the modifier, I guess -gmt */
841 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, typename, &new_dt) == -1);
845 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, typename, &new_dt) == -1);
848 PTS_ABORTIF(ptd, stabs_pts_read_array(ptd, &new_dt) == -1);
851 PTS_ABORTIF(ptd, stabs_pts_read_range(ptd, typename, &new_dt) == -1);
852 assert(!*stabs_find_ref(filenr1, subnr1));
853 *stabs_find_ref(filenr1, subnr1) = new_dt;
856 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &ref_dt) == -1);
857 new_dt = &symt_new_function_signature(ptd->module, ref_dt, -1)->symt;
860 new_dt = &symt_new_enum(ptd->module, typename)->symt;
861 PTS_ABORTIF(ptd, stabs_pts_read_enum(ptd, (struct symt_enum*)new_dt) == -1);
866 struct symt_udt* udt;
867 enum UdtKind kind = (ptd->ptr[-1] == 's') ? UdtStruct : UdtUnion;
868 /* udt can have been already defined in a forward definition */
869 udt = (struct symt_udt*)*stabs_find_ref(filenr1, subnr1);
872 udt = symt_new_udt(ptd->module, typename, 0, kind);
873 /* we need to set it here, because a struct can hold a pointer
876 new_dt = *stabs_find_ref(filenr1, subnr1) = &udt->symt;
881 if (udt->symt.tag != SymTagUDT)
883 ERR("Forward declaration (%p/%s) is not an aggregate (%u)\n",
884 udt, symt_get_name(&udt->symt), udt->symt.tag);
887 /* FIXME: we currently don't correctly construct nested C++
888 * classes names. Therefore, we could be here with either:
889 * - typename and udt->hash_elt.name being the same string
890 * (non embedded case)
891 * - typename being foo::bar while udt->hash_elt.name being
893 * So, we twist the comparison to test both occurrences. When
894 * we have proper C++ types in this file, this twist has to be
897 l1 = strlen(udt->hash_elt.name);
898 l2 = strlen(typename);
899 if (l1 > l2 || strcmp(udt->hash_elt.name, typename + l2 - l1))
900 ERR("Forward declaration name mismatch %s <> %s\n",
901 udt->hash_elt.name, typename);
904 PTS_ABORTIF(ptd, stabs_pts_read_aggregate(ptd, udt) == -1);
910 PTS_ABORTIF(ptd, stabs_pts_read_id(ptd) == -1);
914 new_dt = &symt_new_enum(ptd->module, ptd->buf + idx)->symt;
917 new_dt = &symt_new_udt(ptd->module, ptd->buf + idx, 0, UdtStruct)->symt;
920 new_dt = &symt_new_udt(ptd->module, ptd->buf + idx, 0, UdtUnion)->symt;
929 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &tmp) == -1);
930 PTS_ABORTIF(ptd, stabs_get_basic(ptd, tmp, &new_dt) == -1);
931 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
935 if (*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, -1)->symt;
946 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &cls_dt) == -1);
947 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
948 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &ref_dt) == -1);
949 new_dt = &symt_new_function_signature(ptd->module, ref_dt, -1)->symt;
950 while (*ptd->ptr == ',')
953 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &pmt_dt) == -1);
962 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &type) == -1);
963 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
964 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &len) == -1);
965 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
966 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &unk) == -1);
967 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
969 switch (type) /* see stabs_get_basic for the details */
971 case 1: basic = 12; break;
972 case 2: basic = 13; break;
973 case 3: basic = 25; break;
974 case 4: basic = 26; break;
975 case 5: basic = 35; break;
976 case 6: basic = 14; break;
977 default: PTS_ABORTIF(ptd, 1);
979 PTS_ABORTIF(ptd, stabs_get_basic(ptd, basic, &new_dt) == -1);
983 ERR("Unknown type '%c'\n", ptd->ptr[-1]);
990 /* is it a forward declaration that has been filled ? */
991 new_dt = *stabs_find_ref(filenr1, subnr1);
992 /* if not, this should be void (which is defined as a ref to itself, but we
993 * don't correctly catch it)
995 if (!new_dt && typename)
997 new_dt = &symt_new_basic(ptd->module, btVoid, typename, 0)->symt;
998 PTS_ABORTIF(ptd, strcmp(typename, "void"));
1002 *stabs_find_ref(filenr1, subnr1) = *ret_dt = new_dt;
1004 TRACE("Adding (%ld,%ld) %s\n", filenr1, subnr1, typename);
1009 static int stabs_parse_typedef(struct module* module, const char* ptr,
1010 const char* typename)
1012 struct ParseTypedefData ptd;
1016 /* check for already existing definition */
1018 TRACE("%s => %s\n", typename, debugstr_a(ptr));
1019 ptd.module = module;
1024 for (ptd.ptr = ptr - 1; ;)
1026 ptd.ptr = strchr(ptd.ptr + 1, ':');
1027 if (ptd.ptr == NULL || *++ptd.ptr != ':') break;
1031 if (*ptd.ptr != '(') ptd.ptr++;
1032 /* most of type definitions take one char, except Tt */
1033 if (*ptd.ptr != '(') ptd.ptr++;
1034 ret = stabs_pts_read_type_def(&ptd, typename, &dt);
1037 if (ret == -1 || *ptd.ptr)
1041 TRACE("Failure on %s\n", debugstr_a(ptr));
1044 for (i = 0; i < ptd.err_idx; i++)
1046 TRACE("[%d]: line %d => %s\n",
1047 i, ptd.errors[i].line, debugstr_a(ptd.errors[i].ptr));
1051 TRACE("[0]: => %s\n", debugstr_a(ptd.ptr));
1054 ERR("Failure on %s at %s\n", debugstr_a(ptr), debugstr_a(ptd.ptr));
1062 static struct symt* stabs_parse_type(const char* stab)
1064 const char* c = stab - 1;
1067 * Look through the stab definition, and figure out what struct symt
1068 * this represents. If we have something we know about, assign the
1070 * According to "The \"stabs\" debug format" (Rev 2.130) the name may be
1071 * a C++ name and contain double colons e.g. foo::bar::baz:t5=*6.
1075 if ((c = strchr(c + 1, ':')) == NULL) return NULL;
1076 } while (*++c == ':');
1079 * The next characters say more about the type (i.e. data, function, etc)
1080 * of symbol. Skip them. (C++ for example may have Tt).
1081 * Actually this is a very weak description; I think Tt is the only
1082 * multiple combination we should see.
1084 while (*c && *c != '(' && !isdigit(*c))
1087 * The next is either an integer or a (integer,integer).
1088 * The stabs_read_type_enum() takes care that stab_types is large enough.
1090 return *stabs_read_type_enum(&c);
1093 struct pending_loc_var
1098 struct location loc;
1101 struct pending_block
1103 struct pending_loc_var* vars;
1108 static inline void pending_add(struct pending_block* pending, const char* name,
1109 enum DataKind dt, const struct location* loc)
1111 if (pending->num == pending->allocated)
1113 pending->allocated += 8;
1115 pending->vars = HeapAlloc(GetProcessHeap(), 0,
1116 pending->allocated * sizeof(pending->vars[0]));
1118 pending->vars = HeapReAlloc(GetProcessHeap(), 0, pending->vars,
1119 pending->allocated * sizeof(pending->vars[0]));
1121 stab_strcpy(pending->vars[pending->num].name,
1122 sizeof(pending->vars[pending->num].name), name);
1123 pending->vars[pending->num].type = stabs_parse_type(name);
1124 pending->vars[pending->num].kind = dt;
1125 pending->vars[pending->num].loc = *loc;
1129 static void pending_flush(struct pending_block* pending, struct module* module,
1130 struct symt_function* func, struct symt_block* block)
1134 for (i = 0; i < pending->num; i++)
1136 symt_add_func_local(module, func,
1137 pending->vars[i].kind, &pending->vars[i].loc,
1138 block, pending->vars[i].type, pending->vars[i].name);
1143 /******************************************************************
1144 * stabs_finalize_function
1146 * Ends function creation: mainly:
1147 * - cleans up line number information
1148 * - tries to set up a debug-start tag (FIXME: heuristic to be enhanced)
1149 * - for stabs which have abolute address in them, initializes the size of the
1150 * function (assuming that current function ends where next function starts)
1152 static void stabs_finalize_function(struct module* module, struct symt_function* func,
1156 struct location loc;
1159 symt_normalize_function(module, func);
1160 /* To define the debug-start of the function, we use the second line number.
1161 * Not 100% bullet proof, but better than nothing
1163 if (symt_fill_func_line_info(module, func, func->address, &il) &&
1164 symt_get_func_line_next(module, &il))
1166 loc.kind = loc_absolute;
1167 loc.offset = il.Address - func->address;
1168 symt_add_function_point(module, func, SymTagFuncDebugStart,
1171 if (size) func->size = size;
1174 BOOL stabs_parse(struct module* module, unsigned long load_offset,
1175 const void* pv_stab_ptr, int stablen,
1176 const char* strs, int strtablen)
1178 struct symt_function* curr_func = NULL;
1179 struct symt_block* block = NULL;
1180 struct symt_compiland* compiland = NULL;
1181 char srcpath[PATH_MAX]; /* path to directory source file is in */
1186 unsigned int stabbufflen;
1187 const struct stab_nlist* stab_ptr = pv_stab_ptr;
1188 const char* strs_end;
1193 int source_idx = -1;
1194 struct pending_block pending;
1196 struct location loc;
1198 nstab = stablen / sizeof(struct stab_nlist);
1199 strs_end = strs + strtablen;
1201 memset(srcpath, 0, sizeof(srcpath));
1202 memset(stabs_basic, 0, sizeof(stabs_basic));
1203 memset(&pending, 0, sizeof(pending));
1206 * Allocate a buffer into which we can build stab strings for cases
1207 * where the stab is continued over multiple lines.
1209 stabbufflen = 65536;
1210 stabbuff = HeapAlloc(GetProcessHeap(), 0, stabbufflen);
1214 for (i = 0; i < nstab; i++, stab_ptr++)
1216 ptr = strs + stab_ptr->n_un.n_strx;
1217 if ((ptr > strs_end) || (ptr + strlen(ptr) > strs_end))
1219 WARN("Bad stabs string %p\n", ptr);
1222 if (*ptr != '\0' && (ptr[strlen(ptr) - 1] == '\\'))
1225 * Indicates continuation. Append this to the buffer, and go onto the
1226 * next record. Repeat the process until we find a stab without the
1227 * '/' character, as this indicates we have the whole thing.
1229 unsigned len = strlen(ptr);
1230 if (strlen(stabbuff) + len > stabbufflen)
1232 stabbufflen += 65536;
1233 stabbuff = HeapReAlloc(GetProcessHeap(), 0, stabbuff, stabbufflen);
1235 strncat(stabbuff, ptr, len - 1);
1238 else if (stabbuff[0] != '\0')
1240 strcat(stabbuff, ptr);
1244 /* only symbol entries contain a typedef */
1245 switch (stab_ptr->n_type)
1254 if (strchr(ptr, '=') != NULL)
1257 * The stabs aren't in writable memory, so copy it over so we are
1258 * sure we can scribble on it.
1260 if (ptr != stabbuff)
1262 strcpy(stabbuff, ptr);
1265 stab_strcpy(symname, sizeof(symname), ptr);
1266 if (!stabs_parse_typedef(module, ptr, symname))
1268 /* skip this definition */
1275 switch (stab_ptr->n_type)
1279 * These are useless with ELF. They have no value, and you have to
1280 * read the normal symbol table to get the address. Thus we
1281 * ignore them, and when we process the normal symbol table
1282 * we should do the right thing.
1284 * With a.out or mingw, they actually do make some amount of sense.
1286 stab_strcpy(symname, sizeof(symname), ptr);
1287 symt_new_global_variable(module, compiland, symname, TRUE /* FIXME */,
1288 load_offset + stab_ptr->n_value, 0,
1289 stabs_parse_type(ptr));
1293 /* These are static symbols and BSS symbols. */
1294 stab_strcpy(symname, sizeof(symname), ptr);
1295 symt_new_global_variable(module, compiland, symname, TRUE /* FIXME */,
1296 load_offset + stab_ptr->n_value, 0,
1297 stabs_parse_type(ptr));
1302 block = symt_open_func_block(module, curr_func, block,
1303 stab_ptr->n_value, 0);
1304 pending_flush(&pending, module, curr_func, block);
1309 block = symt_close_func_block(module, curr_func, block,
1313 /* These are function parameters. */
1314 if (curr_func != NULL)
1316 struct symt* param_type = stabs_parse_type(ptr);
1317 stab_strcpy(symname, sizeof(symname), ptr);
1318 loc.kind = loc_regrel;
1319 loc.reg = 0; /* FIXME */
1320 loc.offset = stab_ptr->n_value;
1321 symt_add_func_local(module, curr_func,
1322 (long)stab_ptr->n_value >= 0 ? DataIsParam : DataIsLocal,
1323 &loc, NULL, param_type, symname);
1324 symt_add_function_signature_parameter(module,
1325 (struct symt_function_signature*)curr_func->type,
1330 /* These are registers (as local variables) */
1331 if (curr_func != NULL)
1333 loc.kind = loc_register;
1336 switch (stab_ptr->n_value)
1338 case 0: loc.reg = CV_REG_EAX; break;
1339 case 1: loc.reg = CV_REG_ECX; break;
1340 case 2: loc.reg = CV_REG_EDX; break;
1341 case 3: loc.reg = CV_REG_EBX; break;
1342 case 4: loc.reg = CV_REG_ESP; break;
1343 case 5: loc.reg = CV_REG_EBP; break;
1344 case 6: loc.reg = CV_REG_ESI; break;
1345 case 7: loc.reg = CV_REG_EDI; break;
1354 case 19: loc.reg = CV_REG_ST0 + stab_ptr->n_value - 12; break;
1356 FIXME("Unknown register value (%lu)\n", stab_ptr->n_value);
1357 loc.reg = CV_REG_NONE;
1360 stab_strcpy(symname, sizeof(symname), ptr);
1361 if (ptr[strlen(symname) + 1] == 'P')
1363 struct symt* param_type = stabs_parse_type(ptr);
1364 stab_strcpy(symname, sizeof(symname), ptr);
1365 symt_add_func_local(module, curr_func, DataIsParam, &loc,
1366 NULL, param_type, symname);
1367 symt_add_function_signature_parameter(module,
1368 (struct symt_function_signature*)curr_func->type,
1372 pending_add(&pending, ptr, DataIsLocal, &loc);
1376 /* These are local variables */
1377 loc.kind = loc_regrel;
1378 loc.reg = 0; /* FIXME */
1379 loc.offset = stab_ptr->n_value;
1380 if (curr_func != NULL) pending_add(&pending, ptr, DataIsLocal, &loc);
1384 * This is a line number. These are always relative to the start
1385 * of the function (N_FUN), and this makes the lookup easier.
1387 if (curr_func != NULL)
1389 assert(source_idx >= 0);
1390 symt_add_func_line(module, curr_func, source_idx,
1391 stab_ptr->n_desc, stab_ptr->n_value);
1396 * For now, just declare the various functions. Later
1397 * on, we will add the line number information and the
1401 * Copy the string to a temp buffer so we
1402 * can kill everything after the ':'. We do
1403 * it this way because otherwise we end up dirtying
1404 * all of the pages related to the stabs, and that
1405 * sucks up swap space like crazy.
1407 stab_strcpy(symname, sizeof(symname), ptr);
1410 struct symt_function_signature* func_type;
1414 /* First, clean up the previous function we were working on.
1415 * Assume size of the func is the delta between current offset
1416 * and offset of last function
1418 stabs_finalize_function(module, curr_func,
1420 (load_offset + stab_ptr->n_value - curr_func->address) : 0);
1422 func_type = symt_new_function_signature(module,
1423 stabs_parse_type(ptr), -1);
1424 curr_func = symt_new_function(module, compiland, symname,
1425 load_offset + stab_ptr->n_value, 0,
1430 /* some versions of GCC to use a N_FUN "" to mark the end of a function
1431 * and n_value contains the size of the func
1433 stabs_finalize_function(module, curr_func, stab_ptr->n_value);
1439 * This indicates a new source file. Append the records
1440 * together, to build the correct path name.
1442 if (*ptr == '\0') /* end of N_SO file */
1444 /* Nuke old path. */
1446 stabs_finalize_function(module, curr_func, 0);
1450 assert(block == NULL);
1455 int len = strlen(ptr);
1456 if (ptr[len-1] != '/')
1458 stabs_reset_includes();
1459 source_idx = source_new(module, srcpath, ptr);
1460 compiland = symt_new_compiland(module, 0 /* FIXME */, source_idx);
1463 strcpy(srcpath, ptr);
1467 source_idx = source_new(module, srcpath, ptr);
1471 strtabinc = stab_ptr->n_value;
1472 /* I'm not sure this is needed, so trace it before we obsolete it */
1475 FIXME("UNDF: curr_func %s\n", curr_func->hash_elt.name);
1476 stabs_finalize_function(module, curr_func, 0); /* FIXME */
1481 /* Ignore this. We don't care what it points to. */
1484 stabs_add_include(stabs_new_include(ptr, stab_ptr->n_value));
1485 assert(incl_stk < (int)(sizeof(incl) / sizeof(incl[0])) - 1);
1486 incl[++incl_stk] = source_idx;
1487 source_idx = source_new(module, NULL, ptr);
1490 assert(incl_stk >= 0);
1491 source_idx = incl[incl_stk--];
1494 if (stabs_add_include(stabs_find_include(ptr, stab_ptr->n_value)) < 0)
1496 ERR("Excluded header not found (%s,%ld)\n", ptr, stab_ptr->n_value);
1497 module_reset_debug_info(module);
1503 /* Always ignore these. GCC doesn't even generate them. */
1507 /* Always ignore these, they seem to be used only on Darwin. */
1510 ERR("Unknown stab type 0x%02x\n", stab_ptr->n_type);
1514 TRACE("0x%02x %lx %s\n",
1515 stab_ptr->n_type, stab_ptr->n_value, debugstr_a(strs + stab_ptr->n_un.n_strx));
1517 module->module.SymType = SymDia;
1518 module->module.CVSig = 'S' | ('T' << 8) | ('A' << 16) | ('B' << 24);
1519 /* FIXME: we could have a finer grain here */
1520 module->module.LineNumbers = TRUE;
1521 module->module.GlobalSymbols = TRUE;
1522 module->module.TypeInfo = TRUE;
1523 module->module.SourceIndexed = TRUE;
1524 module->module.Publics = TRUE;
1526 HeapFree(GetProcessHeap(), 0, stabbuff);
1527 stabs_free_includes();
1528 HeapFree(GetProcessHeap(), 0, pending.vars);