From patchwork Wed Aug 31 20:57:08 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Owen Rafferty X-Patchwork-Id: 12961423 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 941B7ECAAD1 for ; Wed, 31 Aug 2022 21:03:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231447AbiHaVDU (ORCPT ); Wed, 31 Aug 2022 17:03:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48466 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229638AbiHaVDU (ORCPT ); Wed, 31 Aug 2022 17:03:20 -0400 Received: from mail.owenrafferty.com (owenrafferty.com [IPv6:2001:19f0:5c01:1f5a:5400:3ff:fe72:9a03]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4D195EB87A for ; Wed, 31 Aug 2022 14:03:19 -0700 (PDT) Received: from localhost (unknown [184.55.81.148]) by mail.owenrafferty.com (Postfix) with ESMTPSA id D9D3C80BD5; Wed, 31 Aug 2022 21:03:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=owenrafferty.com; s=mail; t=1661979798; bh=6Ljx/h0gGyWywnayCOadECUwxuCIZ3QvtWpmprBnYpo=; h=In-Reply-To:References:From:Date:Subject:To:Cc:From; b=d3bEDPXrAreuu6hCaEULKoA8d3qtHqp3YtCSIkm57iVHQSVw3qwj/D1xzQcUqqoOa Lo3ngxK+VueLy33pQO91vgY4ty0VKfA/xhOVdM8UFwDoRTKOy2+/1gPnhEMeNgLFGF Q9F7KR088wKW0XGCOwiYLObUeq8PbTRTRu/xZrNuEF26vQPwToImUgdDA9Zd8jMWY2 QcEENnN5aZsF8q2tCiDqjck8bNOW+bvzu4WL0pnpVf22M3MKuMlWugC1vhuxolWKbM ax2nnf6kuVQNQkRLdoopIhJlQKGDEaaXsum9F7uXvSwFBLKsuZ/MvhpmsXKn/2SN+H +DQvyCy8VcK3A== X-Mailbox-Line: From b0cf49ac1a3ece40e133fa80e3ceae7891d6ed8d Mon Sep 17 00:00:00 2001 Message-Id: In-Reply-To: References: From: Owen Rafferty Date: Wed, 31 Aug 2022 15:57:08 -0500 Subject: [PATCH v4] kbuild: rewrite check-local-export in sh/awk To: masahiroy@kernel.org Cc: linux-kbuild@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kbuild@vger.kernel.org Signed-off-by: Owen Rafferty --- scripts/check-local-export | 96 +++++++++++++++++++------------------- 1 file changed, 47 insertions(+), 49 deletions(-) diff --git a/scripts/check-local-export b/scripts/check-local-export index 6ccc2f467416..0c049ff44aca 100755 --- a/scripts/check-local-export +++ b/scripts/check-local-export @@ -1,26 +1,14 @@ -#!/usr/bin/env bash +#!/bin/sh # SPDX-License-Identifier: GPL-2.0-only # # Copyright (C) 2022 Masahiro Yamada +# Copyright (C) 2022 Owen Rafferty # # Exit with error if a local exported symbol is found. # EXPORT_SYMBOL should be used for global symbols. set -e -# catch errors from ${NM} -set -o pipefail - -# Run the last element of a pipeline in the current shell. -# Without this, the while-loop would be executed in a subshell, and -# the changes made to 'symbol_types' and 'export_symbols' would be lost. -shopt -s lastpipe - -declare -A symbol_types -declare -a export_symbols - -exit_code=0 - # If there is no symbol in the object, ${NM} (both GNU nm and llvm-nm) shows # 'no symbols' diagnostic (but exits with 0). It is harmless and hidden by # '2>/dev/null'. However, it suppresses real error messages as well. Add a @@ -29,43 +17,53 @@ exit_code=0 # TODO: # Use --quiet instead of 2>/dev/null when we upgrade the minimum version of # binutils to 2.37, llvm to 13.0.0. -# Then, the following line will be really simple: -# ${NM} --quiet ${1} | +# Then, the following line will be simpler: +# { ${NM} --quiet ${1} || kill 0; } | + +{ ${NM} ${1} 2>/dev/null || { echo "${0}: ${NM} failed" >&2; kill 0; } } | +${AWK} -v "file=${1}" ' +BEGIN { + i = 0 +} + +# Skip the line if the number of fields is less than 3. +# +# case 1) +# For undefined symbols, the first field (value) is empty. +# The outout looks like this: +# " U _printk" +# It is unneeded to record undefined symbols. +# +# case 2) +# For Clang LTO, llvm-nm outputs a line with type t but empty name: +# "---------------- t" +!length($3) { + next +} -{ ${NM} ${1} 2>/dev/null || { echo "${0}: ${NM} failed" >&2; false; } } | -while read value type name -do - # Skip the line if the number of fields is less than 3. - # - # case 1) - # For undefined symbols, the first field (value) is empty. - # The outout looks like this: - # " U _printk" - # It is unneeded to record undefined symbols. - # - # case 2) - # For Clang LTO, llvm-nm outputs a line with type 't' but empty name: - # "---------------- t" - if [[ -z ${name} ]]; then - continue - fi +# save (name, type) in the associative array +{ symbol_types[$3]=$2 } - # save (name, type) in the associative array - symbol_types[${name}]=${type} +# append the exported symbol to the array +($3 ~ /^__ksymtab_/) { + export_symbols[i] = $3 + sub(/^__ksymtab_/, "", export_symbols[i]) + i++ +} - # append the exported symbol to the array - if [[ ${name} == __ksymtab_* ]]; then - export_symbols+=(${name#__ksymtab_}) - fi -done +END { + exit_code = 0 + for (j = 0; j < i; ++j) { + name = export_symbols[j] + # nm(3) says "If lowercase, the symbol is usually local" + if (symbol_types[name] ~ /[a-z]/) { + printf "%s: error: local symbol %s was exported\n", + file, name | "cat 1>&2" + exit_code = 1 + } + } -for name in "${export_symbols[@]}" -do - # nm(3) says "If lowercase, the symbol is usually local" - if [[ ${symbol_types[$name]} =~ [a-z] ]]; then - echo "$@: error: local symbol '${name}' was exported" >&2 - exit_code=1 - fi -done + exit exit_code +}' -exit ${exit_code} +exit $?