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