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