Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp958615ybb; Fri, 10 Apr 2020 13:46:43 -0700 (PDT) X-Google-Smtp-Source: APiQypJhZ/ygf3ttbjSGsI7P3rrM2FbYrsnkWtFSE4sLcONGnTKh4pE8zL4ZvWdkJPEuhjmEWi1A X-Received: by 2002:a05:620a:1292:: with SMTP id w18mr6048038qki.276.1586551603254; Fri, 10 Apr 2020 13:46:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1586551603; cv=none; d=google.com; s=arc-20160816; b=iZyCZajdmr/Qgogw7/wvha1Ty32/DkVFI7tmoBwT2Q+V6Mx6O0t23OLSffRkYHeMNm lO8PtGfR9//+AQpVkbG0gqT4jDflmTDEr+jTvneSpYvXqYsX2s5k0U/u9m/TwstFTVYX 7J4YQ+xeTAl/4Dqn1zjWuOMJ+FBJXGudMdE1aPRygSDZWh3k4Cugtf/GMx00ir4oaWkQ FZ9t2ASgYAsVfIqSBEoz76ThZUjpWE8bkuHgewpMOcOYYW+qIAUF8sO2pMloRMuXZwIH fcmABm3BYNJMAXnK4q7t6KBj1IN+hOJZJQkHGkuXtCgstln8M1LFcTbdnLQDuOckqv5a hw/A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=iij7zgytwGd4FsiXK28RwDWIfgaOYgN4ssh5i9GAVys=; b=g3jTorU28syiQDdE33zJb1r76a9sChA4pUTfZGBncq3i1C852UR5bintVFGNYTZN7t 4/u8aw2Wxg8Tst2xVV0AhSaxG5xNFSj+EltN01tyGn7tRtiwgI3fyJf3ocqE9iY1nCh4 8eQjm3/kly59a9puF6cjEMtDo//wFheWcXwSmJf8z+mm1QaEurtB2OV+gM2FaLYKNwR7 m3K6k7HC4q+haKGYYr7e6yZMCY2i6sKE2WFZEnEIRVImNFkbReGar8Qw89N1RgFCz26L 3/At5LBjHTw2ahUzJ+pww4Ya2CTkYjjJhWZJcspE6FBMsagmtsyALaLf+ZfXWT9QQzbz M+Fw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=iIXb8hrV; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v137si1937141qkb.289.2020.04.10.13.46.28; Fri, 10 Apr 2020 13:46:43 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=iIXb8hrV; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726703AbgDJUpl (ORCPT + 99 others); Fri, 10 Apr 2020 16:45:41 -0400 Received: from mail-pl1-f193.google.com ([209.85.214.193]:43626 "EHLO mail-pl1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726263AbgDJUpl (ORCPT ); Fri, 10 Apr 2020 16:45:41 -0400 Received: by mail-pl1-f193.google.com with SMTP id z6so1010933plk.10 for ; Fri, 10 Apr 2020 13:45:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=iij7zgytwGd4FsiXK28RwDWIfgaOYgN4ssh5i9GAVys=; b=iIXb8hrVUdblD8Qt9cfCbOMlx7kiRGdihRYhXdGg/tymxxmG3+EhB8U9nNGGPkuY+H +ifeOuvnFBH9TuzI16pZ6VmLe2Z45tOXmGIHdBUWfAAm6rszOQnwIi7qbWKvXAY6slVq +g+dUftt0GmYEgBflilHq/894shvDcwD3244A= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=iij7zgytwGd4FsiXK28RwDWIfgaOYgN4ssh5i9GAVys=; b=LqS9aFVACEF809JBt3FXOixIKQ7BfSLndxzJmi494YqF0nl6YZCU4jtmTrL5PXRLdN u6n/Kxt3oWvJYZ5S0kQ9SgG2UNto3V2hm5oI+aLVDnb8VzY48YeU16CSOoyvx/G1c/lg q/pMhs8WF4dVlBKPyA9LkhSuZ3TmwuFkLJ0elF70e/X2EVeYVrmHTrmDqU1RZMkE0AY3 xeaFfLueNoHcCU5BYQfJ06+TkNVS87YUFv5LfGD0HOqmSCnIxFklHs10fes1oadnYi0h efwdLe3qlEy8UZ1APykf/TuxsJ9A8AABDM0Nsow1apWEttHpWUTRGuMSwpwxBIvcLSfu pZ3Q== X-Gm-Message-State: AGi0PuZ+xJ7IPqhJjeRIL5U+KsvA7Jto5cof1iUIdQ6kRKGboPCRQUB1 UhfnqCqd+kMEtVFguy/3LW0IuA== X-Received: by 2002:a17:90b:8c4:: with SMTP id ds4mr7707878pjb.44.1586551540896; Fri, 10 Apr 2020 13:45:40 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id 80sm2302307pgb.45.2020.04.10.13.45.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Apr 2020 13:45:40 -0700 (PDT) Date: Fri, 10 Apr 2020 13:45:39 -0700 From: Kees Cook To: Jiaxun Yang Cc: linux-mips@vger.kernel.org, macro@linux-mips.org, Fangrui Song , Nathan Chancellor , Thomas Bogendoerfer , Paul Burton , Borislav Petkov , Heiko Carstens , Masahiro Yamada , Greg Kroah-Hartman , linux-kernel@vger.kernel.org, clang-built-linux@googlegroups.com Subject: Re: [PATCH v3] MIPS: Truncate link address into 32bit for 32bit kernel Message-ID: <202004101334.A641706F0@keescook> References: <20200407080611.859256-1-jiaxun.yang@flygoat.com> <20200410090634.3513101-1-jiaxun.yang@flygoat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200410090634.3513101-1-jiaxun.yang@flygoat.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Apr 10, 2020 at 05:06:23PM +0800, Jiaxun Yang wrote: > LLD failed to link vmlinux with 64bit load address for 32bit ELF > while bfd will strip 64bit address into 32bit silently. > To fix LLD build, we should truncate load address provided by platform > into 32bit for 32bit kernel. > > Signed-off-by: Jiaxun Yang > Reviewed-by: Fangrui Song > Tested-by: Nathan Chancellor > > -- > V2: Take MaskRay's shell magic. > > V3: After spent an hour on dealing with special character issue in > Makefile, I gave up to do shell hacks and write a util in C instead. > Thanks Maciej for pointing out Makefile variable problem. > --- > arch/mips/Makefile | 2 ++ > arch/mips/kernel/Makefile | 11 ++++++++++- > arch/mips/kernel/vmlinux.lds.S | 2 +- > arch/mips/tools/.gitignore | 1 + > arch/mips/tools/Makefile | 5 +++++ > arch/mips/tools/truncate32.c | 29 +++++++++++++++++++++++++++++ > 6 files changed, 48 insertions(+), 2 deletions(-) > create mode 100644 arch/mips/tools/truncate32.c > > diff --git a/arch/mips/Makefile b/arch/mips/Makefile > index e1c44aed8156..633e9de4d262 100644 > --- a/arch/mips/Makefile > +++ b/arch/mips/Makefile > @@ -14,6 +14,7 @@ > > archscripts: scripts_basic > $(Q)$(MAKE) $(build)=arch/mips/tools elf-entry > + $(Q)$(MAKE) $(build)=arch/mips/tools truncate32 > ifeq ($(CONFIG_CPU_LOONGSON3_WORKAROUNDS),y) > $(Q)$(MAKE) $(build)=arch/mips/tools loongson3-llsc-check > endif > @@ -261,6 +262,7 @@ include arch/mips/Kbuild.platforms > ifdef CONFIG_PHYSICAL_START > load-y = $(CONFIG_PHYSICAL_START) > endif > +export VMLINUX_LOAD_ADDRESS := $(load-y) > > entry-y = $(shell $(objtree)/arch/mips/tools/elf-entry vmlinux) > cflags-y += -I$(srctree)/arch/mips/include/asm/mach-generic > diff --git a/arch/mips/kernel/Makefile b/arch/mips/kernel/Makefile > index d6e97df51cfb..0178f7085317 100644 > --- a/arch/mips/kernel/Makefile > +++ b/arch/mips/kernel/Makefile > @@ -112,4 +112,13 @@ obj-$(CONFIG_MIPS_CPC) += mips-cpc.o > obj-$(CONFIG_CPU_PM) += pm.o > obj-$(CONFIG_MIPS_CPS_PM) += pm-cps.o > > -CPPFLAGS_vmlinux.lds := $(KBUILD_CFLAGS) > +# When linking a 32-bit executable the LLVM linker cannot cope with a > +# 32-bit load address that has been sign-extended to 64 bits. Simply > +# remove the upper 32 bits then, as it is safe to do so with other > +# linkers. > +ifdef CONFIG_64BIT > + load-ld = $(VMLINUX_LOAD_ADDRESS) > +else > + load-ld = $(shell $(objtree)/arch/mips/tools/truncate32 $(VMLINUX_LOAD_ADDRESS)) This is major overkill. Just use the Makefile's builtin text manipulation: load-ld = $(subst 0xffffffff,0x,$(VMLINUX_LOAD_ADDRESS)) And note that a shell failure here would be entirely ignored, so the use of die() in the proposed helper wouldn't actually stop a build, etc. > +endif > +CPPFLAGS_vmlinux.lds := $(KBUILD_CFLAGS) -DVMLINUX_LINK_ADDRESS=$(load-ld) > diff --git a/arch/mips/kernel/vmlinux.lds.S b/arch/mips/kernel/vmlinux.lds.S > index a5f00ec73ea6..5226cd8e4bee 100644 > --- a/arch/mips/kernel/vmlinux.lds.S > +++ b/arch/mips/kernel/vmlinux.lds.S > @@ -55,7 +55,7 @@ SECTIONS > /* . = 0xa800000000300000; */ > . = 0xffffffff80300000; > #endif > - . = VMLINUX_LOAD_ADDRESS; > + . = VMLINUX_LINK_ADDRESS; > /* read-only */ > _text = .; /* Text and read-only data */ > .text : { > diff --git a/arch/mips/tools/.gitignore b/arch/mips/tools/.gitignore > index 794817dfb389..58ead412c8d3 100644 > --- a/arch/mips/tools/.gitignore > +++ b/arch/mips/tools/.gitignore > @@ -1,3 +1,4 @@ > # SPDX-License-Identifier: GPL-2.0-only > elf-entry > loongson3-llsc-check > +truncate32 > diff --git a/arch/mips/tools/Makefile b/arch/mips/tools/Makefile > index b851e5dcc65a..69debb18bbb4 100644 > --- a/arch/mips/tools/Makefile > +++ b/arch/mips/tools/Makefile > @@ -8,3 +8,8 @@ hostprogs += loongson3-llsc-check > PHONY += loongson3-llsc-check > loongson3-llsc-check: $(obj)/loongson3-llsc-check > @: > + > +hostprogs += truncate32 > +PHONY += truncate32 Isn't the special variable named ".PHONY"? (And also is that only for things the don't get written to disk, but truncate32 is...) > +truncate32: $(obj)/truncate32 > + @: > diff --git a/arch/mips/tools/truncate32.c b/arch/mips/tools/truncate32.c > new file mode 100644 > index 000000000000..82c19b4c32da > --- /dev/null > +++ b/arch/mips/tools/truncate32.c > @@ -0,0 +1,29 @@ > +// SPDX-License-Identifier: GPL-2.0 > +#include > +#include > +#include > + > +__attribute__((noreturn)) > +static void die(const char *msg) > +{ > + fputs(msg, stderr); > + exit(EXIT_FAILURE); > +} > + > +int main(int argc, const char *argv[]) > +{ > + unsigned long long val; > + > + if (argc != 2) > + die("Usage: truncate32
\n"); > + > + val = strtoull(argv[1], NULL, 0); > + > + if ((val & 0xffffffff00000000) != 0xffffffff00000000) > + die("Invalid input\n"); > + > + val = val & 0xffffffff; > + printf("0x%08llx\n", val); > + > + return EXIT_SUCCESS; > +} > -- > 2.26.0.rc2 > -- Kees Cook