1 /* $NetBSD: read.c,v 1.18 2006/03/22 16:16:59 christos 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.18 2006/03/22 16:16:59 christos 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; 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 tp->t_subt = TP(inptype(cp, &cp)); 623 break; 624 case PTR: 625 tp->t_subt = TP(inptype(cp, &cp)); 626 break; 627 case FUNC: 628 c = *cp; 629 if (isdigit((u_char)c)) { 630 if (!osdef) 631 tp->t_proto = 1; 632 narg = (int)strtol(cp, &eptr, 10); 633 cp = eptr; 634 tp->t_args = xcalloc((size_t)(narg + 1), 635 sizeof (type_t *)); 636 for (i = 0; i < narg; i++) { 637 if (i == narg - 1 && *cp == 'E') { 638 tp->t_vararg = 1; 639 cp++; 640 } else { 641 tp->t_args[i] = TP(inptype(cp, &cp)); 642 } 643 } 644 } 645 tp->t_subt = TP(inptype(cp, &cp)); 646 break; 647 case ENUM: 648 tp->t_tspec = INT; 649 tp->t_isenum = 1; 650 /* FALLTHROUGH */ 651 case STRUCT: 652 case UNION: 653 switch (*cp++) { 654 case '1': 655 tp->t_istag = 1; 656 tp->t_tag = hsearch(inpname(cp, &cp), 1); 657 break; 658 case '2': 659 tp->t_istynam = 1; 660 tp->t_tynam = hsearch(inpname(cp, &cp), 1); 661 break; 662 case '3': 663 tp->t_isuniqpos = 1; 664 tp->t_uniqpos.p_line = strtol(cp, &eptr, 10); 665 cp = eptr; 666 cp++; 667 /* xlate to 'global' file name. */ 668 tp->t_uniqpos.p_file = 669 addoutfile(inpfns[strtol(cp, &eptr, 10)]); 670 cp = eptr; 671 cp++; 672 tp->t_uniqpos.p_uniq = strtol(cp, &eptr, 10); 673 cp = eptr; 674 break; 675 } 676 break; 677 case LONG: 678 case VOID: 679 case LDOUBLE: 680 case DOUBLE: 681 case FLOAT: 682 case UQUAD: 683 case QUAD: 684 case ULONG: 685 case UINT: 686 case INT: 687 case USHORT: 688 case SHORT: 689 case UCHAR: 690 case SCHAR: 691 case CHAR: 692 case BOOL: 693 case UNSIGN: 694 case SIGNED: 695 case NOTSPEC: 696 break; 697 case NTSPEC: 698 abort(); 699 } 700 701 *epp = cp; 702 return (tidx); 703 } 704 705 /* 706 * Get the length of a type string. 707 */ 708 static int 709 gettlen(const char *cp, const char **epp) 710 { 711 const char *cp1; 712 char c, s, *eptr; 713 tspec_t t; 714 int narg, i, cm, vm; 715 716 cp1 = cp; 717 718 c = *cp++; 719 720 cm = vm = 0; 721 722 while (c == 'c' || c == 'v') { 723 if (c == 'c') { 724 if (cm) 725 inperr(); 726 cm = 1; 727 } else { 728 if (vm) 729 inperr(); 730 vm = 1; 731 } 732 c = *cp++; 733 } 734 735 if (c == 's' || c == 'u' || c == 'l' || c == 'e') { 736 s = c; 737 c = *cp++; 738 } else { 739 s = '\0'; 740 } 741 742 t = NOTSPEC; 743 744 switch (c) { 745 case 'B': 746 if (s == '\0') 747 t = BOOL; 748 break; 749 case 'C': 750 if (s == 's') { 751 t = SCHAR; 752 } else if (s == 'u') { 753 t = UCHAR; 754 } else if (s == '\0') { 755 t = CHAR; 756 } 757 break; 758 case 'S': 759 if (s == 'u') { 760 t = USHORT; 761 } else if (s == '\0') { 762 t = SHORT; 763 } 764 break; 765 case 'I': 766 if (s == 'u') { 767 t = UINT; 768 } else if (s == '\0') { 769 t = INT; 770 } 771 break; 772 case 'L': 773 if (s == 'u') { 774 t = ULONG; 775 } else if (s == '\0') { 776 t = LONG; 777 } 778 break; 779 case 'Q': 780 if (s == 'u') { 781 t = UQUAD; 782 } else if (s == '\0') { 783 t = QUAD; 784 } 785 break; 786 case 'D': 787 if (s == 's') { 788 t = FLOAT; 789 } else if (s == 'l') { 790 t = LDOUBLE; 791 } else if (s == '\0') { 792 t = DOUBLE; 793 } 794 break; 795 case 'V': 796 if (s == '\0') 797 t = VOID; 798 break; 799 case 'P': 800 if (s == '\0') 801 t = PTR; 802 break; 803 case 'A': 804 if (s == '\0') 805 t = ARRAY; 806 break; 807 case 'F': 808 case 'f': 809 if (s == '\0') 810 t = FUNC; 811 break; 812 case 'T': 813 if (s == 'e') { 814 t = ENUM; 815 } else if (s == 's') { 816 t = STRUCT; 817 } else if (s == 'u') { 818 t = UNION; 819 } 820 break; 821 default: 822 inperr(); 823 } 824 825 if (t == NOTSPEC) 826 inperr(); 827 828 switch (t) { 829 case ARRAY: 830 (void)strtol(cp, &eptr, 10); 831 if (cp == eptr) 832 inperr(); 833 cp = eptr; 834 (void)gettlen(cp, &cp); 835 break; 836 case PTR: 837 (void)gettlen(cp, &cp); 838 break; 839 case FUNC: 840 c = *cp; 841 if (isdigit((u_char)c)) { 842 narg = (int)strtol(cp, &eptr, 10); 843 cp = eptr; 844 for (i = 0; i < narg; i++) { 845 if (i == narg - 1 && *cp == 'E') { 846 cp++; 847 } else { 848 (void)gettlen(cp, &cp); 849 } 850 } 851 } 852 (void)gettlen(cp, &cp); 853 break; 854 case ENUM: 855 case STRUCT: 856 case UNION: 857 switch (*cp++) { 858 case '1': 859 (void)inpname(cp, &cp); 860 break; 861 case '2': 862 (void)inpname(cp, &cp); 863 break; 864 case '3': 865 /* unique position: line.file.uniquifier */ 866 (void)strtol(cp, &eptr, 10); 867 if (cp == eptr) 868 inperr(); 869 cp = eptr; 870 if (*cp++ != '.') 871 inperr(); 872 (void)strtol(cp, &eptr, 10); 873 if (cp == eptr) 874 inperr(); 875 cp = eptr; 876 if (*cp++ != '.') 877 inperr(); 878 (void)strtol(cp, &eptr, 10); 879 if (cp == eptr) 880 inperr(); 881 cp = eptr; 882 break; 883 default: 884 inperr(); 885 } 886 break; 887 case FLOAT: 888 case USHORT: 889 case SHORT: 890 case UCHAR: 891 case SCHAR: 892 case CHAR: 893 case BOOL: 894 case UNSIGN: 895 case SIGNED: 896 case NOTSPEC: 897 case INT: 898 case UINT: 899 case DOUBLE: 900 case LDOUBLE: 901 case VOID: 902 case ULONG: 903 case QUAD: 904 case UQUAD: 905 case LONG: 906 break; 907 #ifndef __COVERITY__ 908 case NTSPEC: 909 abort(); 910 #endif 911 } 912 913 *epp = cp; 914 return (cp - cp1); 915 } 916 917 /* 918 * Search a type by it's type string. 919 */ 920 static u_short 921 findtype(const char *cp, size_t len, int h) 922 { 923 thtab_t *thte; 924 925 for (thte = thtab[h]; thte != NULL; thte = thte->th_nxt) { 926 if (strncmp(thte->th_name, cp, len) != 0) 927 continue; 928 if (thte->th_name[len] == '\0') 929 return (thte->th_idx); 930 } 931 932 return (0); 933 } 934 935 /* 936 * Store a type and it's type string so we can later share this type 937 * if we read the same type string from the input file. 938 */ 939 static u_short 940 storetyp(type_t *tp, const char *cp, size_t len, int h) 941 { 942 static u_int tidx = 1; /* 0 is reserved */ 943 thtab_t *thte; 944 char *name; 945 946 if (tidx >= USHRT_MAX) 947 errx(1, "sorry, too many types"); 948 949 if (tidx == tlstlen - 1) { 950 tlst = xrealloc(tlst, (tlstlen * 2) * sizeof (type_t *)); 951 (void)memset(tlst + tlstlen, 0, tlstlen * sizeof (type_t *)); 952 tlstlen *= 2; 953 } 954 955 tlst[tidx] = tp; 956 957 /* create a hash table entry */ 958 name = xalloc(len + 1); 959 (void)memcpy(name, cp, len); 960 name[len] = '\0'; 961 962 thte = xalloc(sizeof (thtab_t)); 963 thte->th_name = name; 964 thte->th_idx = tidx; 965 thte->th_nxt = thtab[h]; 966 thtab[h] = thte; 967 968 return ((u_short)tidx++); 969 } 970 971 /* 972 * Hash function for types 973 */ 974 static int 975 thash(const char *s, size_t len) 976 { 977 u_int v; 978 979 v = 0; 980 while (len-- != 0) { 981 v = (v << sizeof (v)) + (u_char)*s++; 982 v ^= v >> (sizeof (v) * CHAR_BIT - sizeof (v)); 983 } 984 return (v % THSHSIZ2); 985 } 986 987 /* 988 * Read a string enclosed by "". This string may contain quoted chars. 989 */ 990 static char * 991 inpqstrg(const char *src, const char **epp) 992 { 993 char *strg, *dst; 994 size_t slen; 995 int c; 996 int v; 997 998 dst = strg = xmalloc(slen = 32); 999 1000 if ((c = *src++) != '"') 1001 inperr(); 1002 if ((c = *src++) == '\0') 1003 inperr(); 1004 1005 while (c != '"') { 1006 if (c == '\\') { 1007 if ((c = *src++) == '\0') 1008 inperr(); 1009 switch (c) { 1010 case 'n': 1011 c = '\n'; 1012 break; 1013 case 't': 1014 c = '\t'; 1015 break; 1016 case 'v': 1017 c = '\v'; 1018 break; 1019 case 'b': 1020 c = '\b'; 1021 break; 1022 case 'r': 1023 c = '\r'; 1024 break; 1025 case 'f': 1026 c = '\f'; 1027 break; 1028 case 'a': 1029 c = '\a'; 1030 break; 1031 case '\\': 1032 c = '\\'; 1033 break; 1034 case '"': 1035 c = '"'; 1036 break; 1037 case '\'': 1038 c = '\''; 1039 break; 1040 case '0': case '1': case '2': case '3': 1041 v = (c - '0') << 6; 1042 if ((c = *src++) < '0' || c > '7') 1043 inperr(); 1044 v |= (c - '0') << 3; 1045 if ((c = *src++) < '0' || c > '7') 1046 inperr(); 1047 v |= c - '0'; 1048 c = (u_char)v; 1049 break; 1050 default: 1051 inperr(); 1052 } 1053 } 1054 /* keep space for trailing '\0' */ 1055 if (dst - strg == slen - 1) { 1056 strg = xrealloc(strg, slen * 2); 1057 dst = strg + (slen - 1); 1058 slen *= 2; 1059 } 1060 *dst++ = (char)c; 1061 if ((c = *src++) == '\0') 1062 inperr(); 1063 } 1064 *dst = '\0'; 1065 1066 *epp = src; 1067 return (strg); 1068 } 1069 1070 /* 1071 * Read the name of a symbol in static memory. 1072 */ 1073 static const char * 1074 inpname(const char *cp, const char **epp) 1075 { 1076 static char *buf; 1077 static size_t blen = 0; 1078 size_t len, i; 1079 char *eptr, c; 1080 1081 len = (int)strtol(cp, &eptr, 10); 1082 if (cp == eptr) 1083 inperr(); 1084 cp = eptr; 1085 if (len + 1 > blen) 1086 buf = xrealloc(buf, blen = len + 1); 1087 for (i = 0; i < len; i++) { 1088 c = *cp++; 1089 if (!isalnum((unsigned char)c) && c != '_') 1090 inperr(); 1091 buf[i] = c; 1092 } 1093 buf[i] = '\0'; 1094 1095 *epp = cp; 1096 return (buf); 1097 } 1098 1099 /* 1100 * Return the index of a file name. If the name cannot be found, create 1101 * a new entry and return the index of the newly created entry. 1102 */ 1103 static int 1104 getfnidx(const char *fn) 1105 { 1106 int i; 1107 1108 /* 0 ist reserved */ 1109 for (i = 1; fnames[i] != NULL; i++) { 1110 if (strcmp(fnames[i], fn) == 0) 1111 break; 1112 } 1113 if (fnames[i] != NULL) 1114 return (i); 1115 1116 if (i == nfnames - 1) { 1117 fnames = xrealloc(fnames, (nfnames * 2) * sizeof (char *)); 1118 (void)memset(fnames + nfnames, 0, nfnames * sizeof (char *)); 1119 nfnames *= 2; 1120 } 1121 1122 fnames[i] = xstrdup(fn); 1123 return (i); 1124 } 1125 1126 /* 1127 * Separate symbols with static and external linkage. 1128 */ 1129 void 1130 mkstatic(hte_t *hte) 1131 { 1132 sym_t *sym1, **symp, *sym; 1133 fcall_t **callp, *call; 1134 usym_t **usymp, *usym; 1135 hte_t *nhte; 1136 int ofnd; 1137 1138 /* Look for first static definition */ 1139 for (sym1 = hte->h_syms; sym1 != NULL; sym1 = sym1->s_nxt) { 1140 if (sym1->s_static) 1141 break; 1142 } 1143 if (sym1 == NULL) 1144 return; 1145 1146 /* Do nothing if this name is used only in one translation unit. */ 1147 ofnd = 0; 1148 for (sym = hte->h_syms; sym != NULL && !ofnd; sym = sym->s_nxt) { 1149 if (sym->s_pos.p_src != sym1->s_pos.p_src) 1150 ofnd = 1; 1151 } 1152 for (call = hte->h_calls; call != NULL && !ofnd; call = call->f_nxt) { 1153 if (call->f_pos.p_src != sym1->s_pos.p_src) 1154 ofnd = 1; 1155 } 1156 for (usym = hte->h_usyms; usym != NULL && !ofnd; usym = usym->u_nxt) { 1157 if (usym->u_pos.p_src != sym1->s_pos.p_src) 1158 ofnd = 1; 1159 } 1160 if (!ofnd) { 1161 hte->h_used = 1; 1162 /* errors about undef. static symbols are printed in lint1 */ 1163 hte->h_def = 1; 1164 hte->h_static = 1; 1165 return; 1166 } 1167 1168 /* 1169 * Create a new hash table entry 1170 * 1171 * XXX this entry should be put at the beginning of the list to 1172 * avoid to process the same symbol twice. 1173 */ 1174 for (nhte = hte; nhte->h_link != NULL; nhte = nhte->h_link) 1175 continue; 1176 nhte->h_link = xmalloc(sizeof (hte_t)); 1177 nhte = nhte->h_link; 1178 nhte->h_name = hte->h_name; 1179 nhte->h_used = 1; 1180 nhte->h_def = 1; /* error in lint1 */ 1181 nhte->h_static = 1; 1182 nhte->h_syms = NULL; 1183 nhte->h_lsym = &nhte->h_syms; 1184 nhte->h_calls = NULL; 1185 nhte->h_lcall = &nhte->h_calls; 1186 nhte->h_usyms = NULL; 1187 nhte->h_lusym = &nhte->h_usyms; 1188 nhte->h_link = NULL; 1189 nhte->h_hte = NULL; 1190 1191 /* 1192 * move all symbols used in this translation unit into the new 1193 * hash table entry. 1194 */ 1195 for (symp = &hte->h_syms; (sym = *symp) != NULL; ) { 1196 if (sym->s_pos.p_src == sym1->s_pos.p_src) { 1197 sym->s_static = 1; 1198 (*symp) = sym->s_nxt; 1199 if (hte->h_lsym == &sym->s_nxt) 1200 hte->h_lsym = symp; 1201 sym->s_nxt = NULL; 1202 *nhte->h_lsym = sym; 1203 nhte->h_lsym = &sym->s_nxt; 1204 } else { 1205 symp = &sym->s_nxt; 1206 } 1207 } 1208 for (callp = &hte->h_calls; (call = *callp) != NULL; ) { 1209 if (call->f_pos.p_src == sym1->s_pos.p_src) { 1210 (*callp) = call->f_nxt; 1211 if (hte->h_lcall == &call->f_nxt) 1212 hte->h_lcall = callp; 1213 call->f_nxt = NULL; 1214 *nhte->h_lcall = call; 1215 nhte->h_lcall = &call->f_nxt; 1216 } else { 1217 callp = &call->f_nxt; 1218 } 1219 } 1220 for (usymp = &hte->h_usyms; (usym = *usymp) != NULL; ) { 1221 if (usym->u_pos.p_src == sym1->s_pos.p_src) { 1222 (*usymp) = usym->u_nxt; 1223 if (hte->h_lusym == &usym->u_nxt) 1224 hte->h_lusym = usymp; 1225 usym->u_nxt = NULL; 1226 *nhte->h_lusym = usym; 1227 nhte->h_lusym = &usym->u_nxt; 1228 } else { 1229 usymp = &usym->u_nxt; 1230 } 1231 } 1232 1233 /* h_def must be recalculated for old hte */ 1234 hte->h_def = nhte->h_def = 0; 1235 for (sym = hte->h_syms; sym != NULL; sym = sym->s_nxt) { 1236 if (sym->s_def == DEF || sym->s_def == TDEF) { 1237 hte->h_def = 1; 1238 break; 1239 } 1240 } 1241 1242 mkstatic(hte); 1243 } 1244