xref: /freebsd/tools/tools/kernxref/kernxref.sh (revision 069ac184)
1:
2#
3# SPDX-License-Identifier: Beerware
4#
5# ----------------------------------------------------------------------------
6# "THE BEER-WARE LICENSE" (Revision 42):
7# <phk@FreeBSD.org> wrote this file.  As long as you retain this notice you
8# can do whatever you want with this stuff. If we meet some day, and you think
9# this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
10# ----------------------------------------------------------------------------
11#
12# Sort options by "Matthew Emmerton" <matt@gsicomp.on.ca>
13#
14#
15# This shell script will make a cross reference of the symbols of a kernel.
16#
17
18COMPILEDIR=/sys/`uname -m`/compile
19KERNELNAME=LINT
20SORTORDER=-k1
21
22args=`getopt h?k:s: $*`;
23if [ $? != 0 ]
24then
25	args="-h";
26fi
27set -- $args;
28for i
29do
30	case "$i"
31	in
32	-h|-\?)
33		echo "Usage: $0 [ -k <kernelname> ] [ -s [ 'symbol' | 'filename' ] ]";
34		exit 0;
35		;;
36	-k)
37		KERNELNAME=$2
38		if [ -d ${COMPILEDIR}/${KERNELNAME} ];
39		then
40			shift; shift;
41			continue;
42		fi
43		echo "Kernel '$KERNELNAME' does not exist in ${COMPILEDIR}!";
44		exit 1;
45		;;
46	-s)
47		if [ "x$2" = "xsymbol" ]
48		then
49			SORTORDER=-k1
50			shift; shift;
51			continue;
52		fi
53		if [ "x$2" = "xfilename" ]
54		then
55			SORTORDER=-k2
56			shift; shift;
57			continue;
58		fi
59		echo "Invalid selection for -s: $2";
60		exit 1;
61		;;
62	--)
63		shift;
64		break;
65		;;
66	esac
67done
68
69cd ${COMPILEDIR}/${KERNELNAME}
70
71MOD_OBJS=`find modules -name \*.o`
72
73for i in *.o $MOD_OBJS
74do
75	nm -gon $i
76done | sed '
77/aicasm.*:/d
78/genassym.*:/d
79s/.*\///
80s/:/ /
81' |  awk '
82NF > 1	{
83	if ($2 == "t")
84		next
85	if ($2 == "F")
86		next
87	if ($2 == "U") {
88		ref[$3]=ref[$3]" "$1
89		nm[$3]++
90	} else if ($3 == "D" || $3 == "T" || $3 == "B" || $3 == "R" || $3 == "A") {
91		if (def[$4] != "")
92			def[$4]=def[$4]" "$1
93		else
94			def[$4]=$1
95		nm[$4]++
96	} else if ($2 == "?") {
97		if (def[$3] == "S")
98			i++
99		else if (def[$3] != "")
100			def[$3]=def[$3]",S"
101		else
102			def[$3]="S"
103		ref[$3]=ref[$3]" "$1
104		nm[$3]++
105	} else if ($2 == "C") {
106		if (def[$3] == $2)
107			i++
108		else if (def[$3] == "")
109			def[$3]=$1
110		else
111			ref[$3]=ref[$3]" "$1
112		nm[$3]++
113	} else {
114		print ">>>",$0
115	}
116	}
117END	{
118	for (i in nm) {
119		printf "%s {%s} %s\n",i,def[i],ref[i]
120	}
121	}
122' | sort $SORTORDER | awk '
123	{
124	if ($2 == "{S}")
125		$2 = "<Linker set>"
126	if (length($3) == 0) {
127		printf "%-31s %d %s\tUNREF\n",$1,0, $2
128		N1++
129	} else if ($2 == "{}") {
130		printf "%-31s %d {UNDEF}\n",$1, NF-2
131		N2++
132	} else {
133		printf "%-31s %d %s",$1,NF-2,$2
134		p = 80;
135		for (i = 3 ; i <= NF; i++) {
136			if (p+length ($i)+1 > 48) {
137				printf "\n\t\t\t\t\t%s", $i
138				p = 7;
139			} else {
140				printf " %s", $i
141			}
142			p += 1 + length ($i)
143		}
144		printf "\n"
145		N3++
146		if (NF-2 == 1)
147			N4++
148		if (NF-2 == 2)
149			N5++
150	}
151	}
152END	{
153	printf "Total symbols: %5d\n",N1+N2+N3
154	printf "unref symbols: %5d\n",N1
155	printf "undef symbols: %5d\n",N2
156	printf "1 ref symbols: %5d\n",N4
157	printf "2 ref symbols: %5d\n",N5
158	}
159'
160