Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp107415rwl; Tue, 28 Mar 2023 22:01:11 -0700 (PDT) X-Google-Smtp-Source: AKy350ZrcSUPnqoEAbiPtDcU+DZGImER5T/rPY5ap2NpQVX20rWqSU3yE7clXtgGoSGlsJtXfV2k X-Received: by 2002:a17:906:e112:b0:933:1b05:8851 with SMTP id gj18-20020a170906e11200b009331b058851mr17368379ejb.16.1680066071491; Tue, 28 Mar 2023 22:01:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680066071; cv=none; d=google.com; s=arc-20160816; b=Yk0nrWAwGeynHS8wPiR5a2IyrJ+azRo/9Mb05CRtOiGLpzigzoRZDnYyielbqjJ6fl y4dLcZwaop7vOEgoKri5yXrcb8fV0Q/0u3XNu4HwjePw2BdzTRiljEu0crf+Zaic9OAH 8+n8uvlduVk6m0ZomEmbBiw9ISmPAs1OBXXk9OL6RaB4XWj1wSNFHR52dSxyX0+DcaQo FsL7Heg6sDogq+1L8Jx12Ue73wum9eqq8ZBOqw9zZZcZUhL4eko78yYsVTq4e2/0fMD3 X75sBm5TKcnrl25KhkTQfEB7stUgONVXtCYq/C3xfN0JYZhoguVKO0ACl203v+m1YKWD cgdw== 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=ZSs9kTLEr3gcw2nAKoZU9JNtCpmjcd0KenqM1ZfQgo4=; b=nV1qqOjNM1YCAyf24vMEm35w7xpmD7mLqMUR1bI7+KZxkrDr/c7c4CBKseh0oNjtHN tREEqg+Kpr9eEfZNsSz+Atyz9mO4zxkEQqgvPrCUb/PxaOlsufiqGJOUkRNIxBBJr0nA n9aTcIWW5HLp9e0Htji3Ke1hXMFCJcG8OaS+f7j9cHodRKmETT1lFRWsdJCd50jxWA4E x5wBb/IcpCawcOiEoblnHQi23aU24imTPCZ0rqkG7m9Idf/7vvEx0p3J+vmlwsyGFNP9 yofRGaI1tHnlrinE8rX7eBT0EmUlrPEVgIj2E7cx3VE2Kgf9PoIQUeHXlWwVvZt5qVpn 94zQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b=lO1y4yq1; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id tx26-20020a1709078e9a00b0093348efaf0asi13390675ejc.198.2023.03.28.22.00.46; Tue, 28 Mar 2023 22:01:11 -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=@rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b=lO1y4yq1; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229436AbjC2E6o (ORCPT + 99 others); Wed, 29 Mar 2023 00:58:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42430 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229452AbjC2E6n (ORCPT ); Wed, 29 Mar 2023 00:58:43 -0400 Received: from mail-wr1-x42e.google.com (mail-wr1-x42e.google.com [IPv6:2a00:1450:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9DEC62723 for ; Tue, 28 Mar 2023 21:58:40 -0700 (PDT) Received: by mail-wr1-x42e.google.com with SMTP id q19so11256231wrc.5 for ; Tue, 28 Mar 2023 21:58:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20210112.gappssmtp.com; s=20210112; t=1680065919; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ZSs9kTLEr3gcw2nAKoZU9JNtCpmjcd0KenqM1ZfQgo4=; b=lO1y4yq1PwBxe+8AJb6u6BDoMPFaCMy/BL0GvwPGmyd2EIze0IcibTOK/QPflRVmnA hFY/Ukba0Dp3YB+iOiOOfTTbrDUDqxk57w8qLlZaBwAVPw6orKn+jDfJTUabT0vjO0Gs MfPreJqaqCjbjpaep9XRh3Fz1bImQeDyizQ6Au5kaxkj2sVzhTrc2F4yTwlM9Y6JNti6 3AGBBA0b/J0E8pFGlRtm4995Qb3cbWvFO5oIp26KsHCaNh2+toBq9Ct/1yIrbic4HaBy WkA3LLbqfJ8UkkYCF0H5KXjY5f/NBPGG0LChQRG7/6oYxj54WKXo7p7eTNs+mi7QSLNx xG8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680065919; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ZSs9kTLEr3gcw2nAKoZU9JNtCpmjcd0KenqM1ZfQgo4=; b=XXG6HSVjcLBiMpu5RLzsYUH9Nyo4plspnYjvc9Jj3B6FMxEk2K3WTAjn1o7BJ7essb YkqUXIjMFlClsQTqTmd43vSIx67faA4uE4g88AqSGmvEO2pdbhqV1S8Sen1CXOT2uUow azixHeSArgWQyEceA0eOJqeUl3G3m/C8kKLalwNhtpo8tEAjICEjfM/HowV4qjKKf+ki 5NQVL5y5iTq1/K6bRkHxvEAI/8lso6nVVnmhZ1/kL2o/i8k8fotIAziuC7dzOj2y2iQ2 qjDuE6fro681d7IAwCkYnh/8RigdTpBU32Lkc6Q8gWOQ5pTkFprTlMNxNSBB0RLKKnTb f/rA== X-Gm-Message-State: AAQBX9ciJdZ+JQVWN+EI7QRVc9HQCiTkY2SZBZJrjGhRgbh4yAeZPHb0 NBSdGzeLOtvafq4hpsz/LTSdZQ== X-Received: by 2002:adf:eace:0:b0:2cf:e3d0:2a43 with SMTP id o14-20020adfeace000000b002cfe3d02a43mr16161943wrn.4.1680065919124; Tue, 28 Mar 2023 21:58:39 -0700 (PDT) Received: from alex-rivos.home (amontpellier-656-1-456-62.w92-145.abo.wanadoo.fr. [92.145.124.62]) by smtp.gmail.com with ESMTPSA id p12-20020a5d48cc000000b002d431f61b18sm27284287wrs.103.2023.03.28.21.58.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Mar 2023 21:58:38 -0700 (PDT) From: Alexandre Ghiti To: Nick Desaulniers , =?UTF-8?q?Bj=C3=B6rn=20T=C3=B6pel?= , Michael Ellerman , Nicholas Piggin , Christophe Leroy , Paul Walmsley , Palmer Dabbelt , Albert Ou , linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kbuild@vger.kernel.org Cc: Alexandre Ghiti , Anup Patel Subject: [PATCH v9 5/6] riscv: Check relocations at compile time Date: Wed, 29 Mar 2023 06:53:28 +0200 Message-Id: <20230329045329.64565-6-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20230329045329.64565-1-alexghiti@rivosinc.com> References: <20230329045329.64565-1-alexghiti@rivosinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=0.0 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=unavailable 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 From: Alexandre Ghiti Relocating kernel at runtime is done very early in the boot process, so it is not convenient to check for relocations there and react in case a relocation was not expected. There exists a script in scripts/ that extracts the relocations from vmlinux that is then used at postlink to check the relocations. Signed-off-by: Alexandre Ghiti Reviewed-by: Anup Patel --- arch/riscv/Makefile.postlink | 36 ++++++++++++++++++++++++++++++++ arch/riscv/tools/relocs_check.sh | 26 +++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 arch/riscv/Makefile.postlink create mode 100755 arch/riscv/tools/relocs_check.sh diff --git a/arch/riscv/Makefile.postlink b/arch/riscv/Makefile.postlink new file mode 100644 index 000000000000..d5de8d520d3e --- /dev/null +++ b/arch/riscv/Makefile.postlink @@ -0,0 +1,36 @@ +# SPDX-License-Identifier: GPL-2.0 +# =========================================================================== +# Post-link riscv pass +# =========================================================================== +# +# Check that vmlinux relocations look sane + +PHONY := __archpost +__archpost: + +-include include/config/auto.conf +include $(srctree)/scripts/Kbuild.include + +quiet_cmd_relocs_check = CHKREL $@ +cmd_relocs_check = \ + $(CONFIG_SHELL) $(srctree)/arch/riscv/tools/relocs_check.sh "$(OBJDUMP)" "$(NM)" "$@" + +# `@true` prevents complaint when there is nothing to be done + +vmlinux: FORCE + @true +ifdef CONFIG_RELOCATABLE + $(call if_changed,relocs_check) +endif + +%.ko: FORCE + @true + +clean: + @true + +PHONY += FORCE clean + +FORCE: + +.PHONY: $(PHONY) diff --git a/arch/riscv/tools/relocs_check.sh b/arch/riscv/tools/relocs_check.sh new file mode 100755 index 000000000000..baeb2e7b2290 --- /dev/null +++ b/arch/riscv/tools/relocs_check.sh @@ -0,0 +1,26 @@ +#!/bin/sh +# SPDX-License-Identifier: GPL-2.0-or-later +# Based on powerpc relocs_check.sh + +# This script checks the relocations of a vmlinux for "suspicious" +# relocations. + +if [ $# -lt 3 ]; then + echo "$0 [path to objdump] [path to nm] [path to vmlinux]" 1>&2 + exit 1 +fi + +bad_relocs=$( +${srctree}/scripts/relocs_check.sh "$@" | + # These relocations are okay + # R_RISCV_RELATIVE + grep -F -w -v 'R_RISCV_RELATIVE' +) + +if [ -z "$bad_relocs" ]; then + exit 0 +fi + +num_bad=$(echo "$bad_relocs" | wc -l) +echo "WARNING: $num_bad bad relocations" +echo "$bad_relocs" -- 2.37.2