Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp390674ybb; Fri, 10 Apr 2020 02:08:10 -0700 (PDT) X-Google-Smtp-Source: APiQypIPbkBITVJPTtl7z7u8YNn8VfixHpY9XDLng689oZ6pk6atNawKXaj4pAB/uqcaRyKK3vDc X-Received: by 2002:ac8:72cd:: with SMTP id o13mr3425378qtp.326.1586509689936; Fri, 10 Apr 2020 02:08:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1586509689; cv=none; d=google.com; s=arc-20160816; b=V3PFt/IHbKCdjkhXIgAgwAAaLovVOsm1jqMMJRJ3sJ32s+1B7GGEUcGngnDZpNGrEf dPcP3s2a/yVPZ3LVUT0zdIBxEJOW6OqMqdbnQVyaZTEqBhAmJAY6fXAGcnuAbTss33sf 1rFny/4ME9BkUxvPM33McId+Jddvk5wrzaE4H+YAvrZ80BYp8KRF1I0ffTuJF786Jguh uFegCUuPvCXuGvfmwZ1dkq9umm83DexpJ194C8eDVpO0nNiMkDfUlq8Q/R7J1QPUSbRJ gZgAYUJVqDle3RrDvO2WMOwkiH2LR010vBzO6//yn+NUva33JM6AzWFPIFxN1KFyOn5O WC7A== 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 :dkim-signature; bh=62B5Q8PAKecr1OoxczSRE3+/srYJQsxZv5OVnvpf2o8=; b=vGiXENBxzqxxkBCYZAEecOGDyRiBWjqPuyTf1YaOzfIL5vEMd839I6rtZ0+Ja5WPCi NQj7B/KK+uEVJTQqM3KTPUHH2taoifh/Y3ZoumN6yF3GqOPjF9obTxQzAD0SMH470cUW rO90jX6LET9a5sZi01VABonDL5h9p/FgLPHc6j6LcHzBcpPNELRnnAfVKaAci/RHD263 PvqJ9K2M7m2OgdllNPx5IlyAD4Ptujm23vGyE/KENngHnSHJgrBDEeOGtaebx93YRCBg fUoVWwEv7oKE9Z64oyMDQzZHqF4dDF821D9S74h+5hTk+n7moX6Z2YtqL+9T1xX55Tcw Jo/A== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@flygoat.com header.s=vultr header.b="drnb/M7b"; 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=fail (p=NONE sp=NONE dis=NONE) header.from=flygoat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id bc1si413232qvb.40.2020.04.10.02.07.53; Fri, 10 Apr 2020 02:08:09 -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=fail header.i=@flygoat.com header.s=vultr header.b="drnb/M7b"; 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=fail (p=NONE sp=NONE dis=NONE) header.from=flygoat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726092AbgDJJGt (ORCPT + 99 others); Fri, 10 Apr 2020 05:06:49 -0400 Received: from vultr.net.flygoat.com ([149.28.68.211]:54510 "EHLO vultr.net.flygoat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725858AbgDJJGs (ORCPT ); Fri, 10 Apr 2020 05:06:48 -0400 Received: from localhost.localdomain (unknown [IPv6:2001:da8:20f:4430:250:56ff:fe9a:7470]) by vultr.net.flygoat.com (Postfix) with ESMTPSA id 87CF12022B; Fri, 10 Apr 2020 09:06:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=flygoat.com; s=vultr; t=1586509609; bh=YG+nx11gNJTjjlaYzycx3RqwGwY+EHpSqBmBtv2jB0Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=drnb/M7bS1Eio+05eLmmuqLyuZWQFJIToLpamLgeatCl3yUfO1oOEYsRTed+08JMf yyZmD694g3xCquT4sXtbvoyfJhHRl8q3gMT6dBq4iQEsRQd0+4+uB0IRUUZ8edxnLz 4EMeNJdPUpyBiwm6CezIbsYKHHN26RRYUQj+Y2GqEu6vf3bUDMu5mRcxxfOJvrbuRj erArh4LUucuhswpIU9S8nRwMou7JCHnBsXcCserLHBYPNaRl+qK8JZc+RclvjGdaTY HyEZbd64HhNFXvGPoSN81dwu1AcLivt5jkw6pN66b17XVbCAqF0C4qAxmMxFbNhwfy iQNP84J15M/Hw== From: Jiaxun Yang To: linux-mips@vger.kernel.org Cc: macro@linux-mips.org, Jiaxun Yang , Fangrui Song , Nathan Chancellor , Thomas Bogendoerfer , Paul Burton , Borislav Petkov , Kees Cook , Heiko Carstens , Masahiro Yamada , Greg Kroah-Hartman , linux-kernel@vger.kernel.org, clang-built-linux@googlegroups.com Subject: [PATCH v3] MIPS: Truncate link address into 32bit for 32bit kernel Date: Fri, 10 Apr 2020 17:06:23 +0800 Message-Id: <20200410090634.3513101-1-jiaxun.yang@flygoat.com> X-Mailer: git-send-email 2.26.0.rc2 In-Reply-To: <20200407080611.859256-1-jiaxun.yang@flygoat.com> References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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)) +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 +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