From nobody Thu Apr 9 12:54:10 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2A4B2ECAAD2 for ; Mon, 29 Aug 2022 13:35:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229817AbiH2NfK (ORCPT ); Mon, 29 Aug 2022 09:35:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54104 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229577AbiH2NfG (ORCPT ); Mon, 29 Aug 2022 09:35:06 -0400 Received: from xry111.site (xry111.site [IPv6:2001:470:683e::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 212B51EEDD for ; Mon, 29 Aug 2022 06:35:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=xry111.site; s=default; t=1661780102; bh=owF+FNmRrMbe7xv+RTni/1sE0a+t4GDngroii48Nl7g=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=B+8Gj6thkXofgbnv7dQT3fcXFy7j5xLihma+EuHZDfvFU6SOfxdjKb07rD6VCh8Bo OZYHXdnz3DUtAY//u/0bn0eOt9qGtvqNl46BVy7YZjNdgEAkqmcuPMIr3CCZiHel9d F8Nh2zIZkoS6Yuo8nLa6YCgKWP2ZzxhzHUrW2SBs= Received: from xry111-x57s1.. (unknown [IPv6:240e:358:11dd:1900:dc73:854d:832e:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (Client did not present a certificate) (Authenticated sender: xry111@xry111.site) by xry111.site (Postfix) with ESMTPSA id 2F72E66903; Mon, 29 Aug 2022 09:34:56 -0400 (EDT) From: Xi Ruoyao To: loongarch@lists.linux.dev Cc: linux-kernel@vger.kernel.org, WANG Xuerui , Huacai Chen , Youling Tang , Jinyang He , Xi Ruoyao Subject: [PATCH v6 1/6] LoongArch: Add CONFIG_AS_HAS_EXPLICIT_RELOCS Date: Mon, 29 Aug 2022 21:31:41 +0800 Message-Id: <20220829133146.15236-2-xry111@xry111.site> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220829133146.15236-1-xry111@xry111.site> References: <20220829133146.15236-1-xry111@xry111.site> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" GNU as >=3D 2.40 and GCC >=3D 13 will support using explicit relocation hints in the assembly code, instead of la.* macros. The usage of explicit relocation hints can improve code generation so it's enabled by default by GCC >=3D 13. Introduce CONFIG_AS_HAS_EXPLICIT_RELOCS as the switch for "use explicit relocation hints or not." Signed-off-by: Xi Ruoyao --- arch/loongarch/Kconfig | 3 +++ 1 file changed, 3 insertions(+) diff --git a/arch/loongarch/Kconfig b/arch/loongarch/Kconfig index d3ce96a1a744..0721b4b2207a 100644 --- a/arch/loongarch/Kconfig +++ b/arch/loongarch/Kconfig @@ -204,6 +204,9 @@ config SCHED_OMIT_FRAME_POINTER bool default y =20 +config AS_HAS_EXPLICIT_RELOCS + def_bool $(as-instr,x:pcalau12i \$t0$(comma)%pc_hi20(x)) + menu "Kernel type and options" =20 source "kernel/Kconfig.hz" --=20 2.37.0 From nobody Thu Apr 9 12:54:10 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3A44FECAAD5 for ; Mon, 29 Aug 2022 13:35:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229875AbiH2NfP (ORCPT ); Mon, 29 Aug 2022 09:35:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54356 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229827AbiH2NfK (ORCPT ); Mon, 29 Aug 2022 09:35:10 -0400 Received: from xry111.site (xry111.site [89.208.246.23]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7834F24081 for ; Mon, 29 Aug 2022 06:35:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=xry111.site; s=default; t=1661780108; bh=kTsk985O1vybSmvpjcdvVMIIa8361S7JA67NI5+R/ZA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=g/JelLMDgQRXrAiP8mZoOABhFjUQAeubwe+WJazxHrnVrpLcNr4bwmH4m5cU0V3K2 ATqCp04y959YlUbxWF+wTeCPEgZrRcesVJZjUayTeIgpebrqQA7R2aaybkPcewgHCm fh6mEh8YuEerzHNvn/xOx1G4hG1UlExZTkqWjsws= Received: from xry111-x57s1.. (unknown [IPv6:240e:358:11dd:1900:dc73:854d:832e:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (Client did not present a certificate) (Authenticated sender: xry111@xry111.site) by xry111.site (Postfix) with ESMTPSA id 6BB3466904; Mon, 29 Aug 2022 09:35:02 -0400 (EDT) From: Xi Ruoyao To: loongarch@lists.linux.dev Cc: linux-kernel@vger.kernel.org, WANG Xuerui , Huacai Chen , Youling Tang , Jinyang He , Xi Ruoyao Subject: [PATCH v6 2/6] LoongArch: Adjust CFLAGS for CONFIG_AS_HAS_EXPLICIT_RELOCS Date: Mon, 29 Aug 2022 21:31:42 +0800 Message-Id: <20220829133146.15236-3-xry111@xry111.site> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220829133146.15236-1-xry111@xry111.site> References: <20220829133146.15236-1-xry111@xry111.site> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" If explicit relocation hints is used by the toolchain, -Wa,-mla-* options will be useless for C code. Only use them for !CONFIG_AS_HAS_EXPLICIT_RELOCS. Replace "la" with "la.pcrel" in head.S to keep the semantic consistent with new and old toolchains for the low level startup code. Remove -fplt because it's the default for all known LoongArch C compilers. The behavior with different assemblers and compilers are summarized in the following table: AS has CC has explicit reloc explicit reloc Behavior =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D No No Use la.* macros. No change from Linux 6.0. -------------------------------------------------------------- No Yes Disable explicit reloc. No change from Linux 6.0. -------------------------------------------------------------- Yes No Not supported. -------------------------------------------------------------- Yes Yes Use explicit relocs. No -Wa,-mla* options. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Signed-off-by: Xi Ruoyao --- arch/loongarch/Makefile | 19 ++++++++++++++++++- arch/loongarch/kernel/head.S | 10 +++++----- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/arch/loongarch/Makefile b/arch/loongarch/Makefile index 7051a95f7f31..1563747c4fa8 100644 --- a/arch/loongarch/Makefile +++ b/arch/loongarch/Makefile @@ -40,10 +40,27 @@ endif =20 cflags-y +=3D -G0 -pipe -msoft-float LDFLAGS_vmlinux +=3D -G0 -static -n -nostdlib + +# When the assembler supports explicit relocation hint, we must use it. +# GCC may have -mexplicit-relocs off by default if it was built with an old +# assembler, so we force it via an option. +# +# When the assembler does not supports explicit relocation hint, we can't = use +# it. Disable it if the compiler supports it. +# +# If you've seen "unknown reloc hint" message building the kernel and you = are +# now wondering why "-mexplicit-relocs" is not wrapped with cc-option: the +# combination of a "new" assembler and "old" compiler is not supported. E= ither +# upgrade the compiler or downgrade the assembler. +ifdef CONFIG_AS_HAS_EXPLICIT_RELOCS +cflags-y +=3D -mexplicit-relocs +else +cflags-y +=3D $(call cc-option,-mno-explicit-relocs) KBUILD_AFLAGS_KERNEL +=3D -Wa,-mla-global-with-pcrel KBUILD_CFLAGS_KERNEL +=3D -Wa,-mla-global-with-pcrel KBUILD_AFLAGS_MODULE +=3D -Wa,-mla-global-with-abs -KBUILD_CFLAGS_MODULE +=3D -fplt -Wa,-mla-global-with-abs,-mla-local-with-= abs +KBUILD_CFLAGS_MODULE +=3D -Wa,-mla-global-with-abs,-mla-local-with-abs +endif =20 cflags-y +=3D -ffreestanding cflags-y +=3D $(call cc-option, -mno-check-zero-division) diff --git a/arch/loongarch/kernel/head.S b/arch/loongarch/kernel/head.S index 01bac62a6442..eb3f641d5915 100644 --- a/arch/loongarch/kernel/head.S +++ b/arch/loongarch/kernel/head.S @@ -55,17 +55,17 @@ SYM_CODE_START(kernel_entry) # kernel entry point li.w t0, 0x00 # FPE=3D0, SXE=3D0, ASXE=3D0, BTE=3D0 csrwr t0, LOONGARCH_CSR_EUEN =20 - la t0, __bss_start # clear .bss + la.pcrel t0, __bss_start # clear .bss st.d zero, t0, 0 - la t1, __bss_stop - LONGSIZE + la.pcrel t1, __bss_stop - LONGSIZE 1: addi.d t0, t0, LONGSIZE st.d zero, t0, 0 bne t0, t1, 1b =20 - la t0, fw_arg0 + la.pcrel t0, fw_arg0 st.d a0, t0, 0 # firmware arguments - la t0, fw_arg1 + la.pcrel t0, fw_arg1 st.d a1, t0, 0 =20 /* KSave3 used for percpu base, initialized as 0 */ @@ -73,7 +73,7 @@ SYM_CODE_START(kernel_entry) # kernel entry point /* GPR21 used for percpu base (runtime), initialized as 0 */ move u0, zero =20 - la tp, init_thread_union + la.pcrel tp, init_thread_union /* Set the SP after an empty pt_regs. */ PTR_LI sp, (_THREAD_SIZE - 32 - PT_SIZE) PTR_ADD sp, sp, tp --=20 2.37.0 From nobody Thu Apr 9 12:54:10 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4C1A5C0502C for ; Mon, 29 Aug 2022 13:35:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229953AbiH2NfY (ORCPT ); Mon, 29 Aug 2022 09:35:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54952 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229909AbiH2NfT (ORCPT ); Mon, 29 Aug 2022 09:35:19 -0400 Received: from xry111.site (xry111.site [89.208.246.23]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 132904DB7A for ; Mon, 29 Aug 2022 06:35:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=xry111.site; s=default; t=1661780115; bh=TwdAnZTgoj4EYEJwsRSSBSF3le/kWOA9MA+6UkrMJuo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gPriV1snZWAJnOMBQq3lW6JNaPqN66GxTiKaq8D6a7q+9clLZ+x/AOWezXdsBluAB u7XrsElOvTmUXqxuDD4peuLrcadeobkIb2/ndMK8jwsUYSV9ksdsiVre8BEMEQuKo1 puenuWwYkUxIvNDjV66pnQGz7q79LpdYuHDIXIfg= Received: from xry111-x57s1.. (unknown [IPv6:240e:358:11dd:1900:dc73:854d:832e:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (Client did not present a certificate) (Authenticated sender: xry111@xry111.site) by xry111.site (Postfix) with ESMTPSA id A7BF866905; Mon, 29 Aug 2022 09:35:09 -0400 (EDT) From: Xi Ruoyao To: loongarch@lists.linux.dev Cc: linux-kernel@vger.kernel.org, WANG Xuerui , Huacai Chen , Youling Tang , Jinyang He , Xi Ruoyao Subject: [PATCH v6 3/6] LoongArch: Use model("extreme") attribute for per-CPU variables in module if CONFIG_AS_HAS_EXPLICIT_RELOCS Date: Mon, 29 Aug 2022 21:31:43 +0800 Message-Id: <20220829133146.15236-4-xry111@xry111.site> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220829133146.15236-1-xry111@xry111.site> References: <20220829133146.15236-1-xry111@xry111.site> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" On LoongArch, The "address" of a per-CPU variable symbol is actually an offset from $r21. The value is nearing the loading address of main kernel image, but far from the address of modules. We need to tell the compiler that a PC-relative addressing with 32-bit offset is not sufficient for local per-CPU variables. After some discussion with GCC maintainers, we implemented this attribute to indicate that a PC-relative addressing with 64-bit offset should be used. This attribute should be available in GCC 13 release. Some early GCC 13 snapshots may support -mexplicit-relocs but lack this attribute, we simply reject them. Signed-off-by: Xi Ruoyao Link: https://gcc.gnu.org/r13-2199 --- arch/loongarch/Makefile | 3 +++ arch/loongarch/include/asm/percpu.h | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/arch/loongarch/Makefile b/arch/loongarch/Makefile index 1563747c4fa8..593818a61741 100644 --- a/arch/loongarch/Makefile +++ b/arch/loongarch/Makefile @@ -53,6 +53,9 @@ LDFLAGS_vmlinux +=3D -G0 -static -n -nostdlib # combination of a "new" assembler and "old" compiler is not supported. E= ither # upgrade the compiler or downgrade the assembler. ifdef CONFIG_AS_HAS_EXPLICIT_RELOCS +ifeq ($(shell echo '__has_attribute(model)' | $(CC) -E -P - 2> /dev/null),= 0) +$(error "C compiler must support model attribute if using explicit relocs") +endif cflags-y +=3D -mexplicit-relocs else cflags-y +=3D $(call cc-option,-mno-explicit-relocs) diff --git a/arch/loongarch/include/asm/percpu.h b/arch/loongarch/include/a= sm/percpu.h index 0bd6b0110198..dd7fcc553efa 100644 --- a/arch/loongarch/include/asm/percpu.h +++ b/arch/loongarch/include/asm/percpu.h @@ -8,6 +8,14 @@ #include #include =20 +#if defined(MODULE) && defined(CONFIG_AS_HAS_EXPLICIT_RELOCS) +/* The "address" (in fact, offset from $r21) of a per-CPU variable is close + * to the load address of main kernel image, but far from where the module= s are + * loaded. Tell the compiler this fact. + */ +# define PER_CPU_ATTRIBUTES __attribute__((model("extreme"))) +#endif + /* Use r21 for fast access */ register unsigned long __my_cpu_offset __asm__("$r21"); =20 --=20 2.37.0 From nobody Thu Apr 9 12:54:10 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8A1FDECAAD2 for ; Mon, 29 Aug 2022 13:35:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229830AbiH2Nfd (ORCPT ); Mon, 29 Aug 2022 09:35:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54950 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230050AbiH2NfX (ORCPT ); Mon, 29 Aug 2022 09:35:23 -0400 Received: from xry111.site (xry111.site [89.208.246.23]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B69335C9FB for ; Mon, 29 Aug 2022 06:35:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=xry111.site; s=default; t=1661780121; bh=YGYJfp6hCx6eH/Nfs4eCDr4R+EBIr89AN05AxFZ+kgc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HgVw0YARg1ql0VgB+vs4jIpCMh+GAI+g78V/SbMDayWLYuMvp6XH7iHGr5r/qc6Ip yuchfhet/FvBDclKH2lDff/vw6UqOe/EryIa4d3+Zcoh8svkHXaTf2DOiRcewb/Pbc EW3ulmmY+0LeA0tM+/UyY4SMxfgKTJcrdZ1Rs+us= Received: from xry111-x57s1.. (unknown [IPv6:240e:358:11dd:1900:dc73:854d:832e:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (Client did not present a certificate) (Authenticated sender: xry111@xry111.site) by xry111.site (Postfix) with ESMTPSA id A1F9766903; Mon, 29 Aug 2022 09:35:16 -0400 (EDT) From: Xi Ruoyao To: loongarch@lists.linux.dev Cc: linux-kernel@vger.kernel.org, WANG Xuerui , Huacai Chen , Youling Tang , Jinyang He , Xi Ruoyao Subject: [PATCH v6 4/6] LoongArch: Define ELF relocation types added in v2.00 ABI Date: Mon, 29 Aug 2022 21:31:44 +0800 Message-Id: <20220829133146.15236-5-xry111@xry111.site> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220829133146.15236-1-xry111@xry111.site> References: <20220829133146.15236-1-xry111@xry111.site> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" These relocation types are used by GNU binutils >=3D 2.40 and GCC >=3D 13. Add their definitions so we will be able to use them in later patches. Link: https://github.com/loongson/LoongArch-Documentation/pull/57 Signed-off-by: Xi Ruoyao --- arch/loongarch/include/asm/elf.h | 37 ++++++++++++++++++++++++++++++++ arch/loongarch/kernel/module.c | 2 +- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/arch/loongarch/include/asm/elf.h b/arch/loongarch/include/asm/= elf.h index 5f3ff4781fda..7af0cebf28d7 100644 --- a/arch/loongarch/include/asm/elf.h +++ b/arch/loongarch/include/asm/elf.h @@ -74,6 +74,43 @@ #define R_LARCH_SUB64 56 #define R_LARCH_GNU_VTINHERIT 57 #define R_LARCH_GNU_VTENTRY 58 +#define R_LARCH_B16 64 +#define R_LARCH_B21 65 +#define R_LARCH_B26 66 +#define R_LARCH_ABS_HI20 67 +#define R_LARCH_ABS_LO12 68 +#define R_LARCH_ABS64_LO20 69 +#define R_LARCH_ABS64_HI12 70 +#define R_LARCH_PCALA_HI20 71 +#define R_LARCH_PCALA_LO12 72 +#define R_LARCH_PCALA64_LO20 73 +#define R_LARCH_PCALA64_HI12 74 +#define R_LARCH_GOT_PC_HI20 75 +#define R_LARCH_GOT_PC_LO12 76 +#define R_LARCH_GOT64_PC_LO20 77 +#define R_LARCH_GOT64_PC_HI12 78 +#define R_LARCH_GOT_HI20 79 +#define R_LARCH_GOT_LO12 80 +#define R_LARCH_GOT64_LO20 81 +#define R_LARCH_GOT64_HI12 82 +#define R_LARCH_TLS_LE_HI20 83 +#define R_LARCH_TLS_LE_LO12 84 +#define R_LARCH_TLS_LE64_LO20 85 +#define R_LARCH_TLS_LE64_HI12 86 +#define R_LARCH_TLS_IE_PC_HI20 87 +#define R_LARCH_TLS_IE_PC_LO12 88 +#define R_LARCH_TLS_IE64_PC_LO20 89 +#define R_LARCH_TLS_IE64_PC_HI12 90 +#define R_LARCH_TLS_IE_HI20 91 +#define R_LARCH_TLS_IE_LO12 92 +#define R_LARCH_TLS_IE64_LO20 93 +#define R_LARCH_TLS_IE64_HI12 94 +#define R_LARCH_TLS_LD_PC_HI20 95 +#define R_LARCH_TLS_LD_HI20 96 +#define R_LARCH_TLS_GD_PC_HI20 97 +#define R_LARCH_TLS_GD_HI20 98 +#define R_LARCH_32_PCREL 99 +#define R_LARCH_RELAX 100 =20 #ifndef ELF_ARCH =20 diff --git a/arch/loongarch/kernel/module.c b/arch/loongarch/kernel/module.c index 638427ff0d51..755d91ef8d85 100644 --- a/arch/loongarch/kernel/module.c +++ b/arch/loongarch/kernel/module.c @@ -296,7 +296,7 @@ typedef int (*reloc_rela_handler)(struct module *mod, u= 32 *location, Elf_Addr v, =20 /* The handlers for known reloc types */ static reloc_rela_handler reloc_rela_handlers[] =3D { - [R_LARCH_NONE ... R_LARCH_SUB64] =3D apply_r_larch_error, + [R_LARCH_NONE ... R_LARCH_RELAX] =3D apply_r_larch_error, =20 [R_LARCH_NONE] =3D apply_r_larch_none, [R_LARCH_32] =3D apply_r_larch_32, --=20 2.37.0 From nobody Thu Apr 9 12:54:10 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B6CF3ECAAD5 for ; Mon, 29 Aug 2022 13:35:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230050AbiH2Nfp (ORCPT ); Mon, 29 Aug 2022 09:35:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54996 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229909AbiH2Nfd (ORCPT ); Mon, 29 Aug 2022 09:35:33 -0400 Received: from xry111.site (xry111.site [89.208.246.23]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ECF065C95C for ; Mon, 29 Aug 2022 06:35:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=xry111.site; s=default; t=1661780130; bh=BJCBxUWKejGmzZrZS6QO1molGZ5Mm2lyPP2WX6zLhcA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YKkmU3ntmfBf5eKxRqcbIlB0vYzdHikeD/OzjT4nIllmxeWPmWL4lzd6b0XwJmKES SV+QKQqMTUA0S65QCS887JSLKS4/6q0G+OtNhe5pWxU1XbJn7E43EmhIZq9g4wfOnu OsK4Rok8jqSgg7yCjwgoCF0CzqBM92wSdqa5HIeI= Received: from xry111-x57s1.. (unknown [IPv6:240e:358:11dd:1900:dc73:854d:832e:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (Client did not present a certificate) (Authenticated sender: xry111@xry111.site) by xry111.site (Postfix) with ESMTPSA id 66BDD66905; Mon, 29 Aug 2022 09:35:21 -0400 (EDT) From: Xi Ruoyao To: loongarch@lists.linux.dev Cc: linux-kernel@vger.kernel.org, WANG Xuerui , Huacai Chen , Youling Tang , Jinyang He , Xi Ruoyao Subject: [PATCH v6 5/6] LoongArch: Support PC-relative relocations in modules Date: Mon, 29 Aug 2022 21:31:45 +0800 Message-Id: <20220829133146.15236-6-xry111@xry111.site> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220829133146.15236-1-xry111@xry111.site> References: <20220829133146.15236-1-xry111@xry111.site> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Binutils >=3D 2.40 uses R_LARCH_B26 instead of R_LARCH_SOP_PUSH_PLT_PCREL, and R_LARCH_PCALA* instead of R_LARCH_SOP_PUSH_PCREL. Handle R_LARCH_B26 and R_LARCH_PCALA* in the module loader. For R_LARCH_B26, also create a PLT entry as needed. Signed-off-by: Xi Ruoyao --- arch/loongarch/kernel/module-sections.c | 7 ++- arch/loongarch/kernel/module.c | 75 +++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 1 deletion(-) diff --git a/arch/loongarch/kernel/module-sections.c b/arch/loongarch/kerne= l/module-sections.c index 6d498288977d..c67b9cb220eb 100644 --- a/arch/loongarch/kernel/module-sections.c +++ b/arch/loongarch/kernel/module-sections.c @@ -56,9 +56,14 @@ static void count_max_entries(Elf_Rela *relas, int num, = unsigned int *plts) =20 for (i =3D 0; i < num; i++) { type =3D ELF_R_TYPE(relas[i].r_info); - if (type =3D=3D R_LARCH_SOP_PUSH_PLT_PCREL) { + switch (type) { + case R_LARCH_SOP_PUSH_PLT_PCREL: + case R_LARCH_B26: if (!duplicate_rela(relas, i)) (*plts)++; + break; + default: + /* Do nothing. */ } } } diff --git a/arch/loongarch/kernel/module.c b/arch/loongarch/kernel/module.c index 755d91ef8d85..0024bc6c4af1 100644 --- a/arch/loongarch/kernel/module.c +++ b/arch/loongarch/kernel/module.c @@ -281,6 +281,79 @@ static int apply_r_larch_add_sub(struct module *mod, u= 32 *location, Elf_Addr v, } } =20 +static int apply_r_larch_b26(struct module *mod, u32 *location, Elf_Addr v, + s64 *rela_stack, size_t *rela_stack_top, unsigned int type) +{ + ptrdiff_t offset =3D (void *)v - (void *)location; + union loongarch_instruction *insn =3D (union loongarch_instruction *)loca= tion; + + if (offset >=3D SZ_128M) + v =3D module_emit_plt_entry(mod, v); + + if (offset < -SZ_128M) + v =3D module_emit_plt_entry(mod, v); + + offset =3D (void *)v - (void *)location; + + if (offset & 3) { + pr_err("module %s: jump offset =3D 0x%llx unaligned! dangerous R_LARCH_B= 26 (%u) relocation\n", + mod->name, (long long)offset, type); + return -ENOEXEC; + } + + if (!signed_imm_check(offset, 28)) { + pr_err("module %s: jump offset =3D 0x%llx overflow! dangerous R_LARCH_B2= 6 (%u) relocation\n", + mod->name, (long long)offset, type); + return -ENOEXEC; + } + + offset >>=3D 2; + insn->reg0i26_format.immediate_l =3D offset & 0xffff; + insn->reg0i26_format.immediate_h =3D (offset >> 16) & 0x3ff; + return 0; +} + +static int apply_r_larch_pcala(struct module *mod, u32 *location, Elf_Addr= v, + s64 *rela_stack, size_t *rela_stack_top, unsigned int type) +{ + union loongarch_instruction *insn =3D (union loongarch_instruction *)loca= tion; + /* Use s32 for a sign-extension deliberately. */ + s32 offset_hi20 =3D (void *)((v + 0x800) & ~0xfff) - + (void *)((Elf_Addr)location & ~0xfff); + Elf_Addr anchor =3D (((Elf_Addr)location) & ~0xfff) + offset_hi20; + ptrdiff_t offset_rem =3D (void *)v - (void *)anchor; + + switch (type) { + case R_LARCH_PCALA_HI20: + v =3D offset_hi20 >> 12; + break; + case R_LARCH_PCALA64_LO20: + v =3D offset_rem >> 32; + break; + case R_LARCH_PCALA64_HI12: + v =3D offset_rem >> 52; + break; + default: + /* Do nothing. */ + } + + switch (type) { + case R_LARCH_PCALA_HI20: + case R_LARCH_PCALA64_LO20: + insn->reg1i20_format.immediate =3D v & 0xfffff; + break; + case R_LARCH_PCALA_LO12: + case R_LARCH_PCALA64_HI12: + insn->reg2i12_format.immediate =3D v & 0xfff; + break; + default: + pr_err("%s: Unsupport relocation type %u\n", mod->name, type); + return -EINVAL; + } + + return 0; +} + /* * reloc_handlers_rela() - Apply a particular relocation to a module * @mod: the module to apply the reloc to @@ -310,6 +383,8 @@ static reloc_rela_handler reloc_rela_handlers[] =3D { [R_LARCH_SOP_SUB ... R_LARCH_SOP_IF_ELSE] =3D apply_r_larch_sop, [R_LARCH_SOP_POP_32_S_10_5 ... R_LARCH_SOP_POP_32_U] =3D apply_r_larch_so= p_imm_field, [R_LARCH_ADD32 ... R_LARCH_SUB64] =3D apply_r_larch_add_sub, + [R_LARCH_B26] =3D apply_r_larch_b26, + [R_LARCH_PCALA_HI20...R_LARCH_PCALA64_HI12] =3D apply_r_larch_pcala, }; =20 int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab, --=20 2.37.0 From nobody Thu Apr 9 12:54:10 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0EB22ECAAD5 for ; Mon, 29 Aug 2022 13:36:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229550AbiH2NgB (ORCPT ); Mon, 29 Aug 2022 09:36:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54916 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230121AbiH2Nfm (ORCPT ); Mon, 29 Aug 2022 09:35:42 -0400 Received: from xry111.site (xry111.site [IPv6:2001:470:683e::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 633395A80B for ; Mon, 29 Aug 2022 06:35:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=xry111.site; s=default; t=1661780136; bh=QpnAx1agWxMNXcmkHUBsqZ9806QkcAaunrOos31jXBE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=A2Js9G1EA+PzLIhThjvAzyXPM2IwVMbxtaXmymHicXP4wCs9kzXRvWicsq1M8zo8q V4iBTm0i2qhkVo0h2nAK9WEctw0iwcDq9X+swoDzfSF8JpJ9kZEdtN6Xim/aV56mVi UEz4hKBcoXf0gSue6zS7hATm06Omorv7MQ+URqRk= Received: from xry111-x57s1.. (unknown [IPv6:240e:358:11dd:1900:dc73:854d:832e:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (Client did not present a certificate) (Authenticated sender: xry111@xry111.site) by xry111.site (Postfix) with ESMTPSA id 68D4666903; Mon, 29 Aug 2022 09:35:30 -0400 (EDT) From: Xi Ruoyao To: loongarch@lists.linux.dev Cc: linux-kernel@vger.kernel.org, WANG Xuerui , Huacai Chen , Youling Tang , Jinyang He , Xi Ruoyao Subject: [PATCH v6 6/6] LoongArch: Support R_LARCH_GOT_PC* in modules Date: Mon, 29 Aug 2022 21:31:46 +0800 Message-Id: <20220829133146.15236-7-xry111@xry111.site> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220829133146.15236-1-xry111@xry111.site> References: <20220829133146.15236-1-xry111@xry111.site> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" GCC >=3D 13 and GNU assembler >=3D 2.40 use these relocations to address external symbols, so we need to add them. Let the module loader emit GOT entries for data symbols so we would be able to handle GOT relocations. The GOT entry is just the data symbol address. In module.lds, emit a stub .got section for a section header entry. The actual content of the entry will be filled at runtime by module_frob_arch_sections. Signed-off-by: Xi Ruoyao --- arch/loongarch/include/asm/module.h | 23 ++++++++++++ arch/loongarch/include/asm/module.lds.h | 1 + arch/loongarch/kernel/module-sections.c | 49 +++++++++++++++++++++++-- arch/loongarch/kernel/module.c | 24 ++++++++++++ 4 files changed, 94 insertions(+), 3 deletions(-) diff --git a/arch/loongarch/include/asm/module.h b/arch/loongarch/include/a= sm/module.h index 9f6718df1854..76a98a0ab8a0 100644 --- a/arch/loongarch/include/asm/module.h +++ b/arch/loongarch/include/asm/module.h @@ -19,6 +19,7 @@ struct mod_section { struct mod_arch_specific { struct mod_section plt; struct mod_section plt_idx; + struct mod_section got; }; =20 struct plt_entry { @@ -28,11 +29,16 @@ struct plt_entry { u32 inst_jirl; }; =20 +struct got_entry { + Elf_Addr symbol_addr; +}; + struct plt_idx_entry { unsigned long symbol_addr; }; =20 Elf_Addr module_emit_plt_entry(struct module *mod, unsigned long val); +Elf_Addr module_emit_got_entry(struct module *mod, Elf_Addr val); =20 static inline struct plt_entry emit_plt_entry(unsigned long val) { @@ -51,6 +57,11 @@ static inline struct plt_idx_entry emit_plt_idx_entry(un= signed long val) return (struct plt_idx_entry) { val }; } =20 +static inline struct got_entry emit_got_entry(Elf_Addr val) +{ + return (struct got_entry) { val }; +} + static inline int get_plt_idx(unsigned long val, const struct mod_section = *sec) { int i; @@ -77,4 +88,16 @@ static inline struct plt_entry *get_plt_entry(unsigned l= ong val, return plt + plt_idx; } =20 +static inline struct got_entry *get_got_entry(Elf_Addr val, + const struct mod_section *sec) +{ + struct got_entry *got =3D (struct got_entry *)sec->shdr->sh_addr; + int i; + + for (i =3D 0; i < sec->num_entries; i++) + if (got[i].symbol_addr =3D=3D val) + return &got[i]; + return NULL; +} + #endif /* _ASM_MODULE_H */ diff --git a/arch/loongarch/include/asm/module.lds.h b/arch/loongarch/inclu= de/asm/module.lds.h index 31c1c0db11a3..57bbd0cedd26 100644 --- a/arch/loongarch/include/asm/module.lds.h +++ b/arch/loongarch/include/asm/module.lds.h @@ -4,4 +4,5 @@ SECTIONS { . =3D ALIGN(4); .plt : { BYTE(0) } .plt.idx : { BYTE(0) } + .got : { BYTE(0) } } diff --git a/arch/loongarch/kernel/module-sections.c b/arch/loongarch/kerne= l/module-sections.c index c67b9cb220eb..4c99737cd8dc 100644 --- a/arch/loongarch/kernel/module-sections.c +++ b/arch/loongarch/kernel/module-sections.c @@ -33,6 +33,31 @@ Elf_Addr module_emit_plt_entry(struct module *mod, unsig= ned long val) return (Elf_Addr)&plt[nr]; } =20 +Elf_Addr module_emit_got_entry(struct module *mod, Elf_Addr val) +{ + struct mod_section *got_sec =3D &mod->arch.got; + int i =3D got_sec->num_entries; + struct got_entry *got =3D get_got_entry(val, got_sec); + + if (got) + return (Elf_Addr)got; + + /* There is no GOT entry existing for val yet. Create a new one. */ + got =3D (struct got_entry *)got_sec->shdr->sh_addr; + got[i] =3D emit_got_entry(val); + + got_sec->num_entries++; + if (got_sec->num_entries > got_sec->max_entries) { + /* This may happen when the module contains a GOT_HI20 without + * a paired GOT_LO12. Such a module is broken, reject it. + */ + pr_err("%s: module contains bad GOT relocation\n", mod->name); + return 0; + } + + return (Elf_Addr)&got[i]; +} + static int is_rela_equal(const Elf_Rela *x, const Elf_Rela *y) { return x->r_info =3D=3D y->r_info && x->r_addend =3D=3D y->r_addend; @@ -50,7 +75,8 @@ static bool duplicate_rela(const Elf_Rela *rela, int idx) return false; } =20 -static void count_max_entries(Elf_Rela *relas, int num, unsigned int *plts) +static void count_max_entries(Elf_Rela *relas, int num, + unsigned int *plts, unsigned int *gots) { unsigned int i, type; =20 @@ -62,6 +88,10 @@ static void count_max_entries(Elf_Rela *relas, int num, = unsigned int *plts) if (!duplicate_rela(relas, i)) (*plts)++; break; + case R_LARCH_GOT_PC_HI20: + if (!duplicate_rela(relas, i)) + (*gots)++; + break; default: /* Do nothing. */ } @@ -71,7 +101,7 @@ static void count_max_entries(Elf_Rela *relas, int num, = unsigned int *plts) int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr *sechdrs, char *secstrings, struct module *mod) { - unsigned int i, num_plts =3D 0; + unsigned int i, num_plts =3D 0, num_gots =3D 0; =20 /* * Find the empty .plt sections. @@ -81,6 +111,8 @@ int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr *= sechdrs, mod->arch.plt.shdr =3D sechdrs + i; else if (!strcmp(secstrings + sechdrs[i].sh_name, ".plt.idx")) mod->arch.plt_idx.shdr =3D sechdrs + i; + else if (!strcmp(secstrings + sechdrs[i].sh_name, ".got")) + mod->arch.got.shdr =3D sechdrs + i; } =20 if (!mod->arch.plt.shdr) { @@ -91,6 +123,10 @@ int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr = *sechdrs, pr_err("%s: module PLT.IDX section(s) missing\n", mod->name); return -ENOEXEC; } + if (!mod->arch.got.shdr) { + pr_err("%s: module GOT section(s) missing\n", mod->name); + return -ENOEXEC; + } =20 /* Calculate the maxinum number of entries */ for (i =3D 0; i < ehdr->e_shnum; i++) { @@ -105,7 +141,7 @@ int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr = *sechdrs, if (!(dst_sec->sh_flags & SHF_EXECINSTR)) continue; =20 - count_max_entries(relas, num_rela, &num_plts); + count_max_entries(relas, num_rela, &num_plts, &num_gots); } =20 mod->arch.plt.shdr->sh_type =3D SHT_NOBITS; @@ -122,5 +158,12 @@ int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr= *sechdrs, mod->arch.plt_idx.num_entries =3D 0; mod->arch.plt_idx.max_entries =3D num_plts; =20 + mod->arch.got.shdr->sh_type =3D SHT_NOBITS; + mod->arch.got.shdr->sh_flags =3D SHF_ALLOC; + mod->arch.got.shdr->sh_addralign =3D L1_CACHE_BYTES; + mod->arch.got.shdr->sh_size =3D (num_gots + 1) * sizeof(struct got_entry); + mod->arch.got.num_entries =3D 0; + mod->arch.got.max_entries =3D num_gots; + return 0; } diff --git a/arch/loongarch/kernel/module.c b/arch/loongarch/kernel/module.c index 0024bc6c4af1..46f323845f29 100644 --- a/arch/loongarch/kernel/module.c +++ b/arch/loongarch/kernel/module.c @@ -354,6 +354,29 @@ static int apply_r_larch_pcala(struct module *mod, u32= *location, Elf_Addr v, return 0; } =20 +static int apply_r_larch_got_pc(struct module *mod, u32 *location, Elf_Add= r v, + s64 *rela_stack, size_t *rela_stack_top, unsigned int type) +{ + Elf_Addr got =3D module_emit_got_entry(mod, v); + + if (!got) + return -EINVAL; + + switch (type) { + case R_LARCH_GOT_PC_LO12: + type =3D R_LARCH_PCALA_LO12; + break; + case R_LARCH_GOT_PC_HI20: + type =3D R_LARCH_PCALA_HI20; + break; + default: + pr_err("%s: Unsupport relocation type %u\n", mod->name, type); + return -EINVAL; + } + + return apply_r_larch_pcala(mod, location, got, rela_stack, rela_stack_top= , type); +} + /* * reloc_handlers_rela() - Apply a particular relocation to a module * @mod: the module to apply the reloc to @@ -385,6 +408,7 @@ static reloc_rela_handler reloc_rela_handlers[] =3D { [R_LARCH_ADD32 ... R_LARCH_SUB64] =3D apply_r_larch_add_sub, [R_LARCH_B26] =3D apply_r_larch_b26, [R_LARCH_PCALA_HI20...R_LARCH_PCALA64_HI12] =3D apply_r_larch_pcala, + [R_LARCH_GOT_PC_HI20...R_LARCH_GOT_PC_LO12] =3D apply_r_larch_got_pc, }; =20 int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab, --=20 2.37.0