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