xref: /titanic_51/usr/src/cmd/sgs/lorder/lorder.sh (revision 177d5b5f8c0e969013441207a0a705ae66b08cf7)
1#! /bin/sh -f
2#
3# CDDL HEADER START
4#
5# The contents of this file are subject to the terms of the
6# Common Development and Distribution License (the "License").
7# You may not use this file except in compliance with the License.
8#
9# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10# or http://www.opensolaris.org/os/licensing.
11# See the License for the specific language governing permissions
12# and limitations under the License.
13#
14# When distributing Covered Code, include this CDDL HEADER in each
15# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16# If applicable, add the following below this CDDL HEADER, with the
17# fields enclosed by brackets "[]" replaced with your own identifying
18# information: Portions Copyright [yyyy] [name of copyright owner]
19#
20# CDDL HEADER END
21#
22#	Copyright (c) 1989 AT&T
23#	  All Rights Reserved
24#
25# Copyright (c) 1993, 2010, Oracle and/or its affiliates. All rights reserved.
26#
27
28#	COMMON LORDER
29#
30#
31if [ -z "$TMPDIR" ]
32then
33	TDIR="/tmp"
34else
35	TDIR=$TMPDIR
36fi
37trap "rm -f $TDIR/$$symdef $TDIR/$$symref $TDIR/$$tmp; exit"  1 2 13 15
38PFX=
39WHERE=/usr/bin
40
41USAGE="Usage: ${PFX}lorder file ..."
42for i in "$@"
43do
44	case "$i" in
45	-*)	echo "$USAGE";
46		exit 2;;
47	esac
48
49	if [ ! -r "$i" ]
50	then
51		echo "${PFX}lorder: $i: cannot open"
52		exit 2;
53	fi
54done
55
56case $# in
570)	echo "$USAGE"
58	exit 2;;
591)	case $1 in
60	*.o)	set $1 $1
61	esac
62esac
63
64#	The following sed script is commented here.
65#	The first three expressions in the sed script
66#	insures that we only have lines
67#	that contain file names and the external
68#	declarations associated with each file.
69#	The next two parts of the sed script put the pattern
70#	(in this case the file name) into the hold space
71#	and creates the "filename filename" lines and
72#	writes them out. The first part is for .o files,
73#	the second is for .o's in archives.
74#	The last 2 sections of code are exactly alike but
75#	they handle different external symbols, namely the
76#	symbols that are defined in the text section, data section, bss
77#	section or common symbols and symbols
78#	that are referenced but not defined in this file.
79#	A line containing the symbol (from the pattern space) and
80#	the file it is referenced in (from the hold space) is
81#	put into the pattern space.
82#	If its text, data, bss or common it is written out to the
83#	symbol definition (symdef) file, otherwise it was referenced
84#	but not declared in this file so it is written out to the
85#	symbol referenced (symref) file.
86#
87#
88${WHERE}/${PFX}nm -p $* 2>$TDIR/$$tmp | sed -e '/^[ 	]*$/d' -e '
89	/^[0-9]* R $/d
90	/ [a-zFLS] /d
91	/[^]]:$/{
92		s/://
93		h
94		s/.*/& &/
95		p
96		d
97	}
98	/]:$/{
99		s/]://
100		s/^.*\[//
101		h
102		s/.*/& &/
103		p
104		d
105	}
106	/ [TDBNCAR] /{
107		s/^.* [TDBNCAR] //
108		G
109		s/\n/ /
110		w '$TDIR/$$symdef'
111		d
112	}
113	/ U /{
114		s/^.* U //
115		G
116		s/\n/ /
117		w '$TDIR/$$symref'
118		d
119	}
120'
121if [ -s $TDIR/$$tmp ]
122then
123	sed -e "s/^${PFX}nm:/${PFX}lorder:/" < $TDIR/$$tmp >&2
124	rm -f $TDIR/$$symdef $TDIR/$$symref $TDIR/$$tmp
125	exit 1
126fi
127sort $TDIR/$$symdef -o $TDIR/$$symdef
128sort $TDIR/$$symref -o $TDIR/$$symref
129join $TDIR/$$symref $TDIR/$$symdef | sed 's/[^ ]* *//'
130rm -f $TDIR/$$symdef $TDIR/$$symref $TDIR/$$tmp
131