Sync to trunk head(r38096)
[reactos.git] / reactos / dll / win32 / dbghelp / stabs.c
1 /*
2 * File stabs.c - read stabs information from the modules
3 *
4 * Copyright (C) 1996, Eric Youngdale.
5 * 1999-2005, Eric Pouech
6 *
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.
11 *
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.
16 *
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
20 *
21 *
22 * Maintenance Information
23 * -----------------------
24 *
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
30 */
31
32 #include "config.h"
33 #include "wine/port.h"
34
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>
55
56 #include "windef.h"
57 #include "winbase.h"
58 #include "winnls.h"
59
60 #include "dbghelp_private.h"
61
62 #include "wine/debug.h"
63
64 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp_stabs);
65
66 #define strtoull _strtoui64
67
68 #ifndef N_UNDF
69 #define N_UNDF 0x00
70 #endif
71
72 #define N_GSYM 0x20
73 #define N_FUN 0x24
74 #define N_STSYM 0x26
75 #define N_LCSYM 0x28
76 #define N_MAIN 0x2a
77 #define N_ROSYM 0x2c
78 #define N_BNSYM 0x2e
79 #define N_OPT 0x3c
80 #define N_RSYM 0x40
81 #define N_SLINE 0x44
82 #define N_ENSYM 0x4e
83 #define N_SO 0x64
84 #define N_LSYM 0x80
85 #define N_BINCL 0x82
86 #define N_SOL 0x84
87 #define N_PSYM 0xa0
88 #define N_EINCL 0xa2
89 #define N_LBRAC 0xc0
90 #define N_EXCL 0xc2
91 #define N_RBRAC 0xe0
92
93 struct stab_nlist
94 {
95 union
96 {
97 char* n_name;
98 struct stab_nlist* n_next;
99 long n_strx;
100 } n_un;
101 unsigned char n_type;
102 char n_other;
103 short n_desc;
104 unsigned long n_value;
105 };
106
107 static void stab_strcpy(char* dest, int sz, const char* source)
108 {
109 char* ptr = dest;
110 /*
111 * A strcpy routine that stops when we hit the ':' character.
112 * Faster than copying the whole thing, and then nuking the
113 * ':'.
114 * Takes also care of (valid) a::b constructs
115 */
116 while (*source != '\0')
117 {
118 if (source[0] != ':' && sz-- > 0) *ptr++ = *source++;
119 else if (source[1] == ':' && (sz -= 2) > 0)
120 {
121 *ptr++ = *source++;
122 *ptr++ = *source++;
123 }
124 else break;
125 }
126 *ptr-- = '\0';
127 /* GCC emits, in some cases, a .<digit>+ suffix.
128 * This is used for static variable inside functions, so
129 * that we can have several such variables with same name in
130 * the same compilation unit
131 * We simply ignore that suffix when present (we also get rid
132 * of it in ELF symtab parsing)
133 */
134 if (ptr >= dest && isdigit(*ptr))
135 {
136 while (ptr > dest && isdigit(*ptr)) ptr--;
137 if (*ptr == '.') *ptr = '\0';
138 }
139 assert(sz > 0);
140 }
141
142 typedef struct
143 {
144 char* name;
145 unsigned long value;
146 struct symt** vector;
147 int nrofentries;
148 } include_def;
149
150 #define MAX_INCLUDES 5120
151
152 static include_def* include_defs = NULL;
153 static int num_include_def = 0;
154 static int num_alloc_include_def = 0;
155 static int cu_include_stack[MAX_INCLUDES];
156 static int cu_include_stk_idx = 0;
157 static struct symt** cu_vector = NULL;
158 static int cu_nrofentries = 0;
159 static struct symt_basic* stabs_basic[36];
160
161 static int stabs_new_include(const char* file, unsigned long val)
162 {
163 if (num_include_def == num_alloc_include_def)
164 {
165 num_alloc_include_def += 256;
166 if (!include_defs)
167 include_defs = HeapAlloc(GetProcessHeap(), 0,
168 sizeof(include_defs[0]) * num_alloc_include_def);
169 else
170 include_defs = HeapReAlloc(GetProcessHeap(), 0, include_defs,
171 sizeof(include_defs[0]) * num_alloc_include_def);
172 memset(include_defs + num_include_def, 0, sizeof(include_defs[0]) * 256);
173 }
174 include_defs[num_include_def].name = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(file) + 1), file);
175 include_defs[num_include_def].value = val;
176 include_defs[num_include_def].vector = NULL;
177 include_defs[num_include_def].nrofentries = 0;
178
179 return num_include_def++;
180 }
181
182 static int stabs_find_include(const char* file, unsigned long val)
183 {
184 int i;
185
186 for (i = 0; i < num_include_def; i++)
187 {
188 if (val == include_defs[i].value &&
189 strcmp(file, include_defs[i].name) == 0)
190 return i;
191 }
192 return -1;
193 }
194
195 static int stabs_add_include(int idx)
196 {
197 if (idx < 0) return -1;
198 cu_include_stk_idx++;
199
200 /* if this happens, just bump MAX_INCLUDES */
201 /* we could also handle this as another dynarray */
202 assert(cu_include_stk_idx < MAX_INCLUDES);
203 cu_include_stack[cu_include_stk_idx] = idx;
204 return cu_include_stk_idx;
205 }
206
207 static void stabs_reset_includes(void)
208 {
209 /*
210 * The struct symt:s that we would need to use are reset when
211 * we start a new file. (at least the ones in filenr == 0)
212 */
213 cu_include_stk_idx = 0;/* keep 0 as index for the .c file itself */
214 memset(cu_vector, 0, sizeof(cu_vector[0]) * cu_nrofentries);
215 }
216
217 static void stabs_free_includes(void)
218 {
219 int i;
220
221 stabs_reset_includes();
222 for (i = 0; i < num_include_def; i++)
223 {
224 HeapFree(GetProcessHeap(), 0, include_defs[i].name);
225 HeapFree(GetProcessHeap(), 0, include_defs[i].vector);
226 }
227 HeapFree(GetProcessHeap(), 0, include_defs);
228 include_defs = NULL;
229 num_include_def = 0;
230 num_alloc_include_def = 0;
231 HeapFree(GetProcessHeap(), 0, cu_vector);
232 cu_vector = NULL;
233 cu_nrofentries = 0;
234 }
235
236 static struct symt** stabs_find_ref(long filenr, long subnr)
237 {
238 struct symt** ret;
239
240 /* FIXME: I could perhaps create a dummy include_def for each compilation
241 * unit which would allow not to handle those two cases separately
242 */
243 if (filenr == 0)
244 {
245 if (cu_nrofentries <= subnr)
246 {
247 if (!cu_vector)
248 cu_vector = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
249 sizeof(cu_vector[0]) * (subnr+1));
250 else
251 cu_vector = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
252 cu_vector, sizeof(cu_vector[0]) * (subnr+1));
253 cu_nrofentries = subnr + 1;
254 }
255 ret = &cu_vector[subnr];
256 }
257 else
258 {
259 include_def* idef;
260
261 assert(filenr <= cu_include_stk_idx);
262 idef = &include_defs[cu_include_stack[filenr]];
263
264 if (idef->nrofentries <= subnr)
265 {
266 if (!idef->vector)
267 idef->vector = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
268 sizeof(idef->vector[0]) * (subnr+1));
269 else
270 idef->vector = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
271 idef->vector, sizeof(idef->vector[0]) * (subnr+1));
272 idef->nrofentries = subnr + 1;
273 }
274 ret = &idef->vector[subnr];
275 }
276 TRACE("(%ld,%ld) => %p (%p)\n", filenr, subnr, ret, *ret);
277 return ret;
278 }
279
280 static struct symt** stabs_read_type_enum(const char** x)
281 {
282 long filenr, subnr;
283 const char* iter;
284 char* end;
285
286 iter = *x;
287 if (*iter == '(')
288 {
289 ++iter; /* '(' */
290 filenr = strtol(iter, &end, 10); /* <int> */
291 iter = ++end; /* ',' */
292 subnr = strtol(iter, &end, 10); /* <int> */
293 iter = ++end; /* ')' */
294 }
295 else
296 {
297 filenr = 0;
298 subnr = strtol(iter, &end, 10); /* <int> */
299 iter = end;
300 }
301 *x = iter;
302 return stabs_find_ref(filenr, subnr);
303 }
304
305 #define PTS_DEBUG
306 struct ParseTypedefData
307 {
308 const char* ptr;
309 char buf[1024];
310 int idx;
311 struct module* module;
312 #ifdef PTS_DEBUG
313 struct PTS_Error
314 {
315 const char* ptr;
316 unsigned line;
317 } errors[16];
318 int err_idx;
319 #endif
320 };
321
322 #ifdef PTS_DEBUG
323 static void stabs_pts_push(struct ParseTypedefData* ptd, unsigned line)
324 {
325 assert(ptd->err_idx < sizeof(ptd->errors) / sizeof(ptd->errors[0]));
326 ptd->errors[ptd->err_idx].line = line;
327 ptd->errors[ptd->err_idx].ptr = ptd->ptr;
328 ptd->err_idx++;
329 }
330 #define PTS_ABORTIF(ptd, t) do { if (t) { stabs_pts_push((ptd), __LINE__); return -1;} } while (0)
331 #else
332 #define PTS_ABORTIF(ptd, t) do { if (t) return -1; } while (0)
333 #endif
334
335 static int stabs_get_basic(struct ParseTypedefData* ptd, unsigned basic, struct symt** symt)
336 {
337 PTS_ABORTIF(ptd, basic >= sizeof(stabs_basic) / sizeof(stabs_basic[0]));
338
339 if (!stabs_basic[basic])
340 {
341 switch (basic)
342 {
343 case 1: stabs_basic[basic] = symt_new_basic(ptd->module, btInt, "int", 4); break;
344 case 2: stabs_basic[basic] = symt_new_basic(ptd->module, btChar, "char", 1); break;
345 case 3: stabs_basic[basic] = symt_new_basic(ptd->module, btInt, "short int", 2); break;
346 case 4: stabs_basic[basic] = symt_new_basic(ptd->module, btInt, "long int", 4); break;
347 case 5: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt, "unsigned char", 1); break;
348 case 6: stabs_basic[basic] = symt_new_basic(ptd->module, btInt, "signed char", 1); break;
349 case 7: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt, "unsigned short int", 2); break;
350 case 8: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt, "unsigned int", 4); break;
351 case 9: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt, "unsigned", 2); break;
352 case 10: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt, "unsigned long int", 2); break;
353 case 11: stabs_basic[basic] = symt_new_basic(ptd->module, btVoid, "void", 0); break;
354 case 12: stabs_basic[basic] = symt_new_basic(ptd->module, btFloat, "float", 4); break;
355 case 13: stabs_basic[basic] = symt_new_basic(ptd->module, btFloat, "double", 8); break;
356 case 14: stabs_basic[basic] = symt_new_basic(ptd->module, btFloat, "long double", 12); break;
357 case 15: stabs_basic[basic] = symt_new_basic(ptd->module, btInt, "integer", 4); break;
358 case 16: stabs_basic[basic] = symt_new_basic(ptd->module, btBool, "bool", 1); break;
359 /* case 17: short real */
360 /* case 18: real */
361 case 25: stabs_basic[basic] = symt_new_basic(ptd->module, btComplex, "float complex", 8); break;
362 case 26: stabs_basic[basic] = symt_new_basic(ptd->module, btComplex, "double complex", 16); break;
363 case 30: stabs_basic[basic] = symt_new_basic(ptd->module, btWChar, "wchar_t", 2); break;
364 case 31: stabs_basic[basic] = symt_new_basic(ptd->module, btInt, "long long int", 8); break;
365 case 32: stabs_basic[basic] = symt_new_basic(ptd->module, btUInt, "long long unsigned", 8); break;
366 /* starting at 35 are wine extensions (especially for R implementation) */
367 case 35: stabs_basic[basic] = symt_new_basic(ptd->module, btComplex, "long double complex", 24); break;
368 default: PTS_ABORTIF(ptd, 1);
369 }
370 }
371 *symt = &stabs_basic[basic]->symt;
372 return 0;
373 }
374
375 static int stabs_pts_read_type_def(struct ParseTypedefData* ptd,
376 const char* typename, struct symt** dt);
377
378 static int stabs_pts_read_id(struct ParseTypedefData* ptd)
379 {
380 const char* first = ptd->ptr;
381 unsigned int template = 0;
382 char ch;
383
384 while ((ch = *ptd->ptr++) != '\0')
385 {
386 switch (ch)
387 {
388 case ':':
389 if (template == 0)
390 {
391 unsigned int len = ptd->ptr - first - 1;
392 PTS_ABORTIF(ptd, len >= sizeof(ptd->buf) - ptd->idx);
393 memcpy(ptd->buf + ptd->idx, first, len);
394 ptd->buf[ptd->idx + len] = '\0';
395 ptd->idx += len + 1;
396 return 0;
397 }
398 break;
399 case '<': template++; break;
400 case '>': PTS_ABORTIF(ptd, template == 0); template--; break;
401 }
402 }
403 return -1;
404 }
405
406 static int stabs_pts_read_number(struct ParseTypedefData* ptd, long* v)
407 {
408 char* last;
409
410 *v = strtol(ptd->ptr, &last, 10);
411 PTS_ABORTIF(ptd, last == ptd->ptr);
412 ptd->ptr = last;
413 return 0;
414 }
415
416 static int stabs_pts_read_type_reference(struct ParseTypedefData* ptd,
417 long* filenr, long* subnr)
418 {
419 if (*ptd->ptr == '(')
420 {
421 /* '(' <int> ',' <int> ')' */
422 ptd->ptr++;
423 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, filenr) == -1);
424 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
425 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, subnr) == -1);
426 PTS_ABORTIF(ptd, *ptd->ptr++ != ')');
427 }
428 else
429 {
430 *filenr = 0;
431 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, subnr) == -1);
432 }
433 return 0;
434 }
435
436 struct pts_range_value
437 {
438 ULONGLONG val;
439 int sign;
440 };
441
442 static int stabs_pts_read_range_value(struct ParseTypedefData* ptd, struct pts_range_value* prv)
443 {
444 char* last;
445
446 switch (*ptd->ptr)
447 {
448 case '0':
449 while (*ptd->ptr == '0') ptd->ptr++;
450 if (*ptd->ptr >= '1' && *ptd->ptr <= '7')
451 {
452 switch (ptd->ptr[1])
453 {
454 case '0':
455 PTS_ABORTIF(ptd, ptd->ptr[0] != '1');
456 prv->sign = -1;
457 prv->val = 0;
458 while (isdigit(*ptd->ptr)) prv->val = (prv->val << 3) + *ptd->ptr++ - '0';
459 break;
460 case '7':
461 prv->sign = 1;
462 prv->val = 0;
463 while (isdigit(*ptd->ptr)) prv->val = (prv->val << 3) + *ptd->ptr++ - '0';
464 break;
465 default: PTS_ABORTIF(ptd, 1); break;
466 }
467 } else prv->sign = 0;
468 break;
469 case '-':
470 prv->sign = -1;
471 prv->val = strtoull(++ptd->ptr, &last, 10);
472 ptd->ptr = last;
473 break;
474 case '+':
475 default:
476 prv->sign = 1;
477 prv->val = strtoull(ptd->ptr, &last, 10);
478 ptd->ptr = last;
479 break;
480 }
481 return 0;
482 }
483
484 static int stabs_pts_read_range(struct ParseTypedefData* ptd, const char* typename,
485 struct symt** dt)
486 {
487 struct symt* ref;
488 struct pts_range_value lo;
489 struct pts_range_value hi;
490 unsigned size;
491 enum BasicType bt;
492 int i;
493 ULONGLONG v;
494
495 /* type ';' <int> ';' <int> ';' */
496 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &ref) == -1);
497 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
498 PTS_ABORTIF(ptd, stabs_pts_read_range_value(ptd, &lo) == -1);
499 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
500 PTS_ABORTIF(ptd, stabs_pts_read_range_value(ptd, &hi) == -1);
501 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
502
503 /* basically, we don't use ref... in some cases, for example, float is declared
504 * as a derived type of int... which won't help us... so we guess the types
505 * from the various formats
506 */
507 if (lo.sign == 0 && hi.sign < 0)
508 {
509 bt = btUInt;
510 size = hi.val;
511 }
512 else if (lo.sign < 0 && hi.sign == 0)
513 {
514 bt = btUInt;
515 size = lo.val;
516 }
517 else if (lo.sign > 0 && hi.sign == 0)
518 {
519 bt = btFloat;
520 size = lo.val;
521 }
522 else if (lo.sign < 0 && hi.sign > 0)
523 {
524 v = 1 << 7;
525 for (i = 7; i < 64; i += 8)
526 {
527 if (lo.val == v && hi.val == v - 1)
528 {
529 bt = btInt;
530 size = (i + 1) / 8;
531 break;
532 }
533 v <<= 8;
534 }
535 PTS_ABORTIF(ptd, i >= 64);
536 }
537 else if (lo.sign == 0 && hi.sign > 0)
538 {
539 if (hi.val == 127) /* specific case for char... */
540 {
541 bt = btChar;
542 size = 1;
543 }
544 else
545 {
546 v = 1;
547 for (i = 8; i <= 64; i += 8)
548 {
549 v <<= 8;
550 if (hi.val + 1 == v)
551 {
552 bt = btUInt;
553 size = (i + 1) / 8;
554 break;
555 }
556 }
557 PTS_ABORTIF(ptd, i > 64);
558 }
559 }
560 else PTS_ABORTIF(ptd, 1);
561
562 *dt = &symt_new_basic(ptd->module, bt, typename, size)->symt;
563 return 0;
564 }
565
566 static inline int stabs_pts_read_method_info(struct ParseTypedefData* ptd)
567 {
568 struct symt* dt;
569 char* tmp;
570 char mthd;
571
572 do
573 {
574 /* get type of return value */
575 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
576 if (*ptd->ptr == ';') ptd->ptr++;
577
578 /* get types of parameters */
579 if (*ptd->ptr == ':')
580 {
581 PTS_ABORTIF(ptd, !(tmp = strchr(ptd->ptr + 1, ';')));
582 ptd->ptr = tmp + 1;
583 }
584 PTS_ABORTIF(ptd, !(*ptd->ptr >= '0' && *ptd->ptr <= '9'));
585 ptd->ptr++;
586 PTS_ABORTIF(ptd, !(ptd->ptr[0] >= 'A' && *ptd->ptr <= 'D'));
587 mthd = *++ptd->ptr;
588 PTS_ABORTIF(ptd, mthd != '.' && mthd != '?' && mthd != '*');
589 ptd->ptr++;
590 if (mthd == '*')
591 {
592 long int ofs;
593 struct symt* dt;
594
595 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &ofs) == -1);
596 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
597 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
598 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
599 }
600 } while (*ptd->ptr != ';');
601 ptd->ptr++;
602
603 return 0;
604 }
605
606 static inline int stabs_pts_read_aggregate(struct ParseTypedefData* ptd,
607 struct symt_udt* sdt)
608 {
609 long sz, ofs;
610 struct symt* adt;
611 struct symt* dt = NULL;
612 int idx;
613 int doadd;
614
615 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &sz) == -1);
616
617 doadd = symt_set_udt_size(ptd->module, sdt, sz);
618 if (*ptd->ptr == '!') /* C++ inheritence */
619 {
620 long num_classes;
621
622 ptd->ptr++;
623 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &num_classes) == -1);
624 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
625 while (--num_classes >= 0)
626 {
627 ptd->ptr += 2; /* skip visibility and inheritence */
628 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &ofs) == -1);
629 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
630
631 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &adt) == -1);
632
633 if (doadd)
634 {
635 char tmp[256];
636 WCHAR* name;
637 DWORD64 size;
638
639 symt_get_info(adt, TI_GET_SYMNAME, &name);
640 strcpy(tmp, "__inherited_class_");
641 WideCharToMultiByte(CP_ACP, 0, name, -1,
642 tmp + strlen(tmp), sizeof(tmp) - strlen(tmp),
643 NULL, NULL);
644 HeapFree(GetProcessHeap(), 0, name);
645 /* FIXME: TI_GET_LENGTH will not always work, especially when adt
646 * has just been seen as a forward definition and not the real stuff
647 * yet.
648 * As we don't use much the size of members in structs, this may not
649 * be much of a problem
650 */
651 symt_get_info(adt, TI_GET_LENGTH, &size);
652 symt_add_udt_element(ptd->module, sdt, tmp, adt, ofs, (DWORD)size * 8);
653 }
654 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
655 }
656
657 }
658 /* if the structure has already been filled, just redo the parsing
659 * but don't store results into the struct
660 * FIXME: there's a quite ugly memory leak in there...
661 */
662
663 /* Now parse the individual elements of the structure/union. */
664 while (*ptd->ptr != ';')
665 {
666 /* agg_name : type ',' <int:offset> ',' <int:size> */
667 idx = ptd->idx;
668
669 if (ptd->ptr[0] == '$' && ptd->ptr[1] == 'v')
670 {
671 long x;
672
673 if (ptd->ptr[2] == 'f')
674 {
675 /* C++ virtual method table */
676 ptd->ptr += 3;
677 stabs_read_type_enum(&ptd->ptr);
678 PTS_ABORTIF(ptd, *ptd->ptr++ != ':');
679 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
680 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
681 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &x) == -1);
682 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
683 ptd->idx = idx;
684 continue;
685 }
686 else if (ptd->ptr[2] == 'b')
687 {
688 ptd->ptr += 3;
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_type_def(ptd, NULL, &dt) == -1);
692 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
693 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &x) == -1);
694 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
695 ptd->idx = idx;
696 continue;
697 }
698 }
699
700 PTS_ABORTIF(ptd, stabs_pts_read_id(ptd) == -1);
701 /* Ref. TSDF R2.130 Section 7.4. When the field name is a method name
702 * it is followed by two colons rather than one.
703 */
704 if (*ptd->ptr == ':')
705 {
706 ptd->ptr++;
707 stabs_pts_read_method_info(ptd);
708 ptd->idx = idx;
709 continue;
710 }
711 else
712 {
713 /* skip C++ member protection /0 /1 or /2 */
714 if (*ptd->ptr == '/') ptd->ptr += 2;
715 }
716 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &adt) == -1);
717
718 switch (*ptd->ptr++)
719 {
720 case ',':
721 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &ofs) == -1);
722 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
723 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &sz) == -1);
724 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
725
726 if (doadd) symt_add_udt_element(ptd->module, sdt, ptd->buf + idx, adt, ofs, sz);
727 break;
728 case ':':
729 {
730 char* tmp;
731 /* method parameters... terminated by ';' */
732 PTS_ABORTIF(ptd, !(tmp = strchr(ptd->ptr, ';')));
733 ptd->ptr = tmp + 1;
734 }
735 break;
736 default:
737 PTS_ABORTIF(ptd, TRUE);
738 }
739 ptd->idx = idx;
740 }
741 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
742 if (*ptd->ptr == '~')
743 {
744 ptd->ptr++;
745 PTS_ABORTIF(ptd, *ptd->ptr++ != '%');
746 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &dt) == -1);
747 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
748 }
749 return 0;
750 }
751
752 static inline int stabs_pts_read_enum(struct ParseTypedefData* ptd,
753 struct symt_enum* edt)
754 {
755 long value;
756 int idx;
757
758 while (*ptd->ptr != ';')
759 {
760 idx = ptd->idx;
761 PTS_ABORTIF(ptd, stabs_pts_read_id(ptd) == -1);
762 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &value) == -1);
763 PTS_ABORTIF(ptd, *ptd->ptr++ != ',');
764 symt_add_enum_element(ptd->module, edt, ptd->buf + idx, value);
765 ptd->idx = idx;
766 }
767 ptd->ptr++;
768 return 0;
769 }
770
771 static inline int stabs_pts_read_array(struct ParseTypedefData* ptd,
772 struct symt** adt)
773 {
774 long lo, hi;
775 struct symt* range_dt;
776 struct symt* base_dt;
777
778 /* ar<typeinfo_nodef>;<int>;<int>;<typeinfo> */
779
780 PTS_ABORTIF(ptd, *ptd->ptr++ != 'r');
781
782 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &range_dt) == -1);
783 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
784 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &lo) == -1);
785 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
786 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &hi) == -1);
787 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
788
789 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &base_dt) == -1);
790
791 *adt = &symt_new_array(ptd->module, lo, hi, base_dt, range_dt)->symt;
792 return 0;
793 }
794
795 static int stabs_pts_read_type_def(struct ParseTypedefData* ptd, const char* typename,
796 struct symt** ret_dt)
797 {
798 int idx;
799 long sz = -1;
800 struct symt* new_dt = NULL; /* newly created data type */
801 struct symt* ref_dt; /* referenced data type (pointer...) */
802 long filenr1, subnr1, tmp;
803
804 /* things are a bit complicated because of the way the typedefs are stored inside
805 * the file, because addresses can change when realloc is done, so we must call
806 * over and over stabs_find_ref() to keep the correct values around
807 */
808 PTS_ABORTIF(ptd, stabs_pts_read_type_reference(ptd, &filenr1, &subnr1) == -1);
809
810 while (*ptd->ptr == '=')
811 {
812 ptd->ptr++;
813 PTS_ABORTIF(ptd, new_dt != NULL);
814
815 /* first handle attribute if any */
816 switch (*ptd->ptr)
817 {
818 case '@':
819 if (*++ptd->ptr == 's')
820 {
821 ptd->ptr++;
822 if (stabs_pts_read_number(ptd, &sz) == -1)
823 {
824 ERR("Not an attribute... NIY\n");
825 ptd->ptr -= 2;
826 return -1;
827 }
828 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
829 }
830 break;
831 }
832 /* then the real definitions */
833 switch (*ptd->ptr++)
834 {
835 case '*':
836 case '&':
837 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &ref_dt) == -1);
838 new_dt = &symt_new_pointer(ptd->module, ref_dt)->symt;
839 break;
840 case 'k': /* 'const' modifier */
841 case 'B': /* 'volatile' modifier */
842 /* just kinda ignore the modifier, I guess -gmt */
843 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, typename, &new_dt) == -1);
844 break;
845 case '(':
846 ptd->ptr--;
847 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, typename, &new_dt) == -1);
848 break;
849 case 'a':
850 PTS_ABORTIF(ptd, stabs_pts_read_array(ptd, &new_dt) == -1);
851 break;
852 case 'r':
853 PTS_ABORTIF(ptd, stabs_pts_read_range(ptd, typename, &new_dt) == -1);
854 assert(!*stabs_find_ref(filenr1, subnr1));
855 *stabs_find_ref(filenr1, subnr1) = new_dt;
856 break;
857 case 'f':
858 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &ref_dt) == -1);
859 new_dt = &symt_new_function_signature(ptd->module, ref_dt, -1)->symt;
860 break;
861 case 'e':
862 stabs_get_basic(ptd, 1 /* int */, &ref_dt);
863 new_dt = &symt_new_enum(ptd->module, typename, ref_dt)->symt;
864 PTS_ABORTIF(ptd, stabs_pts_read_enum(ptd, (struct symt_enum*)new_dt) == -1);
865 break;
866 case 's':
867 case 'u':
868 {
869 struct symt_udt* udt;
870 enum UdtKind kind = (ptd->ptr[-1] == 's') ? UdtStruct : UdtUnion;
871 /* udt can have been already defined in a forward definition */
872 udt = (struct symt_udt*)*stabs_find_ref(filenr1, subnr1);
873 if (!udt)
874 {
875 udt = symt_new_udt(ptd->module, typename, 0, kind);
876 /* we need to set it here, because a struct can hold a pointer
877 * to itself
878 */
879 new_dt = *stabs_find_ref(filenr1, subnr1) = &udt->symt;
880 }
881 else
882 {
883 unsigned l1, l2;
884 if (udt->symt.tag != SymTagUDT)
885 {
886 ERR("Forward declaration (%p/%s) is not an aggregate (%u)\n",
887 udt, symt_get_name(&udt->symt), udt->symt.tag);
888 return -1;
889 }
890 /* FIXME: we currently don't correctly construct nested C++
891 * classes names. Therefore, we could be here with either:
892 * - typename and udt->hash_elt.name being the same string
893 * (non embedded case)
894 * - typename being foo::bar while udt->hash_elt.name being
895 * just bar
896 * So, we twist the comparison to test both occurrences. When
897 * we have proper C++ types in this file, this twist has to be
898 * removed
899 */
900 l1 = strlen(udt->hash_elt.name);
901 l2 = strlen(typename);
902 if (l1 > l2 || strcmp(udt->hash_elt.name, typename + l2 - l1))
903 ERR("Forward declaration name mismatch %s <> %s\n",
904 udt->hash_elt.name, typename);
905 new_dt = &udt->symt;
906 }
907 PTS_ABORTIF(ptd, stabs_pts_read_aggregate(ptd, udt) == -1);
908 }
909 break;
910 case 'x':
911 idx = ptd->idx;
912 tmp = *ptd->ptr++;
913 PTS_ABORTIF(ptd, stabs_pts_read_id(ptd) == -1);
914 switch (tmp)
915 {
916 case 'e':
917 stabs_get_basic(ptd, 1 /* int */, &ref_dt);
918 new_dt = &symt_new_enum(ptd->module, ptd->buf + idx, ref_dt)->symt;
919 break;
920 case 's':
921 new_dt = &symt_new_udt(ptd->module, ptd->buf + idx, 0, UdtStruct)->symt;
922 break;
923 case 'u':
924 new_dt = &symt_new_udt(ptd->module, ptd->buf + idx, 0, UdtUnion)->symt;
925 break;
926 default:
927 return -1;
928 }
929 ptd->idx = idx;
930 break;
931 case '-':
932 {
933 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &tmp) == -1);
934 PTS_ABORTIF(ptd, stabs_get_basic(ptd, tmp, &new_dt) == -1);
935 PTS_ABORTIF(ptd, *ptd->ptr++ != ';');
936 }
937 break;
938 case '#':
939 if (*ptd->ptr == '#')
940 {
941 ptd->ptr++;
942 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &ref_dt) == -1);
943 new_dt = &symt_new_function_signature(ptd->module, ref_dt, -1)->symt;
944 }
945 else
946 {
947 struct symt* cls_dt;
948 struct symt* pmt_dt;
949
950 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &cls_dt) == -1);
951 PTS_ABORTIF(ptd, *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;
954 while (*ptd->ptr == ',')
955 {
956 ptd->ptr++;
957 PTS_ABORTIF(ptd, stabs_pts_read_type_def(ptd, NULL, &pmt_dt) == -1);
958 }
959 }
960 break;
961 case 'R':
962 {
963 long type, len, unk;
964 int basic;
965
966 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &type) == -1);
967 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
968 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &len) == -1);
969 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
970 PTS_ABORTIF(ptd, stabs_pts_read_number(ptd, &unk) == -1);
971 PTS_ABORTIF(ptd, *ptd->ptr++ != ';'); /* ';' */
972
973 switch (type) /* see stabs_get_basic for the details */
974 {
975 case 1: basic = 12; break;
976 case 2: basic = 13; break;
977 case 3: basic = 25; break;
978 case 4: basic = 26; break;
979 case 5: basic = 35; break;
980 case 6: basic = 14; break;
981 default: PTS_ABORTIF(ptd, 1);
982 }
983 PTS_ABORTIF(ptd, stabs_get_basic(ptd, basic, &new_dt) == -1);
984 }
985 break;
986 default:
987 ERR("Unknown type '%c'\n", ptd->ptr[-1]);
988 return -1;
989 }
990 }
991
992 if (!new_dt)
993 {
994 /* is it a forward declaration that has been filled ? */
995 new_dt = *stabs_find_ref(filenr1, subnr1);
996 /* if not, this should be void (which is defined as a ref to itself, but we
997 * don't correctly catch it)
998 */
999 if (!new_dt && typename)
1000 {
1001 new_dt = &symt_new_basic(ptd->module, btVoid, typename, 0)->symt;
1002 PTS_ABORTIF(ptd, strcmp(typename, "void"));
1003 }
1004 }
1005
1006 *stabs_find_ref(filenr1, subnr1) = *ret_dt = new_dt;
1007
1008 TRACE("Adding (%ld,%ld) %s\n", filenr1, subnr1, debugstr_a(typename));
1009
1010 return 0;
1011 }
1012
1013 static int stabs_parse_typedef(struct module* module, const char* ptr,
1014 const char* typename)
1015 {
1016 struct ParseTypedefData ptd;
1017 struct symt* dt;
1018 int ret = -1;
1019
1020 /* check for already existing definition */
1021
1022 TRACE("%s => %s\n", typename, debugstr_a(ptr));
1023 ptd.module = module;
1024 ptd.idx = 0;
1025 #ifdef PTS_DEBUG
1026 ptd.err_idx = 0;
1027 #endif
1028 for (ptd.ptr = ptr - 1; ;)
1029 {
1030 ptd.ptr = strchr(ptd.ptr + 1, ':');
1031 if (ptd.ptr == NULL || *++ptd.ptr != ':') break;
1032 }
1033 if (ptd.ptr)
1034 {
1035 if (*ptd.ptr != '(') ptd.ptr++;
1036 /* most of type definitions take one char, except Tt */
1037 if (*ptd.ptr != '(') ptd.ptr++;
1038 ret = stabs_pts_read_type_def(&ptd, typename, &dt);
1039 }
1040
1041 if (ret == -1 || *ptd.ptr)
1042 {
1043 #ifdef PTS_DEBUG
1044 int i;
1045 TRACE("Failure on %s\n", debugstr_a(ptr));
1046 if (ret == -1)
1047 {
1048 for (i = 0; i < ptd.err_idx; i++)
1049 {
1050 TRACE("[%d]: line %d => %s\n",
1051 i, ptd.errors[i].line, debugstr_a(ptd.errors[i].ptr));
1052 }
1053 }
1054 else
1055 TRACE("[0]: => %s\n", debugstr_a(ptd.ptr));
1056
1057 #else
1058 ERR("Failure on %s at %s\n", debugstr_a(ptr), debugstr_a(ptd.ptr));
1059 #endif
1060 return FALSE;
1061 }
1062
1063 return TRUE;
1064 }
1065
1066 static struct symt* stabs_parse_type(const char* stab)
1067 {
1068 const char* c = stab - 1;
1069
1070 /*
1071 * Look through the stab definition, and figure out what struct symt
1072 * this represents. If we have something we know about, assign the
1073 * type.
1074 * According to "The \"stabs\" debug format" (Rev 2.130) the name may be
1075 * a C++ name and contain double colons e.g. foo::bar::baz:t5=*6.
1076 */
1077 do
1078 {
1079 if ((c = strchr(c + 1, ':')) == NULL) return NULL;
1080 } while (*++c == ':');
1081
1082 /*
1083 * The next characters say more about the type (i.e. data, function, etc)
1084 * of symbol. Skip them. (C++ for example may have Tt).
1085 * Actually this is a very weak description; I think Tt is the only
1086 * multiple combination we should see.
1087 */
1088 while (*c && *c != '(' && !isdigit(*c))
1089 c++;
1090 /*
1091 * The next is either an integer or a (integer,integer).
1092 * The stabs_read_type_enum() takes care that stab_types is large enough.
1093 */
1094 return *stabs_read_type_enum(&c);
1095 }
1096
1097 struct pending_loc_var
1098 {
1099 char name[256];
1100 struct symt* type;
1101 enum DataKind kind;
1102 struct location loc;
1103 };
1104
1105 struct pending_block
1106 {
1107 struct pending_loc_var* vars;
1108 unsigned num;
1109 unsigned allocated;
1110 };
1111
1112 static inline void pending_add(struct pending_block* pending, const char* name,
1113 enum DataKind dt, const struct location* loc)
1114 {
1115 if (pending->num == pending->allocated)
1116 {
1117 pending->allocated += 8;
1118 if (!pending->vars)
1119 pending->vars = HeapAlloc(GetProcessHeap(), 0,
1120 pending->allocated * sizeof(pending->vars[0]));
1121 else
1122 pending->vars = HeapReAlloc(GetProcessHeap(), 0, pending->vars,
1123 pending->allocated * sizeof(pending->vars[0]));
1124 }
1125 stab_strcpy(pending->vars[pending->num].name,
1126 sizeof(pending->vars[pending->num].name), name);
1127 pending->vars[pending->num].type = stabs_parse_type(name);
1128 pending->vars[pending->num].kind = dt;
1129 pending->vars[pending->num].loc = *loc;
1130 pending->num++;
1131 }
1132
1133 static void pending_flush(struct pending_block* pending, struct module* module,
1134 struct symt_function* func, struct symt_block* block)
1135 {
1136 unsigned int i;
1137
1138 for (i = 0; i < pending->num; i++)
1139 {
1140 symt_add_func_local(module, func,
1141 pending->vars[i].kind, &pending->vars[i].loc,
1142 block, pending->vars[i].type, pending->vars[i].name);
1143 }
1144 pending->num = 0;
1145 }
1146
1147 /******************************************************************
1148 * stabs_finalize_function
1149 *
1150 * Ends function creation: mainly:
1151 * - cleans up line number information
1152 * - tries to set up a debug-start tag (FIXME: heuristic to be enhanced)
1153 * - for stabs which have absolute address in them, initializes the size of the
1154 * function (assuming that current function ends where next function starts)
1155 */
1156 static void stabs_finalize_function(struct module* module, struct symt_function* func,
1157 unsigned long size)
1158 {
1159 IMAGEHLP_LINE il;
1160 struct location loc;
1161
1162 if (!func) return;
1163 symt_normalize_function(module, func);
1164 /* To define the debug-start of the function, we use the second line number.
1165 * Not 100% bullet proof, but better than nothing
1166 */
1167 if (symt_fill_func_line_info(module, func, func->address, &il) &&
1168 symt_get_func_line_next(module, &il))
1169 {
1170 loc.kind = loc_absolute;
1171 loc.offset = il.Address - func->address;
1172 symt_add_function_point(module, func, SymTagFuncDebugStart,
1173 &loc, NULL);
1174 }
1175 if (size) func->size = size;
1176 }
1177
1178 BOOL stabs_parse(struct module* module, unsigned long load_offset,
1179 const void* pv_stab_ptr, int stablen,
1180 const char* strs, int strtablen)
1181 {
1182 struct symt_function* curr_func = NULL;
1183 struct symt_block* block = NULL;
1184 struct symt_compiland* compiland = NULL;
1185 char srcpath[PATH_MAX]; /* path to directory source file is in */
1186 int i;
1187 int nstab;
1188 const char* ptr;
1189 char* stabbuff;
1190 unsigned int stabbufflen;
1191 const struct stab_nlist* stab_ptr = pv_stab_ptr;
1192 const char* strs_end;
1193 int strtabinc;
1194 char symname[4096];
1195 unsigned incl[32];
1196 int incl_stk = -1;
1197 int source_idx = -1;
1198 struct pending_block pending;
1199 BOOL ret = TRUE;
1200 struct location loc;
1201
1202 nstab = stablen / sizeof(struct stab_nlist);
1203 strs_end = strs + strtablen;
1204
1205 memset(srcpath, 0, sizeof(srcpath));
1206 memset(stabs_basic, 0, sizeof(stabs_basic));
1207 memset(&pending, 0, sizeof(pending));
1208
1209 /*
1210 * Allocate a buffer into which we can build stab strings for cases
1211 * where the stab is continued over multiple lines.
1212 */
1213 stabbufflen = 65536;
1214 stabbuff = HeapAlloc(GetProcessHeap(), 0, stabbufflen);
1215
1216 strtabinc = 0;
1217 stabbuff[0] = '\0';
1218 for (i = 0; i < nstab; i++, stab_ptr++)
1219 {
1220 ptr = strs + stab_ptr->n_un.n_strx;
1221 if ((ptr > strs_end) || (ptr + strlen(ptr) > strs_end))
1222 {
1223 WARN("Bad stabs string %p\n", ptr);
1224 continue;
1225 }
1226 if (*ptr != '\0' && (ptr[strlen(ptr) - 1] == '\\'))
1227 {
1228 /*
1229 * Indicates continuation. Append this to the buffer, and go onto the
1230 * next record. Repeat the process until we find a stab without the
1231 * '/' character, as this indicates we have the whole thing.
1232 */
1233 unsigned len = strlen(ptr);
1234 if (strlen(stabbuff) + len > stabbufflen)
1235 {
1236 stabbufflen += 65536;
1237 stabbuff = HeapReAlloc(GetProcessHeap(), 0, stabbuff, stabbufflen);
1238 }
1239 strncat(stabbuff, ptr, len - 1);
1240 continue;
1241 }
1242 else if (stabbuff[0] != '\0')
1243 {
1244 strcat(stabbuff, ptr);
1245 ptr = stabbuff;
1246 }
1247
1248 /* only symbol entries contain a typedef */
1249 switch (stab_ptr->n_type)
1250 {
1251 case N_GSYM:
1252 case N_LCSYM:
1253 case N_STSYM:
1254 case N_RSYM:
1255 case N_LSYM:
1256 case N_ROSYM:
1257 case N_PSYM:
1258 if (strchr(ptr, '=') != NULL)
1259 {
1260 /*
1261 * The stabs aren't in writable memory, so copy it over so we are
1262 * sure we can scribble on it.
1263 */
1264 if (ptr != stabbuff)
1265 {
1266 strcpy(stabbuff, ptr);
1267 ptr = stabbuff;
1268 }
1269 stab_strcpy(symname, sizeof(symname), ptr);
1270 if (!stabs_parse_typedef(module, ptr, symname))
1271 {
1272 /* skip this definition */
1273 stabbuff[0] = '\0';
1274 continue;
1275 }
1276 }
1277 }
1278
1279 switch (stab_ptr->n_type)
1280 {
1281 case N_GSYM:
1282 /*
1283 * These are useless with ELF. They have no value, and you have to
1284 * read the normal symbol table to get the address. Thus we
1285 * ignore them, and when we process the normal symbol table
1286 * we should do the right thing.
1287 *
1288 * With a.out or mingw, they actually do make some amount of sense.
1289 */
1290 stab_strcpy(symname, sizeof(symname), ptr);
1291 symt_new_global_variable(module, compiland, symname, TRUE /* FIXME */,
1292 load_offset + stab_ptr->n_value, 0,
1293 stabs_parse_type(ptr));
1294 break;
1295 case N_LCSYM:
1296 case N_STSYM:
1297 /* These are static symbols and BSS symbols. */
1298 stab_strcpy(symname, sizeof(symname), ptr);
1299 symt_new_global_variable(module, compiland, symname, TRUE /* FIXME */,
1300 load_offset + stab_ptr->n_value, 0,
1301 stabs_parse_type(ptr));
1302 break;
1303 case N_LBRAC:
1304 if (curr_func)
1305 {
1306 block = symt_open_func_block(module, curr_func, block,
1307 stab_ptr->n_value, 0);
1308 pending_flush(&pending, module, curr_func, block);
1309 }
1310 break;
1311 case N_RBRAC:
1312 if (curr_func)
1313 block = symt_close_func_block(module, curr_func, block,
1314 stab_ptr->n_value);
1315 break;
1316 case N_PSYM:
1317 /* These are function parameters. */
1318 if (curr_func != NULL)
1319 {
1320 struct symt* param_type = stabs_parse_type(ptr);
1321 stab_strcpy(symname, sizeof(symname), ptr);
1322 loc.kind = loc_regrel;
1323 loc.reg = 0; /* FIXME */
1324 loc.offset = stab_ptr->n_value;
1325 symt_add_func_local(module, curr_func,
1326 (long)stab_ptr->n_value >= 0 ? DataIsParam : DataIsLocal,
1327 &loc, NULL, param_type, symname);
1328 symt_add_function_signature_parameter(module,
1329 (struct symt_function_signature*)curr_func->type,
1330 param_type);
1331 }
1332 break;
1333 case N_RSYM:
1334 /* These are registers (as local variables) */
1335 if (curr_func != NULL)
1336 {
1337 loc.kind = loc_register;
1338 loc.offset = 0;
1339
1340 switch (stab_ptr->n_value)
1341 {
1342 case 0: loc.reg = CV_REG_EAX; break;
1343 case 1: loc.reg = CV_REG_ECX; break;
1344 case 2: loc.reg = CV_REG_EDX; break;
1345 case 3: loc.reg = CV_REG_EBX; break;
1346 case 4: loc.reg = CV_REG_ESP; break;
1347 case 5: loc.reg = CV_REG_EBP; break;
1348 case 6: loc.reg = CV_REG_ESI; break;
1349 case 7: loc.reg = CV_REG_EDI; break;
1350 case 11:
1351 case 12:
1352 case 13:
1353 case 14:
1354 case 15:
1355 case 16:
1356 case 17:
1357 case 18:
1358 case 19: loc.reg = CV_REG_ST0 + stab_ptr->n_value - 12; break;
1359 default:
1360 FIXME("Unknown register value (%lu)\n", stab_ptr->n_value);
1361 loc.reg = CV_REG_NONE;
1362 break;
1363 }
1364 stab_strcpy(symname, sizeof(symname), ptr);
1365 if (ptr[strlen(symname) + 1] == 'P')
1366 {
1367 struct symt* param_type = stabs_parse_type(ptr);
1368 stab_strcpy(symname, sizeof(symname), ptr);
1369 symt_add_func_local(module, curr_func, DataIsParam, &loc,
1370 NULL, param_type, symname);
1371 symt_add_function_signature_parameter(module,
1372 (struct symt_function_signature*)curr_func->type,
1373 param_type);
1374 }
1375 else
1376 pending_add(&pending, ptr, DataIsLocal, &loc);
1377 }
1378 break;
1379 case N_LSYM:
1380 /* These are local variables */
1381 loc.kind = loc_regrel;
1382 loc.reg = 0; /* FIXME */
1383 loc.offset = stab_ptr->n_value;
1384 if (curr_func != NULL) pending_add(&pending, ptr, DataIsLocal, &loc);
1385 break;
1386 case N_SLINE:
1387 /*
1388 * This is a line number. These are always relative to the start
1389 * of the function (N_FUN), and this makes the lookup easier.
1390 */
1391 if (curr_func != NULL)
1392 {
1393 assert(source_idx >= 0);
1394 symt_add_func_line(module, curr_func, source_idx,
1395 stab_ptr->n_desc, stab_ptr->n_value);
1396 }
1397 break;
1398 case N_FUN:
1399 /*
1400 * For now, just declare the various functions. Later
1401 * on, we will add the line number information and the
1402 * local symbols.
1403 */
1404 /*
1405 * Copy the string to a temp buffer so we
1406 * can kill everything after the ':'. We do
1407 * it this way because otherwise we end up dirtying
1408 * all of the pages related to the stabs, and that
1409 * sucks up swap space like crazy.
1410 */
1411 stab_strcpy(symname, sizeof(symname), ptr);
1412 if (*symname)
1413 {
1414 struct symt_function_signature* func_type;
1415
1416 if (curr_func)
1417 {
1418 /* First, clean up the previous function we were working on.
1419 * Assume size of the func is the delta between current offset
1420 * and offset of last function
1421 */
1422 stabs_finalize_function(module, curr_func,
1423 stab_ptr->n_value ?
1424 (load_offset + stab_ptr->n_value - curr_func->address) : 0);
1425 }
1426 func_type = symt_new_function_signature(module,
1427 stabs_parse_type(ptr), -1);
1428 curr_func = symt_new_function(module, compiland, symname,
1429 load_offset + stab_ptr->n_value, 0,
1430 &func_type->symt);
1431 }
1432 else
1433 {
1434 /* some versions of GCC to use a N_FUN "" to mark the end of a function
1435 * and n_value contains the size of the func
1436 */
1437 stabs_finalize_function(module, curr_func, stab_ptr->n_value);
1438 curr_func = NULL;
1439 }
1440 break;
1441 case N_SO:
1442 /*
1443 * This indicates a new source file. Append the records
1444 * together, to build the correct path name.
1445 */
1446 if (*ptr == '\0') /* end of N_SO file */
1447 {
1448 /* Nuke old path. */
1449 srcpath[0] = '\0';
1450 stabs_finalize_function(module, curr_func, 0);
1451 curr_func = NULL;
1452 source_idx = -1;
1453 incl_stk = -1;
1454 assert(block == NULL);
1455 compiland = NULL;
1456 }
1457 else
1458 {
1459 int len = strlen(ptr);
1460 if (ptr[len-1] != '/')
1461 {
1462 stabs_reset_includes();
1463 source_idx = source_new(module, srcpath, ptr);
1464 compiland = symt_new_compiland(module, 0 /* FIXME */, source_idx);
1465 }
1466 else
1467 strcpy(srcpath, ptr);
1468 }
1469 break;
1470 case N_SOL:
1471 source_idx = source_new(module, srcpath, ptr);
1472 break;
1473 case N_UNDF:
1474 strs += strtabinc;
1475 strtabinc = stab_ptr->n_value;
1476 /* I'm not sure this is needed, so trace it before we obsolete it */
1477 if (curr_func)
1478 {
1479 FIXME("UNDF: curr_func %s\n", curr_func->hash_elt.name);
1480 stabs_finalize_function(module, curr_func, 0); /* FIXME */
1481 curr_func = NULL;
1482 }
1483 break;
1484 case N_OPT:
1485 /* Ignore this. We don't care what it points to. */
1486 break;
1487 case N_BINCL:
1488 stabs_add_include(stabs_new_include(ptr, stab_ptr->n_value));
1489 assert(incl_stk < (int)(sizeof(incl) / sizeof(incl[0])) - 1);
1490 incl[++incl_stk] = source_idx;
1491 source_idx = source_new(module, NULL, ptr);
1492 break;
1493 case N_EINCL:
1494 assert(incl_stk >= 0);
1495 source_idx = incl[incl_stk--];
1496 break;
1497 case N_EXCL:
1498 if (stabs_add_include(stabs_find_include(ptr, stab_ptr->n_value)) < 0)
1499 {
1500 ERR("Excluded header not found (%s,%ld)\n", ptr, stab_ptr->n_value);
1501 module_reset_debug_info(module);
1502 ret = FALSE;
1503 goto done;
1504 }
1505 break;
1506 case N_MAIN:
1507 /* Always ignore these. GCC doesn't even generate them. */
1508 break;
1509 case N_BNSYM:
1510 case N_ENSYM:
1511 /* Always ignore these, they seem to be used only on Darwin. */
1512 break;
1513 default:
1514 ERR("Unknown stab type 0x%02x\n", stab_ptr->n_type);
1515 break;
1516 }
1517 stabbuff[0] = '\0';
1518 TRACE("0x%02x %lx %s\n",
1519 stab_ptr->n_type, stab_ptr->n_value, debugstr_a(strs + stab_ptr->n_un.n_strx));
1520 }
1521 module->module.SymType = SymDia;
1522 module->module.CVSig = 'S' | ('T' << 8) | ('A' << 16) | ('B' << 24);
1523 /* FIXME: we could have a finer grain here */
1524 module->module.LineNumbers = TRUE;
1525 module->module.GlobalSymbols = TRUE;
1526 module->module.TypeInfo = TRUE;
1527 module->module.SourceIndexed = TRUE;
1528 module->module.Publics = TRUE;
1529 done:
1530 HeapFree(GetProcessHeap(), 0, stabbuff);
1531 stabs_free_includes();
1532 HeapFree(GetProcessHeap(), 0, pending.vars);
1533
1534 return ret;
1535 }