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