Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp922508rwd; Sun, 14 May 2023 08:56:26 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5JS+8MfImfPHELpAnalOVx/bGbS/qlDJzWrxga3dsP3ky96UawK+tf3mkoAQgYbAe3Buij X-Received: by 2002:a17:90a:13ce:b0:24e:3ed9:23a1 with SMTP id s14-20020a17090a13ce00b0024e3ed923a1mr31920889pjf.28.1684079786118; Sun, 14 May 2023 08:56:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684079786; cv=none; d=google.com; s=arc-20160816; b=EOHdYbpAVNbABAQbFFT5L9BAxo6tgStqIMNtyOXEtOMjHajBcG+YAUUaePoILPnqc2 xjZbXlp/tU+NRIzhRFbcK7c1lqcHQbSoAIRjH6CB4LEAkDsmbTMhHTICS6DfNvtDVw/F kSRAk+g/gJ86iFJ8EiBbAkkEbnlEb3SLFLc5fB308ocSSCiM8rcslSVn+z9cN2GqPvJJ FakQWLfG1gCSxbnbuuNe8Rlm9uV8YqyTEmpPjON++6AZjiWdt0F3h5hERnt6zG9MBmqA nCnyPZOA4FBFwxwAYS/u2+6hjkz3mY1IeNQkCy50QfwBHfJU8os/LiPNOiEpD9oLDfxR mhIg== 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=CIbp4oE5g60IYs3skHK+pUIC3MXSRt/nLgP7Z03+XQw=; b=fGVZzzW4EQ3qS96jf5Gc+TrUf55+GoFMis7Zy8LLz6EJzyFKPu4SDpgoj+REBV6Uug R0b0lGF3FifXyI7EfesqecmCMdxDnxjsC5sSoHmvS70u6cO8MEMJ/yCRtxVUbPPMwTwV Gd/g4oPVUc+aLoBifr3IBFJPIgUhshVORNRK3eGEjfFfIWQ85MTefG4JVcR58YeLNhji YtKuRd3Yh6KAtML3P/wkXq6bvFzQay5rBMFd55U7pX+KqEerdm42ndykaYV1Ff80K36J rP9W09ssq4nzZStMI1fLqPeM6jkek0v8VkF97mO8TgntqWsn4Kps8EMA+ohVULIjwnOu wFWQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=rdtC4hRL; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c20-20020a17090a8d1400b0024df69fdff6si27527242pjo.168.2023.05.14.08.56.12; Sun, 14 May 2023 08:56:26 -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=rdtC4hRL; 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 S233729AbjENPag (ORCPT + 99 others); Sun, 14 May 2023 11:30:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48002 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237874AbjENPaK (ORCPT ); Sun, 14 May 2023 11:30:10 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 07C9159FD; Sun, 14 May 2023 08:29:29 -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 BBD0761136; Sun, 14 May 2023 15:28:26 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 724BBC4339E; Sun, 14 May 2023 15:28:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1684078106; bh=NQPHya/XkKWgqv7ysb2b2k6A2wvOILsQgA9vkgGuaWc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rdtC4hRLfqJC3F+umQoUAQfleN7GUOFgzapsmCKQb/inu+xDcRE+exjflhJcdcIfV RB4Uo1yCOTzX25wQspKgOC2AjJ2Bs9OnS67igY+GzG70Uf5jPewhe1sqWcVZjlwyDS SypRXOn2x8zcfOj4NMALtT8+mKPKW269/IO+drMICgSFMcZxjnkRWJ4sSK4zMOYsJz PUOmR/x0drufHK7khgOCZ7c6zwqlVB/siq3EopqR4JnVNu+0T1xjLHfzGkMpapNhF6 Tc8jG4PUwJzlmCWs0vcIPIcVPbJ2GQg8VvEwdwVutJK+k5IHEDbj/NO6/aJcWTRiUO SJKPR3dv691bg== From: Masahiro Yamada To: linux-kbuild@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Nathan Chancellor , Nick Desaulniers , Nicolas Pitre , Nicolas Schier , Masahiro Yamada Subject: [PATCH v5 18/21] modpost: check static EXPORT_SYMBOL* by modpost again Date: Mon, 15 May 2023 00:27:36 +0900 Message-Id: <20230514152739.962109-19-masahiroy@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230514152739.962109-1-masahiroy@kernel.org> References: <20230514152739.962109-1-masahiroy@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, 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 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 --- scripts/Makefile.build | 4 --- scripts/check-local-export | 70 -------------------------------------- scripts/mod/modpost.c | 6 ++++ 3 files changed, 6 insertions(+), 74 deletions(-) delete mode 100755 scripts/check-local-export diff --git a/scripts/Makefile.build b/scripts/Makefile.build index 6bf026a304e4..bd4123795299 100644 --- a/scripts/Makefile.build +++ b/scripts/Makefile.build @@ -220,8 +220,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 @@ -229,7 +227,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) @@ -241,7 +238,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 969a313b9299..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_(gpl)?_.*/) { - export_symbols[i] = $3 - sub(/^__export_symbol_(gpl)?_/, "", 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 921a61fea0b4..5db6a6f76971 100644 --- a/scripts/mod/modpost.c +++ b/scripts/mod/modpost.c @@ -1259,6 +1259,12 @@ 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, name); + return; + } + s = sym_add_exported(name, mod, is_gpl); sym_update_namespace(name, sym_get_data(elf, label)); -- 2.39.2