1 /* $NetBSD: read.c,v 1.19 2007/09/28 21:53:50 uwe Exp $ */ 2 3 /* 4 * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved. 5 * Copyright (c) 1994, 1995 Jochen Pohl 6 * All Rights Reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by Jochen Pohl for 19 * The NetBSD Project. 20 * 4. The name of the author may not be used to endorse or promote products 21 * derived from this software without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 #if HAVE_NBTOOL_CONFIG_H 36 #include "nbtool_config.h" 37 #endif 38 39 #include <sys/cdefs.h> 40 #if defined(__RCSID) && !defined(lint) 41 __RCSID("$NetBSD: read.c,v 1.19 2007/09/28 21:53:50 uwe Exp $"); 42 #endif 43 44 #include <ctype.h> 45 #include <limits.h> 46 #include <stdio.h> 47 #include <stdlib.h> 48 #include <string.h> 49 50 #include "lint2.h" 51 52 53 /* index of current (included) source file */ 54 static int srcfile; 55 56 /* 57 * The array pointed to by inpfns maps the file name indices of input files 58 * to the file name indices used in lint2 59 */ 60 static short *inpfns; 61 static size_t ninpfns; 62 63 /* 64 * The array pointed to by *fnames maps file name indizes to file names. 65 * Indices of type short are used instead of pointers to save memory. 66 */ 67 const char **fnames; 68 static size_t nfnames; 69 70 /* 71 * Types are shared (to save memory for the types itself) and accessed 72 * via indices (to save memory for references to types (indices are short)). 73 * To share types, a equal type must be located fast. This is done by a 74 * hash table. Access by indices is done via an array of pointers to the 75 * types. 76 */ 77 typedef struct thtab { 78 const char *th_name; 79 u_short th_idx; 80 struct thtab *th_nxt; 81 } thtab_t; 82 static thtab_t **thtab; /* hash table */ 83 type_t **tlst; /* array for indexed access */ 84 static size_t tlstlen; /* length of tlst */ 85 86 static hte_t **renametab; 87 88 /* index of current C source file (as spezified at the command line) */ 89 static int csrcfile; 90 91 92 #define inperr() inperror(__FILE__, __LINE__) 93 static void inperror(const char *, size_t); 94 static void setsrc(const char *); 95 static void setfnid(int, const char *); 96 static void funccall(pos_t *, const char *); 97 static void decldef(pos_t *, const char *); 98 static void usedsym(pos_t *, const char *); 99 static u_short inptype(const char *, const char **); 100 static int gettlen(const char *, const char **); 101 static u_short findtype(const char *, size_t, int); 102 static u_short storetyp(type_t *, const char *, size_t, int); 103 static int thash(const char *, size_t); 104 static char *inpqstrg(const char *, const char **); 105 static const char *inpname(const char *, const char **); 106 static int getfnidx(const char *); 107 108 void 109 readfile(const char *name) 110 { 111 FILE *inp; 112 size_t len; 113 const char *cp; 114 char *line, *eptr, rt = '\0'; 115 int cline, isrc, iline; 116 pos_t pos; 117 118 if (inpfns == NULL) 119 inpfns = xcalloc(ninpfns = 128, sizeof (short)); 120 if (fnames == NULL) 121 fnames = xcalloc(nfnames = 256, sizeof (char *)); 122 if (tlstlen == 0) 123 tlst = xcalloc(tlstlen = 256, sizeof (type_t *)); 124 if (thtab == NULL) 125 thtab = xcalloc(THSHSIZ2, sizeof (thtab_t)); 126 127 _inithash(&renametab); 128 129 srcfile = getfnidx(name); 130 131 if ((inp = fopen(name, "r")) == NULL) 132 err(1, "cannot open %s", name); 133 134 while ((line = fgetln(inp, &len)) != NULL) { 135 136 if (len == 0 || line[len - 1] != '\n') 137 inperr(); 138 line[len - 1] = '\0'; 139 cp = line; 140 141 /* line number in csrcfile */ 142 cline = (int)strtol(cp, &eptr, 10); 143 if (cp == eptr) { 144 cline = -1; 145 } else { 146 cp = eptr; 147 } 148 149 /* record type */ 150 if (*cp != '\0') { 151 rt = *cp++; 152 } else { 153 inperr(); 154 } 155 156 if (rt == 'S') { 157 setsrc(cp); 158 continue; 159 } else if (rt == 's') { 160 setfnid(cline, cp); 161 continue; 162 } 163 164 /* 165 * Index of (included) source file. If this index is 166 * different from csrcfile, it refers to an included 167 * file. 168 */ 169 isrc = (int)strtol(cp, &eptr, 10); 170 if (cp == eptr) 171 inperr(); 172 cp = eptr; 173 isrc = inpfns[isrc]; 174 175 /* line number in isrc */ 176 if (*cp++ != '.') 177 inperr(); 178 iline = (int)strtol(cp, &eptr, 10); 179 if (cp == eptr) 180 inperr(); 181 cp = eptr; 182 183 pos.p_src = (u_short)csrcfile; 184 pos.p_line = (u_short)cline; 185 pos.p_isrc = (u_short)isrc; 186 pos.p_iline = (u_short)iline; 187 188 /* process rest of this record */ 189 switch (rt) { 190 case 'c': 191 funccall(&pos, cp); 192 break; 193 case 'd': 194 decldef(&pos, cp); 195 break; 196 case 'u': 197 usedsym(&pos, cp); 198 break; 199 default: 200 inperr(); 201 } 202 203 } 204 205 _destroyhash(renametab); 206 207 if (ferror(inp)) 208 err(1, "read error on %s", name); 209 210 (void)fclose(inp); 211 } 212 213 214 static void 215 inperror(const char *file, size_t line) 216 { 217 218 errx(1, "%s,%zd: input file error: %s", file, line, fnames[srcfile]); 219 } 220 221 /* 222 * Set the name of the C source file of the .ln file which is 223 * currently read. 224 */ 225 static void 226 setsrc(const char *cp) 227 { 228 229 csrcfile = getfnidx(cp); 230 } 231 232 /* 233 * setfnid() gets as input an index as used in an input file and the 234 * associated file name. If necessary, it creates a new lint2 file 235 * name index for this file name and creates the mapping of the index 236 * as used in the input file to the index used in lint2. 237 */ 238 static void 239 setfnid(int fid, const char *cp) 240 { 241 242 if (fid == -1) 243 inperr(); 244 245 if (fid >= ninpfns) { 246 inpfns = xrealloc(inpfns, (ninpfns * 2) * sizeof (short)); 247 (void)memset(inpfns + ninpfns, 0, ninpfns * sizeof (short)); 248 ninpfns *= 2; 249 } 250 /* 251 * Should always be true because indices written in the output 252 * file by lint1 are always the previous index + 1. 253 */ 254 if (fid >= ninpfns) 255 errx(1, "internal error: setfnid()"); 256 inpfns[fid] = (u_short)getfnidx(cp); 257 } 258 259 /* 260 * Process a function call record (c-record). 261 */ 262 static void 263 funccall(pos_t *posp, const char *cp) 264 { 265 arginf_t *ai, **lai; 266 char c, *eptr; 267 int rused, rdisc; 268 hte_t *hte; 269 fcall_t *fcall; 270 const char *name; 271 272 fcall = xalloc(sizeof (fcall_t)); 273 STRUCT_ASSIGN(fcall->f_pos, *posp); 274 275 /* read flags */ 276 rused = rdisc = 0; 277 lai = &fcall->f_args; 278 while ((c = *cp) == 'u' || c == 'i' || c == 'd' || 279 c == 'z' || c == 'p' || c == 'n' || c == 's') { 280 cp++; 281 switch (c) { 282 case 'u': 283 if (rused || rdisc) 284 inperr(); 285 rused = 1; 286 break; 287 case 'i': 288 if (rused || rdisc) 289 inperr(); 290 break; 291 case 'd': 292 if (rused || rdisc) 293 inperr(); 294 rdisc = 1; 295 break; 296 case 'z': 297 case 'p': 298 case 'n': 299 case 's': 300 ai = xalloc(sizeof (arginf_t)); 301 ai->a_num = (int)strtol(cp, &eptr, 10); 302 if (cp == eptr) 303 inperr(); 304 cp = eptr; 305 if (c == 'z') { 306 ai->a_pcon = ai->a_zero = 1; 307 } else if (c == 'p') { 308 ai->a_pcon = 1; 309 } else if (c == 'n') { 310 ai->a_ncon = 1; 311 } else { 312 ai->a_fmt = 1; 313 ai->a_fstrg = inpqstrg(cp, &cp); 314 } 315 *lai = ai; 316 lai = &ai->a_nxt; 317 break; 318 } 319 } 320 fcall->f_rused = rused; 321 fcall->f_rdisc = rdisc; 322 323 /* read name of function */ 324 name = inpname(cp, &cp); 325 326 /* first look it up in the renaming table, then in the normal table */ 327 hte = _hsearch(renametab, name, 0); 328 if (hte != NULL) 329 hte = hte->h_hte; 330 else 331 hte = hsearch(name, 1); 332 hte->h_used = 1; 333 334 fcall->f_type = inptype(cp, &cp); 335 336 *hte->h_lcall = fcall; 337 hte->h_lcall = &fcall->f_nxt; 338 339 if (*cp != '\0') 340 inperr(); 341 } 342 343 /* 344 * Process a declaration or definition (d-record). 345 */ 346 static void 347 decldef(pos_t *posp, const char *cp) 348 { 349 sym_t *symp, sym; 350 char c, *ep, *pos1; 351 int used, renamed; 352 hte_t *hte, *renamehte = NULL; 353 const char *name, *rename; 354 355 (void)memset(&sym, 0, sizeof (sym)); 356 STRUCT_ASSIGN(sym.s_pos, *posp); 357 sym.s_def = NODECL; 358 359 used = 0; 360 361 while (strchr("tdeurosvPS", (c = *cp)) != NULL) { 362 cp++; 363 switch (c) { 364 case 't': 365 if (sym.s_def != NODECL) 366 inperr(); 367 sym.s_def = TDEF; 368 break; 369 case 'd': 370 if (sym.s_def != NODECL) 371 inperr(); 372 sym.s_def = DEF; 373 break; 374 case 'e': 375 if (sym.s_def != NODECL) 376 inperr(); 377 sym.s_def = DECL; 378 break; 379 case 'u': 380 if (used) 381 inperr(); 382 used = 1; 383 break; 384 case 'r': 385 if (sym.s_rval) 386 inperr(); 387 sym.s_rval = 1; 388 break; 389 case 'o': 390 if (sym.s_osdef) 391 inperr(); 392 sym.s_osdef = 1; 393 break; 394 case 's': 395 if (sym.s_static) 396 inperr(); 397 sym.s_static = 1; 398 break; 399 case 'v': 400 if (sym.s_va) 401 inperr(); 402 sym.s_va = 1; 403 sym.s_nva = (short)strtol(cp, &ep, 10); 404 if (cp == ep) 405 inperr(); 406 cp = ep; 407 break; 408 case 'P': 409 if (sym.s_prfl) 410 inperr(); 411 sym.s_prfl = 1; 412 sym.s_nprfl = (short)strtol(cp, &ep, 10); 413 if (cp == ep) 414 inperr(); 415 cp = ep; 416 break; 417 case 'S': 418 if (sym.s_scfl) 419 inperr(); 420 sym.s_scfl = 1; 421 sym.s_nscfl = (short)strtol(cp, &ep, 10); 422 if (cp == ep) 423 inperr(); 424 cp = ep; 425 break; 426 } 427 } 428 429 /* read symbol name, doing renaming if necessary */ 430 name = inpname(cp, &cp); 431 renamed = 0; 432 if (*cp == 'r') { 433 cp++; 434 name = xstrdup(name); 435 rename = inpname(cp, &cp); 436 437 /* enter it and see if it's already been renamed */ 438 renamehte = _hsearch(renametab, name, 1); 439 if (renamehte->h_hte == NULL) { 440 hte = hsearch(rename, 1); 441 renamehte->h_hte = hte; 442 renamed = 1; 443 } else if (strcmp((hte = renamehte->h_hte)->h_name, rename)) { 444 pos1 = xstrdup(mkpos(&renamehte->h_syms->s_pos)); 445 /* %s renamed multiple times\t%s :: %s */ 446 msg(18, name, pos1, mkpos(&sym.s_pos)); 447 free(pos1); 448 } 449 free((char *)name); 450 } else { 451 /* it might be a previously-done rename */ 452 hte = _hsearch(renametab, name, 0); 453 if (hte != NULL) 454 hte = hte->h_hte; 455 else 456 hte = hsearch(name, 1); 457 } 458 hte->h_used |= used; 459 if (sym.s_def == DEF || sym.s_def == TDEF) 460 hte->h_def = 1; 461 462 sym.s_type = inptype(cp, &cp); 463 464 /* 465 * Allocate memory for this symbol only if it was not already 466 * declared or tentatively defined at the same location with 467 * the same type. Works only for symbols with external linkage, 468 * because static symbols, tentatively defined at the same location 469 * but in different translation units are really different symbols. 470 */ 471 for (symp = hte->h_syms; symp != NULL; symp = symp->s_nxt) { 472 if (symp->s_pos.p_isrc == sym.s_pos.p_isrc && 473 symp->s_pos.p_iline == sym.s_pos.p_iline && 474 symp->s_type == sym.s_type && 475 ((symp->s_def == DECL && sym.s_def == DECL) || 476 (!sflag && symp->s_def == TDEF && sym.s_def == TDEF)) && 477 !symp->s_static && !sym.s_static) { 478 break; 479 } 480 } 481 482 if (symp == NULL) { 483 /* allocsym reserviert keinen Platz fuer s_nva */ 484 if (sym.s_va || sym.s_prfl || sym.s_scfl) { 485 symp = xalloc(sizeof (sym_t)); 486 STRUCT_ASSIGN(*symp, sym); 487 } else { 488 symp = xalloc(sizeof (symp->s_s)); 489 STRUCT_ASSIGN(symp->s_s, sym.s_s); 490 } 491 *hte->h_lsym = symp; 492 hte->h_lsym = &symp->s_nxt; 493 494 /* XXX hack so we can remember where a symbol was renamed */ 495 if (renamed) 496 renamehte->h_syms = symp; 497 } 498 499 if (*cp != '\0') 500 inperr(); 501 } 502 503 /* 504 * Read an u-record (emited by lint1 if a symbol was used). 505 */ 506 static void 507 usedsym(pos_t *posp, const char *cp) 508 { 509 usym_t *usym; 510 hte_t *hte; 511 const char *name; 512 513 usym = xalloc(sizeof (usym_t)); 514 STRUCT_ASSIGN(usym->u_pos, *posp); 515 516 /* needed as delimiter between two numbers */ 517 if (*cp++ != 'x') 518 inperr(); 519 520 name = inpname(cp, &cp); 521 hte = _hsearch(renametab, name, 0); 522 if (hte != NULL) 523 hte = hte->h_hte; 524 else 525 hte = hsearch(name, 1); 526 hte->h_used = 1; 527 528 *hte->h_lusym = usym; 529 hte->h_lusym = &usym->u_nxt; 530 } 531 532 /* 533 * Read a type and return the index of this type. 534 */ 535 static u_short 536 inptype(const char *cp, const char **epp) 537 { 538 char c, s, *eptr; 539 const char *ep; 540 type_t *tp; 541 int narg, i, osdef = 0; 542 size_t tlen; 543 u_short tidx, sidx; 544 int h; 545 546 /* If we have this type already, return it's index. */ 547 tlen = gettlen(cp, &ep); 548 h = thash(cp, tlen); 549 if ((tidx = findtype(cp, tlen, h)) != 0) { 550 *epp = ep; 551 return (tidx); 552 } 553 554 /* No, we must create a new type. */ 555 tp = xalloc(sizeof (type_t)); 556 557 tidx = storetyp(tp, cp, tlen, h); 558 559 c = *cp++; 560 561 while (c == 'c' || c == 'v') { 562 if (c == 'c') { 563 tp->t_const = 1; 564 } else { 565 tp->t_volatile = 1; 566 } 567 c = *cp++; 568 } 569 570 if (c == 's' || c == 'u' || c == 'l' || c == 'e') { 571 s = c; 572 c = *cp++; 573 } else { 574 s = '\0'; 575 } 576 577 switch (c) { 578 case 'B': 579 tp->t_tspec = BOOL; 580 break; 581 case 'C': 582 tp->t_tspec = s == 's' ? SCHAR : (s == 'u' ? UCHAR : CHAR); 583 break; 584 case 'S': 585 tp->t_tspec = s == 'u' ? USHORT : SHORT; 586 break; 587 case 'I': 588 tp->t_tspec = s == 'u' ? UINT : INT; 589 break; 590 case 'L': 591 tp->t_tspec = s == 'u' ? ULONG : LONG; 592 break; 593 case 'Q': 594 tp->t_tspec = s == 'u' ? UQUAD : QUAD; 595 break; 596 case 'D': 597 tp->t_tspec = s == 's' ? FLOAT : (s == 'l' ? LDOUBLE : DOUBLE); 598 break; 599 case 'V': 600 tp->t_tspec = VOID; 601 break; 602 case 'P': 603 tp->t_tspec = PTR; 604 break; 605 case 'A': 606 tp->t_tspec = ARRAY; 607 break; 608 case 'F': 609 case 'f': 610 osdef = c == 'f'; 611 tp->t_tspec = FUNC; 612 break; 613 case 'T': 614 tp->t_tspec = s == 'e' ? ENUM : (s == 's' ? STRUCT : UNION); 615 break; 616 } 617 618 switch (tp->t_tspec) { 619 case ARRAY: 620 tp->t_dim = (int)strtol(cp, &eptr, 10); 621 cp = eptr; 622 sidx = inptype(cp, &cp); /* force seq. point! (ditto below) */ 623 tp->t_subt = TP(sidx); 624 break; 625 case PTR: 626 sidx = inptype(cp, &cp); 627 tp->t_subt = TP(sidx); 628 break; 629 case FUNC: 630 c = *cp; 631 if (isdigit((u_char)c)) { 632 if (!osdef) 633 tp->t_proto = 1; 634 narg = (int)strtol(cp, &eptr, 10); 635 cp = eptr; 636 tp->t_args = xcalloc((size_t)(narg + 1), 637 sizeof (type_t *)); 638 for (i = 0; i < narg; i++) { 639 if (i == narg - 1 && *cp == 'E') { 640 tp->t_vararg = 1; 641 cp++; 642 } else { 643 sidx = inptype(cp, &cp); 644 tp->t_args[i] = TP(sidx); 645 } 646 } 647 } 648 sidx = inptype(cp, &cp); 649 tp->t_subt = TP(sidx); 650 break; 651 case ENUM: 652 tp->t_tspec = INT; 653 tp->t_isenum = 1; 654 /* FALLTHROUGH */ 655 case STRUCT: 656 case UNION: 657 switch (*cp++) { 658 case '1': 659 tp->t_istag = 1; 660 tp->t_tag = hsearch(inpname(cp, &cp), 1); 661 break; 662 case '2': 663 tp->t_istynam = 1; 664 tp->t_tynam = hsearch(inpname(cp, &cp), 1); 665 break; 666 case '3': 667 tp->t_isuniqpos = 1; 668 tp->t_uniqpos.p_line = strtol(cp, &eptr, 10); 669 cp = eptr; 670 cp++; 671 /* xlate to 'global' file name. */ 672 tp->t_uniqpos.p_file = 673 addoutfile(inpfns[strtol(cp, &eptr, 10)]); 674 cp = eptr; 675 cp++; 676 tp->t_uniqpos.p_uniq = strtol(cp, &eptr, 10); 677 cp = eptr; 678 break; 679 } 680 break; 681 case LONG: 682 case VOID: 683 case LDOUBLE: 684 case DOUBLE: 685 case FLOAT: 686 case UQUAD: 687 case QUAD: 688 case ULONG: 689 case UINT: 690 case INT: 691 case USHORT: 692 case SHORT: 693 case UCHAR: 694 case SCHAR: 695 case CHAR: 696 case BOOL: 697 case UNSIGN: 698 case SIGNED: 699 case NOTSPEC: 700 break; 701 case NTSPEC: 702 abort(); 703 } 704 705 *epp = cp; 706 return (tidx); 707 } 708 709 /* 710 * Get the length of a type string. 711 */ 712 static int 713 gettlen(const char *cp, const char **epp) 714 { 715 const char *cp1; 716 char c, s, *eptr; 717 tspec_t t; 718 int narg, i, cm, vm; 719 720 cp1 = cp; 721 722 c = *cp++; 723 724 cm = vm = 0; 725 726 while (c == 'c' || c == 'v') { 727 if (c == 'c') { 728 if (cm) 729 inperr(); 730 cm = 1; 731 } else { 732 if (vm) 733 inperr(); 734 vm = 1; 735 } 736 c = *cp++; 737 } 738 739 if (c == 's' || c == 'u' || c == 'l' || c == 'e') { 740 s = c; 741 c = *cp++; 742 } else { 743 s = '\0'; 744 } 745 746 t = NOTSPEC; 747 748 switch (c) { 749 case 'B': 750 if (s == '\0') 751 t = BOOL; 752 break; 753 case 'C': 754 if (s == 's') { 755 t = SCHAR; 756 } else if (s == 'u') { 757 t = UCHAR; 758 } else if (s == '\0') { 759 t = CHAR; 760 } 761 break; 762 case 'S': 763 if (s == 'u') { 764 t = USHORT; 765 } else if (s == '\0') { 766 t = SHORT; 767 } 768 break; 769 case 'I': 770 if (s == 'u') { 771 t = UINT; 772 } else if (s == '\0') { 773 t = INT; 774 } 775 break; 776 case 'L': 777 if (s == 'u') { 778 t = ULONG; 779 } else if (s == '\0') { 780 t = LONG; 781 } 782 break; 783 case 'Q': 784 if (s == 'u') { 785 t = UQUAD; 786 } else if (s == '\0') { 787 t = QUAD; 788 } 789 break; 790 case 'D': 791 if (s == 's') { 792 t = FLOAT; 793 } else if (s == 'l') { 794 t = LDOUBLE; 795 } else if (s == '\0') { 796 t = DOUBLE; 797 } 798 break; 799 case 'V': 800 if (s == '\0') 801 t = VOID; 802 break; 803 case 'P': 804 if (s == '\0') 805 t = PTR; 806 break; 807 case 'A': 808 if (s == '\0') 809 t = ARRAY; 810 break; 811 case 'F': 812 case 'f': 813 if (s == '\0') 814 t = FUNC; 815 break; 816 case 'T': 817 if (s == 'e') { 818 t = ENUM; 819 } else if (s == 's') { 820 t = STRUCT; 821 } else if (s == 'u') { 822 t = UNION; 823 } 824 break; 825 default: 826 inperr(); 827 } 828 829 if (t == NOTSPEC) 830 inperr(); 831 832 switch (t) { 833 case ARRAY: 834 (void)strtol(cp, &eptr, 10); 835 if (cp == eptr) 836 inperr(); 837 cp = eptr; 838 (void)gettlen(cp, &cp); 839 break; 840 case PTR: 841 (void)gettlen(cp, &cp); 842 break; 843 case FUNC: 844 c = *cp; 845 if (isdigit((u_char)c)) { 846 narg = (int)strtol(cp, &eptr, 10); 847 cp = eptr; 848 for (i = 0; i < narg; i++) { 849 if (i == narg - 1 && *cp == 'E') { 850 cp++; 851 } else { 852 (void)gettlen(cp, &cp); 853 } 854 } 855 } 856 (void)gettlen(cp, &cp); 857 break; 858 case ENUM: 859 case STRUCT: 860 case UNION: 861 switch (*cp++) { 862 case '1': 863 (void)inpname(cp, &cp); 864 break; 865 case '2': 866 (void)inpname(cp, &cp); 867 break; 868 case '3': 869 /* unique position: line.file.uniquifier */ 870 (void)strtol(cp, &eptr, 10); 871 if (cp == eptr) 872 inperr(); 873 cp = eptr; 874 if (*cp++ != '.') 875 inperr(); 876 (void)strtol(cp, &eptr, 10); 877 if (cp == eptr) 878 inperr(); 879 cp = eptr; 880 if (*cp++ != '.') 881 inperr(); 882 (void)strtol(cp, &eptr, 10); 883 if (cp == eptr) 884 inperr(); 885 cp = eptr; 886 break; 887 default: 888 inperr(); 889 } 890 break; 891 case FLOAT: 892 case USHORT: 893 case SHORT: 894 case UCHAR: 895 case SCHAR: 896 case CHAR: 897 case BOOL: 898 case UNSIGN: 899 case SIGNED: 900 case NOTSPEC: 901 case INT: 902 case UINT: 903 case DOUBLE: 904 case LDOUBLE: 905 case VOID: 906 case ULONG: 907 case QUAD: 908 case UQUAD: 909 case LONG: 910 break; 911 #ifndef __COVERITY__ 912 case NTSPEC: 913 abort(); 914 #endif 915 } 916 917 *epp = cp; 918 return (cp - cp1); 919 } 920 921 /* 922 * Search a type by it's type string. 923 */ 924 static u_short 925 findtype(const char *cp, size_t len, int h) 926 { 927 thtab_t *thte; 928 929 for (thte = thtab[h]; thte != NULL; thte = thte->th_nxt) { 930 if (strncmp(thte->th_name, cp, len) != 0) 931 continue; 932 if (thte->th_name[len] == '\0') 933 return (thte->th_idx); 934 } 935 936 return (0); 937 } 938 939 /* 940 * Store a type and it's type string so we can later share this type 941 * if we read the same type string from the input file. 942 */ 943 static u_short 944 storetyp(type_t *tp, const char *cp, size_t len, int h) 945 { 946 static u_int tidx = 1; /* 0 is reserved */ 947 thtab_t *thte; 948 char *name; 949 950 if (tidx >= USHRT_MAX) 951 errx(1, "sorry, too many types"); 952 953 if (tidx == tlstlen - 1) { 954 tlst = xrealloc(tlst, (tlstlen * 2) * sizeof (type_t *)); 955 (void)memset(tlst + tlstlen, 0, tlstlen * sizeof (type_t *)); 956 tlstlen *= 2; 957 } 958 959 tlst[tidx] = tp; 960 961 /* create a hash table entry */ 962 name = xalloc(len + 1); 963 (void)memcpy(name, cp, len); 964 name[len] = '\0'; 965 966 thte = xalloc(sizeof (thtab_t)); 967 thte->th_name = name; 968 thte->th_idx = tidx; 969 thte->th_nxt = thtab[h]; 970 thtab[h] = thte; 971 972 return ((u_short)tidx++); 973 } 974 975 /* 976 * Hash function for types 977 */ 978 static int 979 thash(const char *s, size_t len) 980 { 981 u_int v; 982 983 v = 0; 984 while (len-- != 0) { 985 v = (v << sizeof (v)) + (u_char)*s++; 986 v ^= v >> (sizeof (v) * CHAR_BIT - sizeof (v)); 987 } 988 return (v % THSHSIZ2); 989 } 990 991 /* 992 * Read a string enclosed by "". This string may contain quoted chars. 993 */ 994 static char * 995 inpqstrg(const char *src, const char **epp) 996 { 997 char *strg, *dst; 998 size_t slen; 999 int c; 1000 int v; 1001 1002 dst = strg = xmalloc(slen = 32); 1003 1004 if ((c = *src++) != '"') 1005 inperr(); 1006 if ((c = *src++) == '\0') 1007 inperr(); 1008 1009 while (c != '"') { 1010 if (c == '\\') { 1011 if ((c = *src++) == '\0') 1012 inperr(); 1013 switch (c) { 1014 case 'n': 1015 c = '\n'; 1016 break; 1017 case 't': 1018 c = '\t'; 1019 break; 1020 case 'v': 1021 c = '\v'; 1022 break; 1023 case 'b': 1024 c = '\b'; 1025 break; 1026 case 'r': 1027 c = '\r'; 1028 break; 1029 case 'f': 1030 c = '\f'; 1031 break; 1032 case 'a': 1033 c = '\a'; 1034 break; 1035 case '\\': 1036 c = '\\'; 1037 break; 1038 case '"': 1039 c = '"'; 1040 break; 1041 case '\'': 1042 c = '\''; 1043 break; 1044 case '0': case '1': case '2': case '3': 1045 v = (c - '0') << 6; 1046 if ((c = *src++) < '0' || c > '7') 1047 inperr(); 1048 v |= (c - '0') << 3; 1049 if ((c = *src++) < '0' || c > '7') 1050 inperr(); 1051 v |= c - '0'; 1052 c = (u_char)v; 1053 break; 1054 default: 1055 inperr(); 1056 } 1057 } 1058 /* keep space for trailing '\0' */ 1059 if (dst - strg == slen - 1) { 1060 strg = xrealloc(strg, slen * 2); 1061 dst = strg + (slen - 1); 1062 slen *= 2; 1063 } 1064 *dst++ = (char)c; 1065 if ((c = *src++) == '\0') 1066 inperr(); 1067 } 1068 *dst = '\0'; 1069 1070 *epp = src; 1071 return (strg); 1072 } 1073 1074 /* 1075 * Read the name of a symbol in static memory. 1076 */ 1077 static const char * 1078 inpname(const char *cp, const char **epp) 1079 { 1080 static char *buf; 1081 static size_t blen = 0; 1082 size_t len, i; 1083 char *eptr, c; 1084 1085 len = (int)strtol(cp, &eptr, 10); 1086 if (cp == eptr) 1087 inperr(); 1088 cp = eptr; 1089 if (len + 1 > blen) 1090 buf = xrealloc(buf, blen = len + 1); 1091 for (i = 0; i < len; i++) { 1092 c = *cp++; 1093 if (!isalnum((unsigned char)c) && c != '_') 1094 inperr(); 1095 buf[i] = c; 1096 } 1097 buf[i] = '\0'; 1098 1099 *epp = cp; 1100 return (buf); 1101 } 1102 1103 /* 1104 * Return the index of a file name. If the name cannot be found, create 1105 * a new entry and return the index of the newly created entry. 1106 */ 1107 static int 1108 getfnidx(const char *fn) 1109 { 1110 int i; 1111 1112 /* 0 ist reserved */ 1113 for (i = 1; fnames[i] != NULL; i++) { 1114 if (strcmp(fnames[i], fn) == 0) 1115 break; 1116 } 1117 if (fnames[i] != NULL) 1118 return (i); 1119 1120 if (i == nfnames - 1) { 1121 fnames = xrealloc(fnames, (nfnames * 2) * sizeof (char *)); 1122 (void)memset(fnames + nfnames, 0, nfnames * sizeof (char *)); 1123 nfnames *= 2; 1124 } 1125 1126 fnames[i] = xstrdup(fn); 1127 return (i); 1128 } 1129 1130 /* 1131 * Separate symbols with static and external linkage. 1132 */ 1133 void 1134 mkstatic(hte_t *hte) 1135 { 1136 sym_t *sym1, **symp, *sym; 1137 fcall_t **callp, *call; 1138 usym_t **usymp, *usym; 1139 hte_t *nhte; 1140 int ofnd; 1141 1142 /* Look for first static definition */ 1143 for (sym1 = hte->h_syms; sym1 != NULL; sym1 = sym1->s_nxt) { 1144 if (sym1->s_static) 1145 break; 1146 } 1147 if (sym1 == NULL) 1148 return; 1149 1150 /* Do nothing if this name is used only in one translation unit. */ 1151 ofnd = 0; 1152 for (sym = hte->h_syms; sym != NULL && !ofnd; sym = sym->s_nxt) { 1153 if (sym->s_pos.p_src != sym1->s_pos.p_src) 1154 ofnd = 1; 1155 } 1156 for (call = hte->h_calls; call != NULL && !ofnd; call = call->f_nxt) { 1157 if (call->f_pos.p_src != sym1->s_pos.p_src) 1158 ofnd = 1; 1159 } 1160 for (usym = hte->h_usyms; usym != NULL && !ofnd; usym = usym->u_nxt) { 1161 if (usym->u_pos.p_src != sym1->s_pos.p_src) 1162 ofnd = 1; 1163 } 1164 if (!ofnd) { 1165 hte->h_used = 1; 1166 /* errors about undef. static symbols are printed in lint1 */ 1167 hte->h_def = 1; 1168 hte->h_static = 1; 1169 return; 1170 } 1171 1172 /* 1173 * Create a new hash table entry 1174 * 1175 * XXX this entry should be put at the beginning of the list to 1176 * avoid to process the same symbol twice. 1177 */ 1178 for (nhte = hte; nhte->h_link != NULL; nhte = nhte->h_link) 1179 continue; 1180 nhte->h_link = xmalloc(sizeof (hte_t)); 1181 nhte = nhte->h_link; 1182 nhte->h_name = hte->h_name; 1183 nhte->h_used = 1; 1184 nhte->h_def = 1; /* error in lint1 */ 1185 nhte->h_static = 1; 1186 nhte->h_syms = NULL; 1187 nhte->h_lsym = &nhte->h_syms; 1188 nhte->h_calls = NULL; 1189 nhte->h_lcall = &nhte->h_calls; 1190 nhte->h_usyms = NULL; 1191 nhte->h_lusym = &nhte->h_usyms; 1192 nhte->h_link = NULL; 1193 nhte->h_hte = NULL; 1194 1195 /* 1196 * move all symbols used in this translation unit into the new 1197 * hash table entry. 1198 */ 1199 for (symp = &hte->h_syms; (sym = *symp) != NULL; ) { 1200 if (sym->s_pos.p_src == sym1->s_pos.p_src) { 1201 sym->s_static = 1; 1202 (*symp) = sym->s_nxt; 1203 if (hte->h_lsym == &sym->s_nxt) 1204 hte->h_lsym = symp; 1205 sym->s_nxt = NULL; 1206 *nhte->h_lsym = sym; 1207 nhte->h_lsym = &sym->s_nxt; 1208 } else { 1209 symp = &sym->s_nxt; 1210 } 1211 } 1212 for (callp = &hte->h_calls; (call = *callp) != NULL; ) { 1213 if (call->f_pos.p_src == sym1->s_pos.p_src) { 1214 (*callp) = call->f_nxt; 1215 if (hte->h_lcall == &call->f_nxt) 1216 hte->h_lcall = callp; 1217 call->f_nxt = NULL; 1218 *nhte->h_lcall = call; 1219 nhte->h_lcall = &call->f_nxt; 1220 } else { 1221 callp = &call->f_nxt; 1222 } 1223 } 1224 for (usymp = &hte->h_usyms; (usym = *usymp) != NULL; ) { 1225 if (usym->u_pos.p_src == sym1->s_pos.p_src) { 1226 (*usymp) = usym->u_nxt; 1227 if (hte->h_lusym == &usym->u_nxt) 1228 hte->h_lusym = usymp; 1229 usym->u_nxt = NULL; 1230 *nhte->h_lusym = usym; 1231 nhte->h_lusym = &usym->u_nxt; 1232 } else { 1233 usymp = &usym->u_nxt; 1234 } 1235 } 1236 1237 /* h_def must be recalculated for old hte */ 1238 hte->h_def = nhte->h_def = 0; 1239 for (sym = hte->h_syms; sym != NULL; sym = sym->s_nxt) { 1240 if (sym->s_def == DEF || sym->s_def == TDEF) { 1241 hte->h_def = 1; 1242 break; 1243 } 1244 } 1245 1246 mkstatic(hte); 1247 } 1248