winex11.drv: Mark xrenderdrv_ExtTextOut as static.
[wine.git] / dlls / dbghelp / stabs.c
blob1bdd706c1e77ceab311eea7eeab149c3d57ab489
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 "config.h"
33 #include "wine/port.h"
35 #include <sys/types.h>
36 #include <fcntl.h>
37 #ifdef HAVE_SYS_STAT_H
38 # include <sys/stat.h>
39 #endif
40 #ifdef HAVE_SYS_MMAN_H
41 #include <sys/mman.h>
42 #endif
43 #include <limits.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #ifdef HAVE_UNISTD_H
47 # include <unistd.h>
48 #endif
49 #include <stdio.h>
50 #ifndef PATH_MAX
51 #define PATH_MAX MAX_PATH
52 #endif
53 #include <assert.h>
54 #include <stdarg.h>
56 #ifdef HAVE_MACH_O_NLIST_H
57 # include <mach-o/nlist.h>
58 #endif
60 #include "windef.h"
61 #include "winbase.h"
62 #include "winnls.h"
64 #include "dbghelp_private.h"
66 #include "wine/debug.h"
68 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp_stabs);
70 /* Masks for n_type field */
71 #ifndef N_STAB
72 #define N_STAB 0xe0
73 #endif
74 #ifndef N_TYPE
75 #define N_TYPE 0x1e
76 #endif
77 #ifndef N_EXT
78 #define N_EXT 0x01
79 #endif
81 /* Values for (n_type & N_TYPE) */
82 #ifndef N_UNDF
83 #define N_UNDF 0x00
84 #endif
85 #ifndef N_ABS
86 #define N_ABS 0x02
87 #endif
89 #define N_GSYM 0x20
90 #define N_FUN 0x24
91 #define N_STSYM 0x26
92 #define N_LCSYM 0x28
93 #define N_MAIN 0x2a
94 #define N_ROSYM 0x2c
95 #define N_BNSYM 0x2e
96 #define N_OPT 0x3c
97 #define N_RSYM 0x40
98 #define N_SLINE 0x44
99 #define N_ENSYM 0x4e
100 #define N_SO 0x64
101 #define N_OSO 0x66
102 #define N_LSYM 0x80
103 #define N_BINCL 0x82
104 #define N_SOL 0x84
105 #define N_PSYM 0xa0
106 #define N_EINCL 0xa2
107 #define N_LBRAC 0xc0
108 #define N_EXCL 0xc2
109 #define N_RBRAC 0xe0
111 struct stab_nlist
113 unsigned n_strx;
114 unsigned char n_type;
115 char n_other;
116 short n_desc;
117 unsigned n_value;
120 static void stab_strcpy(char* dest, int sz, const char* source)
122 char* ptr = dest;
124 * A strcpy routine that stops when we hit the ':' character.
125 * Faster than copying the whole thing, and then nuking the
126 * ':'.
127 * Takes also care of (valid) a::b constructs
129 while (*source != '\0')
131 if (source[0] != ':' && sz-- > 0) *ptr++ = *source++;
132 else if (source[1] == ':' && (sz -= 2) > 0)
134 *ptr++ = *source++;
135 *ptr++ = *source++;
137 else break;
139 *ptr-- = '\0';
140 /* GCC emits, in some cases, a .<digit>+ suffix.
141 * This is used for static variable inside functions, so
142 * that we can have several such variables with same name in
143 * the same compilation unit
144 * We simply ignore that suffix when present (we also get rid
145 * of it in ELF symtab parsing)
147 if (ptr >= dest && isdigit(*ptr))
149 while (ptr > dest && isdigit(*ptr)) ptr--;
150 if (*ptr == '.') *ptr = '\0';
152 assert(sz > 0);
155 typedef struct
157 char* name;
158 unsigned long value;
159 struct symt** vector;
160 int nrofentries;
161 } include_def;
163 #define MAX_INCLUDES 5120
165 static include_def* include_defs = NULL;
166 static int num_include_def = 0;
167 static int num_alloc_include_def = 0;
168 static int cu_include_stack[MAX_INCLUDES];
169 static int cu_include_stk_idx = 0;
170 static struct symt** cu_vector = NULL;
171 static int cu_nrofentries = 0;
172 static struct symt_basic* stabs_basic[36];
174 static int stabs_new_include(const char* file, unsigned long val)
176 if (num_include_def == num_alloc_include_def)
178 if (!include_defs)
180 num_alloc_include_def = 256;
181 include_defs = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
182 sizeof(include_defs[0]) * num_alloc_include_def);
184 else
186 num_alloc_include_def *= 2;
187 include_defs = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, include_defs,
188 sizeof(include_defs[0]) * num_alloc_include_def);
191 include_defs[num_include_def].name = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(file) + 1), file);
192 include_defs[num_include_def].value = val;
193 include_defs[num_include_def].vector = NULL;
194 include_defs[num_include_def].nrofentries = 0;
196 return num_include_def++;
199 static int stabs_find_include(const char* file, unsigned long val)
201 int i;
203 for (i = 0; i < num_include_def; i++)
205 if (val == include_defs[i].value &&
206 strcmp(file, include_defs[i].name) == 0)
207 return i;
209 return -1;
212 static int stabs_add_include(int idx)
214 if (idx < 0) return -1;
215 cu_include_stk_idx++;
217 /* if this happens, just bump MAX_INCLUDES */
218 /* we could also handle this as another dynarray */
219 assert(cu_include_stk_idx < MAX_INCLUDES);
220 cu_include_stack[cu_include_stk_idx] = idx;
221 return cu_include_stk_idx;
224 static void stabs_reset_includes(void)
227 * The struct symt:s that we would need to use are reset when
228 * we start a new file. (at least the ones in filenr == 0)
230 cu_include_stk_idx = 0;/* keep 0 as index for the .c file itself */
231 memset(cu_vector, 0, sizeof(cu_vector[0]) * cu_nrofentries);
234 static void stabs_free_includes(void)
236 int i;
238 stabs_reset_includes();
239 for (i = 0; i < num_include_def; i++)
241 HeapFree(GetProcessHeap(), 0, include_defs[i].name);
242 HeapFree(GetProcessHeap(), 0, include_defs[i].vector);
244 HeapFree(GetProcessHeap(), 0, include_defs);
245 include_defs = NULL;
246 num_include_def = 0;
247 num_alloc_include_def = 0;
248 HeapFree(GetProcessHeap(), 0, cu_vector);
249 cu_vector = NULL;
250 cu_nrofentries = 0;
253 static struct symt** stabs_find_ref(long filenr, long subnr)
255 struct symt** ret;
257 /* FIXME: I could perhaps create a dummy include_def for each compilation
258 * unit which would allow not to handle those two cases separately
260 if (filenr == 0)
262 if (cu_nrofentries <= subnr)
264 cu_nrofentries = max( cu_nrofentries * 2, subnr + 1 );
265 if (!cu_vector)
266 cu_vector = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
267 sizeof(cu_vector[0]) * cu_nrofentries);
268 else
269 cu_vector = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
270 cu_vector, sizeof(cu_vector[0]) * cu_nrofentries);
272 ret = &cu_vector[subnr];
274 else
276 include_def* idef;
278 assert(filenr <= cu_include_stk_idx);
279 idef = &include_defs[cu_include_stack[filenr]];
281 if (idef->nrofentries <= subnr)
283 idef->nrofentries = max( idef->nrofentries * 2, subnr + 1 );
284 if (!idef->vector)
285 idef->vector = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
286 sizeof(idef->vector[0]) * idef->nrofentries);
287 else
288 idef->vector = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
289 idef->vector, sizeof(idef->vector[0]) * idef->nrofentries);
291 ret = &idef->vector[subnr];
293 TRACE("(%ld,%ld) => %p (%p)\n", filenr, subnr, ret, *ret);
294 return ret;
297 static struct symt** stabs_read_type_enum(const char** x)
299 long filenr, subnr;
300 const char* iter;
301 char* end;
303 iter = *x;
304 if (*iter == '(')
306 ++iter; /* '(' */
307 filenr = strtol(iter, &end, 10); /* <int> */
308 iter = ++end; /* ',' */
309 subnr = strtol(iter, &end, 10); /* <int> */
310 iter = ++end; /* ')' */
312 else
314 filenr = 0;
315 subnr = strtol(iter, &end, 10); /* <int> */
316 iter = end;
318 *x = iter;
319 return stabs_find_ref(filenr, subnr);
322 #define PTS_DEBUG
323 struct ParseTypedefData
325 const char* ptr;
326 char buf[1024];
327 int idx;
328 struct module* module;
329 #ifdef PTS_DEBUG
330 struct PTS_Error
332 const char* ptr;
333 unsigned line;
334 } errors[16];
335 int err_idx;
336 #endif
339 #ifdef PTS_DEBUG
340 static void stabs_pts_push(struct ParseTypedefData* ptd, unsigned line)
342 assert(ptd->err_idx < sizeof(ptd->errors) / sizeof(ptd->errors[0]));
343 ptd->errors[ptd->err_idx].line = line;
344 ptd->errors[ptd->err_idx].ptr = ptd->ptr;
345 ptd->err_idx++;
347 #define PTS_ABORTIF(ptd, t) do { if (t) { stabs_pts_push((ptd), __LINE__); return -1;} } while (0)
348 #else
349 #define PTS_ABORTIF(ptd, t) do { if (t) return -1; } while (0)
350 #endif
352 static int stabs_get_basic(struct ParseTypedefData* ptd, unsigned basic, struct symt** symt)
354 PTS_ABORTIF(ptd, basic >= sizeof(stabs_basic) / sizeof(stabs_basic[0]));
356 if (!stabs_basic[basic])
358 switch (basic)
360 case 1: stabs_basic[basic] = symt_new_basic(ptd->module, btInt, "int", 4); break;
361 case 2: stabs_basic[basic] = symt_new_basic(ptd->module, btChar, "char", 1); break;
362 case 3: stabs_basic[basic] = symt_new_basic(ptd->module, btInt, "short int", 2); break;
363 case 4: stabs_basic[basic] = symt_new_basic(ptd->module, btInt, "long int", 4); break;
364 case 5: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt, "unsigned char", 1); break;
365 case 6: stabs_basic[basic] = symt_new_basic(ptd->module, btInt, "signed char", 1); break;
366 case 7: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt, "unsigned short int", 2); break;
367 case 8: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt, "unsigned int", 4); break;
368 case 9: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt, "unsigned", 2); break;
369 case 10: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt, "unsigned long int", 2); break;
370 case 11: stabs_basic[basic] = symt_new_basic(ptd->module, btVoid, "void", 0); break;
371 case 12: stabs_basic[basic] = symt_new_basic(ptd->module, btFloat, "float", 4); break;
372 case 13: stabs_basic[basic] = symt_new_basic(ptd->module, btFloat, "double", 8); break;
373 case 14: stabs_basic[basic] = symt_new_basic(ptd->module, btFloat, "long double", 12); break;
374 case 15: stabs_basic[basic] = symt_new_basic(ptd->module, btInt, "integer", 4); break;
375 case 16: stabs_basic[basic] = symt_new_basic(ptd->module, btBool, "bool", 1); break;
376 /* case 17: short real */
377 /* case 18: real */
378 case 25: stabs_basic[basic] = symt_new_basic(ptd->module, btComplex, "float complex", 8); break;
379 case 26: stabs_basic[basic] = symt_new_basic(ptd->module, btComplex, "double complex", 16); break;
380 case 30: stabs_basic[basic] = symt_new_basic(ptd->module, btWChar, "wchar_t", 2); break;
381 case 31: stabs_basic[basic] = symt_new_basic(ptd->module, btInt, "long long int", 8); break;
382 case 32: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt, "long long unsigned", 8); break;
383 /* starting at 35 are wine extensions (especially for R implementation) */
384 case 35: stabs_basic[basic] = symt_new_basic(ptd->module, btComplex, "long double complex", 24); break;
385 default: PTS_ABORTIF(ptd, 1);
388 *symt = &stabs_basic[basic]->symt;
389 return 0;
392 static int stabs_pts_read_type_def(struct ParseTypedefData* ptd,
393 const char* typename, struct symt** dt);
395 static int stabs_pts_read_id(struct ParseTypedefData* ptd)
397 const char* first = ptd->ptr;
398 unsigned int template = 0;
399 char ch;
401 while ((ch = *ptd->ptr++) != '\0')
403 switch (ch)
405 case ':':
406 if (template == 0)
408 unsigned int len = ptd->ptr - first - 1;
409 PTS_ABORTIF(ptd, len >= sizeof(ptd->buf) - ptd->idx);
410 memcpy(ptd->buf + ptd->idx, first, len);
411 ptd->buf[ptd->idx + len] = '\0';
412 ptd->idx += len + 1;
413 return 0;
415 break;
416 case '<': template++; break;
417 case '>': PTS_ABORTIF(ptd, template == 0); template--; break;
420 return -1;
423 static int stabs_pts_read_number(struct ParseTypedefData* ptd, long* v)
425 char* last;
427 *v = strtol(ptd->ptr, &last, 10);
428 PTS_ABORTIF(ptd, last == ptd->ptr);
429 ptd->ptr = last;
430 return 0;
433 static int stabs_pts_read_type_reference(struct ParseTypedefData* ptd,
434 long* filenr, long* subnr)
436 if (*ptd->ptr == '(')
438 /* '(' <int> ',' <int> ')' */
439 ptd->ptr++;
440 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, filenr) == -1);
441 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
442 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, subnr) == -1);
443 PTS_ABORTIF(ptd, *ptd->ptr++ != ')');
445 else
447 *filenr = 0;
448 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, subnr) == -1);
450 return 0;
453 struct pts_range_value
455 ULONGLONG val;
456 int sign;
459 static int stabs_pts_read_range_value(struct ParseTypedefData* ptd, struct pts_range_value* prv)
461 char* last;
463 switch (*ptd->ptr)
465 case '0':
466 while (*ptd->ptr == '0') ptd->ptr++;
467 if (*ptd->ptr >= '1' && *ptd->ptr <= '7')
469 switch (ptd->ptr[1])
471 case '0':
472 PTS_ABORTIF(ptd, ptd->ptr[0] != '1');
473 prv->sign = -1;
474 prv->val = 0;
475 while (isdigit(*ptd->ptr)) prv->val = (prv->val << 3) + *ptd->ptr++ - '0';
476 break;
477 case '7':
478 prv->sign = 1;
479 prv->val = 0;
480 while (isdigit(*ptd->ptr)) prv->val = (prv->val << 3) + *ptd->ptr++ - '0';
481 break;
482 default: PTS_ABORTIF(ptd, 1); break;
484 } else prv->sign = 0;
485 break;
486 case '-':
487 prv->sign = -1;
488 prv->val = strtoull(++ptd->ptr, &last, 10);
489 ptd->ptr = last;
490 break;
491 case '+':
492 default:
493 prv->sign = 1;
494 prv->val = strtoull(ptd->ptr, &last, 10);
495 ptd->ptr = last;
496 break;
498 return 0;
501 static int stabs_pts_read_range(struct ParseTypedefData* ptd, const char* typename,
502 struct symt** dt)
504 struct symt* ref;
505 struct pts_range_value lo;
506 struct pts_range_value hi;
507 unsigned size;
508 enum BasicType bt;
509 int i;
510 ULONGLONG v;
512 /* type ';' <int> ';' <int> ';' */
513 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &ref) == -1);
514 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
515 PTS_ABORTIF(ptd, stabs_pts_read_range_value(ptd, &lo) == -1);
516 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
517 PTS_ABORTIF(ptd, stabs_pts_read_range_value(ptd, &hi) == -1);
518 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
520 /* basically, we don't use ref... in some cases, for example, float is declared
521 * as a derived type of int... which won't help us... so we guess the types
522 * from the various formats
524 if (lo.sign == 0 && hi.sign < 0)
526 bt = btUInt;
527 size = hi.val;
529 else if (lo.sign < 0 && hi.sign == 0)
531 bt = btUInt;
532 size = lo.val;
534 else if (lo.sign > 0 && hi.sign == 0)
536 bt = btFloat;
537 size = lo.val;
539 else if (lo.sign < 0 && hi.sign > 0)
541 v = 1 << 7;
542 for (i = 7; i < 64; i += 8)
544 if (lo.val == v && hi.val == v - 1)
546 bt = btInt;
547 size = (i + 1) / 8;
548 break;
550 v <<= 8;
552 PTS_ABORTIF(ptd, i >= 64);
554 else if (lo.sign == 0 && hi.sign > 0)
556 if (hi.val == 127) /* specific case for char... */
558 bt = btChar;
559 size = 1;
561 else
563 v = 1;
564 for (i = 8; i <= 64; i += 8)
566 v <<= 8;
567 if (hi.val + 1 == v)
569 bt = btUInt;
570 size = (i + 1) / 8;
571 break;
574 PTS_ABORTIF(ptd, i > 64);
577 else PTS_ABORTIF(ptd, 1);
579 *dt = &symt_new_basic(ptd->module, bt, typename, size)->symt;
580 return 0;
583 static inline int stabs_pts_read_method_info(struct ParseTypedefData* ptd)
585 struct symt* dt;
586 const char* tmp;
587 char mthd;
591 /* get type of return value */
592 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
593 if (*ptd->ptr == ';') ptd->ptr++;
595 /* get types of parameters */
596 if (*ptd->ptr == ':')
598 PTS_ABORTIF(ptd, !(tmp = strchr(ptd->ptr + 1, ';')));
599 ptd->ptr = tmp + 1;
601 PTS_ABORTIF(ptd, !(*ptd->ptr >= '0' && *ptd->ptr <= '9'));
602 ptd->ptr++;
603 PTS_ABORTIF(ptd, !(ptd->ptr[0] >= 'A' && *ptd->ptr <= 'D'));
604 mthd = *++ptd->ptr;
605 PTS_ABORTIF(ptd, mthd != '.' && mthd != '?' && mthd != '*');
606 ptd->ptr++;
607 if (mthd == '*')
609 long int ofs;
610 struct symt* dt;
612 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &ofs) == -1);
613 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
614 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
615 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
617 } while (*ptd->ptr != ';');
618 ptd->ptr++;
620 return 0;
623 static inline int stabs_pts_read_aggregate(struct ParseTypedefData* ptd,
624 struct symt_udt* sdt)
626 long sz, ofs;
627 struct symt* adt;
628 struct symt* dt = NULL;
629 int idx;
630 int doadd;
632 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &sz) == -1);
634 doadd = symt_set_udt_size(ptd->module, sdt, sz);
635 if (*ptd->ptr == '!') /* C++ inheritence */
637 long num_classes;
639 ptd->ptr++;
640 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &num_classes) == -1);
641 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
642 while (--num_classes >= 0)
644 ptd->ptr += 2; /* skip visibility and inheritence */
645 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &ofs) == -1);
646 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
648 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &adt) == -1);
650 if (doadd && adt)
652 char tmp[256];
653 DWORD64 size;
655 strcpy(tmp, "__inherited_class_");
656 strcat(tmp, symt_get_name(adt));
658 /* FIXME: TI_GET_LENGTH will not always work, especially when adt
659 * has just been seen as a forward definition and not the real stuff
660 * yet.
661 * As we don't use much the size of members in structs, this may not
662 * be much of a problem
664 symt_get_info(ptd->module, adt, TI_GET_LENGTH, &size);
665 symt_add_udt_element(ptd->module, sdt, tmp, adt, ofs, (DWORD)size * 8);
667 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
671 /* if the structure has already been filled, just redo the parsing
672 * but don't store results into the struct
673 * FIXME: there's a quite ugly memory leak in there...
676 /* Now parse the individual elements of the structure/union. */
677 while (*ptd->ptr != ';')
679 /* agg_name : type ',' <int:offset> ',' <int:size> */
680 idx = ptd->idx;
682 if (ptd->ptr[0] == '$' && ptd->ptr[1] == 'v')
684 long x;
686 if (ptd->ptr[2] == 'f')
688 /* C++ virtual method table */
689 ptd->ptr += 3;
690 stabs_read_type_enum(&ptd->ptr);
691 PTS_ABORTIF(ptd, *ptd->ptr++ != ':');
692 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
693 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
694 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &x) == -1);
695 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
696 ptd->idx = idx;
697 continue;
699 else if (ptd->ptr[2] == 'b')
701 ptd->ptr += 3;
702 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
703 PTS_ABORTIF(ptd, *ptd->ptr++ != ':');
704 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
705 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
706 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &x) == -1);
707 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
708 ptd->idx = idx;
709 continue;
713 PTS_ABORTIF(ptd, stabs_pts_read_id(ptd) == -1);
714 /* Ref. TSDF R2.130 Section 7.4. When the field name is a method name
715 * it is followed by two colons rather than one.
717 if (*ptd->ptr == ':')
719 ptd->ptr++;
720 stabs_pts_read_method_info(ptd);
721 ptd->idx = idx;
722 continue;
724 else
726 /* skip C++ member protection /0 /1 or /2 */
727 if (*ptd->ptr == '/') ptd->ptr += 2;
729 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &adt) == -1);
731 switch (*ptd->ptr++)
733 case ',':
734 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &ofs) == -1);
735 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
736 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &sz) == -1);
737 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
739 if (doadd) symt_add_udt_element(ptd->module, sdt, ptd->buf + idx, adt, ofs, sz);
740 break;
741 case ':':
743 const char* tmp;
744 /* method parameters... terminated by ';' */
745 PTS_ABORTIF(ptd, !(tmp = strchr(ptd->ptr, ';')));
746 ptd->ptr = tmp + 1;
748 break;
749 default:
750 PTS_ABORTIF(ptd, TRUE);
752 ptd->idx = idx;
754 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
755 if (*ptd->ptr == '~')
757 ptd->ptr++;
758 PTS_ABORTIF(ptd, *ptd->ptr++ != '%');
759 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
760 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
762 return 0;
765 static inline int stabs_pts_read_enum(struct ParseTypedefData* ptd,
766 struct symt_enum* edt)
768 long value;
769 int idx;
771 while (*ptd->ptr != ';')
773 idx = ptd->idx;
774 PTS_ABORTIF(ptd, stabs_pts_read_id(ptd) == -1);
775 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &value) == -1);
776 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
777 symt_add_enum_element(ptd->module, edt, ptd->buf + idx, value);
778 ptd->idx = idx;
780 ptd->ptr++;
781 return 0;
784 static inline int stabs_pts_read_array(struct ParseTypedefData* ptd,
785 struct symt** adt)
787 long lo, hi;
788 struct symt* range_dt;
789 struct symt* base_dt;
791 /* ar<typeinfo_nodef>;<int>;<int>;<typeinfo> */
793 PTS_ABORTIF(ptd, *ptd->ptr++ != 'r');
795 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &range_dt) == -1);
796 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
797 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &lo) == -1);
798 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
799 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &hi) == -1);
800 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
802 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &base_dt) == -1);
804 *adt = &symt_new_array(ptd->module, lo, hi, base_dt, range_dt)->symt;
805 return 0;
808 static int stabs_pts_read_type_def(struct ParseTypedefData* ptd, const char* typename,
809 struct symt** ret_dt)
811 int idx;
812 long sz = -1;
813 struct symt* new_dt = NULL; /* newly created data type */
814 struct symt* ref_dt; /* referenced data type (pointer...) */
815 long filenr1, subnr1, tmp;
817 /* things are a bit complicated because of the way the typedefs are stored inside
818 * the file, because addresses can change when realloc is done, so we must call
819 * over and over stabs_find_ref() to keep the correct values around
821 PTS_ABORTIF(ptd, stabs_pts_read_type_reference(ptd, &filenr1, &subnr1) == -1);
823 while (*ptd->ptr == '=')
825 ptd->ptr++;
826 PTS_ABORTIF(ptd, new_dt != NULL);
828 /* first handle attribute if any */
829 switch (*ptd->ptr)
831 case '@':
832 if (*++ptd->ptr == 's')
834 ptd->ptr++;
835 if (stabs_pts_read_number(ptd, &sz) == -1)
837 ERR("Not an attribute... NIY\n");
838 ptd->ptr -= 2;
839 return -1;
841 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
843 break;
845 /* then the real definitions */
846 switch (*ptd->ptr++)
848 case '*':
849 case '&':
850 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &ref_dt) == -1);
851 new_dt = &symt_new_pointer(ptd->module, ref_dt, sizeof(void*))->symt;
852 break;
853 case 'k': /* 'const' modifier */
854 case 'B': /* 'volatile' modifier */
855 /* just kinda ignore the modifier, I guess -gmt */
856 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, typename, &new_dt) == -1);
857 break;
858 case '(':
859 ptd->ptr--;
860 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, typename, &new_dt) == -1);
861 break;
862 case 'a':
863 PTS_ABORTIF(ptd, stabs_pts_read_array(ptd, &new_dt) == -1);
864 break;
865 case 'r':
866 PTS_ABORTIF(ptd, stabs_pts_read_range(ptd, typename, &new_dt) == -1);
867 assert(!*stabs_find_ref(filenr1, subnr1));
868 *stabs_find_ref(filenr1, subnr1) = new_dt;
869 break;
870 case 'f':
871 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &ref_dt) == -1);
872 new_dt = &symt_new_function_signature(ptd->module, ref_dt, -1)->symt;
873 break;
874 case 'e':
875 stabs_get_basic(ptd, 1 /* int */, &ref_dt);
876 new_dt = &symt_new_enum(ptd->module, typename, ref_dt)->symt;
877 PTS_ABORTIF(ptd, stabs_pts_read_enum(ptd, (struct symt_enum*)new_dt) == -1);
878 break;
879 case 's':
880 case 'u':
882 struct symt_udt* udt;
883 enum UdtKind kind = (ptd->ptr[-1] == 's') ? UdtStruct : UdtUnion;
884 /* udt can have been already defined in a forward definition */
885 udt = (struct symt_udt*)*stabs_find_ref(filenr1, subnr1);
886 if (!udt)
888 udt = symt_new_udt(ptd->module, typename, 0, kind);
889 /* we need to set it here, because a struct can hold a pointer
890 * to itself
892 new_dt = *stabs_find_ref(filenr1, subnr1) = &udt->symt;
894 else
896 unsigned l1, l2;
897 if (udt->symt.tag != SymTagUDT)
899 ERR("Forward declaration (%p/%s) is not an aggregate (%u)\n",
900 udt, symt_get_name(&udt->symt), udt->symt.tag);
901 return -1;
903 /* FIXME: we currently don't correctly construct nested C++
904 * classes names. Therefore, we could be here with either:
905 * - typename and udt->hash_elt.name being the same string
906 * (non embedded case)
907 * - typename being foo::bar while udt->hash_elt.name being
908 * just bar
909 * So, we twist the comparison to test both occurrences. When
910 * we have proper C++ types in this file, this twist has to be
911 * removed
913 l1 = strlen(udt->hash_elt.name);
914 l2 = strlen(typename);
915 if (l1 > l2 || strcmp(udt->hash_elt.name, typename + l2 - l1))
916 ERR("Forward declaration name mismatch %s <> %s\n",
917 udt->hash_elt.name, typename);
918 new_dt = &udt->symt;
920 PTS_ABORTIF(ptd, stabs_pts_read_aggregate(ptd, udt) == -1);
922 break;
923 case 'x':
924 idx = ptd->idx;
925 tmp = *ptd->ptr++;
926 PTS_ABORTIF(ptd, stabs_pts_read_id(ptd) == -1);
927 switch (tmp)
929 case 'e':
930 stabs_get_basic(ptd, 1 /* int */, &ref_dt);
931 new_dt = &symt_new_enum(ptd->module, ptd->buf + idx, ref_dt)->symt;
932 break;
933 case 's':
934 new_dt = &symt_new_udt(ptd->module, ptd->buf + idx, 0, UdtStruct)->symt;
935 break;
936 case 'u':
937 new_dt = &symt_new_udt(ptd->module, ptd->buf + idx, 0, UdtUnion)->symt;
938 break;
939 default:
940 return -1;
942 ptd->idx = idx;
943 break;
944 case '-':
946 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &tmp) == -1);
947 PTS_ABORTIF(ptd, stabs_get_basic(ptd, tmp, &new_dt) == -1);
948 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
950 break;
951 case '#':
952 if (*ptd->ptr == '#')
954 ptd->ptr++;
955 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &ref_dt) == -1);
956 new_dt = &symt_new_function_signature(ptd->module, ref_dt, -1)->symt;
958 else
960 struct symt* cls_dt;
961 struct symt* pmt_dt;
963 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &cls_dt) == -1);
964 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
965 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &ref_dt) == -1);
966 new_dt = &symt_new_function_signature(ptd->module, ref_dt, -1)->symt;
967 while (*ptd->ptr == ',')
969 ptd->ptr++;
970 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &pmt_dt) == -1);
973 break;
974 case 'R':
976 long type, len, unk;
977 int basic;
979 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &type) == -1);
980 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
981 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &len) == -1);
982 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
983 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &unk) == -1);
984 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
986 switch (type) /* see stabs_get_basic for the details */
988 case 1: basic = 12; break;
989 case 2: basic = 13; break;
990 case 3: basic = 25; break;
991 case 4: basic = 26; break;
992 case 5: basic = 35; break;
993 case 6: basic = 14; break;
994 default: PTS_ABORTIF(ptd, 1);
996 PTS_ABORTIF(ptd, stabs_get_basic(ptd, basic, &new_dt) == -1);
998 break;
999 default:
1000 ERR("Unknown type '%c'\n", ptd->ptr[-1]);
1001 return -1;
1005 if (!new_dt)
1007 /* is it a forward declaration that has been filled ? */
1008 new_dt = *stabs_find_ref(filenr1, subnr1);
1009 /* if not, this should be void (which is defined as a ref to itself, but we
1010 * don't correctly catch it)
1012 if (!new_dt && typename)
1014 new_dt = &symt_new_basic(ptd->module, btVoid, typename, 0)->symt;
1015 PTS_ABORTIF(ptd, strcmp(typename, "void"));
1019 *stabs_find_ref(filenr1, subnr1) = *ret_dt = new_dt;
1021 TRACE("Adding (%ld,%ld) %s\n", filenr1, subnr1, debugstr_a(typename));
1023 return 0;
1026 static int stabs_parse_typedef(struct module* module, const char* ptr,
1027 const char* typename)
1029 struct ParseTypedefData ptd;
1030 struct symt* dt;
1031 int ret = -1;
1033 /* check for already existing definition */
1035 TRACE("%s => %s\n", typename, debugstr_a(ptr));
1036 ptd.module = module;
1037 ptd.idx = 0;
1038 #ifdef PTS_DEBUG
1039 ptd.err_idx = 0;
1040 #endif
1041 for (ptd.ptr = ptr - 1; ;)
1043 ptd.ptr = strchr(ptd.ptr + 1, ':');
1044 if (ptd.ptr == NULL || *++ptd.ptr != ':') break;
1046 if (ptd.ptr)
1048 if (*ptd.ptr != '(') ptd.ptr++;
1049 /* most of type definitions take one char, except Tt */
1050 if (*ptd.ptr != '(') ptd.ptr++;
1051 ret = stabs_pts_read_type_def(&ptd, typename, &dt);
1054 if (ret == -1 || *ptd.ptr)
1056 #ifdef PTS_DEBUG
1057 int i;
1058 TRACE("Failure on %s\n", debugstr_a(ptr));
1059 if (ret == -1)
1061 for (i = 0; i < ptd.err_idx; i++)
1063 TRACE("[%d]: line %d => %s\n",
1064 i, ptd.errors[i].line, debugstr_a(ptd.errors[i].ptr));
1067 else
1068 TRACE("[0]: => %s\n", debugstr_a(ptd.ptr));
1070 #else
1071 ERR("Failure on %s at %s\n", debugstr_a(ptr), debugstr_a(ptd.ptr));
1072 #endif
1073 return FALSE;
1076 return TRUE;
1079 static struct symt* stabs_parse_type(const char* stab)
1081 const char* c = stab - 1;
1084 * Look through the stab definition, and figure out what struct symt
1085 * this represents. If we have something we know about, assign the
1086 * type.
1087 * According to "The \"stabs\" debug format" (Rev 2.130) the name may be
1088 * a C++ name and contain double colons e.g. foo::bar::baz:t5=*6.
1092 if ((c = strchr(c + 1, ':')) == NULL) return NULL;
1093 } while (*++c == ':');
1096 * The next characters say more about the type (i.e. data, function, etc)
1097 * of symbol. Skip them. (C++ for example may have Tt).
1098 * Actually this is a very weak description; I think Tt is the only
1099 * multiple combination we should see.
1101 while (*c && *c != '(' && !isdigit(*c))
1102 c++;
1104 * The next is either an integer or a (integer,integer).
1105 * The stabs_read_type_enum() takes care that stab_types is large enough.
1107 return *stabs_read_type_enum(&c);
1110 enum pending_obj_kind
1112 PENDING_VAR,
1113 PENDING_LINE,
1116 struct pending_loc_var
1118 char name[256];
1119 struct symt* type;
1120 enum DataKind kind;
1121 struct location loc;
1124 struct pending_line
1126 int source_idx;
1127 int line_num;
1128 unsigned long offset;
1129 unsigned long load_offset;
1132 struct pending_object
1134 enum pending_obj_kind tag;
1135 union {
1136 struct pending_loc_var var;
1137 struct pending_line line;
1138 } u;
1141 struct pending_list
1143 struct pending_object* objs;
1144 unsigned num;
1145 unsigned allocated;
1148 static inline void pending_make_room(struct pending_list* pending)
1150 if (pending->num == pending->allocated)
1152 if (!pending->objs)
1154 pending->allocated = 8;
1155 pending->objs = HeapAlloc(GetProcessHeap(), 0,
1156 pending->allocated * sizeof(pending->objs[0]));
1158 else
1160 pending->allocated *= 2;
1161 pending->objs = HeapReAlloc(GetProcessHeap(), 0, pending->objs,
1162 pending->allocated * sizeof(pending->objs[0]));
1167 static inline void pending_add_var(struct pending_list* pending, const char* name,
1168 enum DataKind dt, const struct location* loc)
1170 pending_make_room(pending);
1171 pending->objs[pending->num].tag = PENDING_VAR;
1172 stab_strcpy(pending->objs[pending->num].u.var.name,
1173 sizeof(pending->objs[pending->num].u.var.name), name);
1174 pending->objs[pending->num].u.var.type = stabs_parse_type(name);
1175 pending->objs[pending->num].u.var.kind = dt;
1176 pending->objs[pending->num].u.var.loc = *loc;
1177 pending->num++;
1180 static inline void pending_add_line(struct pending_list* pending, int source_idx,
1181 int line_num, unsigned long offset,
1182 unsigned long load_offset)
1184 pending_make_room(pending);
1185 pending->objs[pending->num].tag = PENDING_LINE;
1186 pending->objs[pending->num].u.line.source_idx = source_idx;
1187 pending->objs[pending->num].u.line.line_num = line_num;
1188 pending->objs[pending->num].u.line.offset = offset;
1189 pending->objs[pending->num].u.line.load_offset = load_offset;
1190 pending->num++;
1193 static void pending_flush(struct pending_list* pending, struct module* module,
1194 struct symt_function* func, struct symt_block* block)
1196 unsigned int i;
1198 for (i = 0; i < pending->num; i++)
1200 switch (pending->objs[i].tag)
1202 case PENDING_VAR:
1203 symt_add_func_local(module, func,
1204 pending->objs[i].u.var.kind, &pending->objs[i].u.var.loc,
1205 block, pending->objs[i].u.var.type, pending->objs[i].u.var.name);
1206 break;
1207 case PENDING_LINE:
1208 if (module->type == DMT_MACHO)
1209 pending->objs[i].u.line.offset -= func->address - pending->objs[i].u.line.load_offset;
1210 symt_add_func_line(module, func, pending->objs[i].u.line.source_idx,
1211 pending->objs[i].u.line.line_num, pending->objs[i].u.line.offset);
1212 break;
1213 default:
1214 ERR("Unknown pending object tag %u\n", (unsigned)pending->objs[i].tag);
1215 break;
1218 pending->num = 0;
1221 /******************************************************************
1222 * stabs_finalize_function
1224 * Ends function creation: mainly:
1225 * - cleans up line number information
1226 * - tries to set up a debug-start tag (FIXME: heuristic to be enhanced)
1227 * - for stabs which have absolute address in them, initializes the size of the
1228 * function (assuming that current function ends where next function starts)
1230 static void stabs_finalize_function(struct module* module, struct symt_function* func,
1231 unsigned long size)
1233 IMAGEHLP_LINE64 il;
1234 struct location loc;
1236 if (!func) return;
1237 symt_normalize_function(module, func);
1238 /* To define the debug-start of the function, we use the second line number.
1239 * Not 100% bullet proof, but better than nothing
1241 if (symt_fill_func_line_info(module, func, func->address, &il) &&
1242 symt_get_func_line_next(module, &il))
1244 loc.kind = loc_absolute;
1245 loc.offset = il.Address - func->address;
1246 symt_add_function_point(module, func, SymTagFuncDebugStart,
1247 &loc, NULL);
1249 if (size) func->size = size;
1252 static inline void stabbuf_append(char **buf, unsigned *buf_size, const char *str)
1254 unsigned str_len, buf_len;
1256 str_len = strlen(str);
1257 buf_len = strlen(*buf);
1259 if(str_len+buf_len >= *buf_size) {
1260 *buf_size += buf_len + str_len;
1261 *buf = HeapReAlloc(GetProcessHeap(), 0, *buf, *buf_size);
1264 strcpy(*buf+buf_len, str);
1267 BOOL stabs_parse(struct module* module, unsigned long load_offset,
1268 const void* pv_stab_ptr, int stablen,
1269 const char* strs, int strtablen,
1270 stabs_def_cb callback, void* user)
1272 struct symt_function* curr_func = NULL;
1273 struct symt_block* block = NULL;
1274 struct symt_compiland* compiland = NULL;
1275 char srcpath[PATH_MAX]; /* path to directory source file is in */
1276 int i;
1277 int nstab;
1278 const char* ptr;
1279 char* stabbuff;
1280 unsigned int stabbufflen;
1281 const struct stab_nlist* stab_ptr = pv_stab_ptr;
1282 const char* strs_end;
1283 int strtabinc;
1284 char symname[4096];
1285 unsigned incl[32];
1286 int incl_stk = -1;
1287 int source_idx = -1;
1288 struct pending_list pending_block;
1289 struct pending_list pending_func;
1290 BOOL ret = TRUE;
1291 struct location loc;
1292 unsigned char type;
1294 nstab = stablen / sizeof(struct stab_nlist);
1295 strs_end = strs + strtablen;
1297 memset(srcpath, 0, sizeof(srcpath));
1298 memset(stabs_basic, 0, sizeof(stabs_basic));
1299 memset(&pending_block, 0, sizeof(pending_block));
1300 memset(&pending_func, 0, sizeof(pending_func));
1303 * Allocate a buffer into which we can build stab strings for cases
1304 * where the stab is continued over multiple lines.
1306 stabbufflen = 65536;
1307 stabbuff = HeapAlloc(GetProcessHeap(), 0, stabbufflen);
1309 strtabinc = 0;
1310 stabbuff[0] = '\0';
1311 for (i = 0; i < nstab; i++, stab_ptr++)
1313 ptr = strs + stab_ptr->n_strx;
1314 if ((ptr > strs_end) || (ptr + strlen(ptr) > strs_end))
1316 WARN("Bad stabs string %p\n", ptr);
1317 continue;
1319 if (*ptr != '\0' && (ptr[strlen(ptr) - 1] == '\\'))
1322 * Indicates continuation. Append this to the buffer, and go onto the
1323 * next record. Repeat the process until we find a stab without the
1324 * '/' character, as this indicates we have the whole thing.
1326 stabbuf_append(&stabbuff, &stabbufflen, ptr);
1327 continue;
1329 else if (stabbuff[0] != '\0')
1331 stabbuf_append(&stabbuff, &stabbufflen, ptr);
1332 ptr = stabbuff;
1335 if (stab_ptr->n_type & N_STAB)
1336 type = stab_ptr->n_type;
1337 else
1338 type = (stab_ptr->n_type & N_TYPE);
1340 /* only symbol entries contain a typedef */
1341 switch (type)
1343 case N_GSYM:
1344 case N_LCSYM:
1345 case N_STSYM:
1346 case N_RSYM:
1347 case N_LSYM:
1348 case N_ROSYM:
1349 case N_PSYM:
1350 if (strchr(ptr, '=') != NULL)
1353 * The stabs aren't in writable memory, so copy it over so we are
1354 * sure we can scribble on it.
1356 if (ptr != stabbuff)
1358 stabbuff[0] = 0;
1359 stabbuf_append(&stabbuff, &stabbufflen, ptr);
1360 ptr = stabbuff;
1362 stab_strcpy(symname, sizeof(symname), ptr);
1363 if (!stabs_parse_typedef(module, ptr, symname))
1365 /* skip this definition */
1366 stabbuff[0] = '\0';
1367 continue;
1372 switch (type)
1374 case N_GSYM:
1376 * These are useless with ELF. They have no value, and you have to
1377 * read the normal symbol table to get the address. Thus we
1378 * ignore them, and when we process the normal symbol table
1379 * we should do the right thing.
1381 * With a.out or mingw, they actually do make some amount of sense.
1383 stab_strcpy(symname, sizeof(symname), ptr);
1384 loc.kind = loc_absolute;
1385 loc.reg = 0;
1386 loc.offset = load_offset + stab_ptr->n_value;
1387 symt_new_global_variable(module, compiland, symname, TRUE /* FIXME */,
1388 loc, 0, stabs_parse_type(ptr));
1389 break;
1390 case N_LCSYM:
1391 case N_STSYM:
1392 /* These are static symbols and BSS symbols. */
1393 stab_strcpy(symname, sizeof(symname), ptr);
1394 loc.kind = loc_absolute;
1395 loc.reg = 0;
1396 loc.offset = load_offset + stab_ptr->n_value;
1397 symt_new_global_variable(module, compiland, symname, TRUE /* FIXME */,
1398 loc, 0, stabs_parse_type(ptr));
1399 break;
1400 case N_LBRAC:
1401 if (curr_func)
1403 block = symt_open_func_block(module, curr_func, block,
1404 stab_ptr->n_value, 0);
1405 pending_flush(&pending_block, module, curr_func, block);
1407 break;
1408 case N_RBRAC:
1409 if (curr_func)
1410 block = symt_close_func_block(module, curr_func, block,
1411 stab_ptr->n_value);
1412 break;
1413 case N_PSYM:
1414 /* These are function parameters. */
1415 if (curr_func != NULL)
1417 struct symt* param_type = stabs_parse_type(ptr);
1418 stab_strcpy(symname, sizeof(symname), ptr);
1419 loc.kind = loc_regrel;
1420 loc.reg = dbghelp_current_cpu->frame_regno;
1421 loc.offset = stab_ptr->n_value;
1422 symt_add_func_local(module, curr_func,
1423 (int)stab_ptr->n_value >= 0 ? DataIsParam : DataIsLocal,
1424 &loc, NULL, param_type, symname);
1425 symt_add_function_signature_parameter(module,
1426 (struct symt_function_signature*)curr_func->type,
1427 param_type);
1429 break;
1430 case N_RSYM:
1431 /* These are registers (as local variables) */
1432 if (curr_func != NULL)
1434 loc.kind = loc_register;
1435 loc.offset = 0;
1437 switch (stab_ptr->n_value)
1439 case 0: loc.reg = CV_REG_EAX; break;
1440 case 1: loc.reg = CV_REG_ECX; break;
1441 case 2: loc.reg = CV_REG_EDX; break;
1442 case 3: loc.reg = CV_REG_EBX; break;
1443 case 4: loc.reg = CV_REG_ESP; break;
1444 case 5: loc.reg = CV_REG_EBP; break;
1445 case 6: loc.reg = CV_REG_ESI; break;
1446 case 7: loc.reg = CV_REG_EDI; break;
1447 case 11:
1448 case 12:
1449 case 13:
1450 case 14:
1451 case 15:
1452 case 16:
1453 case 17:
1454 case 18:
1455 case 19: loc.reg = CV_REG_ST0 + stab_ptr->n_value - 12; break;
1456 case 21:
1457 case 22:
1458 case 23:
1459 case 24:
1460 case 25:
1461 case 26:
1462 case 27:
1463 case 28: loc.reg = CV_REG_XMM0 + stab_ptr->n_value - 21; break;
1464 case 29:
1465 case 30:
1466 case 31:
1467 case 32:
1468 case 33:
1469 case 34:
1470 case 35:
1471 case 36: loc.reg = CV_REG_MM0 + stab_ptr->n_value - 29; break;
1472 default:
1473 FIXME("Unknown register value (%u)\n", stab_ptr->n_value);
1474 loc.reg = CV_REG_NONE;
1475 break;
1477 stab_strcpy(symname, sizeof(symname), ptr);
1478 if (ptr[strlen(symname) + 1] == 'P')
1480 struct symt* param_type = stabs_parse_type(ptr);
1481 stab_strcpy(symname, sizeof(symname), ptr);
1482 symt_add_func_local(module, curr_func, DataIsParam, &loc,
1483 NULL, param_type, symname);
1484 symt_add_function_signature_parameter(module,
1485 (struct symt_function_signature*)curr_func->type,
1486 param_type);
1488 else
1489 pending_add_var(&pending_block, ptr, DataIsLocal, &loc);
1491 break;
1492 case N_LSYM:
1493 /* These are local variables */
1494 loc.kind = loc_regrel;
1495 loc.reg = dbghelp_current_cpu->frame_regno;
1496 loc.offset = stab_ptr->n_value;
1497 if (curr_func != NULL) pending_add_var(&pending_block, ptr, DataIsLocal, &loc);
1498 break;
1499 case N_SLINE:
1501 * This is a line number. These are always relative to the start
1502 * of the function (N_FUN), and this makes the lookup easier.
1504 assert(source_idx >= 0);
1505 if (curr_func != NULL)
1507 unsigned long offset = stab_ptr->n_value;
1508 if (module->type == DMT_MACHO)
1509 offset -= curr_func->address - load_offset;
1510 symt_add_func_line(module, curr_func, source_idx,
1511 stab_ptr->n_desc, offset);
1513 else pending_add_line(&pending_func, source_idx, stab_ptr->n_desc,
1514 stab_ptr->n_value, load_offset);
1515 break;
1516 case N_FUN:
1518 * For now, just declare the various functions. Later
1519 * on, we will add the line number information and the
1520 * local symbols.
1523 * Copy the string to a temp buffer so we
1524 * can kill everything after the ':'. We do
1525 * it this way because otherwise we end up dirtying
1526 * all of the pages related to the stabs, and that
1527 * sucks up swap space like crazy.
1529 stab_strcpy(symname, sizeof(symname), ptr);
1530 if (*symname)
1532 struct symt_function_signature* func_type;
1534 if (curr_func)
1536 /* First, clean up the previous function we were working on.
1537 * Assume size of the func is the delta between current offset
1538 * and offset of last function
1540 stabs_finalize_function(module, curr_func,
1541 stab_ptr->n_value ?
1542 (load_offset + stab_ptr->n_value - curr_func->address) : 0);
1544 func_type = symt_new_function_signature(module,
1545 stabs_parse_type(ptr), -1);
1546 curr_func = symt_new_function(module, compiland, symname,
1547 load_offset + stab_ptr->n_value, 0,
1548 &func_type->symt);
1549 pending_flush(&pending_func, module, curr_func, NULL);
1551 else
1553 /* some versions of GCC to use a N_FUN "" to mark the end of a function
1554 * and n_value contains the size of the func
1556 stabs_finalize_function(module, curr_func, stab_ptr->n_value);
1557 curr_func = NULL;
1559 break;
1560 case N_SO:
1562 * This indicates a new source file. Append the records
1563 * together, to build the correct path name.
1565 if (*ptr == '\0') /* end of N_SO file */
1567 /* Nuke old path. */
1568 srcpath[0] = '\0';
1569 stabs_finalize_function(module, curr_func, 0);
1570 curr_func = NULL;
1571 source_idx = -1;
1572 incl_stk = -1;
1573 assert(block == NULL);
1574 compiland = NULL;
1576 else
1578 int len = strlen(ptr);
1579 if (ptr[len-1] != '/')
1581 stabs_reset_includes();
1582 source_idx = source_new(module, srcpath, ptr);
1583 compiland = symt_new_compiland(module, 0 /* FIXME */, source_idx);
1585 else
1586 strcpy(srcpath, ptr);
1588 break;
1589 case N_SOL:
1590 source_idx = source_new(module, srcpath, ptr);
1591 break;
1592 case N_UNDF:
1593 strs += strtabinc;
1594 strtabinc = stab_ptr->n_value;
1595 /* I'm not sure this is needed, so trace it before we obsolete it */
1596 if (curr_func)
1598 FIXME("UNDF: curr_func %s\n", curr_func->hash_elt.name);
1599 stabs_finalize_function(module, curr_func, 0); /* FIXME */
1600 curr_func = NULL;
1602 break;
1603 case N_OPT:
1604 /* Ignore this. We don't care what it points to. */
1605 break;
1606 case N_BINCL:
1607 stabs_add_include(stabs_new_include(ptr, stab_ptr->n_value));
1608 assert(incl_stk < (int)(sizeof(incl) / sizeof(incl[0])) - 1);
1609 incl[++incl_stk] = source_idx;
1610 source_idx = source_new(module, NULL, ptr);
1611 break;
1612 case N_EINCL:
1613 assert(incl_stk >= 0);
1614 source_idx = incl[incl_stk--];
1615 break;
1616 case N_EXCL:
1617 if (stabs_add_include(stabs_find_include(ptr, stab_ptr->n_value)) < 0)
1619 ERR("Excluded header not found (%s,%d)\n", ptr, stab_ptr->n_value);
1620 module_reset_debug_info(module);
1621 ret = FALSE;
1622 goto done;
1624 break;
1625 case N_MAIN:
1626 /* Always ignore these. GCC doesn't even generate them. */
1627 break;
1628 case N_BNSYM:
1629 case N_ENSYM:
1630 case N_OSO:
1631 /* Always ignore these, they seem to be used only on Darwin. */
1632 break;
1633 case N_ABS:
1634 #ifdef N_SECT
1635 case N_SECT:
1636 #endif
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 #ifdef N_PEXT
1644 /* "private extern"; shared among compilation units in a shared
1645 * library, but not accessible from outside the library. */
1646 if (stab_ptr->n_type & N_PEXT)
1648 is_public = FALSE;
1649 is_global = TRUE;
1651 #endif
1653 if (*ptr == '_') ptr++;
1654 stab_strcpy(symname, sizeof(symname), ptr);
1656 callback(module, load_offset, symname, stab_ptr->n_value,
1657 is_public, is_global, stab_ptr->n_other, compiland, user);
1659 break;
1660 default:
1661 ERR("Unknown stab type 0x%02x\n", type);
1662 break;
1664 stabbuff[0] = '\0';
1665 TRACE("0x%02x %x %s\n",
1666 stab_ptr->n_type, stab_ptr->n_value, debugstr_a(strs + stab_ptr->n_strx));
1668 module->module.SymType = SymDia;
1669 module->module.CVSig = 'S' | ('T' << 8) | ('A' << 16) | ('B' << 24);
1670 /* FIXME: we could have a finer grain here */
1671 module->module.LineNumbers = TRUE;
1672 module->module.GlobalSymbols = TRUE;
1673 module->module.TypeInfo = TRUE;
1674 module->module.SourceIndexed = TRUE;
1675 module->module.Publics = TRUE;
1676 done:
1677 HeapFree(GetProcessHeap(), 0, stabbuff);
1678 stabs_free_includes();
1679 HeapFree(GetProcessHeap(), 0, pending_block.objs);
1680 HeapFree(GetProcessHeap(), 0, pending_func.objs);
1682 return ret;