From patchwork Sun Jun 11 15:50:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahiro Yamada X-Patchwork-Id: 106123 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp2110876vqr; Sun, 11 Jun 2023 09:08:49 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6vf+AESmne5JC+2mPi9DumCis7AFYew2qGfy6oDZgK8cc785Lj8d6o/ADEFuOjdegwi661 X-Received: by 2002:a05:6a20:a104:b0:10b:6b1f:acf1 with SMTP id q4-20020a056a20a10400b0010b6b1facf1mr8030980pzk.29.1686499729099; Sun, 11 Jun 2023 09:08:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686499729; cv=none; d=google.com; s=arc-20160816; b=fziiufX/YOpKcCbEYYrkMYRxMWfByOfX5Uuvl3u6YQyRaydGZpky/7XHP5aYZgfJaP XluYcatBrW/ucX0K6WYqQ36Eyln7bz/VFpShvbksjBomY3uFPBcbUU5Bg290aJAXbD2S Dv4k94AKOZGmn7nZ2CfxEQTvExPgIOUKO4dluvn7Nq/dzaGubYvKzfUXWZbcVvt2n6sI 9Dx083YP9T3+zsZoYmj5XzD+kl3qVVsabOf3gXFVExRS7/fa19cONGjqkqYR3flrWTSd Cd9FXUzNov8rC2wFjtZ68nov/B4UKP9jv8KuPO/0KRKTwkniOLOB4DkWnEZzAF3lkzeX uIdQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=mg0R4R5aRMqGvvIM41MhatSRD96Rr0OzYCGyTHQsqys=; b=K8WiPab4Nbui8RH3MKlqZ7SEmXjcRVHbWG/Q1L1OlCaycOdKsv6Ze9Vbw/ol77uCVg 9i25Q0x8uK6OAb7C1qJPMQWd6q7nwxR3CEgvdzdOt0t5BYnamQyQdy6IluJNbrKNqunQ q9e93cxqJdzxeUetkGS4GML7vIE5I88+ccjwhJZ4CG0Y9vwriZoq62OMi6cWwIKPD8nM w9OADc0Tx2VSe5zuIY/FZVaxukPhVVwIeKr2Yu6dLRkti6v0CbI8hgptZxHz9G8YP1T6 kUMSVjuEEz7VcGtIk7F5p1fPm99X3vm/3yW1ilhI1J7GV6CLKUsCzbumsU0j7H9+NzPf zxkw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=bImMTjKr; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o6-20020a639206000000b00542924cbf7bsi5762996pgd.233.2023.06.11.09.08.36; Sun, 11 Jun 2023 09:08:49 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=bImMTjKr; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233280AbjFKPve (ORCPT + 99 others); Sun, 11 Jun 2023 11:51:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56880 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233362AbjFKPvX (ORCPT ); Sun, 11 Jun 2023 11:51:23 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DC46110DD; Sun, 11 Jun 2023 08:51:18 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 4710D6111F; Sun, 11 Jun 2023 15:51:18 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 171EFC433A1; Sun, 11 Jun 2023 15:51:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1686498677; bh=y6jqiQg293+aEfQ+v45/BZNtXoJs+5js+pYlS3odLxE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=bImMTjKrRkjZ4w/6d128wuRwTQ4IMqkvaPX4WPj8/Fr8Jul/PlaDBEhN/RJLa8p/k /5fYLUop8t1QqLAafST+MCmP6Olfau1VxJmJQX/nXL59Z+RksK6wX2AEEwn0scTKts z+2BYMGsooOtF9g/okuwmhyfaj+D6M3vP7qm0XQQQeTogelZLsbzTGWdHFk81cHJRi fxH2woO6YiWxONOiOscn8nja/xTUYU5pwFQrmtY5X6Pw263/3vM+kdwb0bEq+c9q53 tzQGbGRq0WTH/WQX4MV7FpCWqN2jxZq0q8J9wIOdpuotAQOTlXRhPxQeaxcXhgNyr/ sQrxsHStMCYIw== From: Masahiro Yamada To: linux-kbuild@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Nathan Chancellor , Nick Desaulniers , Nicolas Schier , linux-snps-arc@lists.infradead.org, Masahiro Yamada Subject: [PATCH v9 05/11] modpost: check static EXPORT_SYMBOL* by modpost again Date: Mon, 12 Jun 2023 00:50:54 +0900 Message-Id: <20230611155100.2553804-6-masahiroy@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230611155100.2553804-1-masahiroy@kernel.org> References: <20230611155100.2553804-1-masahiroy@kernel.org> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1768423139946984514?= X-GMAIL-MSGID: =?utf-8?q?1768423139946984514?= Commit 31cb50b5590f ("kbuild: check static EXPORT_SYMBOL* by script instead of modpost") moved the static EXPORT_SYMBOL* check from the mostpost to a shell script because I thought it must be checked per compilation unit to avoid false negatives. I came up with an idea to do this in modpost, against combined ELF files. The relocation entries in ELF will find the correct exported symbol even if there exist symbols with the same name in different compilation units. Again, the same sample code. Makefile: obj-y += foo1.o foo2.o foo1.c: #include static void foo(void) {} EXPORT_SYMBOL(foo); foo2.c: void foo(void) {} Then, modpost can catch it correctly. MODPOST Module.symvers ERROR: modpost: vmlinux: local symbol 'foo' was exported Signed-off-by: Masahiro Yamada Reviewed-by: Nick Desaulniers --- (no changes since v6) Changes in v6: - Make the symbol name in the warning more precise scripts/Makefile.build | 4 --- scripts/check-local-export | 70 -------------------------------------- scripts/mod/modpost.c | 7 ++++ 3 files changed, 7 insertions(+), 74 deletions(-) delete mode 100755 scripts/check-local-export diff --git a/scripts/Makefile.build b/scripts/Makefile.build index 4119e737fe87..210142c3ff00 100644 --- a/scripts/Makefile.build +++ b/scripts/Makefile.build @@ -222,8 +222,6 @@ cmd_gen_ksymdeps = \ $(CONFIG_SHELL) $(srctree)/scripts/gen_ksymdeps.sh $@ >> $(dot-target).cmd endif -cmd_check_local_export = $(srctree)/scripts/check-local-export $@ - ifneq ($(findstring 1, $(KBUILD_EXTRA_WARN)),) cmd_warn_shared_object = $(if $(word 2, $(modname-multi)),$(warning $(kbuild-file): $*.o is added to multiple modules: $(modname-multi))) endif @@ -231,7 +229,6 @@ endif define rule_cc_o_c $(call cmd_and_fixdep,cc_o_c) $(call cmd,gen_ksymdeps) - $(call cmd,check_local_export) $(call cmd,checksrc) $(call cmd,checkdoc) $(call cmd,gen_objtooldep) @@ -243,7 +240,6 @@ endef define rule_as_o_S $(call cmd_and_fixdep,as_o_S) $(call cmd,gen_ksymdeps) - $(call cmd,check_local_export) $(call cmd,gen_objtooldep) $(call cmd,gen_symversions_S) $(call cmd,warn_shared_object) diff --git a/scripts/check-local-export b/scripts/check-local-export deleted file mode 100755 index 86ad94647164..000000000000 --- a/scripts/check-local-export +++ /dev/null @@ -1,70 +0,0 @@ -#!/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 -pid=$$ - -# 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 -# hand-crafted error message here. -# -# 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 simpler: -# { ${NM} --quiet ${1} || kill 0; } | - -{ ${NM} ${1} 2>/dev/null || { echo "${0}: ${NM} failed" >&2; kill $pid; } } | -${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 -} - -# save (name, type) in the associative array -{ symbol_types[$3]=$2 } - -# append the exported symbol to the array -($3 ~ /^__export_symbol_.*/) { - export_symbols[i] = $3 - sub(/^__export_symbol_/, "", export_symbols[i]) - i++ -} - -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 - } - } - - exit exit_code -}' - -exit $? diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c index bdf4244da993..10da82ad5874 100644 --- a/scripts/mod/modpost.c +++ b/scripts/mod/modpost.c @@ -1210,6 +1210,13 @@ static void check_export_symbol(struct module *mod, struct elf_info *elf, return; } + if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL && + ELF_ST_BIND(sym->st_info) != STB_WEAK) { + error("%s: local symbol '%s' was exported\n", mod->name, + label_name + strlen(prefix)); + return; + } + name = sym_name(elf, sym); if (strcmp(label_name + strlen(prefix), name)) { error("%s: .export_symbol section references '%s', but it does not seem to be an export symbol\n",