kernel32/tests: Add a test to check some fields in fake dlls.
[wine.git] / dlls / dbghelp / stabs.c
blobbc5ef356e8caefc9913eb5e99047c0336d51dfce
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 <stdint.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #ifdef HAVE_UNISTD_H
48 # include <unistd.h>
49 #endif
50 #include <stdio.h>
51 #include <assert.h>
52 #include <stdarg.h>
54 #ifdef HAVE_MACH_O_NLIST_H
55 # include <mach-o/nlist.h>
56 #endif
58 #include "windef.h"
59 #include "winbase.h"
60 #include "winnls.h"
62 #include "dbghelp_private.h"
64 #include "wine/debug.h"
66 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp_stabs);
68 /* Masks for n_type field */
69 #ifndef N_STAB
70 #define N_STAB 0xe0
71 #endif
72 #ifndef N_TYPE
73 #define N_TYPE 0x1e
74 #endif
75 #ifndef N_EXT
76 #define N_EXT 0x01
77 #endif
79 /* Values for (n_type & N_TYPE) */
80 #ifndef N_UNDF
81 #define N_UNDF 0x00
82 #endif
83 #ifndef N_ABS
84 #define N_ABS 0x02
85 #endif
87 #define N_GSYM 0x20
88 #define N_FUN 0x24
89 #define N_STSYM 0x26
90 #define N_LCSYM 0x28
91 #define N_MAIN 0x2a
92 #define N_ROSYM 0x2c
93 #define N_BNSYM 0x2e
94 #define N_OPT 0x3c
95 #define N_RSYM 0x40
96 #define N_SLINE 0x44
97 #define N_ENSYM 0x4e
98 #define N_SO 0x64
99 #define N_OSO 0x66
100 #define N_LSYM 0x80
101 #define N_BINCL 0x82
102 #define N_SOL 0x84
103 #define N_PSYM 0xa0
104 #define N_EINCL 0xa2
105 #define N_LBRAC 0xc0
106 #define N_EXCL 0xc2
107 #define N_RBRAC 0xe0
109 struct stab_nlist
111 unsigned n_strx;
112 unsigned char n_type;
113 char n_other;
114 short n_desc;
115 unsigned n_value;
118 static void stab_strcpy(char* dest, int sz, const char* source)
120 char* ptr = dest;
122 * A strcpy routine that stops when we hit the ':' character.
123 * Faster than copying the whole thing, and then nuking the
124 * ':'.
125 * Takes also care of (valid) a::b constructs
127 while (*source != '\0')
129 if (source[0] != ':' && sz-- > 0) *ptr++ = *source++;
130 else if (source[1] == ':' && (sz -= 2) > 0)
132 *ptr++ = *source++;
133 *ptr++ = *source++;
135 else break;
137 *ptr-- = '\0';
138 /* GCC emits, in some cases, a .<digit>+ suffix.
139 * This is used for static variable inside functions, so
140 * that we can have several such variables with same name in
141 * the same compilation unit
142 * We simply ignore that suffix when present (we also get rid
143 * of it in ELF symtab parsing)
145 if (ptr >= dest && isdigit(*ptr))
147 while (ptr > dest && isdigit(*ptr)) ptr--;
148 if (*ptr == '.') *ptr = '\0';
150 assert(sz > 0);
153 typedef struct
155 char* name;
156 unsigned long value;
157 struct symt** vector;
158 int nrofentries;
159 } include_def;
161 #define MAX_INCLUDES 5120
163 static include_def* include_defs = NULL;
164 static int num_include_def = 0;
165 static int num_alloc_include_def = 0;
166 static int cu_include_stack[MAX_INCLUDES];
167 static int cu_include_stk_idx = 0;
168 static struct symt** cu_vector = NULL;
169 static int cu_nrofentries = 0;
170 static struct symt_basic* stabs_basic[36];
172 static int stabs_new_include(const char* file, unsigned long val)
174 if (num_include_def == num_alloc_include_def)
176 if (!include_defs)
178 num_alloc_include_def = 256;
179 include_defs = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
180 sizeof(include_defs[0]) * num_alloc_include_def);
182 else
184 num_alloc_include_def *= 2;
185 include_defs = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, include_defs,
186 sizeof(include_defs[0]) * num_alloc_include_def);
189 include_defs[num_include_def].name = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(file) + 1), file);
190 include_defs[num_include_def].value = val;
191 include_defs[num_include_def].vector = NULL;
192 include_defs[num_include_def].nrofentries = 0;
194 return num_include_def++;
197 static int stabs_find_include(const char* file, unsigned long val)
199 int i;
201 for (i = 0; i < num_include_def; i++)
203 if (val == include_defs[i].value &&
204 strcmp(file, include_defs[i].name) == 0)
205 return i;
207 return -1;
210 static int stabs_add_include(int idx)
212 if (idx < 0) return -1;
213 cu_include_stk_idx++;
215 /* if this happens, just bump MAX_INCLUDES */
216 /* we could also handle this as another dynarray */
217 assert(cu_include_stk_idx < MAX_INCLUDES);
218 cu_include_stack[cu_include_stk_idx] = idx;
219 return cu_include_stk_idx;
222 static void stabs_reset_includes(void)
225 * The struct symt:s that we would need to use are reset when
226 * we start a new file. (at least the ones in filenr == 0)
228 cu_include_stk_idx = 0;/* keep 0 as index for the .c file itself */
229 memset(cu_vector, 0, sizeof(cu_vector[0]) * cu_nrofentries);
232 static void stabs_free_includes(void)
234 int i;
236 stabs_reset_includes();
237 for (i = 0; i < num_include_def; i++)
239 HeapFree(GetProcessHeap(), 0, include_defs[i].name);
240 HeapFree(GetProcessHeap(), 0, include_defs[i].vector);
242 HeapFree(GetProcessHeap(), 0, include_defs);
243 include_defs = NULL;
244 num_include_def = 0;
245 num_alloc_include_def = 0;
246 HeapFree(GetProcessHeap(), 0, cu_vector);
247 cu_vector = NULL;
248 cu_nrofentries = 0;
251 static struct symt** stabs_find_ref(long filenr, long subnr)
253 struct symt** ret;
255 /* FIXME: I could perhaps create a dummy include_def for each compilation
256 * unit which would allow not to handle those two cases separately
258 if (filenr == 0)
260 if (cu_nrofentries <= subnr)
262 cu_nrofentries = max( cu_nrofentries * 2, subnr + 1 );
263 if (!cu_vector)
264 cu_vector = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
265 sizeof(cu_vector[0]) * cu_nrofentries);
266 else
267 cu_vector = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
268 cu_vector, sizeof(cu_vector[0]) * cu_nrofentries);
270 ret = &cu_vector[subnr];
272 else
274 include_def* idef;
276 assert(filenr <= cu_include_stk_idx);
277 idef = &include_defs[cu_include_stack[filenr]];
279 if (idef->nrofentries <= subnr)
281 idef->nrofentries = max( idef->nrofentries * 2, subnr + 1 );
282 if (!idef->vector)
283 idef->vector = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
284 sizeof(idef->vector[0]) * idef->nrofentries);
285 else
286 idef->vector = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
287 idef->vector, sizeof(idef->vector[0]) * idef->nrofentries);
289 ret = &idef->vector[subnr];
291 TRACE("(%ld,%ld) => %p (%p)\n", filenr, subnr, ret, *ret);
292 return ret;
295 static struct symt** stabs_read_type_enum(const char** x)
297 long filenr, subnr;
298 const char* iter;
299 char* end;
301 iter = *x;
302 if (*iter == '(')
304 ++iter; /* '(' */
305 filenr = strtol(iter, &end, 10); /* <int> */
306 iter = ++end; /* ',' */
307 subnr = strtol(iter, &end, 10); /* <int> */
308 iter = ++end; /* ')' */
310 else
312 filenr = 0;
313 subnr = strtol(iter, &end, 10); /* <int> */
314 iter = end;
316 *x = iter;
317 return stabs_find_ref(filenr, subnr);
320 #define PTS_DEBUG
321 struct ParseTypedefData
323 const char* ptr;
324 char buf[1024];
325 int idx;
326 struct module* module;
327 #ifdef PTS_DEBUG
328 struct PTS_Error
330 const char* ptr;
331 unsigned line;
332 } errors[16];
333 int err_idx;
334 #endif
337 #ifdef PTS_DEBUG
338 static void stabs_pts_push(struct ParseTypedefData* ptd, unsigned line)
340 assert(ptd->err_idx < ARRAY_SIZE(ptd->errors));
341 ptd->errors[ptd->err_idx].line = line;
342 ptd->errors[ptd->err_idx].ptr = ptd->ptr;
343 ptd->err_idx++;
345 #define PTS_ABORTIF(ptd, t) do { if (t) { stabs_pts_push((ptd), __LINE__); return -1;} } while (0)
346 #else
347 #define PTS_ABORTIF(ptd, t) do { if (t) return -1; } while (0)
348 #endif
350 static int stabs_get_basic(struct ParseTypedefData* ptd, unsigned basic, struct symt** symt)
352 PTS_ABORTIF(ptd, basic >= ARRAY_SIZE(stabs_basic));
354 if (!stabs_basic[basic])
356 switch (basic)
358 case 1: stabs_basic[basic] = symt_new_basic(ptd->module, btInt, "int", 4); break;
359 case 2: stabs_basic[basic] = symt_new_basic(ptd->module, btChar, "char", 1); break;
360 case 3: stabs_basic[basic] = symt_new_basic(ptd->module, btInt, "short int", 2); break;
361 case 4: stabs_basic[basic] = symt_new_basic(ptd->module, btInt, "long int", 4); break;
362 case 5: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt, "unsigned char", 1); break;
363 case 6: stabs_basic[basic] = symt_new_basic(ptd->module, btInt, "signed char", 1); break;
364 case 7: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt, "unsigned short int", 2); break;
365 case 8: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt, "unsigned int", 4); break;
366 case 9: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt, "unsigned", 2); break;
367 case 10: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt, "unsigned long int", 2); break;
368 case 11: stabs_basic[basic] = symt_new_basic(ptd->module, btVoid, "void", 0); break;
369 case 12: stabs_basic[basic] = symt_new_basic(ptd->module, btFloat, "float", 4); break;
370 case 13: stabs_basic[basic] = symt_new_basic(ptd->module, btFloat, "double", 8); break;
371 case 14: stabs_basic[basic] = symt_new_basic(ptd->module, btFloat, "long double", 12); break;
372 case 15: stabs_basic[basic] = symt_new_basic(ptd->module, btInt, "integer", 4); break;
373 case 16: stabs_basic[basic] = symt_new_basic(ptd->module, btBool, "bool", 1); break;
374 /* case 17: short real */
375 /* case 18: real */
376 case 25: stabs_basic[basic] = symt_new_basic(ptd->module, btComplex, "float complex", 8); break;
377 case 26: stabs_basic[basic] = symt_new_basic(ptd->module, btComplex, "double complex", 16); break;
378 case 30: stabs_basic[basic] = symt_new_basic(ptd->module, btWChar, "wchar_t", 2); break;
379 case 31: stabs_basic[basic] = symt_new_basic(ptd->module, btInt, "long long int", 8); break;
380 case 32: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt, "long long unsigned", 8); break;
381 /* starting at 35 are wine extensions (especially for R implementation) */
382 case 35: stabs_basic[basic] = symt_new_basic(ptd->module, btComplex, "long double complex", 24); break;
383 default: PTS_ABORTIF(ptd, 1);
386 *symt = &stabs_basic[basic]->symt;
387 return 0;
390 static int stabs_pts_read_type_def(struct ParseTypedefData* ptd,
391 const char* typename, struct symt** dt);
393 static int stabs_pts_read_id(struct ParseTypedefData* ptd)
395 const char* first = ptd->ptr;
396 unsigned int template = 0;
397 char ch;
399 while ((ch = *ptd->ptr++) != '\0')
401 switch (ch)
403 case ':':
404 if (template == 0)
406 unsigned int len = ptd->ptr - first - 1;
407 PTS_ABORTIF(ptd, len >= sizeof(ptd->buf) - ptd->idx);
408 memcpy(ptd->buf + ptd->idx, first, len);
409 ptd->buf[ptd->idx + len] = '\0';
410 ptd->idx += len + 1;
411 return 0;
413 break;
414 case '<': template++; break;
415 case '>': PTS_ABORTIF(ptd, template == 0); template--; break;
418 return -1;
421 static int stabs_pts_read_number(struct ParseTypedefData* ptd, long* v)
423 char* last;
425 *v = strtol(ptd->ptr, &last, 10);
426 PTS_ABORTIF(ptd, last == ptd->ptr);
427 ptd->ptr = last;
428 return 0;
431 static int stabs_pts_read_type_reference(struct ParseTypedefData* ptd,
432 long* filenr, long* subnr)
434 if (*ptd->ptr == '(')
436 /* '(' <int> ',' <int> ')' */
437 ptd->ptr++;
438 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, filenr) == -1);
439 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
440 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, subnr) == -1);
441 PTS_ABORTIF(ptd, *ptd->ptr++ != ')');
443 else
445 *filenr = 0;
446 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, subnr) == -1);
448 return 0;
451 struct pts_range_value
453 ULONGLONG val;
454 int sign;
457 static int stabs_pts_read_range_value(struct ParseTypedefData* ptd, struct pts_range_value* prv)
459 char* last;
461 switch (*ptd->ptr)
463 case '0':
464 while (*ptd->ptr == '0') ptd->ptr++;
465 if (*ptd->ptr >= '1' && *ptd->ptr <= '7')
467 switch (ptd->ptr[1])
469 case '0':
470 PTS_ABORTIF(ptd, ptd->ptr[0] != '1');
471 prv->sign = -1;
472 prv->val = 0;
473 while (isdigit(*ptd->ptr)) prv->val = (prv->val << 3) + *ptd->ptr++ - '0';
474 break;
475 case '7':
476 prv->sign = 1;
477 prv->val = 0;
478 while (isdigit(*ptd->ptr)) prv->val = (prv->val << 3) + *ptd->ptr++ - '0';
479 break;
480 default: PTS_ABORTIF(ptd, 1); break;
482 } else prv->sign = 0;
483 break;
484 case '-':
485 prv->sign = -1;
486 prv->val = strtoull(++ptd->ptr, &last, 10);
487 ptd->ptr = last;
488 break;
489 case '+':
490 default:
491 prv->sign = 1;
492 prv->val = strtoull(ptd->ptr, &last, 10);
493 ptd->ptr = last;
494 break;
496 return 0;
499 static int stabs_pts_read_range(struct ParseTypedefData* ptd, const char* typename,
500 struct symt** dt)
502 struct symt* ref;
503 struct pts_range_value lo;
504 struct pts_range_value hi;
505 unsigned size;
506 enum BasicType bt;
507 int i;
508 ULONGLONG v;
510 /* type ';' <int> ';' <int> ';' */
511 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &ref) == -1);
512 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
513 PTS_ABORTIF(ptd, stabs_pts_read_range_value(ptd, &lo) == -1);
514 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
515 PTS_ABORTIF(ptd, stabs_pts_read_range_value(ptd, &hi) == -1);
516 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
518 /* basically, we don't use ref... in some cases, for example, float is declared
519 * as a derived type of int... which won't help us... so we guess the types
520 * from the various formats
522 if (lo.sign == 0 && hi.sign < 0)
524 bt = btUInt;
525 size = hi.val;
527 else if (lo.sign < 0 && hi.sign == 0)
529 bt = btUInt;
530 size = lo.val;
532 else if (lo.sign > 0 && hi.sign == 0)
534 bt = btFloat;
535 size = lo.val;
537 else if (lo.sign < 0 && hi.sign > 0)
539 v = 1 << 7;
540 for (i = 7; i < 64; i += 8)
542 if (lo.val == v && hi.val == v - 1)
544 bt = btInt;
545 size = (i + 1) / 8;
546 break;
548 v <<= 8;
550 PTS_ABORTIF(ptd, i >= 64);
552 else if (lo.sign == 0 && hi.sign > 0)
554 if (hi.val == 127) /* specific case for char... */
556 bt = btChar;
557 size = 1;
559 else
561 v = 1;
562 for (i = 8; i <= 64; i += 8)
564 v <<= 8;
565 if (hi.val + 1 == v)
567 bt = btUInt;
568 size = (i + 1) / 8;
569 break;
572 PTS_ABORTIF(ptd, i > 64);
575 else PTS_ABORTIF(ptd, 1);
577 *dt = &symt_new_basic(ptd->module, bt, typename, size)->symt;
578 return 0;
581 static inline int stabs_pts_read_method_info(struct ParseTypedefData* ptd)
583 struct symt* dt;
584 const char* tmp;
585 char mthd;
589 /* get type of return value */
590 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
591 if (*ptd->ptr == ';') ptd->ptr++;
593 /* get types of parameters */
594 if (*ptd->ptr == ':')
596 PTS_ABORTIF(ptd, !(tmp = strchr(ptd->ptr + 1, ';')));
597 ptd->ptr = tmp + 1;
599 PTS_ABORTIF(ptd, !(*ptd->ptr >= '0' && *ptd->ptr <= '9'));
600 ptd->ptr++;
601 PTS_ABORTIF(ptd, !(ptd->ptr[0] >= 'A' && *ptd->ptr <= 'D'));
602 mthd = *++ptd->ptr;
603 PTS_ABORTIF(ptd, mthd != '.' && mthd != '?' && mthd != '*');
604 ptd->ptr++;
605 if (mthd == '*')
607 long int ofs;
609 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &ofs) == -1);
610 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
611 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
612 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
614 } while (*ptd->ptr != ';');
615 ptd->ptr++;
617 return 0;
620 static inline int stabs_pts_read_aggregate(struct ParseTypedefData* ptd,
621 struct symt_udt* sdt)
623 long sz, ofs;
624 struct symt* adt;
625 struct symt* dt = NULL;
626 int idx;
627 int doadd;
629 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &sz) == -1);
631 doadd = symt_set_udt_size(ptd->module, sdt, sz);
632 if (*ptd->ptr == '!') /* C++ inheritance */
634 long num_classes;
636 ptd->ptr++;
637 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &num_classes) == -1);
638 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
639 while (--num_classes >= 0)
641 ptd->ptr += 2; /* skip visibility and inheritance */
642 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &ofs) == -1);
643 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
645 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &adt) == -1);
647 if (doadd && adt)
649 char tmp[256];
650 DWORD64 size;
652 strcpy(tmp, "__inherited_class_");
653 strcat(tmp, symt_get_name(adt));
655 /* FIXME: TI_GET_LENGTH will not always work, especially when adt
656 * has just been seen as a forward definition and not the real stuff
657 * yet.
658 * As we don't use much the size of members in structs, this may not
659 * be much of a problem
661 symt_get_info(ptd->module, adt, TI_GET_LENGTH, &size);
662 symt_add_udt_element(ptd->module, sdt, tmp, adt, ofs, (DWORD)size * 8);
664 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
668 /* if the structure has already been filled, just redo the parsing
669 * but don't store results into the struct
670 * FIXME: there's a quite ugly memory leak in there...
673 /* Now parse the individual elements of the structure/union. */
674 while (*ptd->ptr != ';')
676 /* agg_name : type ',' <int:offset> ',' <int:size> */
677 idx = ptd->idx;
679 if (ptd->ptr[0] == '$' && ptd->ptr[1] == 'v')
681 long x;
683 if (ptd->ptr[2] == 'f')
685 /* C++ virtual method table */
686 ptd->ptr += 3;
687 stabs_read_type_enum(&ptd->ptr);
688 PTS_ABORTIF(ptd, *ptd->ptr++ != ':');
689 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
690 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
691 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &x) == -1);
692 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
693 ptd->idx = idx;
694 continue;
696 else if (ptd->ptr[2] == 'b')
698 ptd->ptr += 3;
699 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
700 PTS_ABORTIF(ptd, *ptd->ptr++ != ':');
701 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
702 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
703 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &x) == -1);
704 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
705 ptd->idx = idx;
706 continue;
710 PTS_ABORTIF(ptd, stabs_pts_read_id(ptd) == -1);
711 /* Ref. TSDF R2.130 Section 7.4. When the field name is a method name
712 * it is followed by two colons rather than one.
714 if (*ptd->ptr == ':')
716 ptd->ptr++;
717 stabs_pts_read_method_info(ptd);
718 ptd->idx = idx;
719 continue;
721 else
723 /* skip C++ member protection /0 /1 or /2 */
724 if (*ptd->ptr == '/') ptd->ptr += 2;
726 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &adt) == -1);
728 switch (*ptd->ptr++)
730 case ',':
731 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &ofs) == -1);
732 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
733 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &sz) == -1);
734 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
736 if (doadd) symt_add_udt_element(ptd->module, sdt, ptd->buf + idx, adt, ofs, sz);
737 break;
738 case ':':
740 const char* tmp;
741 /* method parameters... terminated by ';' */
742 PTS_ABORTIF(ptd, !(tmp = strchr(ptd->ptr, ';')));
743 ptd->ptr = tmp + 1;
745 break;
746 default:
747 PTS_ABORTIF(ptd, TRUE);
749 ptd->idx = idx;
751 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
752 if (*ptd->ptr == '~')
754 ptd->ptr++;
755 PTS_ABORTIF(ptd, *ptd->ptr++ != '%');
756 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
757 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
759 return 0;
762 static inline int stabs_pts_read_enum(struct ParseTypedefData* ptd,
763 struct symt_enum* edt)
765 long value;
766 int idx;
768 while (*ptd->ptr != ';')
770 idx = ptd->idx;
771 PTS_ABORTIF(ptd, stabs_pts_read_id(ptd) == -1);
772 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &value) == -1);
773 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
774 symt_add_enum_element(ptd->module, edt, ptd->buf + idx, value);
775 ptd->idx = idx;
777 ptd->ptr++;
778 return 0;
781 static inline int stabs_pts_read_array(struct ParseTypedefData* ptd,
782 struct symt** adt)
784 long lo, hi;
785 struct symt* range_dt;
786 struct symt* base_dt;
788 /* ar<typeinfo_nodef>;<int>;<int>;<typeinfo> */
790 PTS_ABORTIF(ptd, *ptd->ptr++ != 'r');
792 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &range_dt) == -1);
793 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
794 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &lo) == -1);
795 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
796 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &hi) == -1);
797 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
799 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &base_dt) == -1);
801 *adt = &symt_new_array(ptd->module, lo, hi, base_dt, range_dt)->symt;
802 return 0;
805 static int stabs_pts_read_type_def(struct ParseTypedefData* ptd, const char* typename,
806 struct symt** ret_dt)
808 int idx;
809 long sz = -1;
810 struct symt* new_dt = NULL; /* newly created data type */
811 struct symt* ref_dt; /* referenced data type (pointer...) */
812 long filenr1, subnr1, tmp;
814 /* things are a bit complicated because of the way the typedefs are stored inside
815 * the file, because addresses can change when realloc is done, so we must call
816 * over and over stabs_find_ref() to keep the correct values around
818 PTS_ABORTIF(ptd, stabs_pts_read_type_reference(ptd, &filenr1, &subnr1) == -1);
820 while (*ptd->ptr == '=')
822 ptd->ptr++;
823 PTS_ABORTIF(ptd, new_dt != NULL);
825 /* first handle attribute if any */
826 switch (*ptd->ptr)
828 case '@':
829 if (*++ptd->ptr == 's')
831 ptd->ptr++;
832 if (stabs_pts_read_number(ptd, &sz) == -1)
834 ERR("Not an attribute... NIY\n");
835 ptd->ptr -= 2;
836 return -1;
838 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
840 break;
842 /* then the real definitions */
843 switch (*ptd->ptr++)
845 case '*':
846 case '&':
847 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &ref_dt) == -1);
848 new_dt = &symt_new_pointer(ptd->module, ref_dt, sizeof(void*))->symt;
849 break;
850 case 'k': /* 'const' modifier */
851 case 'B': /* 'volatile' modifier */
852 /* just kinda ignore the modifier, I guess -gmt */
853 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, typename, &new_dt) == -1);
854 break;
855 case '(':
856 ptd->ptr--;
857 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, typename, &new_dt) == -1);
858 break;
859 case 'a':
860 PTS_ABORTIF(ptd, stabs_pts_read_array(ptd, &new_dt) == -1);
861 break;
862 case 'r':
863 PTS_ABORTIF(ptd, stabs_pts_read_range(ptd, typename, &new_dt) == -1);
864 assert(!*stabs_find_ref(filenr1, subnr1));
865 *stabs_find_ref(filenr1, subnr1) = new_dt;
866 break;
867 case 'f':
868 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &ref_dt) == -1);
869 new_dt = &symt_new_function_signature(ptd->module, ref_dt, -1)->symt;
870 break;
871 case 'e':
872 stabs_get_basic(ptd, 1 /* int */, &ref_dt);
873 new_dt = &symt_new_enum(ptd->module, typename, ref_dt)->symt;
874 PTS_ABORTIF(ptd, stabs_pts_read_enum(ptd, (struct symt_enum*)new_dt) == -1);
875 break;
876 case 's':
877 case 'u':
879 struct symt_udt* udt;
880 enum UdtKind kind = (ptd->ptr[-1] == 's') ? UdtStruct : UdtUnion;
881 /* udt can have been already defined in a forward definition */
882 udt = (struct symt_udt*)*stabs_find_ref(filenr1, subnr1);
883 if (!udt)
885 udt = symt_new_udt(ptd->module, typename, 0, kind);
886 /* we need to set it here, because a struct can hold a pointer
887 * to itself
889 new_dt = *stabs_find_ref(filenr1, subnr1) = &udt->symt;
891 else
893 unsigned l1, l2;
894 if (udt->symt.tag != SymTagUDT)
896 ERR("Forward declaration (%p/%s) is not an aggregate (%u)\n",
897 udt, symt_get_name(&udt->symt), udt->symt.tag);
898 return -1;
900 /* FIXME: we currently don't correctly construct nested C++
901 * classes names. Therefore, we could be here with either:
902 * - typename and udt->hash_elt.name being the same string
903 * (non embedded case)
904 * - typename being foo::bar while udt->hash_elt.name being
905 * just bar
906 * So, we twist the comparison to test both occurrences. When
907 * we have proper C++ types in this file, this twist has to be
908 * removed
910 l1 = strlen(udt->hash_elt.name);
911 l2 = strlen(typename);
912 if (l1 > l2 || strcmp(udt->hash_elt.name, typename + l2 - l1))
913 ERR("Forward declaration name mismatch %s <> %s\n",
914 udt->hash_elt.name, typename);
915 new_dt = &udt->symt;
917 PTS_ABORTIF(ptd, stabs_pts_read_aggregate(ptd, udt) == -1);
919 break;
920 case 'x':
921 idx = ptd->idx;
922 tmp = *ptd->ptr++;
923 PTS_ABORTIF(ptd, stabs_pts_read_id(ptd) == -1);
924 switch (tmp)
926 case 'e':
927 stabs_get_basic(ptd, 1 /* int */, &ref_dt);
928 new_dt = &symt_new_enum(ptd->module, ptd->buf + idx, ref_dt)->symt;
929 break;
930 case 's':
931 new_dt = &symt_new_udt(ptd->module, ptd->buf + idx, 0, UdtStruct)->symt;
932 break;
933 case 'u':
934 new_dt = &symt_new_udt(ptd->module, ptd->buf + idx, 0, UdtUnion)->symt;
935 break;
936 default:
937 return -1;
939 ptd->idx = idx;
940 break;
941 case '-':
943 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &tmp) == -1);
944 PTS_ABORTIF(ptd, stabs_get_basic(ptd, tmp, &new_dt) == -1);
945 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
947 break;
948 case '#':
949 if (*ptd->ptr == '#')
951 ptd->ptr++;
952 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &ref_dt) == -1);
953 new_dt = &symt_new_function_signature(ptd->module, ref_dt, -1)->symt;
955 else
957 struct symt* cls_dt;
958 struct symt* pmt_dt;
960 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &cls_dt) == -1);
961 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
962 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &ref_dt) == -1);
963 new_dt = &symt_new_function_signature(ptd->module, ref_dt, -1)->symt;
964 while (*ptd->ptr == ',')
966 ptd->ptr++;
967 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &pmt_dt) == -1);
970 break;
971 case 'R':
973 long type, len, unk;
974 int basic;
976 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &type) == -1);
977 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
978 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &len) == -1);
979 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
980 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &unk) == -1);
981 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
983 switch (type) /* see stabs_get_basic for the details */
985 case 1: basic = 12; break;
986 case 2: basic = 13; break;
987 case 3: basic = 25; break;
988 case 4: basic = 26; break;
989 case 5: basic = 35; break;
990 case 6: basic = 14; break;
991 default: PTS_ABORTIF(ptd, 1);
993 PTS_ABORTIF(ptd, stabs_get_basic(ptd, basic, &new_dt) == -1);
995 break;
996 default:
997 ERR("Unknown type '%c'\n", ptd->ptr[-1]);
998 return -1;
1002 if (!new_dt)
1004 /* is it a forward declaration that has been filled ? */
1005 new_dt = *stabs_find_ref(filenr1, subnr1);
1006 /* if not, this should be void (which is defined as a ref to itself, but we
1007 * don't correctly catch it)
1009 if (!new_dt && typename)
1011 new_dt = &symt_new_basic(ptd->module, btVoid, typename, 0)->symt;
1012 PTS_ABORTIF(ptd, strcmp(typename, "void"));
1016 *stabs_find_ref(filenr1, subnr1) = *ret_dt = new_dt;
1018 TRACE("Adding (%ld,%ld) %s\n", filenr1, subnr1, debugstr_a(typename));
1020 return 0;
1023 static int stabs_parse_typedef(struct module* module, const char* ptr,
1024 const char* typename)
1026 struct ParseTypedefData ptd;
1027 struct symt* dt;
1028 int ret = -1;
1030 /* check for already existing definition */
1032 TRACE("%s => %s\n", typename, debugstr_a(ptr));
1033 ptd.module = module;
1034 ptd.idx = 0;
1035 #ifdef PTS_DEBUG
1036 ptd.err_idx = 0;
1037 #endif
1038 for (ptd.ptr = ptr - 1; ;)
1040 ptd.ptr = strchr(ptd.ptr + 1, ':');
1041 if (ptd.ptr == NULL || *++ptd.ptr != ':') break;
1043 if (ptd.ptr)
1045 if (*ptd.ptr != '(') ptd.ptr++;
1046 /* most of type definitions take one char, except Tt */
1047 if (*ptd.ptr != '(') ptd.ptr++;
1048 ret = stabs_pts_read_type_def(&ptd, typename, &dt);
1051 if (ret == -1 || *ptd.ptr)
1053 #ifdef PTS_DEBUG
1054 int i;
1055 TRACE("Failure on %s\n", debugstr_a(ptr));
1056 if (ret == -1)
1058 for (i = 0; i < ptd.err_idx; i++)
1060 TRACE("[%d]: line %d => %s\n",
1061 i, ptd.errors[i].line, debugstr_a(ptd.errors[i].ptr));
1064 else
1065 TRACE("[0]: => %s\n", debugstr_a(ptd.ptr));
1067 #else
1068 ERR("Failure on %s at %s\n", debugstr_a(ptr), debugstr_a(ptd.ptr));
1069 #endif
1070 return FALSE;
1073 return TRUE;
1076 static struct symt* stabs_parse_type(const char* stab)
1078 const char* c = stab - 1;
1081 * Look through the stab definition, and figure out what struct symt
1082 * this represents. If we have something we know about, assign the
1083 * type.
1084 * According to "The \"stabs\" debug format" (Rev 2.130) the name may be
1085 * a C++ name and contain double colons e.g. foo::bar::baz:t5=*6.
1089 if ((c = strchr(c + 1, ':')) == NULL) return NULL;
1090 } while (*++c == ':');
1093 * The next characters say more about the type (i.e. data, function, etc)
1094 * of symbol. Skip them. (C++ for example may have Tt).
1095 * Actually this is a very weak description; I think Tt is the only
1096 * multiple combination we should see.
1098 while (*c && *c != '(' && !isdigit(*c))
1099 c++;
1101 * The next is either an integer or a (integer,integer).
1102 * The stabs_read_type_enum() takes care that stab_types is large enough.
1104 return *stabs_read_type_enum(&c);
1107 enum pending_obj_kind
1109 PENDING_VAR,
1110 PENDING_LINE,
1113 struct pending_loc_var
1115 char name[256];
1116 struct symt* type;
1117 enum DataKind kind;
1118 struct location loc;
1121 struct pending_line
1123 int source_idx;
1124 int line_num;
1125 unsigned long offset;
1126 unsigned long load_offset;
1129 struct pending_object
1131 enum pending_obj_kind tag;
1132 union {
1133 struct pending_loc_var var;
1134 struct pending_line line;
1135 } u;
1138 struct pending_list
1140 struct pending_object* objs;
1141 unsigned num;
1142 unsigned allocated;
1145 static inline void pending_make_room(struct pending_list* pending)
1147 if (pending->num == pending->allocated)
1149 if (!pending->objs)
1151 pending->allocated = 8;
1152 pending->objs = HeapAlloc(GetProcessHeap(), 0,
1153 pending->allocated * sizeof(pending->objs[0]));
1155 else
1157 pending->allocated *= 2;
1158 pending->objs = HeapReAlloc(GetProcessHeap(), 0, pending->objs,
1159 pending->allocated * sizeof(pending->objs[0]));
1164 static inline void pending_add_var(struct pending_list* pending, const char* name,
1165 enum DataKind dt, const struct location* loc)
1167 pending_make_room(pending);
1168 pending->objs[pending->num].tag = PENDING_VAR;
1169 stab_strcpy(pending->objs[pending->num].u.var.name,
1170 sizeof(pending->objs[pending->num].u.var.name), name);
1171 pending->objs[pending->num].u.var.type = stabs_parse_type(name);
1172 pending->objs[pending->num].u.var.kind = dt;
1173 pending->objs[pending->num].u.var.loc = *loc;
1174 pending->num++;
1177 static inline void pending_add_line(struct pending_list* pending, int source_idx,
1178 int line_num, unsigned long offset,
1179 unsigned long load_offset)
1181 pending_make_room(pending);
1182 pending->objs[pending->num].tag = PENDING_LINE;
1183 pending->objs[pending->num].u.line.source_idx = source_idx;
1184 pending->objs[pending->num].u.line.line_num = line_num;
1185 pending->objs[pending->num].u.line.offset = offset;
1186 pending->objs[pending->num].u.line.load_offset = load_offset;
1187 pending->num++;
1190 static void pending_flush(struct pending_list* pending, struct module* module,
1191 struct symt_function* func, struct symt_block* block)
1193 unsigned int i;
1195 for (i = 0; i < pending->num; i++)
1197 switch (pending->objs[i].tag)
1199 case PENDING_VAR:
1200 symt_add_func_local(module, func,
1201 pending->objs[i].u.var.kind, &pending->objs[i].u.var.loc,
1202 block, pending->objs[i].u.var.type, pending->objs[i].u.var.name);
1203 break;
1204 case PENDING_LINE:
1205 if (module->type == DMT_MACHO)
1206 pending->objs[i].u.line.offset -= func->address - pending->objs[i].u.line.load_offset;
1207 symt_add_func_line(module, func, pending->objs[i].u.line.source_idx,
1208 pending->objs[i].u.line.line_num, pending->objs[i].u.line.offset);
1209 break;
1210 default:
1211 ERR("Unknown pending object tag %u\n", (unsigned)pending->objs[i].tag);
1212 break;
1215 pending->num = 0;
1218 /******************************************************************
1219 * stabs_finalize_function
1221 * Ends function creation: mainly:
1222 * - cleans up line number information
1223 * - tries to set up a debug-start tag (FIXME: heuristic to be enhanced)
1224 * - for stabs which have absolute address in them, initializes the size of the
1225 * function (assuming that current function ends where next function starts)
1227 static void stabs_finalize_function(struct module* module, struct symt_function* func,
1228 unsigned long size)
1230 IMAGEHLP_LINE64 il;
1231 struct location loc;
1233 if (!func) return;
1234 symt_normalize_function(module, func);
1235 /* To define the debug-start of the function, we use the second line number.
1236 * Not 100% bullet proof, but better than nothing
1238 if (symt_fill_func_line_info(module, func, func->address, &il) &&
1239 symt_get_func_line_next(module, &il))
1241 loc.kind = loc_absolute;
1242 loc.offset = il.Address - func->address;
1243 symt_add_function_point(module, func, SymTagFuncDebugStart,
1244 &loc, NULL);
1246 if (size) func->size = size;
1249 static inline void stabbuf_append(char **buf, unsigned *buf_size, const char *str)
1251 unsigned str_len, buf_len;
1253 str_len = strlen(str);
1254 buf_len = strlen(*buf);
1256 if(str_len+buf_len >= *buf_size) {
1257 *buf_size += buf_len + str_len;
1258 *buf = HeapReAlloc(GetProcessHeap(), 0, *buf, *buf_size);
1261 strcpy(*buf+buf_len, str);
1264 BOOL stabs_parse(struct module* module, unsigned long load_offset,
1265 const char* pv_stab_ptr, int stablen,
1266 const char* strs, int strtablen,
1267 stabs_def_cb callback, void* user)
1269 struct symt_function* curr_func = NULL;
1270 struct symt_block* block = NULL;
1271 struct symt_compiland* compiland = NULL;
1272 char* srcpath = NULL;
1273 int i;
1274 int nstab;
1275 const char* ptr;
1276 char* stabbuff;
1277 unsigned int stabbufflen;
1278 const struct stab_nlist* stab_ptr;
1279 const char* strs_end;
1280 int strtabinc;
1281 char symname[4096];
1282 unsigned incl[32];
1283 int incl_stk = -1;
1284 int source_idx = -1;
1285 struct pending_list pending_block;
1286 struct pending_list pending_func;
1287 BOOL ret = TRUE;
1288 struct location loc;
1289 unsigned char type;
1290 size_t stabsize = sizeof(struct stab_nlist);
1291 uint64_t n_value;
1293 #ifdef __APPLE__
1294 if (module->process->is_64bit)
1295 stabsize = sizeof(struct nlist_64);
1296 #endif
1297 nstab = stablen / stabsize;
1298 strs_end = strs + strtablen;
1300 memset(stabs_basic, 0, sizeof(stabs_basic));
1301 memset(&pending_block, 0, sizeof(pending_block));
1302 memset(&pending_func, 0, sizeof(pending_func));
1305 * Allocate a buffer into which we can build stab strings for cases
1306 * where the stab is continued over multiple lines.
1308 stabbufflen = 65536;
1309 stabbuff = HeapAlloc(GetProcessHeap(), 0, stabbufflen);
1311 strtabinc = 0;
1312 stabbuff[0] = '\0';
1313 for (i = 0; i < nstab; i++)
1315 stab_ptr = (struct stab_nlist *)(pv_stab_ptr + i * stabsize);
1316 n_value = stab_ptr->n_value;
1317 #ifdef __APPLE__
1318 if (module->process->is_64bit)
1319 n_value = ((struct nlist_64 *)stab_ptr)->n_value;
1320 #endif
1321 ptr = strs + stab_ptr->n_strx;
1322 if ((ptr > strs_end) || (ptr + strlen(ptr) > strs_end))
1324 WARN("Bad stabs string %p\n", ptr);
1325 continue;
1327 if (*ptr != '\0' && (ptr[strlen(ptr) - 1] == '\\'))
1330 * Indicates continuation. Append this to the buffer, and go onto the
1331 * next record. Repeat the process until we find a stab without the
1332 * '/' character, as this indicates we have the whole thing.
1334 stabbuf_append(&stabbuff, &stabbufflen, ptr);
1335 continue;
1337 else if (stabbuff[0] != '\0')
1339 stabbuf_append(&stabbuff, &stabbufflen, ptr);
1340 ptr = stabbuff;
1343 if (stab_ptr->n_type & N_STAB)
1344 type = stab_ptr->n_type;
1345 else
1346 type = (stab_ptr->n_type & N_TYPE);
1348 /* only symbol entries contain a typedef */
1349 switch (type)
1351 case N_GSYM:
1352 case N_LCSYM:
1353 case N_STSYM:
1354 case N_RSYM:
1355 case N_LSYM:
1356 case N_ROSYM:
1357 case N_PSYM:
1358 if (strchr(ptr, '=') != NULL)
1361 * The stabs aren't in writable memory, so copy it over so we are
1362 * sure we can scribble on it.
1364 if (ptr != stabbuff)
1366 stabbuff[0] = 0;
1367 stabbuf_append(&stabbuff, &stabbufflen, ptr);
1368 ptr = stabbuff;
1370 stab_strcpy(symname, sizeof(symname), ptr);
1371 if (!stabs_parse_typedef(module, ptr, symname))
1373 /* skip this definition */
1374 stabbuff[0] = '\0';
1375 continue;
1380 switch (type)
1382 case N_GSYM:
1384 * These are useless with ELF. They have no value, and you have to
1385 * read the normal symbol table to get the address. Thus we
1386 * ignore them, and when we process the normal symbol table
1387 * we should do the right thing.
1389 * With a.out or mingw, they actually do make some amount of sense.
1391 stab_strcpy(symname, sizeof(symname), ptr);
1392 loc.kind = loc_absolute;
1393 loc.reg = 0;
1394 loc.offset = load_offset + n_value;
1395 symt_new_global_variable(module, compiland, symname, TRUE /* FIXME */,
1396 loc, 0, stabs_parse_type(ptr));
1397 break;
1398 case N_LCSYM:
1399 case N_STSYM:
1400 /* These are static symbols and BSS symbols. */
1401 stab_strcpy(symname, sizeof(symname), ptr);
1402 loc.kind = loc_absolute;
1403 loc.reg = 0;
1404 loc.offset = load_offset + n_value;
1405 symt_new_global_variable(module, compiland, symname, TRUE /* FIXME */,
1406 loc, 0, stabs_parse_type(ptr));
1407 break;
1408 case N_LBRAC:
1409 if (curr_func)
1411 block = symt_open_func_block(module, curr_func, block,
1412 n_value, 0);
1413 pending_flush(&pending_block, module, curr_func, block);
1415 break;
1416 case N_RBRAC:
1417 if (curr_func)
1418 block = symt_close_func_block(module, curr_func, block,
1419 n_value);
1420 break;
1421 case N_PSYM:
1422 /* These are function parameters. */
1423 if (curr_func != NULL)
1425 struct symt* param_type = stabs_parse_type(ptr);
1426 stab_strcpy(symname, sizeof(symname), ptr);
1427 loc.kind = loc_regrel;
1428 loc.reg = dbghelp_current_cpu->frame_regno;
1429 loc.offset = n_value;
1430 symt_add_func_local(module, curr_func,
1431 (int)n_value >= 0 ? DataIsParam : DataIsLocal,
1432 &loc, NULL, param_type, symname);
1433 symt_add_function_signature_parameter(module,
1434 (struct symt_function_signature*)curr_func->type,
1435 param_type);
1437 break;
1438 case N_RSYM:
1439 /* These are registers (as local variables) */
1440 if (curr_func != NULL)
1442 loc.kind = loc_register;
1443 loc.offset = 0;
1445 switch (n_value)
1447 case 0: loc.reg = CV_REG_EAX; break;
1448 case 1: loc.reg = CV_REG_ECX; break;
1449 case 2: loc.reg = CV_REG_EDX; break;
1450 case 3: loc.reg = CV_REG_EBX; break;
1451 case 4: loc.reg = CV_REG_ESP; break;
1452 case 5: loc.reg = CV_REG_EBP; break;
1453 case 6: loc.reg = CV_REG_ESI; break;
1454 case 7: loc.reg = CV_REG_EDI; break;
1455 case 11:
1456 case 12:
1457 case 13:
1458 case 14:
1459 case 15:
1460 case 16:
1461 case 17:
1462 case 18:
1463 case 19: loc.reg = CV_REG_ST0 + n_value - 12; break;
1464 case 21:
1465 case 22:
1466 case 23:
1467 case 24:
1468 case 25:
1469 case 26:
1470 case 27:
1471 case 28: loc.reg = CV_REG_XMM0 + n_value - 21; break;
1472 case 29:
1473 case 30:
1474 case 31:
1475 case 32:
1476 case 33:
1477 case 34:
1478 case 35:
1479 case 36: loc.reg = CV_REG_MM0 + n_value - 29; break;
1480 default:
1481 FIXME("Unknown register value (%lu)\n", (unsigned long)n_value);
1482 loc.reg = CV_REG_NONE;
1483 break;
1485 stab_strcpy(symname, sizeof(symname), ptr);
1486 if (ptr[strlen(symname) + 1] == 'P')
1488 struct symt* param_type = stabs_parse_type(ptr);
1489 stab_strcpy(symname, sizeof(symname), ptr);
1490 symt_add_func_local(module, curr_func, DataIsParam, &loc,
1491 NULL, param_type, symname);
1492 symt_add_function_signature_parameter(module,
1493 (struct symt_function_signature*)curr_func->type,
1494 param_type);
1496 else
1497 pending_add_var(&pending_block, ptr, DataIsLocal, &loc);
1499 break;
1500 case N_LSYM:
1501 /* These are local variables */
1502 loc.kind = loc_regrel;
1503 loc.reg = dbghelp_current_cpu->frame_regno;
1504 loc.offset = n_value;
1505 if (curr_func != NULL) pending_add_var(&pending_block, ptr, DataIsLocal, &loc);
1506 break;
1507 case N_SLINE:
1509 * This is a line number. These are always relative to the start
1510 * of the function (N_FUN), and this makes the lookup easier.
1512 assert(source_idx >= 0);
1513 if (curr_func != NULL)
1515 unsigned long offset = n_value;
1516 if (module->type == DMT_MACHO)
1517 offset -= curr_func->address - load_offset;
1518 symt_add_func_line(module, curr_func, source_idx,
1519 stab_ptr->n_desc, offset);
1521 else pending_add_line(&pending_func, source_idx, stab_ptr->n_desc,
1522 n_value, load_offset);
1523 break;
1524 case N_FUN:
1526 * For now, just declare the various functions. Later
1527 * on, we will add the line number information and the
1528 * local symbols.
1531 * Copy the string to a temp buffer so we
1532 * can kill everything after the ':'. We do
1533 * it this way because otherwise we end up dirtying
1534 * all of the pages related to the stabs, and that
1535 * sucks up swap space like crazy.
1537 stab_strcpy(symname, sizeof(symname), ptr);
1538 if (*symname)
1540 struct symt_function_signature* func_type;
1542 if (curr_func)
1544 /* First, clean up the previous function we were working on.
1545 * Assume size of the func is the delta between current offset
1546 * and offset of last function
1548 stabs_finalize_function(module, curr_func,
1549 n_value ?
1550 (load_offset + n_value - curr_func->address) : 0);
1552 func_type = symt_new_function_signature(module,
1553 stabs_parse_type(ptr), -1);
1554 curr_func = symt_new_function(module, compiland, symname,
1555 load_offset + n_value, 0,
1556 &func_type->symt);
1557 pending_flush(&pending_func, module, curr_func, NULL);
1559 else
1561 /* some versions of GCC to use a N_FUN "" to mark the end of a function
1562 * and n_value contains the size of the func
1564 stabs_finalize_function(module, curr_func, n_value);
1565 curr_func = NULL;
1567 break;
1568 case N_SO:
1570 * This indicates a new source file. Append the records
1571 * together, to build the correct path name.
1573 if (*ptr == '\0') /* end of N_SO file */
1575 /* Nuke old path. */
1576 HeapFree(GetProcessHeap(), 0, srcpath);
1577 srcpath = NULL;
1578 stabs_finalize_function(module, curr_func, 0);
1579 curr_func = NULL;
1580 source_idx = -1;
1581 incl_stk = -1;
1582 assert(block == NULL);
1583 compiland = NULL;
1585 else
1587 int len = strlen(ptr);
1588 if (ptr[len-1] != '/')
1590 stabs_reset_includes();
1591 source_idx = source_new(module, srcpath, ptr);
1592 compiland = symt_new_compiland(module, 0 /* FIXME */, source_idx);
1594 else
1596 srcpath = HeapAlloc(GetProcessHeap(), 0, len + 1);
1597 strcpy(srcpath, ptr);
1600 break;
1601 case N_SOL:
1602 source_idx = source_new(module, srcpath, ptr);
1603 break;
1604 case N_UNDF:
1605 strs += strtabinc;
1606 strtabinc = n_value;
1607 /* I'm not sure this is needed, so trace it before we obsolete it */
1608 if (curr_func)
1610 FIXME("UNDF: curr_func %s\n", curr_func->hash_elt.name);
1611 stabs_finalize_function(module, curr_func, 0); /* FIXME */
1612 curr_func = NULL;
1614 break;
1615 case N_OPT:
1616 /* Ignore this. We don't care what it points to. */
1617 break;
1618 case N_BINCL:
1619 stabs_add_include(stabs_new_include(ptr, n_value));
1620 assert(incl_stk < (int) ARRAY_SIZE(incl) - 1);
1621 incl[++incl_stk] = source_idx;
1622 source_idx = source_new(module, NULL, ptr);
1623 break;
1624 case N_EINCL:
1625 assert(incl_stk >= 0);
1626 source_idx = incl[incl_stk--];
1627 break;
1628 case N_EXCL:
1629 if (stabs_add_include(stabs_find_include(ptr, n_value)) < 0)
1631 ERR("Excluded header not found (%s,%ld)\n", ptr, (unsigned long)n_value);
1632 module_reset_debug_info(module);
1633 ret = FALSE;
1634 goto done;
1636 break;
1637 case N_MAIN:
1638 /* Always ignore these. GCC doesn't even generate them. */
1639 break;
1640 case N_BNSYM:
1641 case N_ENSYM:
1642 case N_OSO:
1643 /* Always ignore these, they seem to be used only on Darwin. */
1644 break;
1645 case N_ABS:
1646 #ifdef N_SECT
1647 case N_SECT:
1648 #endif
1649 /* FIXME: Other definition types (N_TEXT, N_DATA, N_BSS, ...)? */
1650 if (callback)
1652 BOOL is_public = (stab_ptr->n_type & N_EXT);
1653 BOOL is_global = is_public;
1655 #ifdef N_PEXT
1656 /* "private extern"; shared among compilation units in a shared
1657 * library, but not accessible from outside the library. */
1658 if (stab_ptr->n_type & N_PEXT)
1660 is_public = FALSE;
1661 is_global = TRUE;
1663 #endif
1665 if (*ptr == '_') ptr++;
1666 stab_strcpy(symname, sizeof(symname), ptr);
1668 callback(module, load_offset, symname, n_value,
1669 is_public, is_global, stab_ptr->n_other, compiland, user);
1671 break;
1672 default:
1673 ERR("Unknown stab type 0x%02x\n", type);
1674 break;
1676 stabbuff[0] = '\0';
1677 TRACE("0x%02x %lx %s\n",
1678 stab_ptr->n_type, (unsigned long)n_value, debugstr_a(strs + stab_ptr->n_strx));
1680 module->module.SymType = SymDia;
1681 module->module.CVSig = 'S' | ('T' << 8) | ('A' << 16) | ('B' << 24);
1682 /* FIXME: we could have a finer grain here */
1683 module->module.LineNumbers = TRUE;
1684 module->module.GlobalSymbols = TRUE;
1685 module->module.TypeInfo = TRUE;
1686 module->module.SourceIndexed = TRUE;
1687 module->module.Publics = TRUE;
1688 done:
1689 HeapFree(GetProcessHeap(), 0, stabbuff);
1690 stabs_free_includes();
1691 HeapFree(GetProcessHeap(), 0, pending_block.objs);
1692 HeapFree(GetProcessHeap(), 0, pending_func.objs);
1693 HeapFree(GetProcessHeap(), 0, srcpath);
1695 return ret;