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