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
51 #define PATH_MAX MAX_PATH
56 #ifdef HAVE_MACH_O_NLIST_H
57 # include <mach-o/nlist.h>
64 #include "dbghelp_private.h"
66 #include "wine/debug.h"
68 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp_stabs
);
70 /* Masks for n_type field */
81 /* Values for (n_type & N_TYPE) */
116 struct stab_nlist
* n_next
;
119 unsigned char n_type
;
122 unsigned long n_value
;
125 static void stab_strcpy(char* dest
, int sz
, const char* source
)
129 * A strcpy routine that stops when we hit the ':' character.
130 * Faster than copying the whole thing, and then nuking the
132 * Takes also care of (valid) a::b constructs
134 while (*source
!= '\0')
136 if (source
[0] != ':' && sz
-- > 0) *ptr
++ = *source
++;
137 else if (source
[1] == ':' && (sz
-= 2) > 0)
145 /* GCC emits, in some cases, a .<digit>+ suffix.
146 * This is used for static variable inside functions, so
147 * that we can have several such variables with same name in
148 * the same compilation unit
149 * We simply ignore that suffix when present (we also get rid
150 * of it in ELF symtab parsing)
152 if (ptr
>= dest
&& isdigit(*ptr
))
154 while (ptr
> dest
&& isdigit(*ptr
)) ptr
--;
155 if (*ptr
== '.') *ptr
= '\0';
164 struct symt
** vector
;
168 #define MAX_INCLUDES 5120
170 static include_def
* include_defs
= NULL
;
171 static int num_include_def
= 0;
172 static int num_alloc_include_def
= 0;
173 static int cu_include_stack
[MAX_INCLUDES
];
174 static int cu_include_stk_idx
= 0;
175 static struct symt
** cu_vector
= NULL
;
176 static int cu_nrofentries
= 0;
177 static struct symt_basic
* stabs_basic
[36];
179 static int stabs_new_include(const char* file
, unsigned long val
)
181 if (num_include_def
== num_alloc_include_def
)
183 num_alloc_include_def
+= 256;
185 include_defs
= HeapAlloc(GetProcessHeap(), 0,
186 sizeof(include_defs
[0]) * num_alloc_include_def
);
188 include_defs
= HeapReAlloc(GetProcessHeap(), 0, include_defs
,
189 sizeof(include_defs
[0]) * num_alloc_include_def
);
190 memset(include_defs
+ num_include_def
, 0, sizeof(include_defs
[0]) * 256);
192 include_defs
[num_include_def
].name
= strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(file
) + 1), file
);
193 include_defs
[num_include_def
].value
= val
;
194 include_defs
[num_include_def
].vector
= NULL
;
195 include_defs
[num_include_def
].nrofentries
= 0;
197 return num_include_def
++;
200 static int stabs_find_include(const char* file
, unsigned long val
)
204 for (i
= 0; i
< num_include_def
; i
++)
206 if (val
== include_defs
[i
].value
&&
207 strcmp(file
, include_defs
[i
].name
) == 0)
213 static int stabs_add_include(int idx
)
215 if (idx
< 0) return -1;
216 cu_include_stk_idx
++;
218 /* if this happens, just bump MAX_INCLUDES */
219 /* we could also handle this as another dynarray */
220 assert(cu_include_stk_idx
< MAX_INCLUDES
);
221 cu_include_stack
[cu_include_stk_idx
] = idx
;
222 return cu_include_stk_idx
;
225 static void stabs_reset_includes(void)
228 * The struct symt:s that we would need to use are reset when
229 * we start a new file. (at least the ones in filenr == 0)
231 cu_include_stk_idx
= 0;/* keep 0 as index for the .c file itself */
232 memset(cu_vector
, 0, sizeof(cu_vector
[0]) * cu_nrofentries
);
235 static void stabs_free_includes(void)
239 stabs_reset_includes();
240 for (i
= 0; i
< num_include_def
; i
++)
242 HeapFree(GetProcessHeap(), 0, include_defs
[i
].name
);
243 HeapFree(GetProcessHeap(), 0, include_defs
[i
].vector
);
245 HeapFree(GetProcessHeap(), 0, include_defs
);
248 num_alloc_include_def
= 0;
249 HeapFree(GetProcessHeap(), 0, cu_vector
);
254 static struct symt
** stabs_find_ref(long filenr
, long subnr
)
258 /* FIXME: I could perhaps create a dummy include_def for each compilation
259 * unit which would allow not to handle those two cases separately
263 if (cu_nrofentries
<= subnr
)
266 cu_vector
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
267 sizeof(cu_vector
[0]) * (subnr
+1));
269 cu_vector
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
270 cu_vector
, sizeof(cu_vector
[0]) * (subnr
+1));
271 cu_nrofentries
= subnr
+ 1;
273 ret
= &cu_vector
[subnr
];
279 assert(filenr
<= cu_include_stk_idx
);
280 idef
= &include_defs
[cu_include_stack
[filenr
]];
282 if (idef
->nrofentries
<= subnr
)
285 idef
->vector
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
286 sizeof(idef
->vector
[0]) * (subnr
+1));
288 idef
->vector
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
289 idef
->vector
, sizeof(idef
->vector
[0]) * (subnr
+1));
290 idef
->nrofentries
= subnr
+ 1;
292 ret
= &idef
->vector
[subnr
];
294 TRACE("(%ld,%ld) => %p (%p)\n", filenr
, subnr
, ret
, *ret
);
298 static struct symt
** stabs_read_type_enum(const char** x
)
308 filenr
= strtol(iter
, &end
, 10); /* <int> */
309 iter
= ++end
; /* ',' */
310 subnr
= strtol(iter
, &end
, 10); /* <int> */
311 iter
= ++end
; /* ')' */
316 subnr
= strtol(iter
, &end
, 10); /* <int> */
320 return stabs_find_ref(filenr
, subnr
);
324 struct ParseTypedefData
329 struct module
* module
;
341 static void stabs_pts_push(struct ParseTypedefData
* ptd
, unsigned line
)
343 assert(ptd
->err_idx
< sizeof(ptd
->errors
) / sizeof(ptd
->errors
[0]));
344 ptd
->errors
[ptd
->err_idx
].line
= line
;
345 ptd
->errors
[ptd
->err_idx
].ptr
= ptd
->ptr
;
348 #define PTS_ABORTIF(ptd, t) do { if (t) { stabs_pts_push((ptd), __LINE__); return -1;} } while (0)
350 #define PTS_ABORTIF(ptd, t) do { if (t) return -1; } while (0)
353 static int stabs_get_basic(struct ParseTypedefData
* ptd
, unsigned basic
, struct symt
** symt
)
355 PTS_ABORTIF(ptd
, basic
>= sizeof(stabs_basic
) / sizeof(stabs_basic
[0]));
357 if (!stabs_basic
[basic
])
361 case 1: stabs_basic
[basic
] = symt_new_basic(ptd
->module
, btInt
, "int", 4); break;
362 case 2: stabs_basic
[basic
] = symt_new_basic(ptd
->module
, btChar
, "char", 1); break;
363 case 3: stabs_basic
[basic
] = symt_new_basic(ptd
->module
, btInt
, "short int", 2); break;
364 case 4: stabs_basic
[basic
] = symt_new_basic(ptd
->module
, btInt
, "long int", 4); break;
365 case 5: stabs_basic
[basic
] = symt_new_basic(ptd
->module
, btUInt
, "unsigned char", 1); break;
366 case 6: stabs_basic
[basic
] = symt_new_basic(ptd
->module
, btInt
, "signed char", 1); break;
367 case 7: stabs_basic
[basic
] = symt_new_basic(ptd
->module
, btUInt
, "unsigned short int", 2); break;
368 case 8: stabs_basic
[basic
] = symt_new_basic(ptd
->module
, btUInt
, "unsigned int", 4); break;
369 case 9: stabs_basic
[basic
] = symt_new_basic(ptd
->module
, btUInt
, "unsigned", 2); break;
370 case 10: stabs_basic
[basic
] = symt_new_basic(ptd
->module
, btUInt
, "unsigned long int", 2); break;
371 case 11: stabs_basic
[basic
] = symt_new_basic(ptd
->module
, btVoid
, "void", 0); break;
372 case 12: stabs_basic
[basic
] = symt_new_basic(ptd
->module
, btFloat
, "float", 4); break;
373 case 13: stabs_basic
[basic
] = symt_new_basic(ptd
->module
, btFloat
, "double", 8); break;
374 case 14: stabs_basic
[basic
] = symt_new_basic(ptd
->module
, btFloat
, "long double", 12); break;
375 case 15: stabs_basic
[basic
] = symt_new_basic(ptd
->module
, btInt
, "integer", 4); break;
376 case 16: stabs_basic
[basic
] = symt_new_basic(ptd
->module
, btBool
, "bool", 1); break;
377 /* case 17: short real */
379 case 25: stabs_basic
[basic
] = symt_new_basic(ptd
->module
, btComplex
, "float complex", 8); break;
380 case 26: stabs_basic
[basic
] = symt_new_basic(ptd
->module
, btComplex
, "double complex", 16); break;
381 case 30: stabs_basic
[basic
] = symt_new_basic(ptd
->module
, btWChar
, "wchar_t", 2); break;
382 case 31: stabs_basic
[basic
] = symt_new_basic(ptd
->module
, btInt
, "long long int", 8); break;
383 case 32: stabs_basic
[basic
] = symt_new_basic(ptd
->module
, btUInt
, "long long unsigned", 8); break;
384 /* starting at 35 are wine extensions (especially for R implementation) */
385 case 35: stabs_basic
[basic
] = symt_new_basic(ptd
->module
, btComplex
, "long double complex", 24); break;
386 default: PTS_ABORTIF(ptd
, 1);
389 *symt
= &stabs_basic
[basic
]->symt
;
393 static int stabs_pts_read_type_def(struct ParseTypedefData
* ptd
,
394 const char* typename
, struct symt
** dt
);
396 static int stabs_pts_read_id(struct ParseTypedefData
* ptd
)
398 const char* first
= ptd
->ptr
;
399 unsigned int template = 0;
402 while ((ch
= *ptd
->ptr
++) != '\0')
409 unsigned int len
= ptd
->ptr
- first
- 1;
410 PTS_ABORTIF(ptd
, len
>= sizeof(ptd
->buf
) - ptd
->idx
);
411 memcpy(ptd
->buf
+ ptd
->idx
, first
, len
);
412 ptd
->buf
[ptd
->idx
+ len
] = '\0';
417 case '<': template++; break;
418 case '>': PTS_ABORTIF(ptd
, template == 0); template--; break;
424 static int stabs_pts_read_number(struct ParseTypedefData
* ptd
, long* v
)
428 *v
= strtol(ptd
->ptr
, &last
, 10);
429 PTS_ABORTIF(ptd
, last
== ptd
->ptr
);
434 static int stabs_pts_read_type_reference(struct ParseTypedefData
* ptd
,
435 long* filenr
, long* subnr
)
437 if (*ptd
->ptr
== '(')
439 /* '(' <int> ',' <int> ')' */
441 PTS_ABORTIF(ptd
, stabs_pts_read_number(ptd
, filenr
) == -1);
442 PTS_ABORTIF(ptd
, *ptd
->ptr
++ != ',');
443 PTS_ABORTIF(ptd
, stabs_pts_read_number(ptd
, subnr
) == -1);
444 PTS_ABORTIF(ptd
, *ptd
->ptr
++ != ')');
449 PTS_ABORTIF(ptd
, stabs_pts_read_number(ptd
, subnr
) == -1);
454 struct pts_range_value
460 static int stabs_pts_read_range_value(struct ParseTypedefData
* ptd
, struct pts_range_value
* prv
)
467 while (*ptd
->ptr
== '0') ptd
->ptr
++;
468 if (*ptd
->ptr
>= '1' && *ptd
->ptr
<= '7')
473 PTS_ABORTIF(ptd
, ptd
->ptr
[0] != '1');
476 while (isdigit(*ptd
->ptr
)) prv
->val
= (prv
->val
<< 3) + *ptd
->ptr
++ - '0';
481 while (isdigit(*ptd
->ptr
)) prv
->val
= (prv
->val
<< 3) + *ptd
->ptr
++ - '0';
483 default: PTS_ABORTIF(ptd
, 1); break;
485 } else prv
->sign
= 0;
489 prv
->val
= strtoull(++ptd
->ptr
, &last
, 10);
495 prv
->val
= strtoull(ptd
->ptr
, &last
, 10);
502 static int stabs_pts_read_range(struct ParseTypedefData
* ptd
, const char* typename
,
506 struct pts_range_value lo
;
507 struct pts_range_value hi
;
513 /* type ';' <int> ';' <int> ';' */
514 PTS_ABORTIF(ptd
, stabs_pts_read_type_def(ptd
, NULL
, &ref
) == -1);
515 PTS_ABORTIF(ptd
, *ptd
->ptr
++ != ';'); /* ';' */
516 PTS_ABORTIF(ptd
, stabs_pts_read_range_value(ptd
, &lo
) == -1);
517 PTS_ABORTIF(ptd
, *ptd
->ptr
++ != ';'); /* ';' */
518 PTS_ABORTIF(ptd
, stabs_pts_read_range_value(ptd
, &hi
) == -1);
519 PTS_ABORTIF(ptd
, *ptd
->ptr
++ != ';'); /* ';' */
521 /* basically, we don't use ref... in some cases, for example, float is declared
522 * as a derived type of int... which won't help us... so we guess the types
523 * from the various formats
525 if (lo
.sign
== 0 && hi
.sign
< 0)
530 else if (lo
.sign
< 0 && hi
.sign
== 0)
535 else if (lo
.sign
> 0 && hi
.sign
== 0)
540 else if (lo
.sign
< 0 && hi
.sign
> 0)
543 for (i
= 7; i
< 64; i
+= 8)
545 if (lo
.val
== v
&& hi
.val
== v
- 1)
553 PTS_ABORTIF(ptd
, i
>= 64);
555 else if (lo
.sign
== 0 && hi
.sign
> 0)
557 if (hi
.val
== 127) /* specific case for char... */
565 for (i
= 8; i
<= 64; i
+= 8)
575 PTS_ABORTIF(ptd
, i
> 64);
578 else PTS_ABORTIF(ptd
, 1);
580 *dt
= &symt_new_basic(ptd
->module
, bt
, typename
, size
)->symt
;
584 static inline int stabs_pts_read_method_info(struct ParseTypedefData
* ptd
)
592 /* get type of return value */
593 PTS_ABORTIF(ptd
, stabs_pts_read_type_def(ptd
, NULL
, &dt
) == -1);
594 if (*ptd
->ptr
== ';') ptd
->ptr
++;
596 /* get types of parameters */
597 if (*ptd
->ptr
== ':')
599 PTS_ABORTIF(ptd
, !(tmp
= strchr(ptd
->ptr
+ 1, ';')));
602 PTS_ABORTIF(ptd
, !(*ptd
->ptr
>= '0' && *ptd
->ptr
<= '9'));
604 PTS_ABORTIF(ptd
, !(ptd
->ptr
[0] >= 'A' && *ptd
->ptr
<= 'D'));
606 PTS_ABORTIF(ptd
, mthd
!= '.' && mthd
!= '?' && mthd
!= '*');
613 PTS_ABORTIF(ptd
, stabs_pts_read_number(ptd
, &ofs
) == -1);
614 PTS_ABORTIF(ptd
, *ptd
->ptr
++ != ';');
615 PTS_ABORTIF(ptd
, stabs_pts_read_type_def(ptd
, NULL
, &dt
) == -1);
616 PTS_ABORTIF(ptd
, *ptd
->ptr
++ != ';');
618 } while (*ptd
->ptr
!= ';');
624 static inline int stabs_pts_read_aggregate(struct ParseTypedefData
* ptd
,
625 struct symt_udt
* sdt
)
629 struct symt
* dt
= NULL
;
633 PTS_ABORTIF(ptd
, stabs_pts_read_number(ptd
, &sz
) == -1);
635 doadd
= symt_set_udt_size(ptd
->module
, sdt
, sz
);
636 if (*ptd
->ptr
== '!') /* C++ inheritence */
641 PTS_ABORTIF(ptd
, stabs_pts_read_number(ptd
, &num_classes
) == -1);
642 PTS_ABORTIF(ptd
, *ptd
->ptr
++ != ',');
643 while (--num_classes
>= 0)
645 ptd
->ptr
+= 2; /* skip visibility and inheritence */
646 PTS_ABORTIF(ptd
, stabs_pts_read_number(ptd
, &ofs
) == -1);
647 PTS_ABORTIF(ptd
, *ptd
->ptr
++ != ',');
649 PTS_ABORTIF(ptd
, stabs_pts_read_type_def(ptd
, NULL
, &adt
) == -1);
657 symt_get_info(adt
, TI_GET_SYMNAME
, &name
);
658 strcpy(tmp
, "__inherited_class_");
659 WideCharToMultiByte(CP_ACP
, 0, name
, -1,
660 tmp
+ strlen(tmp
), sizeof(tmp
) - strlen(tmp
),
662 HeapFree(GetProcessHeap(), 0, name
);
663 /* FIXME: TI_GET_LENGTH will not always work, especially when adt
664 * has just been seen as a forward definition and not the real stuff
666 * As we don't use much the size of members in structs, this may not
667 * be much of a problem
669 symt_get_info(adt
, TI_GET_LENGTH
, &size
);
670 symt_add_udt_element(ptd
->module
, sdt
, tmp
, adt
, ofs
, (DWORD
)size
* 8);
672 PTS_ABORTIF(ptd
, *ptd
->ptr
++ != ';');
676 /* if the structure has already been filled, just redo the parsing
677 * but don't store results into the struct
678 * FIXME: there's a quite ugly memory leak in there...
681 /* Now parse the individual elements of the structure/union. */
682 while (*ptd
->ptr
!= ';')
684 /* agg_name : type ',' <int:offset> ',' <int:size> */
687 if (ptd
->ptr
[0] == '$' && ptd
->ptr
[1] == 'v')
691 if (ptd
->ptr
[2] == 'f')
693 /* C++ virtual method table */
695 stabs_read_type_enum(&ptd
->ptr
);
696 PTS_ABORTIF(ptd
, *ptd
->ptr
++ != ':');
697 PTS_ABORTIF(ptd
, stabs_pts_read_type_def(ptd
, NULL
, &dt
) == -1);
698 PTS_ABORTIF(ptd
, *ptd
->ptr
++ != ',');
699 PTS_ABORTIF(ptd
, stabs_pts_read_number(ptd
, &x
) == -1);
700 PTS_ABORTIF(ptd
, *ptd
->ptr
++ != ';');
704 else if (ptd
->ptr
[2] == 'b')
707 PTS_ABORTIF(ptd
, stabs_pts_read_type_def(ptd
, NULL
, &dt
) == -1);
708 PTS_ABORTIF(ptd
, *ptd
->ptr
++ != ':');
709 PTS_ABORTIF(ptd
, stabs_pts_read_type_def(ptd
, NULL
, &dt
) == -1);
710 PTS_ABORTIF(ptd
, *ptd
->ptr
++ != ',');
711 PTS_ABORTIF(ptd
, stabs_pts_read_number(ptd
, &x
) == -1);
712 PTS_ABORTIF(ptd
, *ptd
->ptr
++ != ';');
718 PTS_ABORTIF(ptd
, stabs_pts_read_id(ptd
) == -1);
719 /* Ref. TSDF R2.130 Section 7.4. When the field name is a method name
720 * it is followed by two colons rather than one.
722 if (*ptd
->ptr
== ':')
725 stabs_pts_read_method_info(ptd
);
731 /* skip C++ member protection /0 /1 or /2 */
732 if (*ptd
->ptr
== '/') ptd
->ptr
+= 2;
734 PTS_ABORTIF(ptd
, stabs_pts_read_type_def(ptd
, NULL
, &adt
) == -1);
739 PTS_ABORTIF(ptd
, stabs_pts_read_number(ptd
, &ofs
) == -1);
740 PTS_ABORTIF(ptd
, *ptd
->ptr
++ != ',');
741 PTS_ABORTIF(ptd
, stabs_pts_read_number(ptd
, &sz
) == -1);
742 PTS_ABORTIF(ptd
, *ptd
->ptr
++ != ';');
744 if (doadd
) symt_add_udt_element(ptd
->module
, sdt
, ptd
->buf
+ idx
, adt
, ofs
, sz
);
749 /* method parameters... terminated by ';' */
750 PTS_ABORTIF(ptd
, !(tmp
= strchr(ptd
->ptr
, ';')));
755 PTS_ABORTIF(ptd
, TRUE
);
759 PTS_ABORTIF(ptd
, *ptd
->ptr
++ != ';');
760 if (*ptd
->ptr
== '~')
763 PTS_ABORTIF(ptd
, *ptd
->ptr
++ != '%');
764 PTS_ABORTIF(ptd
, stabs_pts_read_type_def(ptd
, NULL
, &dt
) == -1);
765 PTS_ABORTIF(ptd
, *ptd
->ptr
++ != ';');
770 static inline int stabs_pts_read_enum(struct ParseTypedefData
* ptd
,
771 struct symt_enum
* edt
)
776 while (*ptd
->ptr
!= ';')
779 PTS_ABORTIF(ptd
, stabs_pts_read_id(ptd
) == -1);
780 PTS_ABORTIF(ptd
, stabs_pts_read_number(ptd
, &value
) == -1);
781 PTS_ABORTIF(ptd
, *ptd
->ptr
++ != ',');
782 symt_add_enum_element(ptd
->module
, edt
, ptd
->buf
+ idx
, value
);
789 static inline int stabs_pts_read_array(struct ParseTypedefData
* ptd
,
793 struct symt
* range_dt
;
794 struct symt
* base_dt
;
796 /* ar<typeinfo_nodef>;<int>;<int>;<typeinfo> */
798 PTS_ABORTIF(ptd
, *ptd
->ptr
++ != 'r');
800 PTS_ABORTIF(ptd
, stabs_pts_read_type_def(ptd
, NULL
, &range_dt
) == -1);
801 PTS_ABORTIF(ptd
, *ptd
->ptr
++ != ';'); /* ';' */
802 PTS_ABORTIF(ptd
, stabs_pts_read_number(ptd
, &lo
) == -1);
803 PTS_ABORTIF(ptd
, *ptd
->ptr
++ != ';'); /* ';' */
804 PTS_ABORTIF(ptd
, stabs_pts_read_number(ptd
, &hi
) == -1);
805 PTS_ABORTIF(ptd
, *ptd
->ptr
++ != ';'); /* ';' */
807 PTS_ABORTIF(ptd
, stabs_pts_read_type_def(ptd
, NULL
, &base_dt
) == -1);
809 *adt
= &symt_new_array(ptd
->module
, lo
, hi
, base_dt
, range_dt
)->symt
;
813 static int stabs_pts_read_type_def(struct ParseTypedefData
* ptd
, const char* typename
,
814 struct symt
** ret_dt
)
818 struct symt
* new_dt
= NULL
; /* newly created data type */
819 struct symt
* ref_dt
; /* referenced data type (pointer...) */
820 long filenr1
, subnr1
, tmp
;
822 /* things are a bit complicated because of the way the typedefs are stored inside
823 * the file, because addresses can change when realloc is done, so we must call
824 * over and over stabs_find_ref() to keep the correct values around
826 PTS_ABORTIF(ptd
, stabs_pts_read_type_reference(ptd
, &filenr1
, &subnr1
) == -1);
828 while (*ptd
->ptr
== '=')
831 PTS_ABORTIF(ptd
, new_dt
!= NULL
);
833 /* first handle attribute if any */
837 if (*++ptd
->ptr
== 's')
840 if (stabs_pts_read_number(ptd
, &sz
) == -1)
842 ERR("Not an attribute... NIY\n");
846 PTS_ABORTIF(ptd
, *ptd
->ptr
++ != ';');
850 /* then the real definitions */
855 PTS_ABORTIF(ptd
, stabs_pts_read_type_def(ptd
, NULL
, &ref_dt
) == -1);
856 new_dt
= &symt_new_pointer(ptd
->module
, ref_dt
)->symt
;
858 case 'k': /* 'const' modifier */
859 case 'B': /* 'volatile' modifier */
860 /* just kinda ignore the modifier, I guess -gmt */
861 PTS_ABORTIF(ptd
, stabs_pts_read_type_def(ptd
, typename
, &new_dt
) == -1);
865 PTS_ABORTIF(ptd
, stabs_pts_read_type_def(ptd
, typename
, &new_dt
) == -1);
868 PTS_ABORTIF(ptd
, stabs_pts_read_array(ptd
, &new_dt
) == -1);
871 PTS_ABORTIF(ptd
, stabs_pts_read_range(ptd
, typename
, &new_dt
) == -1);
872 assert(!*stabs_find_ref(filenr1
, subnr1
));
873 *stabs_find_ref(filenr1
, subnr1
) = new_dt
;
876 PTS_ABORTIF(ptd
, stabs_pts_read_type_def(ptd
, NULL
, &ref_dt
) == -1);
877 new_dt
= &symt_new_function_signature(ptd
->module
, ref_dt
, -1)->symt
;
880 stabs_get_basic(ptd
, 1 /* int */, &ref_dt
);
881 new_dt
= &symt_new_enum(ptd
->module
, typename
, ref_dt
)->symt
;
882 PTS_ABORTIF(ptd
, stabs_pts_read_enum(ptd
, (struct symt_enum
*)new_dt
) == -1);
887 struct symt_udt
* udt
;
888 enum UdtKind kind
= (ptd
->ptr
[-1] == 's') ? UdtStruct
: UdtUnion
;
889 /* udt can have been already defined in a forward definition */
890 udt
= (struct symt_udt
*)*stabs_find_ref(filenr1
, subnr1
);
893 udt
= symt_new_udt(ptd
->module
, typename
, 0, kind
);
894 /* we need to set it here, because a struct can hold a pointer
897 new_dt
= *stabs_find_ref(filenr1
, subnr1
) = &udt
->symt
;
902 if (udt
->symt
.tag
!= SymTagUDT
)
904 ERR("Forward declaration (%p/%s) is not an aggregate (%u)\n",
905 udt
, symt_get_name(&udt
->symt
), udt
->symt
.tag
);
908 /* FIXME: we currently don't correctly construct nested C++
909 * classes names. Therefore, we could be here with either:
910 * - typename and udt->hash_elt.name being the same string
911 * (non embedded case)
912 * - typename being foo::bar while udt->hash_elt.name being
914 * So, we twist the comparison to test both occurrences. When
915 * we have proper C++ types in this file, this twist has to be
918 l1
= strlen(udt
->hash_elt
.name
);
919 l2
= strlen(typename
);
920 if (l1
> l2
|| strcmp(udt
->hash_elt
.name
, typename
+ l2
- l1
))
921 ERR("Forward declaration name mismatch %s <> %s\n",
922 udt
->hash_elt
.name
, typename
);
925 PTS_ABORTIF(ptd
, stabs_pts_read_aggregate(ptd
, udt
) == -1);
931 PTS_ABORTIF(ptd
, stabs_pts_read_id(ptd
) == -1);
935 stabs_get_basic(ptd
, 1 /* int */, &ref_dt
);
936 new_dt
= &symt_new_enum(ptd
->module
, ptd
->buf
+ idx
, ref_dt
)->symt
;
939 new_dt
= &symt_new_udt(ptd
->module
, ptd
->buf
+ idx
, 0, UdtStruct
)->symt
;
942 new_dt
= &symt_new_udt(ptd
->module
, ptd
->buf
+ idx
, 0, UdtUnion
)->symt
;
951 PTS_ABORTIF(ptd
, stabs_pts_read_number(ptd
, &tmp
) == -1);
952 PTS_ABORTIF(ptd
, stabs_get_basic(ptd
, tmp
, &new_dt
) == -1);
953 PTS_ABORTIF(ptd
, *ptd
->ptr
++ != ';');
957 if (*ptd
->ptr
== '#')
960 PTS_ABORTIF(ptd
, stabs_pts_read_type_def(ptd
, NULL
, &ref_dt
) == -1);
961 new_dt
= &symt_new_function_signature(ptd
->module
, ref_dt
, -1)->symt
;
968 PTS_ABORTIF(ptd
, stabs_pts_read_type_def(ptd
, NULL
, &cls_dt
) == -1);
969 PTS_ABORTIF(ptd
, *ptd
->ptr
++ != ',');
970 PTS_ABORTIF(ptd
, stabs_pts_read_type_def(ptd
, NULL
, &ref_dt
) == -1);
971 new_dt
= &symt_new_function_signature(ptd
->module
, ref_dt
, -1)->symt
;
972 while (*ptd
->ptr
== ',')
975 PTS_ABORTIF(ptd
, stabs_pts_read_type_def(ptd
, NULL
, &pmt_dt
) == -1);
984 PTS_ABORTIF(ptd
, stabs_pts_read_number(ptd
, &type
) == -1);
985 PTS_ABORTIF(ptd
, *ptd
->ptr
++ != ';'); /* ';' */
986 PTS_ABORTIF(ptd
, stabs_pts_read_number(ptd
, &len
) == -1);
987 PTS_ABORTIF(ptd
, *ptd
->ptr
++ != ';'); /* ';' */
988 PTS_ABORTIF(ptd
, stabs_pts_read_number(ptd
, &unk
) == -1);
989 PTS_ABORTIF(ptd
, *ptd
->ptr
++ != ';'); /* ';' */
991 switch (type
) /* see stabs_get_basic for the details */
993 case 1: basic
= 12; break;
994 case 2: basic
= 13; break;
995 case 3: basic
= 25; break;
996 case 4: basic
= 26; break;
997 case 5: basic
= 35; break;
998 case 6: basic
= 14; break;
999 default: PTS_ABORTIF(ptd
, 1);
1001 PTS_ABORTIF(ptd
, stabs_get_basic(ptd
, basic
, &new_dt
) == -1);
1005 ERR("Unknown type '%c'\n", ptd
->ptr
[-1]);
1012 /* is it a forward declaration that has been filled ? */
1013 new_dt
= *stabs_find_ref(filenr1
, subnr1
);
1014 /* if not, this should be void (which is defined as a ref to itself, but we
1015 * don't correctly catch it)
1017 if (!new_dt
&& typename
)
1019 new_dt
= &symt_new_basic(ptd
->module
, btVoid
, typename
, 0)->symt
;
1020 PTS_ABORTIF(ptd
, strcmp(typename
, "void"));
1024 *stabs_find_ref(filenr1
, subnr1
) = *ret_dt
= new_dt
;
1026 TRACE("Adding (%ld,%ld) %s\n", filenr1
, subnr1
, debugstr_a(typename
));
1031 static int stabs_parse_typedef(struct module
* module
, const char* ptr
,
1032 const char* typename
)
1034 struct ParseTypedefData ptd
;
1038 /* check for already existing definition */
1040 TRACE("%s => %s\n", typename
, debugstr_a(ptr
));
1041 ptd
.module
= module
;
1046 for (ptd
.ptr
= ptr
- 1; ;)
1048 ptd
.ptr
= strchr(ptd
.ptr
+ 1, ':');
1049 if (ptd
.ptr
== NULL
|| *++ptd
.ptr
!= ':') break;
1053 if (*ptd
.ptr
!= '(') ptd
.ptr
++;
1054 /* most of type definitions take one char, except Tt */
1055 if (*ptd
.ptr
!= '(') ptd
.ptr
++;
1056 ret
= stabs_pts_read_type_def(&ptd
, typename
, &dt
);
1059 if (ret
== -1 || *ptd
.ptr
)
1063 TRACE("Failure on %s\n", debugstr_a(ptr
));
1066 for (i
= 0; i
< ptd
.err_idx
; i
++)
1068 TRACE("[%d]: line %d => %s\n",
1069 i
, ptd
.errors
[i
].line
, debugstr_a(ptd
.errors
[i
].ptr
));
1073 TRACE("[0]: => %s\n", debugstr_a(ptd
.ptr
));
1076 ERR("Failure on %s at %s\n", debugstr_a(ptr
), debugstr_a(ptd
.ptr
));
1084 static struct symt
* stabs_parse_type(const char* stab
)
1086 const char* c
= stab
- 1;
1089 * Look through the stab definition, and figure out what struct symt
1090 * this represents. If we have something we know about, assign the
1092 * According to "The \"stabs\" debug format" (Rev 2.130) the name may be
1093 * a C++ name and contain double colons e.g. foo::bar::baz:t5=*6.
1097 if ((c
= strchr(c
+ 1, ':')) == NULL
) return NULL
;
1098 } while (*++c
== ':');
1101 * The next characters say more about the type (i.e. data, function, etc)
1102 * of symbol. Skip them. (C++ for example may have Tt).
1103 * Actually this is a very weak description; I think Tt is the only
1104 * multiple combination we should see.
1106 while (*c
&& *c
!= '(' && !isdigit(*c
))
1109 * The next is either an integer or a (integer,integer).
1110 * The stabs_read_type_enum() takes care that stab_types is large enough.
1112 return *stabs_read_type_enum(&c
);
1115 struct pending_loc_var
1120 struct location loc
;
1123 struct pending_block
1125 struct pending_loc_var
* vars
;
1130 static inline void pending_add(struct pending_block
* pending
, const char* name
,
1131 enum DataKind dt
, const struct location
* loc
)
1133 if (pending
->num
== pending
->allocated
)
1135 pending
->allocated
+= 8;
1137 pending
->vars
= HeapAlloc(GetProcessHeap(), 0,
1138 pending
->allocated
* sizeof(pending
->vars
[0]));
1140 pending
->vars
= HeapReAlloc(GetProcessHeap(), 0, pending
->vars
,
1141 pending
->allocated
* sizeof(pending
->vars
[0]));
1143 stab_strcpy(pending
->vars
[pending
->num
].name
,
1144 sizeof(pending
->vars
[pending
->num
].name
), name
);
1145 pending
->vars
[pending
->num
].type
= stabs_parse_type(name
);
1146 pending
->vars
[pending
->num
].kind
= dt
;
1147 pending
->vars
[pending
->num
].loc
= *loc
;
1151 static void pending_flush(struct pending_block
* pending
, struct module
* module
,
1152 struct symt_function
* func
, struct symt_block
* block
)
1156 for (i
= 0; i
< pending
->num
; i
++)
1158 symt_add_func_local(module
, func
,
1159 pending
->vars
[i
].kind
, &pending
->vars
[i
].loc
,
1160 block
, pending
->vars
[i
].type
, pending
->vars
[i
].name
);
1165 /******************************************************************
1166 * stabs_finalize_function
1168 * Ends function creation: mainly:
1169 * - cleans up line number information
1170 * - tries to set up a debug-start tag (FIXME: heuristic to be enhanced)
1171 * - for stabs which have absolute address in them, initializes the size of the
1172 * function (assuming that current function ends where next function starts)
1174 static void stabs_finalize_function(struct module
* module
, struct symt_function
* func
,
1178 struct location loc
;
1181 symt_normalize_function(module
, func
);
1182 /* To define the debug-start of the function, we use the second line number.
1183 * Not 100% bullet proof, but better than nothing
1185 if (symt_fill_func_line_info(module
, func
, func
->address
, &il
) &&
1186 symt_get_func_line_next(module
, &il
))
1188 loc
.kind
= loc_absolute
;
1189 loc
.offset
= il
.Address
- func
->address
;
1190 symt_add_function_point(module
, func
, SymTagFuncDebugStart
,
1193 if (size
) func
->size
= size
;
1196 BOOL
stabs_parse(struct module
* module
, unsigned long load_offset
,
1197 const void* pv_stab_ptr
, int stablen
,
1198 const char* strs
, int strtablen
,
1199 stabs_def_cb callback
, void* user
)
1201 struct symt_function
* curr_func
= NULL
;
1202 struct symt_block
* block
= NULL
;
1203 struct symt_compiland
* compiland
= NULL
;
1204 char srcpath
[PATH_MAX
]; /* path to directory source file is in */
1209 unsigned int stabbufflen
;
1210 const struct stab_nlist
* stab_ptr
= pv_stab_ptr
;
1211 const char* strs_end
;
1216 int source_idx
= -1;
1217 struct pending_block pending
;
1219 struct location loc
;
1222 nstab
= stablen
/ sizeof(struct stab_nlist
);
1223 strs_end
= strs
+ strtablen
;
1225 memset(srcpath
, 0, sizeof(srcpath
));
1226 memset(stabs_basic
, 0, sizeof(stabs_basic
));
1227 memset(&pending
, 0, sizeof(pending
));
1230 * Allocate a buffer into which we can build stab strings for cases
1231 * where the stab is continued over multiple lines.
1233 stabbufflen
= 65536;
1234 stabbuff
= HeapAlloc(GetProcessHeap(), 0, stabbufflen
);
1238 for (i
= 0; i
< nstab
; i
++, stab_ptr
++)
1240 ptr
= strs
+ stab_ptr
->n_un
.n_strx
;
1241 if ((ptr
> strs_end
) || (ptr
+ strlen(ptr
) > strs_end
))
1243 WARN("Bad stabs string %p\n", ptr
);
1246 if (*ptr
!= '\0' && (ptr
[strlen(ptr
) - 1] == '\\'))
1249 * Indicates continuation. Append this to the buffer, and go onto the
1250 * next record. Repeat the process until we find a stab without the
1251 * '/' character, as this indicates we have the whole thing.
1253 unsigned len
= strlen(ptr
);
1254 if (strlen(stabbuff
) + len
> stabbufflen
)
1256 stabbufflen
+= 65536;
1257 stabbuff
= HeapReAlloc(GetProcessHeap(), 0, stabbuff
, stabbufflen
);
1259 strncat(stabbuff
, ptr
, len
- 1);
1262 else if (stabbuff
[0] != '\0')
1264 strcat(stabbuff
, ptr
);
1268 if (stab_ptr
->n_type
& N_STAB
)
1269 type
= stab_ptr
->n_type
;
1271 type
= (stab_ptr
->n_type
& N_TYPE
);
1273 /* only symbol entries contain a typedef */
1283 if (strchr(ptr
, '=') != NULL
)
1286 * The stabs aren't in writable memory, so copy it over so we are
1287 * sure we can scribble on it.
1289 if (ptr
!= stabbuff
)
1291 strcpy(stabbuff
, ptr
);
1294 stab_strcpy(symname
, sizeof(symname
), ptr
);
1295 if (!stabs_parse_typedef(module
, ptr
, symname
))
1297 /* skip this definition */
1308 * These are useless with ELF. They have no value, and you have to
1309 * read the normal symbol table to get the address. Thus we
1310 * ignore them, and when we process the normal symbol table
1311 * we should do the right thing.
1313 * With a.out or mingw, they actually do make some amount of sense.
1315 stab_strcpy(symname
, sizeof(symname
), ptr
);
1316 symt_new_global_variable(module
, compiland
, symname
, TRUE
/* FIXME */,
1317 load_offset
+ stab_ptr
->n_value
, 0,
1318 stabs_parse_type(ptr
));
1322 /* These are static symbols and BSS symbols. */
1323 stab_strcpy(symname
, sizeof(symname
), ptr
);
1324 symt_new_global_variable(module
, compiland
, symname
, TRUE
/* FIXME */,
1325 load_offset
+ stab_ptr
->n_value
, 0,
1326 stabs_parse_type(ptr
));
1331 block
= symt_open_func_block(module
, curr_func
, block
,
1332 stab_ptr
->n_value
, 0);
1333 pending_flush(&pending
, module
, curr_func
, block
);
1338 block
= symt_close_func_block(module
, curr_func
, block
,
1342 /* These are function parameters. */
1343 if (curr_func
!= NULL
)
1345 struct symt
* param_type
= stabs_parse_type(ptr
);
1346 stab_strcpy(symname
, sizeof(symname
), ptr
);
1347 loc
.kind
= loc_regrel
;
1348 loc
.reg
= 0; /* FIXME */
1349 loc
.offset
= stab_ptr
->n_value
;
1350 symt_add_func_local(module
, curr_func
,
1351 (long)stab_ptr
->n_value
>= 0 ? DataIsParam
: DataIsLocal
,
1352 &loc
, NULL
, param_type
, symname
);
1353 symt_add_function_signature_parameter(module
,
1354 (struct symt_function_signature
*)curr_func
->type
,
1359 /* These are registers (as local variables) */
1360 if (curr_func
!= NULL
)
1362 loc
.kind
= loc_register
;
1365 switch (stab_ptr
->n_value
)
1367 case 0: loc
.reg
= CV_REG_EAX
; break;
1368 case 1: loc
.reg
= CV_REG_ECX
; break;
1369 case 2: loc
.reg
= CV_REG_EDX
; break;
1370 case 3: loc
.reg
= CV_REG_EBX
; break;
1371 case 4: loc
.reg
= CV_REG_ESP
; break;
1372 case 5: loc
.reg
= CV_REG_EBP
; break;
1373 case 6: loc
.reg
= CV_REG_ESI
; break;
1374 case 7: loc
.reg
= CV_REG_EDI
; break;
1383 case 19: loc
.reg
= CV_REG_ST0
+ stab_ptr
->n_value
- 12; break;
1391 case 28: loc
.reg
= CV_REG_XMM0
+ stab_ptr
->n_value
- 21; break;
1399 case 36: loc
.reg
= CV_REG_MM0
+ stab_ptr
->n_value
- 29; break;
1401 FIXME("Unknown register value (%lu)\n", stab_ptr
->n_value
);
1402 loc
.reg
= CV_REG_NONE
;
1405 stab_strcpy(symname
, sizeof(symname
), ptr
);
1406 if (ptr
[strlen(symname
) + 1] == 'P')
1408 struct symt
* param_type
= stabs_parse_type(ptr
);
1409 stab_strcpy(symname
, sizeof(symname
), ptr
);
1410 symt_add_func_local(module
, curr_func
, DataIsParam
, &loc
,
1411 NULL
, param_type
, symname
);
1412 symt_add_function_signature_parameter(module
,
1413 (struct symt_function_signature
*)curr_func
->type
,
1417 pending_add(&pending
, ptr
, DataIsLocal
, &loc
);
1421 /* These are local variables */
1422 loc
.kind
= loc_regrel
;
1423 loc
.reg
= 0; /* FIXME */
1424 loc
.offset
= stab_ptr
->n_value
;
1425 if (curr_func
!= NULL
) pending_add(&pending
, ptr
, DataIsLocal
, &loc
);
1429 * This is a line number. These are always relative to the start
1430 * of the function (N_FUN), and this makes the lookup easier.
1432 if (curr_func
!= NULL
)
1434 assert(source_idx
>= 0);
1435 symt_add_func_line(module
, curr_func
, source_idx
,
1436 stab_ptr
->n_desc
, stab_ptr
->n_value
);
1441 * For now, just declare the various functions. Later
1442 * on, we will add the line number information and the
1446 * Copy the string to a temp buffer so we
1447 * can kill everything after the ':'. We do
1448 * it this way because otherwise we end up dirtying
1449 * all of the pages related to the stabs, and that
1450 * sucks up swap space like crazy.
1452 stab_strcpy(symname
, sizeof(symname
), ptr
);
1455 struct symt_function_signature
* func_type
;
1459 /* First, clean up the previous function we were working on.
1460 * Assume size of the func is the delta between current offset
1461 * and offset of last function
1463 stabs_finalize_function(module
, curr_func
,
1465 (load_offset
+ stab_ptr
->n_value
- curr_func
->address
) : 0);
1467 func_type
= symt_new_function_signature(module
,
1468 stabs_parse_type(ptr
), -1);
1469 curr_func
= symt_new_function(module
, compiland
, symname
,
1470 load_offset
+ stab_ptr
->n_value
, 0,
1475 /* some versions of GCC to use a N_FUN "" to mark the end of a function
1476 * and n_value contains the size of the func
1478 stabs_finalize_function(module
, curr_func
, stab_ptr
->n_value
);
1484 * This indicates a new source file. Append the records
1485 * together, to build the correct path name.
1487 if (*ptr
== '\0') /* end of N_SO file */
1489 /* Nuke old path. */
1491 stabs_finalize_function(module
, curr_func
, 0);
1495 assert(block
== NULL
);
1500 int len
= strlen(ptr
);
1501 if (ptr
[len
-1] != '/')
1503 stabs_reset_includes();
1504 source_idx
= source_new(module
, srcpath
, ptr
);
1505 compiland
= symt_new_compiland(module
, 0 /* FIXME */, source_idx
);
1508 strcpy(srcpath
, ptr
);
1512 source_idx
= source_new(module
, srcpath
, ptr
);
1516 strtabinc
= stab_ptr
->n_value
;
1517 /* I'm not sure this is needed, so trace it before we obsolete it */
1520 FIXME("UNDF: curr_func %s\n", curr_func
->hash_elt
.name
);
1521 stabs_finalize_function(module
, curr_func
, 0); /* FIXME */
1526 /* Ignore this. We don't care what it points to. */
1529 stabs_add_include(stabs_new_include(ptr
, stab_ptr
->n_value
));
1530 assert(incl_stk
< (int)(sizeof(incl
) / sizeof(incl
[0])) - 1);
1531 incl
[++incl_stk
] = source_idx
;
1532 source_idx
= source_new(module
, NULL
, ptr
);
1535 assert(incl_stk
>= 0);
1536 source_idx
= incl
[incl_stk
--];
1539 if (stabs_add_include(stabs_find_include(ptr
, stab_ptr
->n_value
)) < 0)
1541 ERR("Excluded header not found (%s,%ld)\n", ptr
, stab_ptr
->n_value
);
1542 module_reset_debug_info(module
);
1548 /* Always ignore these. GCC doesn't even generate them. */
1553 /* Always ignore these, they seem to be used only on Darwin. */
1559 /* FIXME: Other definition types (N_TEXT, N_DATA, N_BSS, ...)? */
1562 BOOL is_public
= (stab_ptr
->n_type
& N_EXT
);
1563 BOOL is_global
= is_public
;
1566 /* "private extern"; shared among compilation units in a shared
1567 * library, but not accessible from outside the library. */
1568 if (stab_ptr
->n_type
& N_PEXT
)
1575 if (*ptr
== '_') ptr
++;
1576 stab_strcpy(symname
, sizeof(symname
), ptr
);
1578 callback(module
, load_offset
, symname
, stab_ptr
->n_value
,
1579 is_public
, is_global
, stab_ptr
->n_other
, compiland
, user
);
1583 ERR("Unknown stab type 0x%02x\n", type
);
1587 TRACE("0x%02x %lx %s\n",
1588 stab_ptr
->n_type
, stab_ptr
->n_value
, debugstr_a(strs
+ stab_ptr
->n_un
.n_strx
));
1590 module
->module
.SymType
= SymDia
;
1591 module
->module
.CVSig
= 'S' | ('T' << 8) | ('A' << 16) | ('B' << 24);
1592 /* FIXME: we could have a finer grain here */
1593 module
->module
.LineNumbers
= TRUE
;
1594 module
->module
.GlobalSymbols
= TRUE
;
1595 module
->module
.TypeInfo
= TRUE
;
1596 module
->module
.SourceIndexed
= TRUE
;
1597 module
->module
.Publics
= TRUE
;
1599 HeapFree(GetProcessHeap(), 0, stabbuff
);
1600 stabs_free_includes();
1601 HeapFree(GetProcessHeap(), 0, pending
.vars
);