Received: by 2002:a25:2c96:0:0:0:0:0 with SMTP id s144csp182154ybs; Sun, 24 May 2020 01:59:42 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwCxhGGcwBQMDOIvZPJeBpimaxe0wO17Nb77tHjoXhOMB8iLwqz/zL/Ha7Ez2phDocpUjcF X-Received: by 2002:a17:906:830f:: with SMTP id j15mr13983057ejx.451.1590310782151; Sun, 24 May 2020 01:59:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1590310782; cv=none; d=google.com; s=arc-20160816; b=hbDWb9g7Lvj/7a38Sm9j5UlcCdrnWQ1TgLgfM3yFlFldPF42MhR0kC/iV9TeZm9Fr2 pLTYL3jgEuZflnPdV5IsoJ7NyoKXB5SV+fwCQCsw/2Gg5tMuUc66vvoFgX2c+Poz3sCX 63MYk7UvlUEuqtCcptzhWXE7UeiTA8YrAfsk8fViDjM5hheIFV+qbbWkKsV7PzNOCVtw wp8M2DVDPUMMK7MksdnKx1GnIeLR4TjmG1cPWpq4uB6IXLPeFAs/eA6rv1L6KynIKK3q 6qRBECMkDYOuTrOmoWU+q4y6UVidgbO3gSLBvbSLgKujzLQj3NzvHzIhWFSR9cAfjImS CX4A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=1QdhIgaEUrmkhEZ9MQc+WXh9TwOaTHrN2Thsu2B6/lg=; b=NIxX/fHEMerSvxvFtt94h3Ou3mV/dotGmhZYOBmKtuUYT2FsEs1yzRV+IQcfvrZ5GP XP1pOkH/e6miyZ9gmjqm684tXVXtWYHUSB5CbV5MSHBxIbfvVPjxYW3WeSeN2V5SONWd C6dX9zZ95guI1Cq1nHiRlFlUJBdoyHDSfeyx787t/rOxSBZHhiAt4mpE/e1c0O8jaL1b YAyNfXG4m3wme/j+ha3TyzyQeyYKHeKSbvu1aA0IyO+x7W0UFwX53c6429A9ixlx3ALN tjaWb3u9CCBgJN1OpQ3WXmUkDTEL0BJ/M2CTTiQXLM9kUqlKbTuJSb8LN8gptS32Y8+Y Lgrw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d22si3305201edx.407.2020.05.24.01.59.18; Sun, 24 May 2020 01:59:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728860AbgEXI4b (ORCPT + 99 others); Sun, 24 May 2020 04:56:31 -0400 Received: from relay10.mail.gandi.net ([217.70.178.230]:46661 "EHLO relay10.mail.gandi.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727848AbgEXI4b (ORCPT ); Sun, 24 May 2020 04:56:31 -0400 Received: from localhost.localdomain (lfbn-gre-1-325-105.w90-112.abo.wanadoo.fr [90.112.45.105]) (Authenticated sender: alex@ghiti.fr) by relay10.mail.gandi.net (Postfix) with ESMTPSA id 673CD240004; Sun, 24 May 2020 08:56:27 +0000 (UTC) From: Alexandre Ghiti To: Michael Ellerman , Benjamin Herrenschmidt , Paul Mackerras , Paul Walmsley , Palmer Dabbelt , Albert Ou , Anup Patel , Atish Patra , Zong Li , linux-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-riscv@lists.infradead.org Cc: Alexandre Ghiti Subject: [PATCH v3 3/3] arch, scripts: Add script to check relocations at compile time Date: Sun, 24 May 2020 04:52:59 -0400 Message-Id: <20200524085259.24784-4-alex@ghiti.fr> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200524085259.24784-1-alex@ghiti.fr> References: <20200524085259.24784-1-alex@ghiti.fr> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. Powerpc architecture has a script that allows to check at compile time for such unexpected relocations: extract the common logic to scripts/ and add arch specific scripts triggered at postlink. At the moment, powerpc and riscv architectures take advantage of this compile-time check. Signed-off-by: Alexandre Ghiti --- arch/powerpc/tools/relocs_check.sh | 18 ++------------- arch/riscv/Makefile.postlink | 36 ++++++++++++++++++++++++++++++ arch/riscv/tools/relocs_check.sh | 26 +++++++++++++++++++++ scripts/relocs_check.sh | 20 +++++++++++++++++ 4 files changed, 84 insertions(+), 16 deletions(-) create mode 100644 arch/riscv/Makefile.postlink create mode 100755 arch/riscv/tools/relocs_check.sh create mode 100755 scripts/relocs_check.sh diff --git a/arch/powerpc/tools/relocs_check.sh b/arch/powerpc/tools/relocs_check.sh index 014e00e74d2b..e367895941ae 100755 --- a/arch/powerpc/tools/relocs_check.sh +++ b/arch/powerpc/tools/relocs_check.sh @@ -15,21 +15,8 @@ if [ $# -lt 3 ]; then exit 1 fi -# Have Kbuild supply the path to objdump and nm so we handle cross compilation. -objdump="$1" -nm="$2" -vmlinux="$3" - -# Remove from the bad relocations those that match an undefined weak symbol -# which will result in an absolute relocation to 0. -# Weak unresolved symbols are of that form in nm output: -# " w _binary__btf_vmlinux_bin_end" -undef_weak_symbols=$($nm "$vmlinux" | awk '$1 ~ /w/ { print $2 }') - bad_relocs=$( -$objdump -R "$vmlinux" | - # Only look at relocation lines. - grep -E '\&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" diff --git a/scripts/relocs_check.sh b/scripts/relocs_check.sh new file mode 100755 index 000000000000..137c660499f3 --- /dev/null +++ b/scripts/relocs_check.sh @@ -0,0 +1,20 @@ +#!/bin/sh +# SPDX-License-Identifier: GPL-2.0-or-later + +# Get a list of all the relocations, remove from it the relocations +# that are known to be legitimate and return this list to arch specific +# script that will look for suspicious relocations. + +objdump="$1" +nm="$2" +vmlinux="$3" + +# Remove from the possible bad relocations those that match an undefined +# weak symbol which will result in an absolute relocation to 0. +# Weak unresolved symbols are of that form in nm output: +# " w _binary__btf_vmlinux_bin_end" +undef_weak_symbols=$($nm "$vmlinux" | awk '$1 ~ /w/ { print $2 }') + +$objdump -R "$vmlinux" | + grep -E '\