xref: /illumos-gate/usr/src/cmd/sgs/lorder/lorder.sh (revision 1b9d0d86)
17c478bd9Sstevel@tonic-gate#! /bin/sh -f
27c478bd9Sstevel@tonic-gate#
37c478bd9Sstevel@tonic-gate# CDDL HEADER START
47c478bd9Sstevel@tonic-gate#
57c478bd9Sstevel@tonic-gate# The contents of this file are subject to the terms of the
623a1cceaSRoger A. Faulkner# Common Development and Distribution License (the "License").
723a1cceaSRoger A. Faulkner# You may not use this file except in compliance with the License.
87c478bd9Sstevel@tonic-gate#
97c478bd9Sstevel@tonic-gate# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate# or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate# See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate# and limitations under the License.
137c478bd9Sstevel@tonic-gate#
147c478bd9Sstevel@tonic-gate# When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate# If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate# fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate# information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate#
207c478bd9Sstevel@tonic-gate# CDDL HEADER END
217c478bd9Sstevel@tonic-gate#
227c478bd9Sstevel@tonic-gate#	Copyright (c) 1989 AT&T
237c478bd9Sstevel@tonic-gate#	  All Rights Reserved
2423a1cceaSRoger A. Faulkner#
2523a1cceaSRoger A. Faulkner# Copyright (c) 1993, 2010, Oracle and/or its affiliates. All rights reserved.
2623a1cceaSRoger A. Faulkner#
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate#	COMMON LORDER
297c478bd9Sstevel@tonic-gate#
307c478bd9Sstevel@tonic-gate#
317c478bd9Sstevel@tonic-gateif [ -z "$TMPDIR" ]
327c478bd9Sstevel@tonic-gatethen
337c478bd9Sstevel@tonic-gate	TDIR="/tmp"
347c478bd9Sstevel@tonic-gateelse
357c478bd9Sstevel@tonic-gate	TDIR=$TMPDIR
367c478bd9Sstevel@tonic-gatefi
377c478bd9Sstevel@tonic-gatetrap "rm -f $TDIR/$$symdef $TDIR/$$symref $TDIR/$$tmp; exit"  1 2 13 15
387c478bd9Sstevel@tonic-gatePFX=
390a708795SJosh WilsdonWHERE=/usr/bin
407c478bd9Sstevel@tonic-gate
417c478bd9Sstevel@tonic-gateUSAGE="Usage: ${PFX}lorder file ..."
427c478bd9Sstevel@tonic-gatefor i in "$@"
437c478bd9Sstevel@tonic-gatedo
447c478bd9Sstevel@tonic-gate	case "$i" in
457c478bd9Sstevel@tonic-gate	-*)	echo "$USAGE";
467c478bd9Sstevel@tonic-gate		exit 2;;
477c478bd9Sstevel@tonic-gate	esac
487c478bd9Sstevel@tonic-gate
497c478bd9Sstevel@tonic-gate	if [ ! -r "$i" ]
507c478bd9Sstevel@tonic-gate	then
517c478bd9Sstevel@tonic-gate		echo "${PFX}lorder: $i: cannot open"
527c478bd9Sstevel@tonic-gate		exit 2;
537c478bd9Sstevel@tonic-gate	fi
547c478bd9Sstevel@tonic-gatedone
557c478bd9Sstevel@tonic-gate
567c478bd9Sstevel@tonic-gatecase $# in
577c478bd9Sstevel@tonic-gate0)	echo "$USAGE"
587c478bd9Sstevel@tonic-gate	exit 2;;
597c478bd9Sstevel@tonic-gate1)	case $1 in
607c478bd9Sstevel@tonic-gate	*.o)	set $1 $1
617c478bd9Sstevel@tonic-gate	esac
627c478bd9Sstevel@tonic-gateesac
637c478bd9Sstevel@tonic-gate
647c478bd9Sstevel@tonic-gate#	The following sed script is commented here.
657c478bd9Sstevel@tonic-gate#	The first three expressions in the sed script
667c478bd9Sstevel@tonic-gate#	insures that we only have lines
677c478bd9Sstevel@tonic-gate#	that contain file names and the external
687c478bd9Sstevel@tonic-gate#	declarations associated with each file.
697c478bd9Sstevel@tonic-gate#	The next two parts of the sed script put the pattern
707c478bd9Sstevel@tonic-gate#	(in this case the file name) into the hold space
717c478bd9Sstevel@tonic-gate#	and creates the "filename filename" lines and
727c478bd9Sstevel@tonic-gate#	writes them out. The first part is for .o files,
737c478bd9Sstevel@tonic-gate#	the second is for .o's in archives.
747c478bd9Sstevel@tonic-gate#	The last 2 sections of code are exactly alike but
757c478bd9Sstevel@tonic-gate#	they handle different external symbols, namely the
767c478bd9Sstevel@tonic-gate#	symbols that are defined in the text section, data section, bss
77*1b9d0d86SToomas Soome#	section or common symbols and symbols
787c478bd9Sstevel@tonic-gate#	that are referenced but not defined in this file.
79*1b9d0d86SToomas Soome#	A line containing the symbol (from the pattern space) and
807c478bd9Sstevel@tonic-gate#	the file it is referenced in (from the hold space) is
817c478bd9Sstevel@tonic-gate#	put into the pattern space.
82*1b9d0d86SToomas Soome#	If its text, data, bss or common it is written out to the
83*1b9d0d86SToomas Soome#	symbol definition (symdef) file, otherwise it was referenced
847c478bd9Sstevel@tonic-gate#	but not declared in this file so it is written out to the
857c478bd9Sstevel@tonic-gate#	symbol referenced (symref) file.
867c478bd9Sstevel@tonic-gate#
877c478bd9Sstevel@tonic-gate#
887c478bd9Sstevel@tonic-gate${WHERE}/${PFX}nm -p $* 2>$TDIR/$$tmp | sed -e '/^[ 	]*$/d' -e '
897c478bd9Sstevel@tonic-gate	/^[0-9]* R $/d
90*1b9d0d86SToomas Soome	/ [[:lower:]FLS] /d
917c478bd9Sstevel@tonic-gate	/[^]]:$/{
927c478bd9Sstevel@tonic-gate		s/://
937c478bd9Sstevel@tonic-gate		h
947c478bd9Sstevel@tonic-gate		s/.*/& &/
957c478bd9Sstevel@tonic-gate		p
967c478bd9Sstevel@tonic-gate		d
977c478bd9Sstevel@tonic-gate	}
987c478bd9Sstevel@tonic-gate	/]:$/{
997c478bd9Sstevel@tonic-gate		s/]://
1007c478bd9Sstevel@tonic-gate		s/^.*\[//
1017c478bd9Sstevel@tonic-gate		h
1027c478bd9Sstevel@tonic-gate		s/.*/& &/
1037c478bd9Sstevel@tonic-gate		p
1047c478bd9Sstevel@tonic-gate		d
1057c478bd9Sstevel@tonic-gate	}
1067c478bd9Sstevel@tonic-gate	/ [TDBNCAR] /{
1077c478bd9Sstevel@tonic-gate		s/^.* [TDBNCAR] //
1087c478bd9Sstevel@tonic-gate		G
1097c478bd9Sstevel@tonic-gate		s/\n/ /
1107c478bd9Sstevel@tonic-gate		w '$TDIR/$$symdef'
1117c478bd9Sstevel@tonic-gate		d
1127c478bd9Sstevel@tonic-gate	}
1137c478bd9Sstevel@tonic-gate	/ U /{
1147c478bd9Sstevel@tonic-gate		s/^.* U //
1157c478bd9Sstevel@tonic-gate		G
1167c478bd9Sstevel@tonic-gate		s/\n/ /
1177c478bd9Sstevel@tonic-gate		w '$TDIR/$$symref'
1187c478bd9Sstevel@tonic-gate		d
1197c478bd9Sstevel@tonic-gate	}
1207c478bd9Sstevel@tonic-gate'
1217c478bd9Sstevel@tonic-gateif [ -s $TDIR/$$tmp ]
1227c478bd9Sstevel@tonic-gatethen
1237c478bd9Sstevel@tonic-gate	sed -e "s/^${PFX}nm:/${PFX}lorder:/" < $TDIR/$$tmp >&2
1247c478bd9Sstevel@tonic-gate	rm -f $TDIR/$$symdef $TDIR/$$symref $TDIR/$$tmp
1257c478bd9Sstevel@tonic-gate	exit 1
1267c478bd9Sstevel@tonic-gatefi
1277c478bd9Sstevel@tonic-gatesort $TDIR/$$symdef -o $TDIR/$$symdef
1287c478bd9Sstevel@tonic-gatesort $TDIR/$$symref -o $TDIR/$$symref
1297c478bd9Sstevel@tonic-gatejoin $TDIR/$$symref $TDIR/$$symdef | sed 's/[^ ]* *//'
1307c478bd9Sstevel@tonic-gaterm -f $TDIR/$$symdef $TDIR/$$symref $TDIR/$$tmp
131