1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 /* 29 * This file is a sewer. 30 */ 31 32 #if HAVE_NBTOOL_CONFIG_H 33 # include "nbtool_config.h" 34 #endif 35 36 #include <limits.h> 37 #include <stdarg.h> 38 #include <stdio.h> 39 #include <assert.h> 40 #include <strings.h> 41 #include <setjmp.h> 42 #include <ctype.h> 43 #include <uts/common/sys/ctf.h> 44 45 #include "ctftools.h" 46 #include "memory.h" 47 #include "list.h" 48 49 #define HASH(NUM) ((int)(NUM & (BUCKETS - 1))) 50 #define BUCKETS 128 51 52 #define TYPEPAIRMULT 10000 53 #define MAKETYPEID(file, num) ((file) * TYPEPAIRMULT + num) 54 #define TYPEFILE(tid) ((tid) / TYPEPAIRMULT) 55 #define TYPENUM(tid) ((tid) % TYPEPAIRMULT) 56 57 #define expected(a, b, c) _expected(a, b, c, __LINE__) 58 59 static int faketypenumber = 100000000; 60 61 static tdesc_t *hash_table[BUCKETS]; 62 static tdesc_t *name_table[BUCKETS]; 63 64 list_t *typedbitfldmems; 65 66 static void reset(void); 67 static jmp_buf resetbuf; 68 69 static char *soudef(char *cp, stabtype_t type, tdesc_t **rtdp); 70 static void enumdef(char *cp, tdesc_t **rtdp); 71 static int compute_sum(const char *w); 72 73 static char *number(char *cp, int *n); 74 static char *name(char *cp, char **w); 75 static char *id(char *cp, int *h); 76 static char *whitesp(char *cp); 77 static void addhash(tdesc_t *tdp, int num); 78 static int tagadd(char *w, int h, tdesc_t *tdp); 79 static char *tdefdecl(char *cp, int h, tdesc_t **rtdp); 80 static char *intrinsic(char *cp, tdesc_t **rtdp); 81 static char *arraydef(char *cp, tdesc_t **rtdp); 82 83 int debug_parse = DEBUG_PARSE; 84 85 /*PRINTFLIKE3*/ 86 static void 87 parse_debug(int level, char *cp, const char *fmt, ...) 88 { 89 va_list ap; 90 char buf[1024]; 91 char tmp[32]; 92 int i; 93 94 if (level > debug_level || !debug_parse) 95 return; 96 97 if (cp != NULL) { 98 for (i = 0; i < 30; i++) { 99 if (cp[i] == '\0') 100 break; 101 if (!iscntrl(cp[i])) 102 tmp[i] = cp[i]; 103 } 104 tmp[i] = '\0'; 105 (void) snprintf(buf, sizeof (buf), "%s [cp='%s']\n", fmt, tmp); 106 } else { 107 strcpy(buf, fmt); 108 strcat(buf, "\n"); 109 } 110 111 va_start(ap, fmt); 112 vadebug(level, buf, ap); 113 va_end(ap); 114 } 115 116 /* Report unexpected syntax in stabs. */ 117 static void 118 _expected( 119 const char *who, /* what function, or part thereof, is reporting */ 120 const char *what, /* what was expected */ 121 const char *where, /* where we were in the line of input */ 122 int line) 123 { 124 fprintf(stderr, "%s, expecting \"%s\" at \"%s\"\n", who, what, where); 125 fprintf(stderr, "code line: %d, file %s\n", line, 126 (curhdr ? curhdr : "NO FILE")); 127 reset(); 128 } 129 130 /*ARGSUSED*/ 131 void 132 parse_init(tdata_t *td __unused) 133 { 134 int i; 135 136 for (i = 0; i < BUCKETS; i++) { 137 hash_table[i] = NULL; 138 name_table[i] = NULL; 139 } 140 141 if (typedbitfldmems != NULL) { 142 list_free(typedbitfldmems, NULL, NULL); 143 typedbitfldmems = NULL; 144 } 145 } 146 147 void 148 parse_finish(tdata_t *td) 149 { 150 td->td_nextid = ++faketypenumber; 151 } 152 153 static tdesc_t * 154 unres_new(int tid) 155 { 156 tdesc_t *tdp; 157 158 tdp = xcalloc(sizeof (*tdp)); 159 tdp->t_type = TYPEDEF_UNRES; 160 tdp->t_id = tid; 161 162 return (tdp); 163 } 164 165 static char * 166 read_tid(char *cp, tdesc_t **tdpp) 167 { 168 tdesc_t *tdp; 169 int tid; 170 171 cp = id(cp, &tid); 172 173 assert(tid != 0); 174 175 if (*cp == '=') { 176 if (!(cp = tdefdecl(cp + 1, tid, &tdp))) 177 return (NULL); 178 if (tdp->t_id && tdp->t_id != tid) { 179 tdesc_t *ntdp = xcalloc(sizeof (*ntdp)); 180 181 ntdp->t_type = TYPEDEF; 182 ntdp->t_tdesc = tdp; 183 tdp = ntdp; 184 } 185 addhash(tdp, tid); 186 } else if ((tdp = lookup(tid)) == NULL) 187 tdp = unres_new(tid); 188 189 *tdpp = tdp; 190 return (cp); 191 } 192 193 static iitype_t 194 parse_fun(char *cp, iidesc_t *ii) 195 { 196 iitype_t iitype = 0; 197 tdesc_t *tdp; 198 tdesc_t **args = NULL; 199 int nargs = 0; 200 int va = 0; 201 202 /* 203 * name:P prototype 204 * name:F global function 205 * name:f static function 206 */ 207 switch (*cp++) { 208 case 'P': 209 iitype = II_NOT; /* not interesting */ 210 break; 211 212 case 'F': 213 iitype = II_GFUN; 214 break; 215 216 case 'f': 217 iitype = II_SFUN; 218 break; 219 220 default: 221 expected("parse_nfun", "[PfF]", cp - 1); 222 } 223 224 if (!(cp = read_tid(cp, &tdp))) 225 return (-1); 226 227 if (*cp) 228 args = xmalloc(sizeof (tdesc_t *) * FUNCARG_DEF); 229 230 while (*cp && *++cp) { 231 if (*cp == '0') { 232 va = 1; 233 continue; 234 } 235 236 nargs++; 237 if (nargs > FUNCARG_DEF) 238 args = xrealloc(args, sizeof (tdesc_t *) * nargs); 239 if (!(cp = read_tid(cp, &args[nargs - 1]))) 240 return (-1); 241 } 242 243 ii->ii_type = iitype; 244 ii->ii_dtype = tdp; 245 ii->ii_nargs = nargs; 246 ii->ii_args = args; 247 ii->ii_vargs = va; 248 249 return (iitype); 250 } 251 252 static iitype_t 253 parse_sym(char *cp, iidesc_t *ii) 254 { 255 tdesc_t *tdp; 256 iitype_t iitype = 0; 257 258 /* 259 * name:G global variable 260 * name:S static variable 261 */ 262 switch (*cp++) { 263 case 'G': 264 iitype = II_GVAR; 265 break; 266 case 'S': 267 iitype = II_SVAR; 268 break; 269 case 'p': 270 iitype = II_PSYM; 271 break; 272 case '(': 273 cp--; 274 /*FALLTHROUGH*/ 275 case 'r': 276 case 'V': 277 iitype = II_NOT; /* not interesting */ 278 break; 279 default: 280 expected("parse_sym", "[GprSV(]", cp - 1); 281 } 282 283 if (!(cp = read_tid(cp, &tdp))) 284 return (-1); 285 286 ii->ii_type = iitype; 287 ii->ii_dtype = tdp; 288 289 return (iitype); 290 } 291 292 static iitype_t 293 parse_type(char *cp, iidesc_t *ii) 294 { 295 tdesc_t *tdp, *ntdp; 296 int tid; 297 298 if (*cp++ != 't') 299 expected("parse_type", "t (type)", cp - 1); 300 301 cp = id(cp, &tid); 302 if ((tdp = lookup(tid)) == NULL) { 303 if (*cp++ != '=') 304 expected("parse_type", "= (definition)", cp - 1); 305 306 (void) tdefdecl(cp, tid, &tdp); 307 308 if (tdp->t_id == tid) { 309 assert(tdp->t_type != TYPEDEF); 310 assert(!lookup(tdp->t_id)); 311 312 if (!streq(tdp->t_name, ii->ii_name)) { 313 ntdp = xcalloc(sizeof (*ntdp)); 314 ntdp->t_name = xstrdup(ii->ii_name); 315 ntdp->t_type = TYPEDEF; 316 ntdp->t_tdesc = tdp; 317 tdp->t_id = faketypenumber++; 318 tdp = ntdp; 319 } 320 } else if (tdp->t_id == 0) { 321 assert(tdp->t_type == FORWARD || 322 tdp->t_type == INTRINSIC); 323 324 if (tdp->t_name && !streq(tdp->t_name, ii->ii_name)) { 325 ntdp = xcalloc(sizeof (*ntdp)); 326 ntdp->t_name = xstrdup(ii->ii_name); 327 ntdp->t_type = TYPEDEF; 328 ntdp->t_tdesc = tdp; 329 tdp->t_id = faketypenumber++; 330 tdp = ntdp; 331 } 332 } else if (tdp->t_id != tid) { 333 ntdp = xcalloc(sizeof (*ntdp)); 334 ntdp->t_name = xstrdup(ii->ii_name); 335 ntdp->t_type = TYPEDEF; 336 ntdp->t_tdesc = tdp; 337 tdp = ntdp; 338 } 339 340 if (tagadd(ii->ii_name, tid, tdp) < 0) 341 return (-1); 342 } 343 344 ii->ii_type = II_TYPE; 345 ii->ii_dtype = tdp; 346 return (II_TYPE); 347 } 348 349 static iitype_t 350 parse_sou(char *cp, iidesc_t *idp) 351 { 352 tdesc_t *rtdp; 353 int tid; 354 355 if (*cp++ != 'T') 356 expected("parse_sou", "T (sou)", cp - 1); 357 358 cp = id(cp, &tid); 359 if (*cp++ != '=') 360 expected("parse_sou", "= (definition)", cp - 1); 361 362 parse_debug(1, NULL, "parse_sou: declaring '%s'", idp->ii_name ? 363 idp->ii_name : "(anon)"); 364 if ((rtdp = lookup(tid)) != NULL) { 365 if (idp->ii_name != NULL) { 366 if (rtdp->t_name != NULL && 367 strcmp(rtdp->t_name, idp->ii_name) != 0) { 368 tdesc_t *tdp; 369 370 tdp = xcalloc(sizeof (*tdp)); 371 tdp->t_name = xstrdup(idp->ii_name); 372 tdp->t_type = TYPEDEF; 373 tdp->t_tdesc = rtdp; 374 addhash(tdp, tid); /* for *(x,y) types */ 375 parse_debug(3, NULL, " %s defined as %s(%d)", 376 idp->ii_name, tdesc_name(rtdp), tid); 377 } else if (rtdp->t_name == NULL) { 378 rtdp->t_name = xstrdup(idp->ii_name); 379 addhash(rtdp, tid); 380 } 381 } 382 } else { 383 rtdp = xcalloc(sizeof (*rtdp)); 384 rtdp->t_name = idp->ii_name ? xstrdup(idp->ii_name) : NULL; 385 addhash(rtdp, tid); 386 } 387 388 switch (*cp++) { 389 case 's': 390 (void) soudef(cp, STRUCT, &rtdp); 391 break; 392 case 'u': 393 (void) soudef(cp, UNION, &rtdp); 394 break; 395 case 'e': 396 enumdef(cp, &rtdp); 397 break; 398 default: 399 expected("parse_sou", "<tag type s/u/e>", cp - 1); 400 break; 401 } 402 403 idp->ii_type = II_SOU; 404 idp->ii_dtype = rtdp; 405 return (II_SOU); 406 } 407 408 int 409 parse_stab(stab_t *stab, char *cp, iidesc_t **iidescp) 410 { 411 iidesc_t *ii = NULL; 412 iitype_t (*parse)(char *, iidesc_t *); 413 int rc; 414 415 /* 416 * set up for reset() 417 */ 418 if (setjmp(resetbuf)) 419 return (-1); 420 421 cp = whitesp(cp); 422 ii = iidesc_new(NULL); 423 cp = name(cp, &ii->ii_name); 424 425 switch (stab->n_type) { 426 case N_FUN: 427 parse = parse_fun; 428 break; 429 430 case N_LSYM: 431 if (*cp == 't') 432 parse = parse_type; 433 else if (*cp == 'T') 434 parse = parse_sou; 435 else 436 parse = parse_sym; 437 break; 438 439 case N_GSYM: 440 case N_LCSYM: 441 case N_PSYM: 442 case N_ROSYM: 443 case N_RSYM: 444 case N_STSYM: 445 parse = parse_sym; 446 break; 447 default: 448 parse_debug(1, cp, "Unknown stab type %#x", stab->n_type); 449 bzero(&resetbuf, sizeof (resetbuf)); 450 return (-1); 451 } 452 453 rc = parse(cp, ii); 454 bzero(&resetbuf, sizeof (resetbuf)); 455 456 if (rc < 0 || ii->ii_type == II_NOT) { 457 iidesc_free(ii, NULL); 458 return (rc); 459 } 460 461 *iidescp = ii; 462 463 return (1); 464 } 465 466 /* 467 * Check if we have this node in the hash table already 468 */ 469 tdesc_t * 470 lookup(int h) 471 { 472 int bucket = HASH(h); 473 tdesc_t *tdp = hash_table[bucket]; 474 475 while (tdp != NULL) { 476 if (tdp->t_id == h) 477 return (tdp); 478 tdp = tdp->t_hash; 479 } 480 return (NULL); 481 } 482 483 static char * 484 whitesp(char *cp) 485 { 486 char c; 487 488 for (c = *cp++; isspace(c); c = *cp++); 489 --cp; 490 return (cp); 491 } 492 493 static char * 494 name(char *cp, char **w) 495 { 496 char *new, *orig, c; 497 int len; 498 499 orig = cp; 500 c = *cp++; 501 if (c == ':') 502 *w = NULL; 503 else if (isalpha(c) || strchr("_.$", c)) { 504 for (c = *cp++; isalnum(c) || strchr(" _.$", c); c = *cp++) 505 ; 506 if (c != ':') 507 reset(); 508 len = cp - orig; 509 new = xmalloc(len); 510 while (orig < cp - 1) 511 *new++ = *orig++; 512 *new = '\0'; 513 *w = new - (len - 1); 514 } else 515 reset(); 516 517 return (cp); 518 } 519 520 static char * 521 number(char *cp, int *n) 522 { 523 char *next; 524 525 *n = (int)strtol(cp, &next, 10); 526 if (next == cp) 527 expected("number", "<number>", cp); 528 return (next); 529 } 530 531 static char * 532 id(char *cp, int *h) 533 { 534 int n1, n2; 535 536 if (*cp == '(') { /* SunPro style */ 537 cp++; 538 cp = number(cp, &n1); 539 if (*cp++ != ',') 540 expected("id", ",", cp - 1); 541 cp = number(cp, &n2); 542 if (*cp++ != ')') 543 expected("id", ")", cp - 1); 544 *h = MAKETYPEID(n1, n2); 545 } else if (isdigit(*cp)) { /* gcc style */ 546 cp = number(cp, &n1); 547 *h = n1; 548 } else { 549 expected("id", "(/0-9", cp); 550 } 551 return (cp); 552 } 553 554 static int 555 tagadd(char *w, int h, tdesc_t *tdp) 556 { 557 tdesc_t *otdp; 558 559 tdp->t_name = w; 560 if (!(otdp = lookup(h))) 561 addhash(tdp, h); 562 else if (otdp != tdp) { 563 warning("duplicate entry\n"); 564 warning(" old: %s %d (%d,%d)\n", tdesc_name(otdp), 565 otdp->t_type, TYPEFILE(otdp->t_id), TYPENUM(otdp->t_id)); 566 warning(" new: %s %d (%d,%d)\n", tdesc_name(tdp), 567 tdp->t_type, TYPEFILE(tdp->t_id), TYPENUM(tdp->t_id)); 568 return (-1); 569 } 570 571 return (0); 572 } 573 574 static char * 575 tdefdecl(char *cp, int h, tdesc_t **rtdp) 576 { 577 tdesc_t *ntdp; 578 char *w; 579 int c, h2; 580 char type; 581 582 parse_debug(3, cp, "tdefdecl h=%d", h); 583 584 /* Type codes */ 585 switch (type = *cp) { 586 case 'b': /* integer */ 587 case 'R': /* fp */ 588 cp = intrinsic(cp, rtdp); 589 break; 590 case '(': /* equiv to another type */ 591 cp = id(cp, &h2); 592 ntdp = lookup(h2); 593 594 if (ntdp != NULL && *cp == '=') { 595 if (ntdp->t_type == FORWARD && *(cp + 1) == 'x') { 596 /* 597 * The 6.2 compiler, and possibly others, will 598 * sometimes emit the same stab for a forward 599 * declaration twice. That is, "(1,2)=xsfoo:" 600 * will sometimes show up in two different 601 * places. This is, of course, quite fun. We 602 * want CTF to work in spite of the compiler, 603 * so we'll let this one through. 604 */ 605 char *c2 = cp + 2; 606 char *nm; 607 608 if (!strchr("sue", *c2++)) { 609 expected("tdefdecl/x-redefine", "[sue]", 610 c2 - 1); 611 } 612 613 c2 = name(c2, &nm); 614 if (strcmp(nm, ntdp->t_name) != 0) { 615 terminate("Stabs error: Attempt to " 616 "redefine type (%d,%d) as " 617 "something else: %s\n", 618 TYPEFILE(h2), TYPENUM(h2), 619 c2 - 1); 620 } 621 free(nm); 622 623 h2 = faketypenumber++; 624 ntdp = NULL; 625 } else { 626 terminate("Stabs error: Attempting to " 627 "redefine type (%d,%d)\n", TYPEFILE(h2), 628 TYPENUM(h2)); 629 } 630 } 631 632 if (ntdp == NULL) { /* if that type isn't defined yet */ 633 if (*cp != '=') { 634 /* record it as unresolved */ 635 parse_debug(3, NULL, "tdefdecl unres type %d", 636 h2); 637 *rtdp = calloc(sizeof (**rtdp), 1); 638 (*rtdp)->t_type = TYPEDEF_UNRES; 639 (*rtdp)->t_id = h2; 640 break; 641 } else 642 cp++; 643 644 /* define a new type */ 645 cp = tdefdecl(cp, h2, rtdp); 646 if ((*rtdp)->t_id && (*rtdp)->t_id != h2) { 647 ntdp = calloc(sizeof (*ntdp), 1); 648 ntdp->t_type = TYPEDEF; 649 ntdp->t_tdesc = *rtdp; 650 *rtdp = ntdp; 651 } 652 653 addhash(*rtdp, h2); 654 655 } else { /* that type is already defined */ 656 if (ntdp->t_type != TYPEDEF || ntdp->t_name != NULL) { 657 *rtdp = ntdp; 658 } else { 659 parse_debug(3, NULL, 660 "No duplicate typedef anon for ref"); 661 *rtdp = ntdp; 662 } 663 } 664 break; 665 case '*': 666 ntdp = NULL; 667 cp = tdefdecl(cp + 1, h, &ntdp); 668 if (ntdp == NULL) 669 expected("tdefdecl/*", "id", cp); 670 671 if (!ntdp->t_id) 672 ntdp->t_id = faketypenumber++; 673 674 *rtdp = xcalloc(sizeof (**rtdp)); 675 (*rtdp)->t_type = POINTER; 676 (*rtdp)->t_size = 0; 677 (*rtdp)->t_id = h; 678 (*rtdp)->t_tdesc = ntdp; 679 break; 680 case 'f': 681 cp = tdefdecl(cp + 1, h, &ntdp); 682 *rtdp = xcalloc(sizeof (**rtdp)); 683 (*rtdp)->t_type = FUNCTION; 684 (*rtdp)->t_size = 0; 685 (*rtdp)->t_id = h; 686 (*rtdp)->t_fndef = xcalloc(sizeof (fndef_t)); 687 /* 688 * The 6.1 compiler will sometimes generate incorrect stabs for 689 * function pointers (it'll get the return type wrong). This 690 * causes merges to fail. We therefore treat function pointers 691 * as if they all point to functions that return int. When 692 * 4432549 is fixed, the lookupname() call below should be 693 * replaced with `ntdp'. 694 */ 695 (*rtdp)->t_fndef->fn_ret = lookupname("int"); 696 break; 697 case 'a': 698 case 'z': 699 cp++; 700 if (*cp++ != 'r') 701 expected("tdefdecl/[az]", "r", cp - 1); 702 *rtdp = xcalloc(sizeof (**rtdp)); 703 (*rtdp)->t_type = ARRAY; 704 (*rtdp)->t_id = h; 705 cp = arraydef(cp, rtdp); 706 break; 707 case 'x': 708 c = *++cp; 709 if (c != 's' && c != 'u' && c != 'e') 710 expected("tdefdecl/x", "[sue]", cp - 1); 711 cp = name(cp + 1, &w); 712 713 ntdp = xcalloc(sizeof (*ntdp)); 714 ntdp->t_type = FORWARD; 715 ntdp->t_name = w; 716 /* 717 * We explicitly don't set t_id here - the caller will do it. 718 * The caller may want to use a real type ID, or they may 719 * choose to make one up. 720 */ 721 722 *rtdp = ntdp; 723 break; 724 725 case 'B': /* volatile */ 726 cp = tdefdecl(cp + 1, h, &ntdp); 727 728 if (!ntdp->t_id) 729 ntdp->t_id = faketypenumber++; 730 731 *rtdp = xcalloc(sizeof (**rtdp)); 732 (*rtdp)->t_type = VOLATILE; 733 (*rtdp)->t_size = 0; 734 (*rtdp)->t_tdesc = ntdp; 735 (*rtdp)->t_id = h; 736 break; 737 738 case 'k': /* const */ 739 cp = tdefdecl(cp + 1, h, &ntdp); 740 741 if (!ntdp->t_id) 742 ntdp->t_id = faketypenumber++; 743 744 *rtdp = xcalloc(sizeof (**rtdp)); 745 (*rtdp)->t_type = CONST; 746 (*rtdp)->t_size = 0; 747 (*rtdp)->t_tdesc = ntdp; 748 (*rtdp)->t_id = h; 749 break; 750 751 case 'K': /* restricted */ 752 cp = tdefdecl(cp + 1, h, &ntdp); 753 754 if (!ntdp->t_id) 755 ntdp->t_id = faketypenumber++; 756 757 *rtdp = xcalloc(sizeof (**rtdp)); 758 (*rtdp)->t_type = RESTRICT; 759 (*rtdp)->t_size = 0; 760 (*rtdp)->t_tdesc = ntdp; 761 (*rtdp)->t_id = h; 762 break; 763 764 case 'u': 765 case 's': 766 cp++; 767 768 *rtdp = xcalloc(sizeof (**rtdp)); 769 (*rtdp)->t_name = NULL; 770 cp = soudef(cp, (type == 'u') ? UNION : STRUCT, rtdp); 771 break; 772 default: 773 expected("tdefdecl", "<type code>", cp); 774 } 775 return (cp); 776 } 777 778 static char * 779 intrinsic(char *cp, tdesc_t **rtdp) 780 { 781 intr_t *intr = xcalloc(sizeof (intr_t)); 782 tdesc_t *tdp; 783 int width, fmt, i; 784 785 switch (*cp++) { 786 case 'b': 787 intr->intr_type = INTR_INT; 788 if (*cp == 's') 789 intr->intr_signed = 1; 790 else if (*cp != 'u') 791 expected("intrinsic/b", "[su]", cp); 792 cp++; 793 794 if (strchr("cbv", *cp)) 795 intr->intr_iformat = *cp++; 796 797 cp = number(cp, &width); 798 if (*cp++ != ';') 799 expected("intrinsic/b", "; (post-width)", cp - 1); 800 801 cp = number(cp, &intr->intr_offset); 802 if (*cp++ != ';') 803 expected("intrinsic/b", "; (post-offset)", cp - 1); 804 805 cp = number(cp, &intr->intr_nbits); 806 break; 807 808 case 'R': 809 intr->intr_type = INTR_REAL; 810 for (fmt = 0, i = 0; isdigit(*(cp + i)); i++) 811 fmt = fmt * 10 + (*(cp + i) - '0'); 812 813 if (fmt < 1 || fmt > CTF_FP_MAX) 814 expected("intrinsic/R", "number <= CTF_FP_MAX", cp); 815 816 intr->intr_fformat = fmt; 817 cp += i; 818 819 if (*cp++ != ';') 820 expected("intrinsic/R", ";", cp - 1); 821 cp = number(cp, &width); 822 823 intr->intr_nbits = width * 8; 824 break; 825 } 826 827 tdp = xcalloc(sizeof (*tdp)); 828 tdp->t_type = INTRINSIC; 829 tdp->t_size = width; 830 tdp->t_name = NULL; 831 tdp->t_intr = intr; 832 parse_debug(3, NULL, "intrinsic: size=%d", width); 833 *rtdp = tdp; 834 835 return (cp); 836 } 837 838 static tdesc_t * 839 bitintrinsic(tdesc_t *template, int nbits) 840 { 841 tdesc_t *newtdp = xcalloc(sizeof (tdesc_t)); 842 843 newtdp->t_name = xstrdup(template->t_name); 844 newtdp->t_id = faketypenumber++; 845 newtdp->t_type = INTRINSIC; 846 newtdp->t_size = template->t_size; 847 newtdp->t_intr = xmalloc(sizeof (intr_t)); 848 bcopy(template->t_intr, newtdp->t_intr, sizeof (intr_t)); 849 newtdp->t_intr->intr_nbits = nbits; 850 851 return (newtdp); 852 } 853 854 static char * 855 offsize(char *cp, mlist_t *mlp) 856 { 857 int offset, size; 858 859 if (*cp == ',') 860 cp++; 861 cp = number(cp, &offset); 862 if (*cp++ != ',') 863 expected("offsize/2", ",", cp - 1); 864 cp = number(cp, &size); 865 if (*cp++ != ';') 866 expected("offsize/3", ";", cp - 1); 867 mlp->ml_offset = offset; 868 mlp->ml_size = size; 869 return (cp); 870 } 871 872 static tdesc_t * 873 find_intrinsic(tdesc_t *tdp) 874 { 875 for (;;) { 876 switch (tdp->t_type) { 877 case TYPEDEF: 878 case VOLATILE: 879 case CONST: 880 case RESTRICT: 881 tdp = tdp->t_tdesc; 882 break; 883 884 default: 885 return (tdp); 886 } 887 } 888 } 889 890 static char * 891 soudef(char *cp, stabtype_t type, tdesc_t **rtdp) 892 { 893 mlist_t *mlp, **prev; 894 char *w; 895 int h; 896 int size; 897 tdesc_t *tdp, *itdp; 898 899 cp = number(cp, &size); 900 (*rtdp)->t_size = size; 901 (*rtdp)->t_type = type; /* s or u */ 902 903 /* 904 * An '@' here indicates a bitmask follows. This is so the 905 * compiler can pass information to debuggers about how structures 906 * are passed in the v9 world. We don't need this information 907 * so we skip over it. 908 */ 909 if (cp[0] == '@') { 910 cp += 3; 911 } 912 913 parse_debug(3, cp, "soudef: %s size=%d", tdesc_name(*rtdp), 914 (*rtdp)->t_size); 915 916 prev = &((*rtdp)->t_members); 917 /* now fill up the fields */ 918 while ((*cp != '\0') && (*cp != ';')) { /* signifies end of fields */ 919 mlp = xcalloc(sizeof (*mlp)); 920 *prev = mlp; 921 cp = name(cp, &w); 922 mlp->ml_name = w; 923 cp = id(cp, &h); 924 /* 925 * find the tdesc struct in the hash table for this type 926 * and stick a ptr in here 927 */ 928 tdp = lookup(h); 929 if (tdp == NULL) { /* not in hash list */ 930 parse_debug(3, NULL, " defines %s (%d)", w, h); 931 if (*cp++ != '=') { 932 tdp = unres_new(h); 933 parse_debug(3, NULL, 934 " refers to %s (unresolved %d)", 935 (w ? w : "anon"), h); 936 } else { 937 cp = tdefdecl(cp, h, &tdp); 938 939 if (tdp->t_id && tdp->t_id != h) { 940 tdesc_t *ntdp = xcalloc(sizeof (*ntdp)); 941 942 ntdp->t_type = TYPEDEF; 943 ntdp->t_tdesc = tdp; 944 tdp = ntdp; 945 } 946 947 addhash(tdp, h); 948 parse_debug(4, cp, 949 " soudef now looking at "); 950 cp++; 951 } 952 } else { 953 parse_debug(3, NULL, " refers to %s (%d, %s)", 954 w ? w : "anon", h, tdesc_name(tdp)); 955 } 956 957 cp = offsize(cp, mlp); 958 959 itdp = find_intrinsic(tdp); 960 if (itdp->t_type == INTRINSIC) { 961 if (mlp->ml_size != itdp->t_intr->intr_nbits) { 962 parse_debug(4, cp, "making %d bit intrinsic " 963 "from %s", mlp->ml_size, tdesc_name(itdp)); 964 mlp->ml_type = bitintrinsic(itdp, mlp->ml_size); 965 } else 966 mlp->ml_type = tdp; 967 } else if (itdp->t_type == TYPEDEF_UNRES) { 968 list_add(&typedbitfldmems, mlp); 969 mlp->ml_type = tdp; 970 } else { 971 mlp->ml_type = tdp; 972 } 973 974 /* cp is now pointing to next field */ 975 prev = &mlp->ml_next; 976 } 977 return (cp); 978 } 979 980 static char * 981 arraydef(char *cp, tdesc_t **rtdp) 982 { 983 int start, end, h; 984 985 cp = id(cp, &h); 986 if (*cp++ != ';') 987 expected("arraydef/1", ";", cp - 1); 988 989 (*rtdp)->t_ardef = xcalloc(sizeof (ardef_t)); 990 (*rtdp)->t_ardef->ad_idxtype = lookup(h); 991 992 cp = number(cp, &start); /* lower */ 993 if (*cp++ != ';') 994 expected("arraydef/2", ";", cp - 1); 995 996 if (*cp == 'S') { 997 /* variable length array - treat as null dimensioned */ 998 cp++; 999 if (*cp++ != '-') 1000 expected("arraydef/fpoff-sep", "-", cp - 1); 1001 cp = number(cp, &end); 1002 end = start; 1003 } else { 1004 /* normal fixed-dimension array */ 1005 cp = number(cp, &end); /* upper */ 1006 } 1007 1008 if (*cp++ != ';') 1009 expected("arraydef/3", ";", cp - 1); 1010 (*rtdp)->t_ardef->ad_nelems = end - start + 1; 1011 cp = tdefdecl(cp, h, &((*rtdp)->t_ardef->ad_contents)); 1012 1013 parse_debug(3, cp, "defined array idx type %d %d-%d next ", 1014 h, start, end); 1015 1016 return (cp); 1017 } 1018 1019 static void 1020 enumdef(char *cp, tdesc_t **rtdp) 1021 { 1022 elist_t *elp, **prev; 1023 char *w; 1024 1025 (*rtdp)->t_type = ENUM; 1026 (*rtdp)->t_emem = NULL; 1027 1028 prev = &((*rtdp)->t_emem); 1029 while (*cp != ';') { 1030 elp = xcalloc(sizeof (*elp)); 1031 elp->el_next = NULL; 1032 *prev = elp; 1033 cp = name(cp, &w); 1034 elp->el_name = w; 1035 cp = number(cp, &elp->el_number); 1036 parse_debug(3, NULL, "enum %s: %s=%d", tdesc_name(*rtdp), 1037 elp->el_name, elp->el_number); 1038 prev = &elp->el_next; 1039 if (*cp++ != ',') 1040 expected("enumdef", ",", cp - 1); 1041 } 1042 } 1043 1044 static tdesc_t * 1045 lookup_name(tdesc_t **hash, const char *name1) 1046 { 1047 int bucket = compute_sum(name1); 1048 tdesc_t *tdp, *ttdp = NULL; 1049 1050 for (tdp = hash[bucket]; tdp != NULL; tdp = tdp->t_next) { 1051 if (tdp->t_name != NULL && strcmp(tdp->t_name, name1) == 0) { 1052 if (tdp->t_type == STRUCT || tdp->t_type == UNION || 1053 tdp->t_type == ENUM || tdp->t_type == INTRINSIC) 1054 return (tdp); 1055 if (tdp->t_type == TYPEDEF) 1056 ttdp = tdp; 1057 } 1058 } 1059 return (ttdp); 1060 } 1061 1062 tdesc_t * 1063 lookupname(const char *name1) 1064 { 1065 return (lookup_name(name_table, name1)); 1066 } 1067 1068 /* 1069 * Add a node to the hash queues. 1070 */ 1071 static void 1072 addhash(tdesc_t *tdp, int num) 1073 { 1074 int hash = HASH(num); 1075 tdesc_t *ttdp; 1076 char added_num = 0, added_name = 0; 1077 1078 /* 1079 * If it already exists in the hash table don't add it again 1080 * (but still check to see if the name should be hashed). 1081 */ 1082 ttdp = lookup(num); 1083 1084 if (ttdp == NULL) { 1085 tdp->t_id = num; 1086 tdp->t_hash = hash_table[hash]; 1087 hash_table[hash] = tdp; 1088 added_num = 1; 1089 } 1090 1091 if (tdp->t_name != NULL) { 1092 ttdp = lookupname(tdp->t_name); 1093 if (ttdp == NULL) { 1094 hash = compute_sum(tdp->t_name); 1095 tdp->t_next = name_table[hash]; 1096 name_table[hash] = tdp; 1097 added_name = 1; 1098 } 1099 } 1100 if (!added_num && !added_name) { 1101 terminate("stabs: broken hash\n"); 1102 } 1103 } 1104 1105 static int 1106 compute_sum(const char *w) 1107 { 1108 char c; 1109 int sum; 1110 1111 for (sum = 0; (c = *w) != '\0'; sum += c, w++) 1112 ; 1113 return (HASH(sum)); 1114 } 1115 1116 static void 1117 reset(void) 1118 { 1119 longjmp(resetbuf, 1); 1120 } 1121 1122 void 1123 check_hash(void) 1124 { 1125 tdesc_t *tdp; 1126 int i; 1127 1128 printf("checking hash\n"); 1129 for (i = 0; i < BUCKETS; i++) { 1130 if (hash_table[i]) { 1131 for (tdp = hash_table[i]->t_hash; 1132 tdp && tdp != hash_table[i]; 1133 tdp = tdp->t_hash) 1134 continue; 1135 if (tdp) { 1136 terminate("cycle in hash bucket %d\n", i); 1137 return; 1138 } 1139 } 1140 1141 if (name_table[i]) { 1142 for (tdp = name_table[i]->t_next; 1143 tdp && tdp != name_table[i]; 1144 tdp = tdp->t_next) 1145 continue; 1146 if (tdp) { 1147 terminate("cycle in name bucket %d\n", i); 1148 return; 1149 } 1150 } 1151 } 1152 printf("done\n"); 1153 } 1154 1155 /*ARGSUSED1*/ 1156 static int 1157 resolve_typed_bitfields_cb(void *arg, void *private __unused) 1158 { 1159 mlist_t *ml = arg; 1160 tdesc_t *tdp = ml->ml_type; 1161 1162 debug(3, "Resolving typed bitfields (member %s)\n", 1163 (ml->ml_name ? ml->ml_name : "(anon)")); 1164 1165 while (tdp) { 1166 switch (tdp->t_type) { 1167 case INTRINSIC: 1168 if (ml->ml_size != tdp->t_intr->intr_nbits) { 1169 debug(3, "making %d bit intrinsic from %s", 1170 ml->ml_size, tdesc_name(tdp)); 1171 ml->ml_type = bitintrinsic(tdp, ml->ml_size); 1172 } else { 1173 debug(3, "using existing %d bit %s intrinsic", 1174 ml->ml_size, tdesc_name(tdp)); 1175 ml->ml_type = tdp; 1176 } 1177 return (1); 1178 1179 case POINTER: 1180 case TYPEDEF: 1181 case VOLATILE: 1182 case CONST: 1183 case RESTRICT: 1184 tdp = tdp->t_tdesc; 1185 break; 1186 1187 default: 1188 return (1); 1189 } 1190 } 1191 1192 terminate("type chain for bitfield member %s has a NULL", ml->ml_name); 1193 /*NOTREACHED*/ 1194 return (0); 1195 } 1196 1197 void 1198 resolve_typed_bitfields(void) 1199 { 1200 (void) list_iter(typedbitfldmems, 1201 resolve_typed_bitfields_cb, NULL); 1202 } 1203