|
root / base / usr / src / tools / lintdump
lintdump Plain Text 1093 lines 28.1 KB
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#

#
# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

PROG      = lintdump
MAN1ONBLDFILES = lintdump.1onbld
CFLAGS   += $(CCVERBOSE)

include ../Makefile.tools

$(ROOTONBLDMAN1ONBLDFILES) : FILEMODE=	644

.KEEP_STATE:

all:    $(PROG)

install: all .WAIT $(ROOTONBLDMACHPROG) $(ROOTONBLDMAN1ONBLDFILES)

lint:	lint_PROG

clean:

include ../Makefile.targ
.\" " CDDL HEADER START
.\" "
.\" " The contents of this file are subject to the terms of the
.\" " Common Development and Distribution License (the "License").
.\" " You may not use this file except in compliance with the License.
.\" "
.\" " You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
.\" " or http://www.opensolaris.org/os/licensing.
.\" " See the License for the specific language governing permissions
.\" " and limitations under the License.
.\" "
.\" " When distributing Covered Code, include this CDDL HEADER in each
.\" " file and include the License file at usr/src/OPENSOLARIS.LICENSE.
.\" " If applicable, add the following below this CDDL HEADER, with the
.\" " fields enclosed by brackets "[]" replaced with your own identifying
.\" " information: Portions Copyright [yyyy] [name of copyright owner]
.\" "
.\" " CDDL HEADER END
.\" "
.\" "Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
.\" "Use is subject to license terms.
.TH LINTDUMP 1ONBLD "Mar 28, 2008"
.I lintdump
\- dump the contents of one or more lint objects
.SH SYNOPSIS
\fBlintdump [-i] [-p 1|2|3] [-r] \fIlintobj\fP [ \fIlintobj\fP ... ]
.SH DESCRIPTION
.LP
The lintdump utility dumps the contents of one or more lint
objects.  This is chiefly useful when trying to understand the cause of
unexpected or obtuse lint warnings (see EXAMPLES), but can also be used to
find differences between lint objects across builds or releases, or to
debug problems in lint itself.
.LP
A lint object is a binary file (typically suffixed with ".ln") constructed
from a C source file via the "-c" option to lint(1).  Multiple lint
objects may be combined into a lint library object (typically prefixed
with "llib-l" and suffixed with ".ln") using the "-o" option to lint.  (As
a convenience, lint "-o" allows a lint library object to be built directly
from C source files). The lintdump utility is capable of dumping both
traditional lint objects and lint library objects.
.LP
The format of a lint object is unstable and subject to change at any time,
but its current structure is summarized here in order to aid in
understanding the current output of lintdump.  A lint object consists of
one or more lint modules (one per C source file).  Each lint module
consists of a header and four sections, called PASS1, PASS2, PASS3, and
STRINGS.  Generally speaking, PASS1 contains definitions, PASS2 contains
declarations, and PASS3 contains information on whether or how functions
or variables are used.  The STRINGS section holds the strings for
printf(3C)/scanf(3C) checking.
.LP
Each PASS section consists of a sequence of binary records of assorted
types.  The sequence of records is further partitioned by FILE records,
which indicate the source or header file that is responsible for the
records that follow.  The remaining record types provide lint with
information about the functions, variables, and structures defined or used
by the object.
.SH OPTIONS
.TP 10
.B -i
Do not output structure tag IDs (see EXAMPLES).
.TP 10
.B -p 1|2|3
Just output the PASS1, PASS2, or PASS3 section.
.TP 10
.B -r
Output records using relative paths (see EXAMPLES).
.SH OUTPUT
.LP
The contents of each specified \fIlintobj\fP is dumped in command-line
order.  For each \fIlintobj\fP, lintdump outputs a single line beginning
with "LINTOBJ:" that provides its name.  For each lint module within that
object, lintdump outputs a single line beginning with "LINTMOD:" that
provides its module ID, the size of its PASS1, PASS2, PASS3, STRING
sections, and its total size, in that order.
.LP
Next, unless the -p option is used, the contents of the PASS1, PASS2, and
PASS3 sections are dumped, in order.  Before each section is dumped,
lintdump outputs a single line beginning with "SECTION:" that
provides the name and size of the section.  For each section,
lintdump outputs each record in order.  The display format of each
record depends on its type:
.LP
.B FILE RECORDS
.RS 4
Each FILE record is displayed on a single line beginning with "FILE:".
Note that FILE records are often found in pairs, the first providing the
absolute path to the file.  FILE records containing absolute paths are
omitted if -r is used.  Other record types following a FILE record are
indented to show their relationship to the FILE record.
.RE
.LP
.B FUNCTION AND VARIABLE RECORDS
.RS 4
Each function or variable record is displayed on a single line using an
extended version of the format used in The C Programming Language, Second
Edition.  In particular, properties contained in the record that cannot be
conveyed in C are displayed in angle brackets following definition or
declaration; a full list of these and their meanings are given below in
RECORD PROPERTIES.  In addition, note that some structures or unions may
only be known by a numeric \fIID\fP, and thus output as "struct <tag
\fIID\fP>".  This ID can be used to pair the structure with its definition
via structure records.  If -i is used, then "struct <anon>" is printed
instead.
.RE
.LP
.B STRUCTURE AND UNION RECORDS
.RS 4
Each structure or union record is displayed using an extended version of
the standard multi-line format used in The C Programming Language, Second
Edition.  In particular, to facilitate problem analysis, unless -i is
used, each structure or union definition includes a numeric ID enclosed in
angle-brackets, such as "struct FILE <tag 1298> {".
.RE
.LP
To illustrate each of the common record formats, suppose the following
lint library is built:
.LP
.nf
$ cat > liba.c
/* LINTLIBRARY */
/* PROTOLIB1 */
int af(int);
struct as {
        char as_name[32];
        int  as_flag;
} as;
$ lint -oa liba.c
.fi
.LP
Then lintdump will produce the following output:
.LP
.nf
LINTOBJ: llib-la.ln
LINTMOD: 6484: 268+24+130+9 = 431 bytes
SECTION: PASS1: 268 bytes
   FILE: /home/meem/hacks/liba.c
   FILE: liba.c
         extern int af(int);
         struct as as;
         struct as <tag 98> {
             char as_name[];
             int as_flag;
         };
SECTION: PASS2: 24 bytes
SECTION: PASS3: 130 bytes
   FILE: /home/meem/hacks/liba.c
   FILE: liba.c
         int af(void) <returns value>;
.fi
.SH RECORD PROPERTIES
.LP
As discussed in OUTPUT, some records are displayed using an extended
format to convey information that cannot be expressed in C.  The following
extended information may be displayed:
.LP
.B <PRINTFLIKE\fIn\fP>
.RS 4
Indicates to lint that argument \fIn\fP to the variable-argument function
is a format string in printf(3C) format, which enhances lint's argument
checking.
.RE
.LP
.B <SCANFLIKE\fIn\fP>
.RS 4
Indicates to lint that argument \fIn\fP to the variable-argument function
is a format string in scanf(3C) format, which enhances lint's argument
checking.
.RE
.LP
.B <definition>
.RS 4
Indicates to lint that this record represents the definition of the given
variable or function (rather than a declaration).
.RE
.LP
.B <use: side-effects context>
.RS 4
Indicates to lint that the associated function is called in a context that
suggests it has side effects.
.RE
.LP
.B <use: return value context>
.RS 4
Indicates to lint that the associated function is called in a context where
its return value is used.
.RE
.LP
.B <use: unspecified context>
.RS 4
Indicates to lint that the associated function is used in an unspecified
manner.
.RE
.LP
.B <returns value>
.RS 4
Indicates to lint that the function returns a value.
.RE
.SH EXAMPLES
.LP
One common problem is that lint does not always provide sufficient
information to understand the reason for a type mismatch.  For instance,
sometimes lint will confusingly report a type mismatch between
apparently-identical types:
.LP
.nf
$ lint msghdr.c -lsocket
function argument ( number ) used inconsistently
    recvmsg (arg 2) llib-lsocket:socket.h(437) struct msghdr * ::
                                 msghdr.c(12)  struct msghdr *
.fi
.LP
By using lintdump, we can pinpoint the problem by examining both
definitions for \fIstruct msghdr\fP:
.LP
.nf
$ lintdump /lib/llib-lsocket.ln
   \fI[ ... ]\fP
   FILE: llib-lsocket:socket.h
         struct msghdr <tag 4532> {
             void *msg_name;
             unsigned int msg_namelen;
             struct iovec *msg_iov;
             int msg_iovlen;
             \fBchar *msg_accrights;\fP
             \fBint msg_accrightslen;\fP
         };
.fi
.LP
.nf
$ lint -omsghdr msghdr.c -lsocket
$ lintdump llib-lmsghdr.ln
   \fI[ ... ]\fP
   FILE: socket.h
         struct msghdr <tag 1315> {
             void *msg_name;
             unsigned int msg_namelen;
             struct iovec *msg_iov;
             int msg_iovlen;
             \fBvoid *msg_control;\fP
             \fBunsigned int msg_controllen;\fP
             \fBint msg_flags;\fP
         };
.fi
.LP
Looking at <sys/socket.h>, the problem becomes apparent: the structure
changes depending on compile-time options, which clearly differ between
the application and the library:
.LP
.nf
struct msghdr {
        void            *msg_name;
        socklen_t       msg_namelen;
        struct iovec    *msg_iov;
        int             msg_iovlen;

#if defined(_XPG4_2) || defined(_KERNEL)
        void            *msg_control;
        socklen_t       msg_controllen;
        int             msg_flags;
#else
        caddr_t         msg_accrights;
        int             msg_accrightslen;
#endif  /* defined(_XPG4_2) || defined(_KERNEL) */
};
.fi
.LP
Another use of lintdump is to compare two versions of a lint object to
see whether anything of significance has changed.  For instance, lintdump
can be used to understand why a lint library is different between a
project gate and a patch gate, and thus to determine whether the library
will need to be redelivered in the patch including the project:
.LP
.nf
$ PATCHROOT=/ws/on10-patch/proto/root_i386
$ diff llib-lkstat.ln $PATCHROOT/lib/llib-lkstat.ln
Binary files llib-lkstat.ln and
             /ws/on10-patch/proto/root_i386/lib/llib-lkstat.ln differ
$ lintdump -ir llib-lkstat.ln > /tmp/proj-kstat.out
$ lintdump -ir $PATCHROOT/lib/llib-lkstat.ln > /tmp/patch-kstat.out
.fi
.LP
.nf
$ diff /tmp/patch-kstat.out /tmp/proj-kstat.out
1,2c1,2
< LINTMOD: 3675: 4995+26812+1045+9 = 32861 bytes
< SECTION: PASS1: 4995 bytes
---
> LINTMOD: 39982: 5144+27302+1057+9 = 33512 bytes
> SECTION: PASS1: 5144 bytes
19c19
<              unsigned char _file;
---
>              unsigned char _magic;
22a23,24
>              unsigned int __extendedfd;
>              unsigned int __xf_nocheck;
\fI[ ... ]\fP
.fi
.LP
Note that -r option removes spurious differences that would otherwise
arise from different absolute paths to the same source file, and the -i
option removes spurious differences due to ID generation inside lint.
.SH SEE ALSO
.LP
.IR lint(1),
.IR printf(3C),
.IR scanf(3C)
.SH NOTES
This utility is provided as an interim solution until a stable utility
can be bundled with Sun Studio.  As such, any use of this utility in
scripts or embedded inside programs should be done with knowledge that
subsequent changes will be required in order to transition to the stable
solution.
.LP
The lint object file format does not have a way to represent bitfields. As
such, bitfield size information cannot be displayed by lintdump.
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * Tool for dumping lint libraries.
 */

#include <ctype.h>
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>

#include "lnstuff.h"		/* silly header name from alint */

typedef struct lsu {
	const char	*name;
	ATYPE		atype;
	struct lsu	*next;
} lsu_t;

#define	LSU_HASHSIZE	512
static lsu_t		*lsu_table[LSU_HASHSIZE];

static boolean_t	showids = B_TRUE;
static boolean_t	justrelpaths = B_FALSE;
static int		justpass = -1;
static int		indentlevel = 9;
static const char	*progname;

static void info(const char *, ...);
static void infohdr(const char *, const char *, ...);
static void warn(const char *, ...);
static void die(const char *, ...);
static void usage(void);
static void indent(void);
static void unindent(void);
static void print_lintmod(const char *, FILE *, FLENS *);
static void print_pass(const char *, FILE *);
static void print_atype(ATYPE *, int, ATYPE *, const char *);
static void print_mods(const char *, ATYPE *, int, ATYPE *, uint_t);
static void getstr(FILE *, char *, size_t);
static void lsu_build(FILE *);
static void lsu_empty(void);
static int lsu_add(const char *, ATYPE *);
static lsu_t *lsu_lookup(unsigned long);

int
main(int argc, char **argv)
{
	int		i, c, mod;
	FILE		*fp;
	FLENS		hdr;
	const char	*lnname;

	progname = strrchr(argv[0], '/');
	if (progname == NULL)
		progname = argv[0];
	else
		progname++;

	while ((c = getopt(argc, argv, "ip:r")) != EOF) {
		switch (c) {
		case 'i':
			showids = B_FALSE;
			break;
		case 'p':
			justpass = strtoul(optarg, NULL, 0);
			if (justpass < 1 || justpass > 3)
				usage();
			break;
		case 'r':
			justrelpaths = B_TRUE;
			break;
		default:
			usage();
		}
	}

	if (optind == argc)
		usage();

	for (i = optind; i < argc; i++) {
		fp = fopen(argv[i], "r");
		if (fp == NULL) {
			warn("cannot open \"%s\"", argv[i]);
			continue;
		}

		lnname = argv[i];
		if (justrelpaths && lnname[0] == '/')
			lnname = strrchr(lnname, '/') + 1;

		/*
		 * Dump out all of the modules in the lint object.
		 */
		for (mod = 1; fread(&hdr, sizeof (hdr), 1, fp) == 1; mod++) {
			if (hdr.ver != LINTVER) {
				warn("%s: unsupported lint object version "
				    "%d\n", argv[i], hdr.ver);
				break;
			}

			if (mod == 1)
				infohdr("LINTOBJ", "%s\n", lnname);

			/*
			 * First build the table of structure/union names,
			 * then print the lint module.  Finally, empty the
			 * table out before dumping the next module.
			 */
			lsu_build(fp);
			print_lintmod(lnname, fp, &hdr);
			lsu_empty();
		}
		(void) fclose(fp);
	}

	return (EXIT_SUCCESS);
}

/*
 * Print a lint module and advance past it in the stream.
 */
static void
print_lintmod(const char *lnname, FILE *fp, FLENS *hp)
{
	ulong_t		psizes[5];
	uint_t		pass;

	psizes[0] = 0;
	psizes[1] = hp->f1;
	psizes[2] = hp->f2;
	psizes[3] = hp->f3;
	psizes[4] = hp->f4;

	infohdr("LINTMOD", "%hu: %lu+%lu+%lu+%lu = %lu bytes\n", hp->mno,
	    hp->f1, hp->f2, hp->f3, hp->f4, hp->f1 + hp->f2 + hp->f3 + hp->f4);

	for (pass = 1; pass <= 4; pass++) {
		if ((justpass < 0 || justpass == pass) && pass < 4) {
			infohdr("SECTION", "PASS%u: %lu bytes\n", pass,
			    psizes[pass]);
			print_pass(lnname, fp);
		} else {
			(void) fseek(fp, psizes[pass], SEEK_CUR);
		}
	}
}

/*
 * Print out a PASS section of a lint module.
 */
static void
print_pass(const char *lnname, FILE *fp)
{
	union rec	rec;
	int		nargs;
	char		name[1024];
	ATYPE		atype, *args;
	LINE		line;
	boolean_t	wasfile = B_FALSE;

	for (;;) {
		if (fread(&rec, sizeof (rec), 1, fp) != 1)
			die("%s: unexpected end of file\n", lnname);

		line = rec.l;
		if (line.decflag & LND)		/* end-of-pass marker */
			break;

		getstr(fp, name, sizeof (name));

		/*
		 * Check if this is a file record.
		 */
		if (line.decflag & LFN) {
			if (wasfile || !justrelpaths)
				infohdr("FILE", "%s\n", name);
			wasfile = B_TRUE;
			continue;
		}
		wasfile = B_FALSE;

		/*
		 * Check if this is a function or variable record.
		 */
		nargs = line.nargs;
		if (line.decflag & (LIB|LDS|LDI|LPR|LDX|LDC|LRV|LUE|LUV|LUM)) {
			if (nargs < 0)
				nargs = -nargs - 1;

			if (line.decflag & LDS)
				info("static ");
			else if (line.decflag & (LPR|LDX|LDC))
				info("extern ");

			args = calloc(sizeof (atype), nargs);
			if (args == NULL)
				die("cannot allocate argument information");

			if (fread(args, sizeof (atype), nargs, fp) != nargs)
				die("%s: unexpected end of file\n", lnname);

			print_atype(&line.type, line.nargs, args, name);
			free(args);

			if (line.decflag & LRV)
				info(" <returns value>");
			if (line.decflag & LUE)
				info(" <use: side-effects context>");
			if (line.decflag & LUV)
				info(" <use: return value context>");
			if (line.decflag & LUM)
				info(" <use: unspecified context>");

			if (line.decflag & LPF)
				info(" <PRINTFLIKE%d>", nargs);
			else if (line.decflag & LSF)
				info(" <SCANFLIKE%d>", nargs);

			if (line.decflag & LDI)
				info(" { <definition> }");
			else if (line.decflag & LDX)
				info(" = <definition>");

			info(";\n");
			continue;
		}

		/*
		 * Check if this is a structure or union record.
		 */
		if (line.decflag & LSU) {
			if (line.decflag & ~(LSU))
				info("??? ");

			info("struct ");
			if (name[0] != '.')
				info("%s ", name);
			if (showids)
				info("<tag %lu> ", line.type.extra.ty);
			info("{ \n");

			indent();
			for (; nargs > 0; nargs--) {
				if (fread(&atype, sizeof (atype), 1, fp) != 1) {
					die("%s: unexpected end of file\n",
					    lnname);
				}
				getstr(fp, name, sizeof (name));
				print_atype(&atype, 0, NULL, name);
				info(";\n");
			}
			unindent();
			info("};\n");
			continue;
		}

		warn("%s: unknown record type 0%o\n", lnname, line.decflag);
	}
}

/*
 * Print the C datatype or function `atp' named `name'.  If `name' is a
 * function, then `nargs' indicates the number of C datatypes pointed to
 * by `args'.
 */
static void
print_atype(ATYPE *atp, int nargs, ATYPE *args, const char *name)
{
	static const char *basetypes[] = {		"",
		"char",		"unsigned char",	"signed char",
		"short",	"unsigned short",	"signed short",
		"int",		"unsigned int",		"signed int",
		"long",		"unsigned long",	"signed long",
		"long long",	"unsigned long long",	"signed long long",
		"enum",		"float",		"double",
		"long double",	"void",			"struct",
		"union",	"_Bool",		"<genchar>",
		"<genshort>",	"<genint>",		"<genlong>",
		"<genlonglong>"
	};
	uint16_t basetype = atp->aty & LNQUAL;
	lsu_t *lsup;

	if (atp->aty & LCON)
		info("const ");
	if (atp->aty & LVOL)
		info("volatile ");
	if (atp->aty & LCONV)
		info("integer const ");

	if (basetype < 1 ||
	    basetype > (sizeof (basetypes) / sizeof (*basetypes)))
		info("<unknown type %x>", basetype);

	switch (basetype) {
	case LN_UNION:
	case LN_STRUCT:
		lsup = lsu_lookup(atp->extra.ty);
		if (lsup != NULL && lsup->name[0] != '.') {
			info("%s %s", basetypes[basetype], lsup->name);
		} else {
			info("%s", basetypes[basetype]);
			if (showids)
				info(" <tag %lu>", atp->extra.ty);
			else
				info(" <anon>");
		}
		break;
	default:
		info("%s", basetypes[basetype]);
	};

	print_mods(name, atp, nargs, args, 14);
}

/*
 * Recursively print type modifiers.
 */
static void
print_mods(const char *name, ATYPE *atp, int nargs, ATYPE *args, uint_t pos)
{
	int arg;
	int mods = atp->dcl_mod >> (pos * 2);
	int lastmods = atp->dcl_mod >> ((pos + 1) * 2);
	boolean_t isvarargs = B_FALSE;

	if (LN_ISPTR(mods)) {
		if (!LN_ISPTR(lastmods) && !LN_ISFTN(lastmods))
			info(" ");
		info("*");
	}

	if (atp->dcl_con & (1 << pos))
		info(" const ");
	if (atp->dcl_vol & (1 << pos))
		info(" volatile ");

	if (pos != 0) {
		if (LN_ISFTN(mods))
			info(" (");
		print_mods(name, atp, nargs, args, pos - 1);
		if (LN_ISFTN(mods))
			info(")()");
		return;
	}

	if (name[0] == '\0')
		return;

	if (!LN_ISPTR(lastmods) && !LN_ISPTR(mods))
		info(" ");
	info("%s", name);

	if (LN_ISARY(mods)) {
		info("[]");
	} else if (LN_ISFTN(mods)) {
		info("(");

		if (nargs < 0) {
			nargs = -nargs - 1;
			isvarargs = B_TRUE;
		}

		if (nargs == 0) {
			info("void");
		} else {
			for (arg = 0; arg < nargs; arg++) {
				print_atype(&args[arg], 0, NULL, "");
				if ((arg + 1) < nargs)
					info(", ");
				else if (isvarargs)
					info(", ...");
			}
		}
		info(")");
	}
}

/*
 * Add an LSU entry to the LSU table.
 */
static int
lsu_add(const char *name, ATYPE *atp)
{
	unsigned int	i = atp->extra.ty % LSU_HASHSIZE;
	lsu_t		*lsup;

	lsup = malloc(sizeof (lsu_t));
	if (lsup == NULL)
		return (ENOMEM);

	lsup->atype = *atp;
	lsup->next = lsu_table[i];
	lsup->name = strdup(name);
	if (lsup->name == NULL) {
		free(lsup);
		return (ENOMEM);
	}

	lsu_table[i] = lsup;
	return (0);
}

/*
 * Lookup an LSU entry by ID.
 */
static lsu_t *
lsu_lookup(T1WORD ty)
{
	unsigned int	i = ty % LSU_HASHSIZE;
	lsu_t		*lsup;

	for (lsup = lsu_table[i]; lsup != NULL; lsup = lsup->next) {
		if (lsup->atype.extra.ty == ty)
			return (lsup);
	}

	return (NULL);
}

/*
 * Read all LSU (structure and union definition) records in order to
 * build a structure and union name table, called the LSU table.
 * Although `fp' is read from, the original file offset is preserved.
 */
static void
lsu_build(FILE *fp)
{
	union rec	rec;
	char		name[1024];
	int		nargs;
	off_t		curoff = ftello(fp);

	for (;;) {
		if (fread(&rec, sizeof (rec), 1, fp) != 1)
			break;

		if (rec.l.decflag & LND)	/* end-of-pass marker */
			break;

		getstr(fp, name, sizeof (name));
		nargs = rec.l.nargs;

		if (rec.l.decflag & (LIB|LDS|LDI)) {
			if (nargs < 0)
				nargs = -nargs - 1;

			(void) fseek(fp, sizeof (ATYPE) * nargs, SEEK_CUR);
			continue;
		}

		if (rec.l.decflag & LSU) {
			if (lsu_add(name, &rec.l.type) != 0)
				warn("cannot allocate struct `%s' info", name);

			for (; nargs > 0; nargs--) {
				(void) fseek(fp, sizeof (ATYPE), SEEK_CUR);
				getstr(fp, name, sizeof (name));
			}
		}
	}

	(void) fseek(fp, curoff, SEEK_SET);
}

/*
 * Empty the LSU table.
 */
static void
lsu_empty(void)
{
	lsu_t		*lsup, *lsup_next;
	unsigned int	i;

	for (i = 0; i < LSU_HASHSIZE; i++) {
		for (lsup = lsu_table[i]; lsup != NULL; lsup = lsup_next) {
			lsup_next = lsup->next;
			free(lsup);
		}
		lsu_table[i] = NULL;
	}
}

/*
 * Read the NUL-terminated string at `fp' into `buf', which is at most
 * `bufsize' bytes.
 */
static void
getstr(FILE *fp, char *buf, size_t bufsize)
{
	int c;
	size_t i;

	for (i = 0; i < bufsize - 1; i++) {
		c = fgetc(fp);
		if (c == EOF || c == '\0' || !isascii(c))
			break;
		buf[i] = (char)c;
	}

	buf[i] = '\0';
}

static void
indent(void)
{
	indentlevel += 4;
}

static void
unindent(void)
{
	indentlevel -= 4;
}

static void
usage(void)
{
	(void) fprintf(stderr, "usage: %s [-i] [-p 1|2|3] [-r] lintobj"
	    " [ lintobj ... ]\n", progname);
	exit(EXIT_FAILURE);
}

/* PRINTFLIKE1 */
static void
info(const char *format, ...)
{
	va_list alist;
	static int complete = 1;

	if (complete)
		(void) printf("%*s", indentlevel, "");

	va_start(alist, format);
	(void) vprintf(format, alist);
	va_end(alist);

	complete = strrchr(format, '\n') != NULL;
}

/* PRINTFLIKE2 */
static void
infohdr(const char *hdr, const char *format, ...)
{
	va_list alist;
	static int complete = 1;

	if (complete)
		(void) printf("%7s: ", hdr);

	va_start(alist, format);
	(void) vprintf(format, alist);
	va_end(alist);

	complete = strrchr(format, '\n') != NULL;
}

/* PRINTFLIKE1 */
static void
warn(const char *format, ...)
{
	va_list alist;
	char *errstr = strerror(errno);

	(void) fprintf(stderr, "%s: warning: ", progname);

	va_start(alist, format);
	(void) vfprintf(stderr, format, alist);
	va_end(alist);

	if (strrchr(format, '\n') == NULL)
		(void) fprintf(stderr, ": %s\n", errstr);
}

/* PRINTFLIKE1 */
static void
die(const char *format, ...)
{
	va_list alist;
	char *errstr = strerror(errno);

	(void) fprintf(stderr, "%s: fatal: ", progname);

	va_start(alist, format);
	(void) vfprintf(stderr, format, alist);
	va_end(alist);

	if (strrchr(format, '\n') == NULL)
		(void) fprintf(stderr, ": %s\n", errstr);

	exit(EXIT_FAILURE);
}
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*	Copyright (c) 1989 AT&T					*/
/*	  All Rights Reserved  					*/

/*
 *	Based on @(#)lnstuff.h 1.5 02/06/05 from lint
 */

#ifndef	LNSTUFF_H
#define	LNSTUFF_H

#include <sys/types.h>

#define	LDI 01		/* defined and initialized: storage set aside	*/
#define	LIB 02		/* defined on a library				*/
#define	LDC 04		/* defined as a common region on UNIX		*/
#define	LDX 010		/* defined by an extern: if ! pflag, same as LDI */
#define	LRV 020		/* function returns a value			*/
#define	LUV 040		/* function used in a value context		*/
#define	LUE 0100	/* function used in effects context		*/
#define	LUM 0200	/* mentioned somewhere other than at the declaration */
#define	LDS 0400	/* defined static object (like LDI)		*/
#define	LFN 01000	/* filename record				*/
#define	LSU 02000	/* struct/union def				*/
#define	LPR 04000	/* prototype declaration			*/
#define	LND 010000	/* end module marker				*/
#define	LPF 020000	/* printf like					*/
#define	LSF 040000	/* scanf like					*/

#define	LNQUAL		00037		/* type w/o qualifiers		*/
#define	LNUNQUAL	0174000		/* remove type, keep other info */
#define	LCON		(1<<15)		/* type qualified by const	*/
#define	LVOL		(1<<14)		/* type qualified by volatile	*/
#define	LNOAL		(1<<13)		/* not used */
#define	LCONV		(1<<12)		/* type is an integer constant	*/
#define	LPTR		(1<<11)		/* last modifier is a pointer	*/
#define	LINTVER		4

typedef unsigned long T1WORD;
typedef long FILEPOS;
typedef short TY;

typedef struct flens {
	long		f1, f2, f3, f4;
	unsigned short	ver, mno;
} FLENS;

typedef struct {
	TY		aty;		/* base type			*/
	unsigned long	dcl_mod;	/* ptr/ftn/ary modifiers	*/
	unsigned short	dcl_con;	/* const qualifiers		*/
	unsigned short	dcl_vol;	/* volatile qualifiers		*/
	union {
		T1WORD	ty;
		FILEPOS	pos;
	} extra;
} ATYPE;

typedef struct {
	short		decflag;	/* what type of record is this	*/
	short		nargs;		/* # of args (or members)	*/
	int		fline;		/* line defined/used in		*/
	ATYPE		type;		/* type information		*/
} LINE;

union rec {
	LINE l;
	struct {
		short decflag;
		char *fn;
	} f;
};

/* type modifiers */
#define	LN_TMASK 3
#define	LN_ISPTR(x)	(((x)&LN_TMASK) == 1)  /* is x a pointer type */
#define	LN_ISFTN(x)	(((x)&LN_TMASK) == 2)  /* is x a function type */
#define	LN_ISARY(x)	(((x)&LN_TMASK) == 3)  /* is x an array type */

/* type numbers for pass2 */
#define	LN_STRUCT	21	/* generic struct */
#define	LN_UNION	22	/* generic union */

#endif	/* LNSTUFF_H */