msvcrt: Remove non-needed function declarations from msvcrt.h.
[wine.git] / dlls / dbghelp / stabs.c
blob517299897c4574a69b857844103d66d738e94704
1 /*
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
28 * of Cygnus Support
29 * available (hopefully) from http://sources.redhat.com/gdb/onlinedocs
32 #include <sys/types.h>
33 #include <fcntl.h>
34 #include <limits.h>
35 #include <stdint.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <stdio.h>
39 #include <assert.h>
40 #include <stdarg.h>
42 #include "windef.h"
43 #include "winbase.h"
44 #include "winnls.h"
46 #include "dbghelp_private.h"
47 #include "image_private.h"
49 #include "wine/debug.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp_stabs);
53 /* Masks for n_type field */
54 #define N_STAB 0xe0
55 #define N_PEXT 0x10
56 #define N_TYPE 0x1e
57 #define N_EXT 0x01
59 /* Values for (n_type & N_TYPE) */
60 #define N_UNDF 0x00
61 #define N_ABS 0x02
62 #define N_INDR 0x0a
63 #define N_SECT 0x0e
64 #define N_GSYM 0x20
65 #define N_FUN 0x24
66 #define N_STSYM 0x26
67 #define N_LCSYM 0x28
68 #define N_MAIN 0x2a
69 #define N_ROSYM 0x2c
70 #define N_BNSYM 0x2e
71 #define N_OPT 0x3c
72 #define N_RSYM 0x40
73 #define N_SLINE 0x44
74 #define N_ENSYM 0x4e
75 #define N_SO 0x64
76 #define N_OSO 0x66
77 #define N_LSYM 0x80
78 #define N_BINCL 0x82
79 #define N_SOL 0x84
80 #define N_PSYM 0xa0
81 #define N_EINCL 0xa2
82 #define N_LBRAC 0xc0
83 #define N_EXCL 0xc2
84 #define N_RBRAC 0xe0
86 static BOOL stab_strcpy(char* dest, int sz, const char* source)
88 char* ptr = dest;
90 * A strcpy routine that stops when we hit the ':' character.
91 * Faster than copying the whole thing, and then nuking the
92 * ':'.
93 * Takes also care of (valid) a::b constructs
95 while (*source != '\0')
97 if (source[0] != ':' && sz-- > 0) *ptr++ = *source++;
98 else if (source[1] == ':' && (sz -= 2) > 0)
100 *ptr++ = *source++;
101 *ptr++ = *source++;
103 else break;
105 *ptr-- = '\0';
106 /* GCC emits, in some cases, a .<digit>+ suffix.
107 * This is used for static variable inside functions, so
108 * that we can have several such variables with same name in
109 * the same compilation unit
110 * We simply ignore that suffix when present (we also get rid
111 * of it in ELF symtab parsing)
113 if (ptr >= dest && isdigit(*ptr))
115 while (ptr > dest && isdigit(*ptr)) ptr--;
116 if (*ptr == '.') *ptr = '\0';
118 return (sz > 0);
121 typedef struct
123 char* name;
124 ULONG_PTR value;
125 struct symt** vector;
126 int nrofentries;
127 } include_def;
129 #define MAX_INCLUDES 5120
131 static include_def* include_defs = NULL;
132 static int num_include_def = 0;
133 static int num_alloc_include_def = 0;
134 static int cu_include_stack[MAX_INCLUDES];
135 static int cu_include_stk_idx = 0;
136 static struct symt** cu_vector = NULL;
137 static int cu_nrofentries = 0;
138 static struct symt_basic* stabs_basic[36];
140 static int stabs_new_include(const char* file, ULONG_PTR val)
142 if (num_include_def == num_alloc_include_def)
144 if (!include_defs)
146 num_alloc_include_def = 256;
147 include_defs = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
148 sizeof(include_defs[0]) * num_alloc_include_def);
150 else
152 num_alloc_include_def *= 2;
153 include_defs = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, include_defs,
154 sizeof(include_defs[0]) * num_alloc_include_def);
157 include_defs[num_include_def].name = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(file) + 1), file);
158 include_defs[num_include_def].value = val;
159 include_defs[num_include_def].vector = NULL;
160 include_defs[num_include_def].nrofentries = 0;
162 return num_include_def++;
165 static int stabs_find_include(const char* file, ULONG_PTR val)
167 int i;
169 for (i = 0; i < num_include_def; i++)
171 if (val == include_defs[i].value &&
172 strcmp(file, include_defs[i].name) == 0)
173 return i;
175 return -1;
178 static int stabs_add_include(int idx)
180 if (idx < 0) return -1;
181 cu_include_stk_idx++;
183 /* if this happens, just bump MAX_INCLUDES */
184 /* we could also handle this as another dynarray */
185 assert(cu_include_stk_idx < MAX_INCLUDES);
186 cu_include_stack[cu_include_stk_idx] = idx;
187 return cu_include_stk_idx;
190 static void stabs_reset_includes(void)
193 * The struct symt:s that we would need to use are reset when
194 * we start a new file. (at least the ones in filenr == 0)
196 cu_include_stk_idx = 0;/* keep 0 as index for the .c file itself */
197 memset(cu_vector, 0, sizeof(cu_vector[0]) * cu_nrofentries);
200 static void stabs_free_includes(void)
202 int i;
204 stabs_reset_includes();
205 for (i = 0; i < num_include_def; i++)
207 HeapFree(GetProcessHeap(), 0, include_defs[i].name);
208 HeapFree(GetProcessHeap(), 0, include_defs[i].vector);
210 HeapFree(GetProcessHeap(), 0, include_defs);
211 include_defs = NULL;
212 num_include_def = 0;
213 num_alloc_include_def = 0;
214 HeapFree(GetProcessHeap(), 0, cu_vector);
215 cu_vector = NULL;
216 cu_nrofentries = 0;
219 static struct symt** stabs_find_ref(LONG_PTR filenr, LONG_PTR subnr)
221 struct symt** ret;
223 /* FIXME: I could perhaps create a dummy include_def for each compilation
224 * unit which would allow not to handle those two cases separately
226 if (filenr == 0)
228 if (cu_nrofentries <= subnr)
230 cu_nrofentries = max( cu_nrofentries * 2, subnr + 1 );
231 if (!cu_vector)
232 cu_vector = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
233 sizeof(cu_vector[0]) * cu_nrofentries);
234 else
235 cu_vector = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
236 cu_vector, sizeof(cu_vector[0]) * cu_nrofentries);
238 ret = &cu_vector[subnr];
240 else
242 include_def* idef;
244 assert(filenr <= cu_include_stk_idx);
245 idef = &include_defs[cu_include_stack[filenr]];
247 if (idef->nrofentries <= subnr)
249 idef->nrofentries = max( idef->nrofentries * 2, subnr + 1 );
250 if (!idef->vector)
251 idef->vector = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
252 sizeof(idef->vector[0]) * idef->nrofentries);
253 else
254 idef->vector = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
255 idef->vector, sizeof(idef->vector[0]) * idef->nrofentries);
257 ret = &idef->vector[subnr];
259 TRACE("(%ld,%ld) => %p (%p)\n", filenr, subnr, ret, *ret);
260 return ret;
263 static struct symt** stabs_read_type_enum(const char** x)
265 LONG_PTR filenr, subnr;
266 const char* iter;
267 char* end;
269 iter = *x;
270 if (*iter == '(')
272 ++iter; /* '(' */
273 filenr = strtol(iter, &end, 10); /* <int> */
274 iter = ++end; /* ',' */
275 subnr = strtol(iter, &end, 10); /* <int> */
276 iter = ++end; /* ')' */
278 else
280 filenr = 0;
281 subnr = strtol(iter, &end, 10); /* <int> */
282 iter = end;
284 *x = iter;
285 return stabs_find_ref(filenr, subnr);
288 #define PTS_DEBUG
289 struct ParseTypedefData
291 const char* ptr;
292 char buf[1024];
293 int idx;
294 struct module* module;
295 #ifdef PTS_DEBUG
296 struct PTS_Error
298 const char* ptr;
299 unsigned line;
300 } errors[16];
301 int err_idx;
302 #endif
305 #ifdef PTS_DEBUG
306 static void stabs_pts_push(struct ParseTypedefData* ptd, unsigned line)
308 assert(ptd->err_idx < ARRAY_SIZE(ptd->errors));
309 ptd->errors[ptd->err_idx].line = line;
310 ptd->errors[ptd->err_idx].ptr = ptd->ptr;
311 ptd->err_idx++;
313 #define PTS_ABORTIF(ptd, t) do { if (t) { stabs_pts_push((ptd), __LINE__); return -1;} } while (0)
314 #else
315 #define PTS_ABORTIF(ptd, t) do { if (t) return -1; } while (0)
316 #endif
318 static int stabs_get_basic(struct ParseTypedefData* ptd, unsigned basic, struct symt** symt)
320 PTS_ABORTIF(ptd, basic >= ARRAY_SIZE(stabs_basic));
322 if (!stabs_basic[basic])
324 switch (basic)
326 case 1: stabs_basic[basic] = symt_new_basic(ptd->module, btInt, "int", 4); break;
327 case 2: stabs_basic[basic] = symt_new_basic(ptd->module, btChar, "char", 1); break;
328 case 3: stabs_basic[basic] = symt_new_basic(ptd->module, btInt, "short int", 2); break;
329 case 4: stabs_basic[basic] = symt_new_basic(ptd->module, btInt, "long int", 4); break;
330 case 5: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt, "unsigned char", 1); break;
331 case 6: stabs_basic[basic] = symt_new_basic(ptd->module, btInt, "signed char", 1); break;
332 case 7: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt, "unsigned short int", 2); break;
333 case 8: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt, "unsigned int", 4); break;
334 case 9: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt, "unsigned", 2); break;
335 case 10: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt, "unsigned long int", 2); break;
336 case 11: stabs_basic[basic] = symt_new_basic(ptd->module, btVoid, "void", 0); break;
337 case 12: stabs_basic[basic] = symt_new_basic(ptd->module, btFloat, "float", 4); break;
338 case 13: stabs_basic[basic] = symt_new_basic(ptd->module, btFloat, "double", 8); break;
339 case 14: stabs_basic[basic] = symt_new_basic(ptd->module, btFloat, "long double", 12); break;
340 case 15: stabs_basic[basic] = symt_new_basic(ptd->module, btInt, "integer", 4); break;
341 case 16: stabs_basic[basic] = symt_new_basic(ptd->module, btBool, "bool", 1); break;
342 /* case 17: short real */
343 /* case 18: real */
344 case 25: stabs_basic[basic] = symt_new_basic(ptd->module, btComplex, "float complex", 8); break;
345 case 26: stabs_basic[basic] = symt_new_basic(ptd->module, btComplex, "double complex", 16); break;
346 case 30: stabs_basic[basic] = symt_new_basic(ptd->module, btWChar, "wchar_t", 2); break;
347 case 31: stabs_basic[basic] = symt_new_basic(ptd->module, btInt, "long long int", 8); break;
348 case 32: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt, "long long unsigned", 8); break;
349 /* starting at 35 are wine extensions (especially for R implementation) */
350 case 35: stabs_basic[basic] = symt_new_basic(ptd->module, btComplex, "long double complex", 24); break;
351 default: PTS_ABORTIF(ptd, 1);
354 *symt = &stabs_basic[basic]->symt;
355 return 0;
358 static int stabs_pts_read_type_def(struct ParseTypedefData* ptd,
359 const char* typename, struct symt** dt);
361 static int stabs_pts_read_id(struct ParseTypedefData* ptd)
363 const char* first = ptd->ptr;
364 unsigned int template = 0;
365 char ch;
367 while ((ch = *ptd->ptr++) != '\0')
369 switch (ch)
371 case ':':
372 if (template == 0)
374 unsigned int len = ptd->ptr - first - 1;
375 PTS_ABORTIF(ptd, len >= sizeof(ptd->buf) - ptd->idx);
376 memcpy(ptd->buf + ptd->idx, first, len);
377 ptd->buf[ptd->idx + len] = '\0';
378 ptd->idx += len + 1;
379 return 0;
381 break;
382 case '<': template++; break;
383 case '>': PTS_ABORTIF(ptd, template == 0); template--; break;
386 return -1;
389 static int stabs_pts_read_number(struct ParseTypedefData* ptd, LONG_PTR* v)
391 char* last;
393 *v = strtol(ptd->ptr, &last, 10);
394 PTS_ABORTIF(ptd, last == ptd->ptr);
395 ptd->ptr = last;
396 return 0;
399 static int stabs_pts_read_type_reference(struct ParseTypedefData* ptd,
400 LONG_PTR* filenr, LONG_PTR* subnr)
402 if (*ptd->ptr == '(')
404 /* '(' <int> ',' <int> ')' */
405 ptd->ptr++;
406 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, filenr) == -1);
407 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
408 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, subnr) == -1);
409 PTS_ABORTIF(ptd, *ptd->ptr++ != ')');
411 else
413 *filenr = 0;
414 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, subnr) == -1);
416 return 0;
419 struct pts_range_value
421 ULONGLONG val;
422 int sign;
425 static int stabs_pts_read_range_value(struct ParseTypedefData* ptd, struct pts_range_value* prv)
427 char* last;
429 switch (*ptd->ptr)
431 case '0':
432 while (*ptd->ptr == '0') ptd->ptr++;
433 if (*ptd->ptr >= '1' && *ptd->ptr <= '7')
435 switch (ptd->ptr[1])
437 case '0':
438 PTS_ABORTIF(ptd, ptd->ptr[0] != '1');
439 prv->sign = -1;
440 prv->val = 0;
441 while (isdigit(*ptd->ptr)) prv->val = (prv->val << 3) + *ptd->ptr++ - '0';
442 break;
443 case '7':
444 prv->sign = 1;
445 prv->val = 0;
446 while (isdigit(*ptd->ptr)) prv->val = (prv->val << 3) + *ptd->ptr++ - '0';
447 break;
448 default: PTS_ABORTIF(ptd, 1); break;
450 } else prv->sign = 0;
451 break;
452 case '-':
453 prv->sign = -1;
454 prv->val = strtoull(++ptd->ptr, &last, 10);
455 ptd->ptr = last;
456 break;
457 case '+':
458 default:
459 prv->sign = 1;
460 prv->val = strtoull(ptd->ptr, &last, 10);
461 ptd->ptr = last;
462 break;
464 return 0;
467 static int stabs_pts_read_range(struct ParseTypedefData* ptd, const char* typename,
468 struct symt** dt)
470 struct symt* ref;
471 struct pts_range_value lo;
472 struct pts_range_value hi;
473 unsigned size;
474 enum BasicType bt;
475 int i;
476 ULONGLONG v;
478 /* type ';' <int> ';' <int> ';' */
479 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &ref) == -1);
480 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
481 PTS_ABORTIF(ptd, stabs_pts_read_range_value(ptd, &lo) == -1);
482 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
483 PTS_ABORTIF(ptd, stabs_pts_read_range_value(ptd, &hi) == -1);
484 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
486 /* basically, we don't use ref... in some cases, for example, float is declared
487 * as a derived type of int... which won't help us... so we guess the types
488 * from the various formats
490 if (lo.sign == 0 && hi.sign < 0)
492 bt = btUInt;
493 size = hi.val;
495 else if (lo.sign < 0 && hi.sign == 0)
497 bt = btUInt;
498 size = lo.val;
500 else if (lo.sign > 0 && hi.sign == 0)
502 bt = btFloat;
503 size = lo.val;
505 else if (lo.sign < 0 && hi.sign > 0)
507 v = 1 << 7;
508 for (i = 7; i < 64; i += 8)
510 if (lo.val == v && hi.val == v - 1)
512 bt = btInt;
513 size = (i + 1) / 8;
514 break;
516 v <<= 8;
518 PTS_ABORTIF(ptd, i >= 64);
520 else if (lo.sign == 0 && hi.sign > 0)
522 if (hi.val == 127) /* specific case for char... */
524 bt = btChar;
525 size = 1;
527 else
529 v = 1;
530 for (i = 8; i <= 64; i += 8)
532 v <<= 8;
533 if (hi.val + 1 == v)
535 bt = btUInt;
536 size = (i + 1) / 8;
537 break;
540 PTS_ABORTIF(ptd, i > 64);
543 else PTS_ABORTIF(ptd, 1);
545 *dt = &symt_new_basic(ptd->module, bt, typename, size)->symt;
546 return 0;
549 static inline int stabs_pts_read_method_info(struct ParseTypedefData* ptd)
551 struct symt* dt;
552 const char* tmp;
553 char mthd;
557 /* get type of return value */
558 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
559 if (*ptd->ptr == ';') ptd->ptr++;
561 /* get types of parameters */
562 if (*ptd->ptr == ':')
564 PTS_ABORTIF(ptd, !(tmp = strchr(ptd->ptr + 1, ';')));
565 ptd->ptr = tmp + 1;
567 PTS_ABORTIF(ptd, !(*ptd->ptr >= '0' && *ptd->ptr <= '9'));
568 ptd->ptr++;
569 PTS_ABORTIF(ptd, !(ptd->ptr[0] >= 'A' && *ptd->ptr <= 'D'));
570 mthd = *++ptd->ptr;
571 PTS_ABORTIF(ptd, mthd != '.' && mthd != '?' && mthd != '*');
572 ptd->ptr++;
573 if (mthd == '*')
575 LONG_PTR ofs;
577 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &ofs) == -1);
578 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
579 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
580 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
582 } while (*ptd->ptr != ';');
583 ptd->ptr++;
585 return 0;
588 static inline int stabs_pts_read_aggregate(struct ParseTypedefData* ptd,
589 struct symt_udt* sdt)
591 LONG_PTR sz, ofs;
592 struct symt* adt;
593 struct symt* dt = NULL;
594 int idx;
595 int doadd;
597 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &sz) == -1);
599 doadd = symt_set_udt_size(ptd->module, sdt, sz);
600 if (*ptd->ptr == '!') /* C++ inheritance */
602 LONG_PTR num_classes;
604 ptd->ptr++;
605 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &num_classes) == -1);
606 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
607 while (--num_classes >= 0)
609 ptd->ptr += 2; /* skip visibility and inheritance */
610 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &ofs) == -1);
611 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
613 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &adt) == -1);
615 if (doadd && adt)
617 char tmp[256];
618 DWORD64 size;
620 strcpy(tmp, "__inherited_class_");
621 strcat(tmp, symt_get_name(adt));
623 /* FIXME: TI_GET_LENGTH will not always work, especially when adt
624 * has just been seen as a forward definition and not the real stuff
625 * yet.
626 * As we don't use much the size of members in structs, this may not
627 * be much of a problem
629 symt_get_info(ptd->module, adt, TI_GET_LENGTH, &size);
630 symt_add_udt_element(ptd->module, sdt, tmp, adt, ofs, (DWORD)size * 8);
632 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
636 /* if the structure has already been filled, just redo the parsing
637 * but don't store results into the struct
638 * FIXME: there's a quite ugly memory leak in there...
641 /* Now parse the individual elements of the structure/union. */
642 while (*ptd->ptr != ';')
644 /* agg_name : type ',' <int:offset> ',' <int:size> */
645 idx = ptd->idx;
647 if (ptd->ptr[0] == '$' && ptd->ptr[1] == 'v')
649 LONG_PTR x;
651 if (ptd->ptr[2] == 'f')
653 /* C++ virtual method table */
654 ptd->ptr += 3;
655 stabs_read_type_enum(&ptd->ptr);
656 PTS_ABORTIF(ptd, *ptd->ptr++ != ':');
657 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
658 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
659 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &x) == -1);
660 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
661 ptd->idx = idx;
662 continue;
664 else if (ptd->ptr[2] == 'b')
666 ptd->ptr += 3;
667 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
668 PTS_ABORTIF(ptd, *ptd->ptr++ != ':');
669 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
670 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
671 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &x) == -1);
672 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
673 ptd->idx = idx;
674 continue;
678 PTS_ABORTIF(ptd, stabs_pts_read_id(ptd) == -1);
679 /* Ref. TSDF R2.130 Section 7.4. When the field name is a method name
680 * it is followed by two colons rather than one.
682 if (*ptd->ptr == ':')
684 ptd->ptr++;
685 stabs_pts_read_method_info(ptd);
686 ptd->idx = idx;
687 continue;
689 else
691 /* skip C++ member protection /0 /1 or /2 */
692 if (*ptd->ptr == '/') ptd->ptr += 2;
694 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &adt) == -1);
696 switch (*ptd->ptr++)
698 case ',':
699 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &ofs) == -1);
700 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
701 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &sz) == -1);
702 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
704 if (doadd) symt_add_udt_element(ptd->module, sdt, ptd->buf + idx, adt, ofs, sz);
705 break;
706 case ':':
708 const char* tmp;
709 /* method parameters... terminated by ';' */
710 PTS_ABORTIF(ptd, !(tmp = strchr(ptd->ptr, ';')));
711 ptd->ptr = tmp + 1;
713 break;
714 default:
715 PTS_ABORTIF(ptd, TRUE);
717 ptd->idx = idx;
719 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
720 if (*ptd->ptr == '~')
722 ptd->ptr++;
723 PTS_ABORTIF(ptd, *ptd->ptr++ != '%');
724 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
725 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
727 return 0;
730 static inline int stabs_pts_read_enum(struct ParseTypedefData* ptd,
731 struct symt_enum* edt)
733 LONG_PTR value;
734 int idx;
736 while (*ptd->ptr != ';')
738 idx = ptd->idx;
739 PTS_ABORTIF(ptd, stabs_pts_read_id(ptd) == -1);
740 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &value) == -1);
741 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
742 symt_add_enum_element(ptd->module, edt, ptd->buf + idx, value);
743 ptd->idx = idx;
745 ptd->ptr++;
746 return 0;
749 static inline int stabs_pts_read_array(struct ParseTypedefData* ptd,
750 struct symt** adt)
752 LONG_PTR lo, hi;
753 struct symt* range_dt;
754 struct symt* base_dt;
756 /* ar<typeinfo_nodef>;<int>;<int>;<typeinfo> */
758 PTS_ABORTIF(ptd, *ptd->ptr++ != 'r');
760 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &range_dt) == -1);
761 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
762 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &lo) == -1);
763 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
764 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &hi) == -1);
765 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
767 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &base_dt) == -1);
769 *adt = &symt_new_array(ptd->module, lo, hi, base_dt, range_dt)->symt;
770 return 0;
773 static int stabs_pts_read_type_def(struct ParseTypedefData* ptd, const char* typename,
774 struct symt** ret_dt)
776 int idx;
777 LONG_PTR sz = -1;
778 struct symt* new_dt = NULL; /* newly created data type */
779 struct symt* ref_dt; /* referenced data type (pointer...) */
780 LONG_PTR filenr1, subnr1, tmp;
782 /* things are a bit complicated because of the way the typedefs are stored inside
783 * the file, because addresses can change when realloc is done, so we must call
784 * over and over stabs_find_ref() to keep the correct values around
786 PTS_ABORTIF(ptd, stabs_pts_read_type_reference(ptd, &filenr1, &subnr1) == -1);
788 while (*ptd->ptr == '=')
790 ptd->ptr++;
791 PTS_ABORTIF(ptd, new_dt != NULL);
793 /* first handle attribute if any */
794 switch (*ptd->ptr)
796 case '@':
797 if (*++ptd->ptr == 's')
799 ptd->ptr++;
800 if (stabs_pts_read_number(ptd, &sz) == -1)
802 ERR("Not an attribute... NIY\n");
803 ptd->ptr -= 2;
804 return -1;
806 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
808 break;
810 /* then the real definitions */
811 switch (*ptd->ptr++)
813 case '*':
814 case '&':
815 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &ref_dt) == -1);
816 new_dt = &symt_new_pointer(ptd->module, ref_dt, sizeof(void*))->symt;
817 break;
818 case 'k': /* 'const' modifier */
819 case 'B': /* 'volatile' modifier */
820 /* just kinda ignore the modifier, I guess -gmt */
821 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, typename, &new_dt) == -1);
822 break;
823 case '(':
824 ptd->ptr--;
825 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, typename, &new_dt) == -1);
826 break;
827 case 'a':
828 PTS_ABORTIF(ptd, stabs_pts_read_array(ptd, &new_dt) == -1);
829 break;
830 case 'r':
831 PTS_ABORTIF(ptd, stabs_pts_read_range(ptd, typename, &new_dt) == -1);
832 assert(!*stabs_find_ref(filenr1, subnr1));
833 *stabs_find_ref(filenr1, subnr1) = new_dt;
834 break;
835 case 'f':
836 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &ref_dt) == -1);
837 new_dt = &symt_new_function_signature(ptd->module, ref_dt, -1)->symt;
838 break;
839 case 'e':
840 stabs_get_basic(ptd, 1 /* int */, &ref_dt);
841 new_dt = &symt_new_enum(ptd->module, typename, ref_dt)->symt;
842 PTS_ABORTIF(ptd, stabs_pts_read_enum(ptd, (struct symt_enum*)new_dt) == -1);
843 break;
844 case 's':
845 case 'u':
847 struct symt_udt* udt;
848 enum UdtKind kind = (ptd->ptr[-1] == 's') ? UdtStruct : UdtUnion;
849 /* udt can have been already defined in a forward definition */
850 udt = (struct symt_udt*)*stabs_find_ref(filenr1, subnr1);
851 if (!udt)
853 udt = symt_new_udt(ptd->module, typename, 0, kind);
854 /* we need to set it here, because a struct can hold a pointer
855 * to itself
857 new_dt = *stabs_find_ref(filenr1, subnr1) = &udt->symt;
859 else
861 unsigned l1, l2;
862 if (udt->symt.tag != SymTagUDT)
864 ERR("Forward declaration (%p/%s) is not an aggregate (%u)\n",
865 udt, symt_get_name(&udt->symt), udt->symt.tag);
866 return -1;
868 /* FIXME: we currently don't correctly construct nested C++
869 * classes names. Therefore, we could be here with either:
870 * - typename and udt->hash_elt.name being the same string
871 * (non embedded case)
872 * - typename being foo::bar while udt->hash_elt.name being
873 * just bar
874 * So, we twist the comparison to test both occurrences. When
875 * we have proper C++ types in this file, this twist has to be
876 * removed
878 l1 = strlen(udt->hash_elt.name);
879 l2 = strlen(typename);
880 if (l1 > l2 || strcmp(udt->hash_elt.name, typename + l2 - l1))
881 ERR("Forward declaration name mismatch %s <> %s\n",
882 udt->hash_elt.name, typename);
883 new_dt = &udt->symt;
885 PTS_ABORTIF(ptd, stabs_pts_read_aggregate(ptd, udt) == -1);
887 break;
888 case 'x':
889 idx = ptd->idx;
890 tmp = *ptd->ptr++;
891 PTS_ABORTIF(ptd, stabs_pts_read_id(ptd) == -1);
892 switch (tmp)
894 case 'e':
895 stabs_get_basic(ptd, 1 /* int */, &ref_dt);
896 new_dt = &symt_new_enum(ptd->module, ptd->buf + idx, ref_dt)->symt;
897 break;
898 case 's':
899 new_dt = &symt_new_udt(ptd->module, ptd->buf + idx, 0, UdtStruct)->symt;
900 break;
901 case 'u':
902 new_dt = &symt_new_udt(ptd->module, ptd->buf + idx, 0, UdtUnion)->symt;
903 break;
904 default:
905 return -1;
907 ptd->idx = idx;
908 break;
909 case '-':
911 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &tmp) == -1);
912 PTS_ABORTIF(ptd, stabs_get_basic(ptd, tmp, &new_dt) == -1);
913 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
915 break;
916 case '#':
917 if (*ptd->ptr == '#')
919 ptd->ptr++;
920 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &ref_dt) == -1);
921 new_dt = &symt_new_function_signature(ptd->module, ref_dt, -1)->symt;
923 else
925 struct symt* cls_dt;
926 struct symt* pmt_dt;
928 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &cls_dt) == -1);
929 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
930 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &ref_dt) == -1);
931 new_dt = &symt_new_function_signature(ptd->module, ref_dt, -1)->symt;
932 while (*ptd->ptr == ',')
934 ptd->ptr++;
935 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &pmt_dt) == -1);
938 break;
939 case 'R':
941 LONG_PTR type, len, unk;
942 int basic;
944 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &type) == -1);
945 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
946 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &len) == -1);
947 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
948 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &unk) == -1);
949 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
951 switch (type) /* see stabs_get_basic for the details */
953 case 1: basic = 12; break;
954 case 2: basic = 13; break;
955 case 3: basic = 25; break;
956 case 4: basic = 26; break;
957 case 5: basic = 35; break;
958 case 6: basic = 14; break;
959 default: PTS_ABORTIF(ptd, 1);
961 PTS_ABORTIF(ptd, stabs_get_basic(ptd, basic, &new_dt) == -1);
963 break;
964 default:
965 ERR("Unknown type '%c'\n", ptd->ptr[-1]);
966 return -1;
970 if (!new_dt)
972 /* is it a forward declaration that has been filled ? */
973 new_dt = *stabs_find_ref(filenr1, subnr1);
974 /* if not, this should be void (which is defined as a ref to itself, but we
975 * don't correctly catch it)
977 if (!new_dt && typename)
979 new_dt = &symt_new_basic(ptd->module, btVoid, typename, 0)->symt;
980 PTS_ABORTIF(ptd, strcmp(typename, "void"));
984 *stabs_find_ref(filenr1, subnr1) = *ret_dt = new_dt;
986 TRACE("Adding (%ld,%ld) %s\n", filenr1, subnr1, debugstr_a(typename));
988 return 0;
991 static int stabs_parse_typedef(struct module* module, const char* ptr,
992 const char* typename)
994 struct ParseTypedefData ptd;
995 struct symt* dt;
996 int ret = -1;
998 /* check for already existing definition */
1000 TRACE("%s => %s\n", typename, debugstr_a(ptr));
1001 ptd.module = module;
1002 ptd.idx = 0;
1003 #ifdef PTS_DEBUG
1004 ptd.err_idx = 0;
1005 #endif
1006 for (ptd.ptr = ptr - 1; ;)
1008 ptd.ptr = strchr(ptd.ptr + 1, ':');
1009 if (ptd.ptr == NULL || *++ptd.ptr != ':') break;
1011 if (ptd.ptr)
1013 if (*ptd.ptr != '(') ptd.ptr++;
1014 /* most of type definitions take one char, except Tt */
1015 if (*ptd.ptr != '(') ptd.ptr++;
1016 ret = stabs_pts_read_type_def(&ptd, typename, &dt);
1019 if (ret == -1 || *ptd.ptr)
1021 #ifdef PTS_DEBUG
1022 int i;
1023 TRACE("Failure on %s\n", debugstr_a(ptr));
1024 if (ret == -1)
1026 for (i = 0; i < ptd.err_idx; i++)
1028 TRACE("[%d]: line %d => %s\n",
1029 i, ptd.errors[i].line, debugstr_a(ptd.errors[i].ptr));
1032 else
1033 TRACE("[0]: => %s\n", debugstr_a(ptd.ptr));
1035 #else
1036 ERR("Failure on %s at %s\n", debugstr_a(ptr), debugstr_a(ptd.ptr));
1037 #endif
1038 return FALSE;
1041 return TRUE;
1044 static struct symt* stabs_parse_type(const char* stab)
1046 const char* c = stab - 1;
1049 * Look through the stab definition, and figure out what struct symt
1050 * this represents. If we have something we know about, assign the
1051 * type.
1052 * According to "The \"stabs\" debug format" (Rev 2.130) the name may be
1053 * a C++ name and contain double colons e.g. foo::bar::baz:t5=*6.
1057 if ((c = strchr(c + 1, ':')) == NULL) return NULL;
1058 } while (*++c == ':');
1061 * The next characters say more about the type (i.e. data, function, etc)
1062 * of symbol. Skip them. (C++ for example may have Tt).
1063 * Actually this is a very weak description; I think Tt is the only
1064 * multiple combination we should see.
1066 while (*c && *c != '(' && !isdigit(*c))
1067 c++;
1069 * The next is either an integer or a (integer,integer).
1070 * The stabs_read_type_enum() takes care that stab_types is large enough.
1072 return *stabs_read_type_enum(&c);
1075 enum pending_obj_kind
1077 PENDING_VAR,
1078 PENDING_LINE,
1081 struct pending_loc_var
1083 char name[256];
1084 struct symt* type;
1085 enum DataKind kind;
1086 struct location loc;
1089 struct pending_line
1091 int source_idx;
1092 int line_num;
1093 ULONG_PTR offset;
1094 ULONG_PTR load_offset;
1097 struct pending_object
1099 enum pending_obj_kind tag;
1100 union {
1101 struct pending_loc_var var;
1102 struct pending_line line;
1103 } u;
1106 struct pending_list
1108 struct pending_object* objs;
1109 unsigned num;
1110 unsigned allocated;
1113 static inline void pending_make_room(struct pending_list* pending)
1115 if (pending->num == pending->allocated)
1117 if (!pending->objs)
1119 pending->allocated = 8;
1120 pending->objs = HeapAlloc(GetProcessHeap(), 0,
1121 pending->allocated * sizeof(pending->objs[0]));
1123 else
1125 pending->allocated *= 2;
1126 pending->objs = HeapReAlloc(GetProcessHeap(), 0, pending->objs,
1127 pending->allocated * sizeof(pending->objs[0]));
1132 static inline void pending_add_var(struct pending_list* pending, const char* name,
1133 enum DataKind dt, const struct location* loc)
1135 pending_make_room(pending);
1136 pending->objs[pending->num].tag = PENDING_VAR;
1137 if (!stab_strcpy(pending->objs[pending->num].u.var.name,
1138 sizeof(pending->objs[pending->num].u.var.name), name))
1140 ERR("symbol too long %s\n", debugstr_a(name));
1141 return;
1143 pending->objs[pending->num].u.var.type = stabs_parse_type(name);
1144 pending->objs[pending->num].u.var.kind = dt;
1145 pending->objs[pending->num].u.var.loc = *loc;
1146 pending->num++;
1149 static inline void pending_add_line(struct pending_list* pending, int source_idx,
1150 int line_num, ULONG_PTR offset,
1151 ULONG_PTR load_offset)
1153 pending_make_room(pending);
1154 pending->objs[pending->num].tag = PENDING_LINE;
1155 pending->objs[pending->num].u.line.source_idx = source_idx;
1156 pending->objs[pending->num].u.line.line_num = line_num;
1157 pending->objs[pending->num].u.line.offset = offset;
1158 pending->objs[pending->num].u.line.load_offset = load_offset;
1159 pending->num++;
1162 static void pending_flush(struct pending_list* pending, struct module* module,
1163 struct symt_function* func, struct symt_block* block)
1165 unsigned int i;
1167 for (i = 0; i < pending->num; i++)
1169 switch (pending->objs[i].tag)
1171 case PENDING_VAR:
1172 symt_add_func_local(module, func,
1173 pending->objs[i].u.var.kind, &pending->objs[i].u.var.loc,
1174 block, pending->objs[i].u.var.type, pending->objs[i].u.var.name);
1175 break;
1176 case PENDING_LINE:
1177 if (module->type == DMT_MACHO)
1178 pending->objs[i].u.line.offset -= func->address - pending->objs[i].u.line.load_offset;
1179 symt_add_func_line(module, func, pending->objs[i].u.line.source_idx,
1180 pending->objs[i].u.line.line_num, pending->objs[i].u.line.offset);
1181 break;
1182 default:
1183 ERR("Unknown pending object tag %u\n", (unsigned)pending->objs[i].tag);
1184 break;
1187 pending->num = 0;
1190 /******************************************************************
1191 * stabs_finalize_function
1193 * Ends function creation: mainly:
1194 * - cleans up line number information
1195 * - tries to set up a debug-start tag (FIXME: heuristic to be enhanced)
1196 * - for stabs which have absolute address in them, initializes the size of the
1197 * function (assuming that current function ends where next function starts)
1199 static void stabs_finalize_function(struct module* module, struct symt_function* func,
1200 ULONG_PTR size)
1202 IMAGEHLP_LINE64 il;
1203 struct location loc;
1205 if (!func) return;
1206 symt_normalize_function(module, func);
1207 /* To define the debug-start of the function, we use the second line number.
1208 * Not 100% bullet proof, but better than nothing
1210 if (symt_fill_func_line_info(module, func, func->address, &il) &&
1211 symt_get_func_line_next(module, &il))
1213 loc.kind = loc_absolute;
1214 loc.offset = il.Address - func->address;
1215 symt_add_function_point(module, func, SymTagFuncDebugStart,
1216 &loc, NULL);
1218 if (size) func->size = size;
1221 static inline void stabbuf_append(char **buf, unsigned *buf_size, const char *str)
1223 unsigned str_len, buf_len;
1225 str_len = strlen(str);
1226 buf_len = strlen(*buf);
1228 if(str_len+buf_len >= *buf_size) {
1229 *buf_size += buf_len + str_len;
1230 *buf = HeapReAlloc(GetProcessHeap(), 0, *buf, *buf_size);
1233 strcpy(*buf+buf_len, str);
1236 BOOL stabs_parse(struct module* module, ULONG_PTR load_offset,
1237 const char* pv_stab_ptr, size_t nstab, size_t stabsize,
1238 const char* strs, int strtablen,
1239 stabs_def_cb callback, void* user)
1241 struct symt_function* curr_func = NULL;
1242 struct symt_block* block = NULL;
1243 struct symt_compiland* compiland = NULL;
1244 char* srcpath = NULL;
1245 int i;
1246 const char* ptr;
1247 char* stabbuff;
1248 unsigned int stabbufflen;
1249 const struct stab_nlist* stab_ptr;
1250 const char* strs_end;
1251 int strtabinc;
1252 char symname[4096];
1253 unsigned incl[32];
1254 int incl_stk = -1;
1255 int source_idx = -1;
1256 struct pending_list pending_block;
1257 struct pending_list pending_func;
1258 BOOL ret = TRUE;
1259 struct location loc;
1260 unsigned char type;
1261 uint64_t n_value;
1263 strs_end = strs + strtablen;
1265 memset(stabs_basic, 0, sizeof(stabs_basic));
1266 memset(&pending_block, 0, sizeof(pending_block));
1267 memset(&pending_func, 0, sizeof(pending_func));
1270 * Allocate a buffer into which we can build stab strings for cases
1271 * where the stab is continued over multiple lines.
1273 stabbufflen = 65536;
1274 stabbuff = HeapAlloc(GetProcessHeap(), 0, stabbufflen);
1276 strtabinc = 0;
1277 stabbuff[0] = '\0';
1278 for (i = 0; i < nstab; i++)
1280 stab_ptr = (struct stab_nlist *)(pv_stab_ptr + i * stabsize);
1281 n_value = stabsize == sizeof(struct macho64_nlist) ? ((struct macho64_nlist *)stab_ptr)->n_value : stab_ptr->n_value;
1282 ptr = strs + stab_ptr->n_strx;
1283 if ((ptr > strs_end) || (ptr + strlen(ptr) > strs_end))
1285 WARN("Bad stabs string %p\n", ptr);
1286 continue;
1288 if (*ptr != '\0' && (ptr[strlen(ptr) - 1] == '\\'))
1291 * Indicates continuation. Append this to the buffer, and go onto the
1292 * next record. Repeat the process until we find a stab without the
1293 * '/' character, as this indicates we have the whole thing.
1295 stabbuf_append(&stabbuff, &stabbufflen, ptr);
1296 continue;
1298 else if (stabbuff[0] != '\0')
1300 stabbuf_append(&stabbuff, &stabbufflen, ptr);
1301 ptr = stabbuff;
1304 if (stab_ptr->n_type & N_STAB)
1305 type = stab_ptr->n_type;
1306 else
1308 type = (stab_ptr->n_type & N_TYPE);
1309 if (module->type == DMT_MACHO) type &= ~N_PEXT;
1312 /* only symbol entries contain a typedef */
1313 switch (type)
1315 case N_GSYM:
1316 case N_LCSYM:
1317 case N_STSYM:
1318 case N_RSYM:
1319 case N_LSYM:
1320 case N_ROSYM:
1321 case N_PSYM:
1322 if (strchr(ptr, '=') != NULL)
1325 * The stabs aren't in writable memory, so copy it over so we are
1326 * sure we can scribble on it.
1328 if (ptr != stabbuff)
1330 stabbuff[0] = 0;
1331 stabbuf_append(&stabbuff, &stabbufflen, ptr);
1332 ptr = stabbuff;
1334 if (!stab_strcpy(symname, sizeof(symname), ptr) ||
1335 !stabs_parse_typedef(module, ptr, symname))
1337 /* skip this definition */
1338 stabbuff[0] = '\0';
1339 continue;
1344 switch (type)
1346 case N_GSYM:
1348 * These are useless with ELF. They have no value, and you have to
1349 * read the normal symbol table to get the address. Thus we
1350 * ignore them, and when we process the normal symbol table
1351 * we should do the right thing.
1353 * With a.out or mingw, they actually do make some amount of sense.
1355 if (!stab_strcpy(symname, sizeof(symname), ptr))
1357 ERR("symbol too long: %s\n", debugstr_a(ptr));
1358 stabbuff[0] = '\0';
1359 continue;
1361 loc.kind = loc_absolute;
1362 loc.reg = 0;
1363 loc.offset = load_offset + n_value;
1364 symt_new_global_variable(module, compiland, symname, TRUE /* FIXME */,
1365 loc, 0, stabs_parse_type(ptr));
1366 break;
1367 case N_LCSYM:
1368 case N_STSYM:
1369 /* These are static symbols and BSS symbols. */
1370 if (!stab_strcpy(symname, sizeof(symname), ptr))
1372 ERR("symbol too long: %s\n", debugstr_a(ptr));
1373 stabbuff[0] = '\0';
1374 continue;
1376 loc.kind = loc_absolute;
1377 loc.reg = 0;
1378 loc.offset = load_offset + n_value;
1379 symt_new_global_variable(module, compiland, symname, TRUE /* FIXME */,
1380 loc, 0, stabs_parse_type(ptr));
1381 break;
1382 case N_LBRAC:
1383 if (curr_func)
1385 block = symt_open_func_block(module, curr_func, block,
1386 n_value, 0);
1387 pending_flush(&pending_block, module, curr_func, block);
1389 break;
1390 case N_RBRAC:
1391 if (curr_func)
1392 block = symt_close_func_block(module, curr_func, block,
1393 n_value);
1394 break;
1395 case N_PSYM:
1396 /* These are function parameters. */
1397 if (curr_func != NULL)
1399 struct symt* param_type = stabs_parse_type(ptr);
1400 if (!stab_strcpy(symname, sizeof(symname), ptr))
1402 ERR("symbol too long: %s\n", debugstr_a(ptr));
1403 stabbuff[0] = '\0';
1404 continue;
1406 loc.kind = loc_regrel;
1407 loc.reg = dbghelp_current_cpu->frame_regno;
1408 loc.offset = n_value;
1409 symt_add_func_local(module, curr_func,
1410 (int)n_value >= 0 ? DataIsParam : DataIsLocal,
1411 &loc, NULL, param_type, symname);
1412 symt_add_function_signature_parameter(module,
1413 (struct symt_function_signature*)curr_func->type,
1414 param_type);
1416 break;
1417 case N_RSYM:
1418 /* These are registers (as local variables) */
1419 if (curr_func != NULL)
1421 loc.kind = loc_register;
1422 loc.offset = 0;
1424 switch (n_value)
1426 case 0: loc.reg = CV_REG_EAX; break;
1427 case 1: loc.reg = CV_REG_ECX; break;
1428 case 2: loc.reg = CV_REG_EDX; break;
1429 case 3: loc.reg = CV_REG_EBX; break;
1430 case 4: loc.reg = CV_REG_ESP; break;
1431 case 5: loc.reg = CV_REG_EBP; break;
1432 case 6: loc.reg = CV_REG_ESI; break;
1433 case 7: loc.reg = CV_REG_EDI; break;
1434 case 11:
1435 case 12:
1436 case 13:
1437 case 14:
1438 case 15:
1439 case 16:
1440 case 17:
1441 case 18:
1442 case 19: loc.reg = CV_REG_ST0 + n_value - 12; break;
1443 case 21:
1444 case 22:
1445 case 23:
1446 case 24:
1447 case 25:
1448 case 26:
1449 case 27:
1450 case 28: loc.reg = CV_REG_XMM0 + n_value - 21; break;
1451 case 29:
1452 case 30:
1453 case 31:
1454 case 32:
1455 case 33:
1456 case 34:
1457 case 35:
1458 case 36: loc.reg = CV_REG_MM0 + n_value - 29; break;
1459 default:
1460 FIXME("Unknown register value (%lu)\n", (ULONG_PTR)n_value);
1461 loc.reg = CV_REG_NONE;
1462 break;
1464 if (!stab_strcpy(symname, sizeof(symname), ptr))
1466 ERR("symbol too long: %s\n", debugstr_a(ptr));
1467 stabbuff[0] = '\0';
1468 continue;
1470 if (ptr[strlen(symname) + 1] == 'P')
1472 struct symt* param_type = stabs_parse_type(ptr);
1473 stab_strcpy(symname, sizeof(symname), ptr);
1474 symt_add_func_local(module, curr_func, DataIsParam, &loc,
1475 NULL, param_type, symname);
1476 symt_add_function_signature_parameter(module,
1477 (struct symt_function_signature*)curr_func->type,
1478 param_type);
1480 else
1481 pending_add_var(&pending_block, ptr, DataIsLocal, &loc);
1483 break;
1484 case N_LSYM:
1485 /* These are local variables */
1486 loc.kind = loc_regrel;
1487 loc.reg = dbghelp_current_cpu->frame_regno;
1488 loc.offset = n_value;
1489 if (curr_func != NULL) pending_add_var(&pending_block, ptr, DataIsLocal, &loc);
1490 break;
1491 case N_SLINE:
1493 * This is a line number. These are always relative to the start
1494 * of the function (N_FUN), and this makes the lookup easier.
1496 assert(source_idx >= 0);
1497 if (curr_func != NULL)
1499 ULONG_PTR offset = n_value;
1500 if (module->type == DMT_MACHO)
1501 offset -= curr_func->address - load_offset;
1502 symt_add_func_line(module, curr_func, source_idx,
1503 stab_ptr->n_desc, offset);
1505 else pending_add_line(&pending_func, source_idx, stab_ptr->n_desc,
1506 n_value, load_offset);
1507 break;
1508 case N_FUN:
1510 * For now, just declare the various functions. Later
1511 * on, we will add the line number information and the
1512 * local symbols.
1515 * Copy the string to a temp buffer so we
1516 * can kill everything after the ':'. We do
1517 * it this way because otherwise we end up dirtying
1518 * all of the pages related to the stabs, and that
1519 * sucks up swap space like crazy.
1521 if (!stab_strcpy(symname, sizeof(symname), ptr))
1523 ERR("symbol too long: %s\n", debugstr_a(ptr));
1524 stabbuff[0] = '\0';
1525 continue;
1527 if (*symname)
1529 struct symt_function_signature* func_type;
1531 if (curr_func)
1533 /* First, clean up the previous function we were working on.
1534 * Assume size of the func is the delta between current offset
1535 * and offset of last function
1537 stabs_finalize_function(module, curr_func,
1538 n_value ?
1539 (load_offset + n_value - curr_func->address) : 0);
1541 func_type = symt_new_function_signature(module,
1542 stabs_parse_type(ptr), -1);
1543 curr_func = symt_new_function(module, compiland, symname,
1544 load_offset + n_value, 0,
1545 &func_type->symt);
1546 pending_flush(&pending_func, module, curr_func, NULL);
1548 else
1550 /* some versions of GCC to use a N_FUN "" to mark the end of a function
1551 * and n_value contains the size of the func
1553 stabs_finalize_function(module, curr_func, n_value);
1554 curr_func = NULL;
1556 break;
1557 case N_SO:
1559 * This indicates a new source file. Append the records
1560 * together, to build the correct path name.
1562 if (*ptr == '\0') /* end of N_SO file */
1564 /* Nuke old path. */
1565 HeapFree(GetProcessHeap(), 0, srcpath);
1566 srcpath = NULL;
1567 stabs_finalize_function(module, curr_func, 0);
1568 curr_func = NULL;
1569 source_idx = -1;
1570 incl_stk = -1;
1571 assert(block == NULL);
1572 compiland = NULL;
1574 else
1576 int len = strlen(ptr);
1577 if (ptr[len-1] != '/')
1579 stabs_reset_includes();
1580 source_idx = source_new(module, srcpath, ptr);
1581 compiland = symt_new_compiland(module, 0 /* FIXME */, source_idx);
1583 else
1585 srcpath = HeapAlloc(GetProcessHeap(), 0, len + 1);
1586 strcpy(srcpath, ptr);
1589 break;
1590 case N_SOL:
1591 source_idx = source_new(module, srcpath, ptr);
1592 break;
1593 case N_UNDF:
1594 strs += strtabinc;
1595 strtabinc = n_value;
1596 /* I'm not sure this is needed, so trace it before we obsolete it */
1597 if (curr_func)
1599 FIXME("UNDF: curr_func %s\n", curr_func->hash_elt.name);
1600 stabs_finalize_function(module, curr_func, 0); /* FIXME */
1601 curr_func = NULL;
1603 break;
1604 case N_OPT:
1605 /* Ignore this. We don't care what it points to. */
1606 break;
1607 case N_BINCL:
1608 stabs_add_include(stabs_new_include(ptr, n_value));
1609 assert(incl_stk < (int) ARRAY_SIZE(incl) - 1);
1610 incl[++incl_stk] = source_idx;
1611 source_idx = source_new(module, NULL, ptr);
1612 break;
1613 case N_EINCL:
1614 assert(incl_stk >= 0);
1615 source_idx = incl[incl_stk--];
1616 break;
1617 case N_EXCL:
1618 if (stabs_add_include(stabs_find_include(ptr, n_value)) < 0)
1620 ERR("Excluded header not found (%s,%ld)\n", ptr, (ULONG_PTR)n_value);
1621 module_reset_debug_info(module);
1622 ret = FALSE;
1623 goto done;
1625 break;
1626 case N_MAIN:
1627 /* Always ignore these. GCC doesn't even generate them. */
1628 break;
1629 case N_BNSYM:
1630 case N_ENSYM:
1631 case N_OSO:
1632 case N_INDR:
1633 /* Always ignore these, they seem to be used only on Darwin. */
1634 break;
1635 case N_ABS:
1636 case N_SECT:
1637 /* FIXME: Other definition types (N_TEXT, N_DATA, N_BSS, ...)? */
1638 if (callback)
1640 BOOL is_public = (stab_ptr->n_type & N_EXT);
1641 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)
1647 is_public = FALSE;
1648 is_global = TRUE;
1651 if (*ptr == '_') ptr++;
1652 if (!stab_strcpy(symname, sizeof(symname), ptr))
1654 ERR("symbol too long: %s\n", debugstr_a(ptr));
1655 stabbuff[0] = '\0';
1656 continue;
1659 callback(module, load_offset, symname, n_value,
1660 is_public, is_global, stab_ptr->n_other, compiland, user);
1662 break;
1663 default:
1664 ERR("Unknown stab type 0x%02x\n", type);
1665 break;
1667 stabbuff[0] = '\0';
1668 TRACE("0x%02x %lx %s\n",
1669 stab_ptr->n_type, (ULONG_PTR)n_value, debugstr_a(strs + stab_ptr->n_strx));
1671 module->module.SymType = SymDia;
1672 module->module.CVSig = 'S' | ('T' << 8) | ('A' << 16) | ('B' << 24);
1673 /* FIXME: we could have a finer grain here */
1674 module->module.LineNumbers = TRUE;
1675 module->module.GlobalSymbols = TRUE;
1676 module->module.TypeInfo = TRUE;
1677 module->module.SourceIndexed = TRUE;
1678 module->module.Publics = TRUE;
1679 done:
1680 HeapFree(GetProcessHeap(), 0, stabbuff);
1681 stabs_free_includes();
1682 HeapFree(GetProcessHeap(), 0, pending_block.objs);
1683 HeapFree(GetProcessHeap(), 0, pending_func.objs);
1684 HeapFree(GetProcessHeap(), 0, srcpath);
1686 return ret;