From nobody Wed Feb 28 15:44:33 2024 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4TlJZ96Cs6z5Bp3j; Wed, 28 Feb 2024 15:44:33 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4TlJZ95Rglz4tNq; Wed, 28 Feb 2024 15:44:33 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1709135073; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=dZK0LmntiEoOsI00dp4/rSPeHUSifSYjWDh80PCpQFU=; b=ftlc8ECO6sqGE8apWWweiRvsb0FgiWAraWLWPqNw1fYmD5W36Vd5fun86UQ0+ORgv7Cd1u bDAlAEaNpsfg5xu1DJAqAVkhzEO/Vwtdk2RDd92QP6gSZqiOfPSf5rdjMpSVk1qMMZkBjH jURi9JgZZOya8/jsC9MDFzHp+VuQSh92iqhQYJvff+1whRKxxjJOzUD+mwQe/FsT2q7BcN Jy7nCH0518rpEyoLJcOTyFZZQ0ycub2OUcxaWw2lcdvcl9MfjzT0Q8EOuM0Q4n6xeCVFan YcW2cCovoWHHeCSMiDv9tc4yifUlwMOJJQrXltqR+Oa4W9/JS/Upfw0h913pbQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1709135073; a=rsa-sha256; cv=none; b=sM+QMwVxwhT0amolT9c9SgYChlhkm3NthVkfSpCb2p48gOkqkLC6Czlk3NY4YIUdx9JZ0+ VRjolquohS7xRQvCqLVhcBUrjnqjwjiZ+q8w3bvUY5dXS6u8QK/4q4TuwXqlaWROXdA/EQ kvTISp3KDww7vXpAOnvWck8mpoOkcXE8R5f3XNpo0YslFvRsBL2PG+0/o5QolZCls2rcTZ PPWq12OjIsbmoo4Cgo4y2CU+pAV+jY4NET+JPlqEAsDtkTjRDr+mJTi/v5ov4gQ/g6o76r tkGHcSRsfTbANaPZnzJZwtp3hDv8/ljP43nfd8pumNfukjqbseKVy6KMwRap/Q== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1709135073; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=dZK0LmntiEoOsI00dp4/rSPeHUSifSYjWDh80PCpQFU=; b=jInGAVdXHC9w8H5/4YFMHxETLCeU5xwBu4xaunD4RjtvAeGY1Od5LVcClbW1Zabjijx5oA 0/ep1b0V4YdfO/jlMV6BnYW24n2epD4dI5EA426H3/GWSrXi3rCo0igoieRz8xFu0hk21V 1rYEJbgDz7alc+HXUoeLvjmzPDeymDp+eeYlx+cn9sSE/pT5yADUhirsj2rK0jdle7pLPU st5ZQTzOuMSdGUVkHAvcvnYe/D0IPDL7tHT2/VQ5yDe6MThkXzPfZAy0GtAcYk2qreFp13 IqBHqrwA+ygvMZHIyd0pggopE5wk++l9Kg/YsHtHXhn1N0PsBe0qaobmb74Rrw== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4TlJZ94B42z13S6; Wed, 28 Feb 2024 15:44:33 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 41SFiX6F017186; Wed, 28 Feb 2024 15:44:33 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 41SFiXMu017183; Wed, 28 Feb 2024 15:44:33 GMT (envelope-from git) Date: Wed, 28 Feb 2024 15:44:33 GMT Message-Id: <202402281544.41SFiXMu017183@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Dag-Erling =?utf-8?Q?Sm=C3=B8rgrav?= Subject: git: 5c7b986c21ed - main - lorder: Clean up and improve robustness. List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: des X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 5c7b986c21ed47545203e8f630fe2281b83112d2 Auto-Submitted: auto-generated The branch main has been updated by des: URL: https://cgit.FreeBSD.org/src/commit/?id=5c7b986c21ed47545203e8f630fe2281b83112d2 commit 5c7b986c21ed47545203e8f630fe2281b83112d2 Author: Dag-Erling Smørgrav AuthorDate: 2024-02-28 15:37:36 +0000 Commit: Dag-Erling Smørgrav CommitDate: 2024-02-28 15:37:36 +0000 lorder: Clean up and improve robustness. * Properly parse (no) command-line options. * Ensure that errors go to stderr and result in a non-zero exit. * Drop the special-case code for a single argument, as it will produce the wrong outcome if the file does not exist or is corrupted. * Don't print anything until after we've collected all the data. * Always create all temporary files before setting the trap. This ensures that the trap can safely fire at any moment, regardless of any previous definition of `T`. * Use a temporary file rather than a pipe between `nm` and `sed` to ensure proper termination if `nm` fails due to a missing or invalid input. * The check for self-referential entries was conditional on testing our argument list against a regex looking for archives. This was a needless and unreliable optimization; make the check unconditional. * Document that lorder will not work properly if any of its inputs have spaces in their name. MFC after: 1 week Sponsored by: Klara, Inc. Reviewed by: allanjude Differential Revision: https://reviews.freebsd.org/D44133 --- usr.bin/lorder/lorder.1 | 22 ++++++------ usr.bin/lorder/lorder.sh | 92 ++++++++++++++++++++++++++++++------------------ 2 files changed, 69 insertions(+), 45 deletions(-) diff --git a/usr.bin/lorder/lorder.1 b/usr.bin/lorder/lorder.1 index b71756b03b49..4c70cd2f4729 100644 --- a/usr.bin/lorder/lorder.1 +++ b/usr.bin/lorder/lorder.1 @@ -25,7 +25,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.Dd March 21, 2023 +.Dd February 27, 2024 .Dt LORDER 1 .Os .Sh NAME @@ -44,13 +44,10 @@ The .Nm utility uses .Xr nm 1 -to determine interdependencies in the list of object files -and library archives -specified on the command line. -The -.Nm -utility outputs a list of file names where the first file contains a symbol -which is defined by the second file. +to determine interdependencies between object files and library +archives listed on its command line. +It then outputs a list of pairs of file names such that the first file +in each pair references at least one symbol defined by the second. .Pp The output is normally used with .Xr tsort 1 @@ -58,11 +55,11 @@ when a library is created to determine the optimum ordering of the object modules so that all references may be resolved in a single pass of the loader. .Pp -When linking static binaries, +Similarly, when linking static binaries, .Nm and .Xr tsort 1 -can be used to properly order library archives automatically. +can be used to sort libraries in order of dependency. .Pp The use of .Nm @@ -97,3 +94,8 @@ A .Nm utility appeared in .At v7 . +.Sh CAVEATS +The +.Nm +utility will not work properly if given file names with spaces or +newlines in them. diff --git a/usr.bin/lorder/lorder.sh b/usr.bin/lorder/lorder.sh index a24f844e5790..be28eec8b9bc 100644 --- a/usr.bin/lorder/lorder.sh +++ b/usr.bin/lorder/lorder.sh @@ -29,35 +29,50 @@ # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF # SUCH DAMAGE. -# only one argument is a special case, just output the name twice -case $# in - 0) - echo "usage: lorder file ..."; - exit ;; - 1) - echo $1 $1; - exit ;; -esac +export LC_CTYPE=C +export LC_COLLATE=C +set -e -# temporary files +usage() { + echo "usage: lorder file ..." >&2 + exit 1 +} + +while getopts "" opt ; do + case $opt in + *) + usage + ;; + esac +done +shift $(($OPTIND - 1)) +if [ $# -eq 0 ] ; then + usage +fi + +# +# Create temporary files. +# +N=$(mktemp -t _nm_) R=$(mktemp -t _reference_) S=$(mktemp -t _symbol_) +T=$(mktemp -t _temp_) NM=${NM:-nm} -# remove temporary files on HUP, INT, QUIT, PIPE, TERM -trap "rm -f $R $S $T; exit 1" 1 2 3 13 15 - -# make sure all the files get into the output -for i in $*; do - echo $i $i -done +# +# Remove temporary files on termination. +# +trap "rm -f $N $R $S $T" EXIT 1 2 3 13 15 -# if the line has " [RTDW] " it's a globally defined symbol, put it -# into the symbol file. # -# if the line has " U " it's a globally undefined symbol, put it into -# the reference file. -${NM} ${NMFLAGS} -go $* | sed " +# A line matching " [RTDW] " indicates that the input defines a symbol +# with external linkage; put it in the symbol file. +# +# A line matching " U " indicates that the input references an +# undefined symbol; put it in the reference file. +# +${NM} ${NMFLAGS} -go "$@" >$N +sed -e " / [RTDW] / { s/:.* [RTDW] / / w $S @@ -68,21 +83,28 @@ ${NM} ${NMFLAGS} -go $* | sed " w $R } d -" +" <$N -export LC_ALL=C -# eliminate references that can be resolved by the same library. -if [ $(expr "$*" : '.*\.a[[:>:]]') -ne 0 ]; then - sort -u -o $S $S - sort -u -o $R $R - T=$(mktemp -t _temp_) - comm -23 $R $S >$T - mv $T $R -fi +# +# Elide entries representing a reference to a symbol from within the +# library that defines it. +# +sort -u -o $S $S +sort -u -o $R $R +comm -23 $R $S >$T +mv $T $R + +# +# Make sure that all inputs get into the output. +# +for i ; do + echo "$i" "$i" +done -# sort references and symbols on the second field (the symbol), -# join on that field, and print out the file names. +# +# Sort references and symbols on the second field (the symbol), join +# on that field, and print out the file names. +# sort -k 2 -o $R $R sort -k 2 -o $S $S join -j 2 -o 1.1 2.1 $R $S -rm -f $R $S