xref: /onnv-gate/usr/src/cmd/sgs/lorder/lorder.sh (revision 13093:48f2dbca79a2)
10Sstevel@tonic-gate#! /bin/sh -f
20Sstevel@tonic-gate#
30Sstevel@tonic-gate# CDDL HEADER START
40Sstevel@tonic-gate#
50Sstevel@tonic-gate# The contents of this file are subject to the terms of the
6*13093SRoger.Faulkner@Oracle.COM# Common Development and Distribution License (the "License").
7*13093SRoger.Faulkner@Oracle.COM# You may not use this file except in compliance with the License.
80Sstevel@tonic-gate#
90Sstevel@tonic-gate# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate# or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate# See the License for the specific language governing permissions
120Sstevel@tonic-gate# and limitations under the License.
130Sstevel@tonic-gate#
140Sstevel@tonic-gate# When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate# If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate# fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate# information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate#
200Sstevel@tonic-gate# CDDL HEADER END
210Sstevel@tonic-gate#
220Sstevel@tonic-gate#	Copyright (c) 1989 AT&T
230Sstevel@tonic-gate#	  All Rights Reserved
24*13093SRoger.Faulkner@Oracle.COM#
25*13093SRoger.Faulkner@Oracle.COM# Copyright (c) 1993, 2010, Oracle and/or its affiliates. All rights reserved.
26*13093SRoger.Faulkner@Oracle.COM#
270Sstevel@tonic-gate
280Sstevel@tonic-gate#	COMMON LORDER
290Sstevel@tonic-gate#
300Sstevel@tonic-gate#
310Sstevel@tonic-gateif [ -z "$TMPDIR" ]
320Sstevel@tonic-gatethen
330Sstevel@tonic-gate	TDIR="/tmp"
340Sstevel@tonic-gateelse
350Sstevel@tonic-gate	TDIR=$TMPDIR
360Sstevel@tonic-gatefi
370Sstevel@tonic-gatetrap "rm -f $TDIR/$$symdef $TDIR/$$symref $TDIR/$$tmp; exit"  1 2 13 15
380Sstevel@tonic-gatePFX=
390Sstevel@tonic-gateWHERE=/usr/ccs/bin
400Sstevel@tonic-gate
410Sstevel@tonic-gateUSAGE="Usage: ${PFX}lorder file ..."
420Sstevel@tonic-gatefor i in "$@"
430Sstevel@tonic-gatedo
440Sstevel@tonic-gate	case "$i" in
450Sstevel@tonic-gate	-*)	echo "$USAGE";
460Sstevel@tonic-gate		exit 2;;
470Sstevel@tonic-gate	esac
480Sstevel@tonic-gate
490Sstevel@tonic-gate	if [ ! -r "$i" ]
500Sstevel@tonic-gate	then
510Sstevel@tonic-gate		echo "${PFX}lorder: $i: cannot open"
520Sstevel@tonic-gate		exit 2;
530Sstevel@tonic-gate	fi
540Sstevel@tonic-gatedone
550Sstevel@tonic-gate
560Sstevel@tonic-gatecase $# in
570Sstevel@tonic-gate0)	echo "$USAGE"
580Sstevel@tonic-gate	exit 2;;
590Sstevel@tonic-gate1)	case $1 in
600Sstevel@tonic-gate	*.o)	set $1 $1
610Sstevel@tonic-gate	esac
620Sstevel@tonic-gateesac
630Sstevel@tonic-gate
640Sstevel@tonic-gate#	The following sed script is commented here.
650Sstevel@tonic-gate#	The first three expressions in the sed script
660Sstevel@tonic-gate#	insures that we only have lines
670Sstevel@tonic-gate#	that contain file names and the external
680Sstevel@tonic-gate#	declarations associated with each file.
690Sstevel@tonic-gate#	The next two parts of the sed script put the pattern
700Sstevel@tonic-gate#	(in this case the file name) into the hold space
710Sstevel@tonic-gate#	and creates the "filename filename" lines and
720Sstevel@tonic-gate#	writes them out. The first part is for .o files,
730Sstevel@tonic-gate#	the second is for .o's in archives.
740Sstevel@tonic-gate#	The last 2 sections of code are exactly alike but
750Sstevel@tonic-gate#	they handle different external symbols, namely the
760Sstevel@tonic-gate#	symbols that are defined in the text section, data section, bss
770Sstevel@tonic-gate#	section or common symbols and symbols
780Sstevel@tonic-gate#	that are referenced but not defined in this file.
790Sstevel@tonic-gate#	A line containing the symbol (from the pattern space) and
800Sstevel@tonic-gate#	the file it is referenced in (from the hold space) is
810Sstevel@tonic-gate#	put into the pattern space.
820Sstevel@tonic-gate#	If its text, data, bss or common it is written out to the
830Sstevel@tonic-gate#	symbol definition (symdef) file, otherwise it was referenced
840Sstevel@tonic-gate#	but not declared in this file so it is written out to the
850Sstevel@tonic-gate#	symbol referenced (symref) file.
860Sstevel@tonic-gate#
870Sstevel@tonic-gate#
880Sstevel@tonic-gate${WHERE}/${PFX}nm -p $* 2>$TDIR/$$tmp | sed -e '/^[ 	]*$/d' -e '
890Sstevel@tonic-gate	/^[0-9]* R $/d
90*13093SRoger.Faulkner@Oracle.COM	/ [a-zFLS] /d
910Sstevel@tonic-gate	/[^]]:$/{
920Sstevel@tonic-gate		s/://
930Sstevel@tonic-gate		h
940Sstevel@tonic-gate		s/.*/& &/
950Sstevel@tonic-gate		p
960Sstevel@tonic-gate		d
970Sstevel@tonic-gate	}
980Sstevel@tonic-gate	/]:$/{
990Sstevel@tonic-gate		s/]://
1000Sstevel@tonic-gate		s/^.*\[//
1010Sstevel@tonic-gate		h
1020Sstevel@tonic-gate		s/.*/& &/
1030Sstevel@tonic-gate		p
1040Sstevel@tonic-gate		d
1050Sstevel@tonic-gate	}
1060Sstevel@tonic-gate	/ [TDBNCAR] /{
1070Sstevel@tonic-gate		s/^.* [TDBNCAR] //
1080Sstevel@tonic-gate		G
1090Sstevel@tonic-gate		s/\n/ /
1100Sstevel@tonic-gate		w '$TDIR/$$symdef'
1110Sstevel@tonic-gate		d
1120Sstevel@tonic-gate	}
1130Sstevel@tonic-gate	/ U /{
1140Sstevel@tonic-gate		s/^.* U //
1150Sstevel@tonic-gate		G
1160Sstevel@tonic-gate		s/\n/ /
1170Sstevel@tonic-gate		w '$TDIR/$$symref'
1180Sstevel@tonic-gate		d
1190Sstevel@tonic-gate	}
1200Sstevel@tonic-gate'
1210Sstevel@tonic-gateif [ -s $TDIR/$$tmp ]
1220Sstevel@tonic-gatethen
1230Sstevel@tonic-gate	sed -e "s/^${PFX}nm:/${PFX}lorder:/" < $TDIR/$$tmp >&2
1240Sstevel@tonic-gate	rm -f $TDIR/$$symdef $TDIR/$$symref $TDIR/$$tmp
1250Sstevel@tonic-gate	exit 1
1260Sstevel@tonic-gatefi
1270Sstevel@tonic-gatesort $TDIR/$$symdef -o $TDIR/$$symdef
1280Sstevel@tonic-gatesort $TDIR/$$symref -o $TDIR/$$symref
1290Sstevel@tonic-gatejoin $TDIR/$$symref $TDIR/$$symdef | sed 's/[^ ]* *//'
1300Sstevel@tonic-gaterm -f $TDIR/$$symdef $TDIR/$$symref $TDIR/$$tmp
131