From mboxrd@z Thu Jan 1 00:00:00 1970 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 52B82C433EF for ; Tue, 30 Nov 2021 13:03:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=uHMaNAsq9kzfEwRDe/Yrbe/iU+VzRcbPhTsph7WOxuA=; b=mtPZssDljEf9gE I7VcZWQKI12dlUhFO3P8HVZrsTE3t9mEthPoud7VY5H244lOEm5dyeS1whz3t+otD78fxNQptwCVv 913/A+1x9n3/669FMsyqhjOryh5aypfOQ3U1z4AZn25lFp+G3T1RZDr5S/ueYKjgvGQXkSBDk2ZnK 8FHzzPAGU7EUdZZSacjOytUntZQ8nyYluaaXETgaYl2OVJLuUZNRop3ciZSKd85PvULmfbBemN7zj aT6g4LxXJIGy3cQix8PiU2LTqEj3l0H1IZTqfB24yBRkworQgtTB0259fwMPF9msS9Dmb4GqjSvQS EPybyk+njPiCYQn4guIg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1ms2lN-005DGx-F1; Tue, 30 Nov 2021 13:01:37 +0000 Received: from ams.source.kernel.org ([2604:1380:4601:e00::1]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1ms2jK-005CPR-L2 for linux-arm-kernel@lists.infradead.org; Tue, 30 Nov 2021 12:59:32 +0000 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 52C55B8166D; Tue, 30 Nov 2021 12:59:29 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id ED643C53FCD; Tue, 30 Nov 2021 12:59:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1638277168; bh=mRnWxuQK52Olb+7hhJ6kKIM30pjz6tbMig/TeGXgAPo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=bZQy9VqNz64kqTwYNf1Ag6bRGLnS6Eq6Zbzq7BX7KL4LEHT1VG45fHpXTrtG9mxLZ CDLgcjP/3rMMzWwBc8HxBFT/iBjJnPzaRIEBAblN0CIwk/lnIre8C69lilzbckOjk6 ZDQxpVo5Q8NSQqEr8LNYNdRy7xhQXSt0WDRWXiP74PI2uxFbUEagmMZtgO8EQA761L 9Cf0ptNBiVz8rUf3yWmQ4ecgUGlJezx5Qrjnufdn6sjKKigv0b9HZeZSYXFmGb636s +RTEks2oIeqtiG2BlmmUJ2m6Klk1jNKTXcy6BdiK7GzgyF123D/SX/vJSfHSq7iXkV FDu9SDumBoHKQ== From: Ard Biesheuvel To: linux-arm-kernel@lists.infradead.org, linux@armlinux.org.uk Cc: Ard Biesheuvel , Nicolas Pitre , Arnd Bergmann , Kees Cook , Keith Packard , Linus Walleij , Nick Desaulniers , Tony Lindgren Subject: [PATCH v2 07/12] ARM: module: implement support for PC-relative group relocations Date: Tue, 30 Nov 2021 13:58:56 +0100 Message-Id: <20211130125901.3054-8-ardb@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20211130125901.3054-1-ardb@kernel.org> References: <20211130125901.3054-1-ardb@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=3679; h=from:subject; bh=mRnWxuQK52Olb+7hhJ6kKIM30pjz6tbMig/TeGXgAPo=; b=owEB7QES/pANAwAKAcNPIjmS2Y8kAcsmYgBhpiAPX2kmVUaf0U9LZopvXRi07t73EKv48xfhw7Ac mnJZvwqJAbMEAAEKAB0WIQT72WJ8QGnJQhU3VynDTyI5ktmPJAUCYaYgDwAKCRDDTyI5ktmPJBGiC/ 9mpgyPyH3te8M3YgagQlfSd5FoqrC0X1ujwurEWuTrcTodQacMfTP1/cvCYmFJkEJ4UbsD3hnucfAm DJ9p+0Aouhe3hVFL11K3V2rBdDOaHVGdtr1Wkdv1/sRB9dfRCy1UDXF0EtbLIw7+7h7sfxWCJzvdQV rgz4duac4gmtwXq84dW64o51EvGoQfPBW6RMFbmQXgfa3L4l+utXerPBmMaSQOiGDYgZd05w1SadpD 4CVwaOeCYYn6PL4wR/KiV8hpQIocl/aEgufLcx4IBJVxdtX3D0yINDJPkmI2v9moB3nlaa9cwkgchi zA8JPZgHjmOgmFmw/RJTVZRbCDKar6ZPl9+LSsI9dUhKsUiGnPfadE/+g8U+D2t0KFOS29LR6o9nDR jQWj7EtV5BqVpxhTMiDiWxdtNqkK8i7ILishRZ2pnF5v9Pbb7cxhrbJ3TNvIjwEEUP6jURvYVFj0zh RXEbw6f5hdhdiep5zQE9VCHOqQx0iaxbRCgcFPRsRZG2U= X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20211130_045931_007986_36D6CC3A X-CRM114-Status: GOOD ( 19.84 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Add support for the R_ARM_ALU_PC_Gn_NC and R_ARM_LDR_PC_G2 group relocations so we can use them in modules. These will be used to load the current task pointer from a global variable without having to rely on a literal pool entry to carry the address of this variable, which would be slightly less efficient. Acked-by: Linus Walleij Acked-by: Nicolas Pitre Signed-off-by: Ard Biesheuvel --- arch/arm/include/asm/elf.h | 3 + arch/arm/kernel/module.c | 63 ++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/arch/arm/include/asm/elf.h b/arch/arm/include/asm/elf.h index b8102a6ddf16..d68101655b74 100644 --- a/arch/arm/include/asm/elf.h +++ b/arch/arm/include/asm/elf.h @@ -61,6 +61,9 @@ typedef struct user_fp elf_fpregset_t; #define R_ARM_MOVT_ABS 44 #define R_ARM_MOVW_PREL_NC 45 #define R_ARM_MOVT_PREL 46 +#define R_ARM_ALU_PC_G0_NC 57 +#define R_ARM_ALU_PC_G1_NC 59 +#define R_ARM_LDR_PC_G2 63 #define R_ARM_THM_CALL 10 #define R_ARM_THM_JUMP24 30 diff --git a/arch/arm/kernel/module.c b/arch/arm/kernel/module.c index beac45e89ba6..22bdce7a7f95 100644 --- a/arch/arm/kernel/module.c +++ b/arch/arm/kernel/module.c @@ -68,6 +68,20 @@ bool module_exit_section(const char *name) strstarts(name, ".ARM.exidx.exit"); } +static u32 get_group_rem(u32 group, u32 *offset) +{ + u32 val = *offset; + u32 shift; + do { + shift = val ? (31 - __fls(val)) & ~1 : 32; + *offset = val; + if (!val) + break; + val &= 0xffffff >> shift; + } while (group--); + return shift; +} + int apply_relocate(Elf32_Shdr *sechdrs, const char *strtab, unsigned int symindex, unsigned int relindex, struct module *module) @@ -82,6 +96,7 @@ apply_relocate(Elf32_Shdr *sechdrs, const char *strtab, unsigned int symindex, unsigned long loc; Elf32_Sym *sym; const char *symname; + u32 shift, group = 1; s32 offset; u32 tmp; #ifdef CONFIG_THUMB2_KERNEL @@ -212,6 +227,54 @@ apply_relocate(Elf32_Shdr *sechdrs, const char *strtab, unsigned int symindex, *(u32 *)loc = __opcode_to_mem_arm(tmp); break; + case R_ARM_ALU_PC_G0_NC: + group = 0; + fallthrough; + case R_ARM_ALU_PC_G1_NC: + tmp = __mem_to_opcode_arm(*(u32 *)loc); + offset = ror32(tmp & 0xff, (tmp & 0xf00) >> 7); + if (tmp & BIT(22)) + offset = -offset; + offset += sym->st_value - loc; + if (offset < 0) { + offset = -offset; + tmp = (tmp & ~BIT(23)) | BIT(22); // SUB opcode + } else { + tmp = (tmp & ~BIT(22)) | BIT(23); // ADD opcode + } + + shift = get_group_rem(group, &offset); + if (shift < 24) { + offset >>= 24 - shift; + offset |= (shift + 8) << 7; + } + *(u32 *)loc = __opcode_to_mem_arm((tmp & ~0xfff) | offset); + break; + + case R_ARM_LDR_PC_G2: + tmp = __mem_to_opcode_arm(*(u32 *)loc); + offset = tmp & 0xfff; + if (~tmp & BIT(23)) // U bit cleared? + offset = -offset; + offset += sym->st_value - loc; + if (offset < 0) { + offset = -offset; + tmp &= ~BIT(23); // clear U bit + } else { + tmp |= BIT(23); // set U bit + } + get_group_rem(2, &offset); + + if (offset > 0xfff) { + pr_err("%s: section %u reloc %u sym '%s': relocation %u out of range (%#lx -> %#x)\n", + module->name, relindex, i, symname, + ELF32_R_TYPE(rel->r_info), loc, + sym->st_value); + return -ENOEXEC; + } + *(u32 *)loc = __opcode_to_mem_arm((tmp & ~0xfff) | offset); + break; + #ifdef CONFIG_THUMB2_KERNEL case R_ARM_THM_CALL: case R_ARM_THM_JUMP24: -- 2.30.2 _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel