From patchwork Wed Nov 1 18:32:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Charlie Jenkins X-Patchwork-Id: 13442988 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 730FEC4167D for ; Wed, 1 Nov 2023 18:33:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 060FD900017; Wed, 1 Nov 2023 14:33:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0109B90000D; Wed, 1 Nov 2023 14:33:09 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DA5FB900017; Wed, 1 Nov 2023 14:33:09 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id CB24690000D for ; Wed, 1 Nov 2023 14:33:09 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 9B8DBC0D2E for ; Wed, 1 Nov 2023 18:33:09 +0000 (UTC) X-FDA: 81410232498.12.D428D07 Received: from mail-oo1-f50.google.com (mail-oo1-f50.google.com [209.85.161.50]) by imf14.hostedemail.com (Postfix) with ESMTP id BAA3F10000D for ; Wed, 1 Nov 2023 18:33:07 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=vElO8PAI; dmarc=none; spf=pass (imf14.hostedemail.com: domain of charlie@rivosinc.com designates 209.85.161.50 as permitted sender) smtp.mailfrom=charlie@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1698863587; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=bmX47uOjeBLgcqnLG2LWRRnp8TOL9YLBnfsBk3939lI=; b=tmh+hkf5TKXt4znzwWnFTTQ+zgbCmR3S0D3FE23SgS6cY82p6JfBMpDRwKvYhFrnTv4Pkt 8x6oENtwl9ntathD8AcvsNOX5uHYEWR36/vbMry/gNbKzpVXVbu+QMcfSHmMShYkQUKoiM Qe+avpvdQltj+mlgsSb5bOZ2s+Ret8s= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=vElO8PAI; dmarc=none; spf=pass (imf14.hostedemail.com: domain of charlie@rivosinc.com designates 209.85.161.50 as permitted sender) smtp.mailfrom=charlie@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698863587; a=rsa-sha256; cv=none; b=VtBfFB+q1GjKHYb/dmrmokuSPefeWM4FIVf09qRp6vHE2KVhncw7LsTIe5CK8Q2lXgM8VE t+Mg7JwCQgq+klcnztsCJoz3X0l7X6zZevxMLvNARo88OL1p6wF1TVzaOnHDA6WlszXM30 FuAtjXUVjuop8WSo+HWrgXOlpRsv+es= Received: by mail-oo1-f50.google.com with SMTP id 006d021491bc7-5842a7fdc61so39313eaf.3 for ; Wed, 01 Nov 2023 11:33:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1698863587; x=1699468387; darn=kvack.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=bmX47uOjeBLgcqnLG2LWRRnp8TOL9YLBnfsBk3939lI=; b=vElO8PAIbXTbrmHWNUqd26L9S4Ib3ewA8Svge9zOw3xq2VckgXP7Hjf6okV+cMNshv 4uWLv9ZUm2cIvEhDoLroJ3J/z92Aek38ae8Uius5Res2hGklC+SAL/8xS6N1t0sha4SQ ZG53JhZd4e1jr/3+uJ+mpsdPtttJKyfPVlaJ/w7gS7IWTHngexjyEliyKEIi2eDOQm58 JMYaR7Hn3rs9s18QmLBzduTT/V7ab3n7pL6cAIf7K8BZ+tMjmYkJ+3kXzxX4bOTJNu/Q blFkshYvu6hUWYwGptQGAHXa4mOsN4zIqdKxgb3T/J2bBoky8FGZ0fdt1YsO/bCkZlWL Jx+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698863587; x=1699468387; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=bmX47uOjeBLgcqnLG2LWRRnp8TOL9YLBnfsBk3939lI=; b=cAkGdnrs0OkG6zfcMWnEa985GNbgBaJzTxrjR7pUrlkipdYlq3ESUYKxqJdZRQh7Np 8HOz9xZseCXY7P8U3FpO2SQkMcGonm/MwNb4WXWjZ+zuvrdUz00E9iKeuZul3Ez88/6w nBJeWGCrnsipLHYX/qIc2UXiOKoq6rF8/6qiZmZU7eUVHUr1Q+k3CpMtKNy7NWeP6pRk 2chxj3BSKPuFYWBZcazi5SQHCouHQpc+5zA1CJIVui9UmrhtZCdK5qvhvJ7gJDpe0otB bKbozVb0U0o1P9SQTixqRJN6I4Rd3jxZrbFy132UHfnomgnO2jlta9+RX6xy5F5TSXQ7 JLpw== X-Gm-Message-State: AOJu0YwKwOHe1nudxOtH71swLhJBob7mLu6InYMNbYgAj5pLBYjzVuQg D4MM7a/IjDktBvtx/+Oez22HQQ== X-Google-Smtp-Source: AGHT+IEy6ddaceBESFTSWHP+MOBVo7SllhOU1bINZBrAu1EBzKWDnREAu5ah35KCCWiY3grX8PD/MA== X-Received: by 2002:a4a:a649:0:b0:582:c8b4:d9df with SMTP id j9-20020a4aa649000000b00582c8b4d9dfmr16068466oom.1.1698863586805; Wed, 01 Nov 2023 11:33:06 -0700 (PDT) Received: from charlie.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id m14-20020a4aab8e000000b00581e090fd1fsm686331oon.8.2023.11.01.11.33.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Nov 2023 11:33:06 -0700 (PDT) From: Charlie Jenkins Date: Wed, 01 Nov 2023 11:32:59 -0700 Subject: [PATCH v9 1/3] riscv: Avoid unaligned access when relocating modules MIME-Version: 1.0 Message-Id: <20231101-module_relocations-v9-1-8dfa3483c400@rivosinc.com> References: <20231101-module_relocations-v9-0-8dfa3483c400@rivosinc.com> In-Reply-To: <20231101-module_relocations-v9-0-8dfa3483c400@rivosinc.com> To: linux-riscv@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Eric Biederman , Kees Cook , Paul Walmsley , Palmer Dabbelt , Albert Ou , Andreas Schwab , Emil Renner Berthing , Samuel Holland , Nelson Chu , Charlie Jenkins , Emil Renner Berthing X-Mailer: b4 0.12.3 X-Rspamd-Queue-Id: BAA3F10000D X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: ks1o5fs6njieadbisp8quxxmx6yedem6 X-HE-Tag: 1698863587-474527 X-HE-Meta: U2FsdGVkX19cPhpLiU3MJs8jkO54G4o9GJbp6skGxYYUVxVgvIpGRiFYnPgrsWhkeP3AgxMRo7+BReHUbXXF/9W2vrGV1UY/XI64pwn1MYV4PTIUVVoiRcBNs5GVXTvAG0kPi1hNNKMRIu4dU5c419pIwmuLAG+lfDDi85yx3ZsqHmwp3Xxb//1VhUgBB1/zg4o+w3TpJy0ZX4tUeVdId/WDHV8dVMAKSEPFrd00DZMMRO/e7mp6db9fi3Oyete+Z2BxC0AQdxp7doWmrKy8/LwzBm1SbIvNZUeICgDu/L0xhsrXlCT9rFtRKTYNoM02Xw/A4UOdyzeHSbaSBHwSR9rV3hwax6S2YYjeQY7CpJj5WYKJgh0YuGhsZrIHn15JsY2gITAjrdtE/jiySLiqve4XZrO1vRU13xH0YygsyJhpsaw+POrnKd8qMGAb+77aQ6isYdRULOaYezW1kqOkxHT5GDupe9Z9mN4VBG0u4yY0lINAjqh4C7fV6JsS4Hx8m4Rwl3/uYqIg6yqzyouzS9JmEsFFK8xUlBvTI7185kzneSWzLwkDJnHjoXeXQRToaWPqhwChBnXwSqA7CBM2Z11Sj0GZVZE5mmInVuoVhLlr80hLcVK7Z0J1octQCvgjC/nN8PsA9KWbf6LEOsGwWTJnaY9cHUsdcGFjBRLTtSB+VMbLIrgykZ+FYTtSoksxbao5hXHXUB+/7H9bLQe6+/lW9vRRRUL6CJ5pWlAPR5y5zVoCS4KyagoEisxL5jdqtiLUwKhZL567Hd0SDY3C2JpfAKUDLaQVsVH82CPHZT6zXKFTTzTXF4IMKdTmPH30KIHan24qOHe4hKlbZ57D20dXOMJyt2l1+CsEytdM3ZNcLfSVtrqNjwevpTDLhcdaVzrwj98V7C5X68FmCnjLNNC6l4SKon3Vhv9wwGA4LX1QDbrsdpBayamSojdM5aigdK548grMFXkM32KBOVH 3B5COMfI aoh69ct+hz0qE8ZrE7T1u1Yx1JcJS3OwFX8Otwhb4mfk9nk/Es6kckO3hZw+QoPwLVc6VDP7WFiUnwm1KrYaPvkELi4NxJSa8e1cImRaxxGCW+ktQFglJtXgH4t0KFCAsznUcF+fjZChTO6qhK0yIb7Z/rOApj1S9utWaRXIOoTZjs4IzscH0MXgKewQv03i75VMosrodyjbxdEtGgfz360gyk4dO53tBwJev2Hhgy3C3NjEdrQ5FbrCjSsK1EPr1vRrK+1Dl1qHzp4PUhId5t1GdSmzsa5z+L/7M7EcB9PnkVWIXASmza95ZYWg70yARjjXjk49nDTYZsT6o+yJHlRvySi6jamIlBhDHRECyZZH8J5xz7adP7iEPzorvkGmpCq03P9y6NYoO9zgrOOS0By0kcvK5S4dxbgbFPLBaEvEbDkNkkZgKaug7YMCtqNqjDdPjF3X/jpADiTVprbU7+UfGx7cC7JkLJop/jN1TPUNbRTSCwZcZLgncUJoNdDIEp2wxP6vxJLDIsva1EQXNkj4hu+a4XT9eZYzG X-Bogosity: Ham, tests=bogofilter, spamicity=0.000591, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Emil Renner Berthing With the C-extension regular 32bit instructions are not necessarily aligned on 4-byte boundaries. RISC-V instructions are in fact an ordered list of 16bit little-endian "parcels", so access the instruction as such. This should also make the code work in case someone builds a big-endian RISC-V machine. Signed-off-by: Emil Renner Berthing Signed-off-by: Charlie Jenkins --- arch/riscv/kernel/module.c | 157 +++++++++++++++++++++++---------------------- 1 file changed, 81 insertions(+), 76 deletions(-) diff --git a/arch/riscv/kernel/module.c b/arch/riscv/kernel/module.c index 7c651d55fcbd..8286d3bb04f4 100644 --- a/arch/riscv/kernel/module.c +++ b/arch/riscv/kernel/module.c @@ -27,68 +27,90 @@ static bool riscv_insn_valid_32bit_offset(ptrdiff_t val) #endif } -static int apply_r_riscv_32_rela(struct module *me, u32 *location, Elf_Addr v) +static int riscv_insn_rmw(void *location, u32 keep, u32 set) +{ + u16 *parcel = location; + u32 insn = (u32)le16_to_cpu(parcel[0]) | (u32)le16_to_cpu(parcel[1]) << 16; + + insn &= keep; + insn |= set; + + parcel[0] = cpu_to_le16(insn); + parcel[1] = cpu_to_le16(insn >> 16); + return 0; +} + +static int riscv_insn_rvc_rmw(void *location, u16 keep, u16 set) +{ + u16 *parcel = location; + u16 insn = le16_to_cpu(*parcel); + + insn &= keep; + insn |= set; + + *parcel = cpu_to_le16(insn); + return 0; +} + +static int apply_r_riscv_32_rela(struct module *me, void *location, Elf_Addr v) { if (v != (u32)v) { pr_err("%s: value %016llx out of range for 32-bit field\n", me->name, (long long)v); return -EINVAL; } - *location = v; + *(u32 *)location = v; return 0; } -static int apply_r_riscv_64_rela(struct module *me, u32 *location, Elf_Addr v) +static int apply_r_riscv_64_rela(struct module *me, void *location, Elf_Addr v) { *(u64 *)location = v; return 0; } -static int apply_r_riscv_branch_rela(struct module *me, u32 *location, +static int apply_r_riscv_branch_rela(struct module *me, void *location, Elf_Addr v) { - ptrdiff_t offset = (void *)v - (void *)location; + ptrdiff_t offset = (void *)v - location; u32 imm12 = (offset & 0x1000) << (31 - 12); u32 imm11 = (offset & 0x800) >> (11 - 7); u32 imm10_5 = (offset & 0x7e0) << (30 - 10); u32 imm4_1 = (offset & 0x1e) << (11 - 4); - *location = (*location & 0x1fff07f) | imm12 | imm11 | imm10_5 | imm4_1; - return 0; + return riscv_insn_rmw(location, 0x1fff07f, imm12 | imm11 | imm10_5 | imm4_1); } -static int apply_r_riscv_jal_rela(struct module *me, u32 *location, +static int apply_r_riscv_jal_rela(struct module *me, void *location, Elf_Addr v) { - ptrdiff_t offset = (void *)v - (void *)location; + ptrdiff_t offset = (void *)v - location; u32 imm20 = (offset & 0x100000) << (31 - 20); u32 imm19_12 = (offset & 0xff000); u32 imm11 = (offset & 0x800) << (20 - 11); u32 imm10_1 = (offset & 0x7fe) << (30 - 10); - *location = (*location & 0xfff) | imm20 | imm19_12 | imm11 | imm10_1; - return 0; + return riscv_insn_rmw(location, 0xfff, imm20 | imm19_12 | imm11 | imm10_1); } -static int apply_r_riscv_rvc_branch_rela(struct module *me, u32 *location, +static int apply_r_riscv_rvc_branch_rela(struct module *me, void *location, Elf_Addr v) { - ptrdiff_t offset = (void *)v - (void *)location; + ptrdiff_t offset = (void *)v - location; u16 imm8 = (offset & 0x100) << (12 - 8); u16 imm7_6 = (offset & 0xc0) >> (6 - 5); u16 imm5 = (offset & 0x20) >> (5 - 2); u16 imm4_3 = (offset & 0x18) << (12 - 5); u16 imm2_1 = (offset & 0x6) << (12 - 10); - *(u16 *)location = (*(u16 *)location & 0xe383) | - imm8 | imm7_6 | imm5 | imm4_3 | imm2_1; - return 0; + return riscv_insn_rvc_rmw(location, 0xe383, + imm8 | imm7_6 | imm5 | imm4_3 | imm2_1); } -static int apply_r_riscv_rvc_jump_rela(struct module *me, u32 *location, +static int apply_r_riscv_rvc_jump_rela(struct module *me, void *location, Elf_Addr v) { - ptrdiff_t offset = (void *)v - (void *)location; + ptrdiff_t offset = (void *)v - location; u16 imm11 = (offset & 0x800) << (12 - 11); u16 imm10 = (offset & 0x400) >> (10 - 8); u16 imm9_8 = (offset & 0x300) << (12 - 11); @@ -98,16 +120,14 @@ static int apply_r_riscv_rvc_jump_rela(struct module *me, u32 *location, u16 imm4 = (offset & 0x10) << (12 - 5); u16 imm3_1 = (offset & 0xe) << (12 - 10); - *(u16 *)location = (*(u16 *)location & 0xe003) | - imm11 | imm10 | imm9_8 | imm7 | imm6 | imm5 | imm4 | imm3_1; - return 0; + return riscv_insn_rvc_rmw(location, 0xe003, + imm11 | imm10 | imm9_8 | imm7 | imm6 | imm5 | imm4 | imm3_1); } -static int apply_r_riscv_pcrel_hi20_rela(struct module *me, u32 *location, +static int apply_r_riscv_pcrel_hi20_rela(struct module *me, void *location, Elf_Addr v) { - ptrdiff_t offset = (void *)v - (void *)location; - s32 hi20; + ptrdiff_t offset = (void *)v - location; if (!riscv_insn_valid_32bit_offset(offset)) { pr_err( @@ -116,23 +136,20 @@ static int apply_r_riscv_pcrel_hi20_rela(struct module *me, u32 *location, return -EINVAL; } - hi20 = (offset + 0x800) & 0xfffff000; - *location = (*location & 0xfff) | hi20; - return 0; + return riscv_insn_rmw(location, 0xfff, (offset + 0x800) & 0xfffff000); } -static int apply_r_riscv_pcrel_lo12_i_rela(struct module *me, u32 *location, +static int apply_r_riscv_pcrel_lo12_i_rela(struct module *me, void *location, Elf_Addr v) { /* * v is the lo12 value to fill. It is calculated before calling this * handler. */ - *location = (*location & 0xfffff) | ((v & 0xfff) << 20); - return 0; + return riscv_insn_rmw(location, 0xfffff, (v & 0xfff) << 20); } -static int apply_r_riscv_pcrel_lo12_s_rela(struct module *me, u32 *location, +static int apply_r_riscv_pcrel_lo12_s_rela(struct module *me, void *location, Elf_Addr v) { /* @@ -142,15 +159,12 @@ static int apply_r_riscv_pcrel_lo12_s_rela(struct module *me, u32 *location, u32 imm11_5 = (v & 0xfe0) << (31 - 11); u32 imm4_0 = (v & 0x1f) << (11 - 4); - *location = (*location & 0x1fff07f) | imm11_5 | imm4_0; - return 0; + return riscv_insn_rmw(location, 0x1fff07f, imm11_5 | imm4_0); } -static int apply_r_riscv_hi20_rela(struct module *me, u32 *location, +static int apply_r_riscv_hi20_rela(struct module *me, void *location, Elf_Addr v) { - s32 hi20; - if (IS_ENABLED(CONFIG_CMODEL_MEDLOW)) { pr_err( "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n", @@ -158,22 +172,20 @@ static int apply_r_riscv_hi20_rela(struct module *me, u32 *location, return -EINVAL; } - hi20 = ((s32)v + 0x800) & 0xfffff000; - *location = (*location & 0xfff) | hi20; - return 0; + return riscv_insn_rmw(location, 0xfff, ((s32)v + 0x800) & 0xfffff000); } -static int apply_r_riscv_lo12_i_rela(struct module *me, u32 *location, +static int apply_r_riscv_lo12_i_rela(struct module *me, void *location, Elf_Addr v) { /* Skip medlow checking because of filtering by HI20 already */ s32 hi20 = ((s32)v + 0x800) & 0xfffff000; s32 lo12 = ((s32)v - hi20); - *location = (*location & 0xfffff) | ((lo12 & 0xfff) << 20); - return 0; + + return riscv_insn_rmw(location, 0xfffff, (lo12 & 0xfff) << 20); } -static int apply_r_riscv_lo12_s_rela(struct module *me, u32 *location, +static int apply_r_riscv_lo12_s_rela(struct module *me, void *location, Elf_Addr v) { /* Skip medlow checking because of filtering by HI20 already */ @@ -181,20 +193,18 @@ static int apply_r_riscv_lo12_s_rela(struct module *me, u32 *location, s32 lo12 = ((s32)v - hi20); u32 imm11_5 = (lo12 & 0xfe0) << (31 - 11); u32 imm4_0 = (lo12 & 0x1f) << (11 - 4); - *location = (*location & 0x1fff07f) | imm11_5 | imm4_0; - return 0; + + return riscv_insn_rmw(location, 0x1fff07f, imm11_5 | imm4_0); } -static int apply_r_riscv_got_hi20_rela(struct module *me, u32 *location, +static int apply_r_riscv_got_hi20_rela(struct module *me, void *location, Elf_Addr v) { - ptrdiff_t offset = (void *)v - (void *)location; - s32 hi20; + ptrdiff_t offset = (void *)v - location; /* Always emit the got entry */ if (IS_ENABLED(CONFIG_MODULE_SECTIONS)) { - offset = module_emit_got_entry(me, v); - offset = (void *)offset - (void *)location; + offset = (void *)module_emit_got_entry(me, v) - location; } else { pr_err( "%s: can not generate the GOT entry for symbol = %016llx from PC = %p\n", @@ -202,22 +212,19 @@ static int apply_r_riscv_got_hi20_rela(struct module *me, u32 *location, return -EINVAL; } - hi20 = (offset + 0x800) & 0xfffff000; - *location = (*location & 0xfff) | hi20; - return 0; + return riscv_insn_rmw(location, 0xfff, (offset + 0x800) & 0xfffff000); } -static int apply_r_riscv_call_plt_rela(struct module *me, u32 *location, +static int apply_r_riscv_call_plt_rela(struct module *me, void *location, Elf_Addr v) { - ptrdiff_t offset = (void *)v - (void *)location; + ptrdiff_t offset = (void *)v - location; u32 hi20, lo12; if (!riscv_insn_valid_32bit_offset(offset)) { /* Only emit the plt entry if offset over 32-bit range */ if (IS_ENABLED(CONFIG_MODULE_SECTIONS)) { - offset = module_emit_plt_entry(me, v); - offset = (void *)offset - (void *)location; + offset = (void *)module_emit_plt_entry(me, v) - location; } else { pr_err( "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n", @@ -228,15 +235,14 @@ static int apply_r_riscv_call_plt_rela(struct module *me, u32 *location, hi20 = (offset + 0x800) & 0xfffff000; lo12 = (offset - hi20) & 0xfff; - *location = (*location & 0xfff) | hi20; - *(location + 1) = (*(location + 1) & 0xfffff) | (lo12 << 20); - return 0; + riscv_insn_rmw(location, 0xfff, hi20); + return riscv_insn_rmw(location + 4, 0xfffff, lo12 << 20); } -static int apply_r_riscv_call_rela(struct module *me, u32 *location, +static int apply_r_riscv_call_rela(struct module *me, void *location, Elf_Addr v) { - ptrdiff_t offset = (void *)v - (void *)location; + ptrdiff_t offset = (void *)v - location; u32 hi20, lo12; if (!riscv_insn_valid_32bit_offset(offset)) { @@ -248,18 +254,17 @@ static int apply_r_riscv_call_rela(struct module *me, u32 *location, hi20 = (offset + 0x800) & 0xfffff000; lo12 = (offset - hi20) & 0xfff; - *location = (*location & 0xfff) | hi20; - *(location + 1) = (*(location + 1) & 0xfffff) | (lo12 << 20); - return 0; + riscv_insn_rmw(location, 0xfff, hi20); + return riscv_insn_rmw(location + 4, 0xfffff, lo12 << 20); } -static int apply_r_riscv_relax_rela(struct module *me, u32 *location, +static int apply_r_riscv_relax_rela(struct module *me, void *location, Elf_Addr v) { return 0; } -static int apply_r_riscv_align_rela(struct module *me, u32 *location, +static int apply_r_riscv_align_rela(struct module *me, void *location, Elf_Addr v) { pr_err( @@ -268,49 +273,49 @@ static int apply_r_riscv_align_rela(struct module *me, u32 *location, return -EINVAL; } -static int apply_r_riscv_add16_rela(struct module *me, u32 *location, +static int apply_r_riscv_add16_rela(struct module *me, void *location, Elf_Addr v) { *(u16 *)location += (u16)v; return 0; } -static int apply_r_riscv_add32_rela(struct module *me, u32 *location, +static int apply_r_riscv_add32_rela(struct module *me, void *location, Elf_Addr v) { *(u32 *)location += (u32)v; return 0; } -static int apply_r_riscv_add64_rela(struct module *me, u32 *location, +static int apply_r_riscv_add64_rela(struct module *me, void *location, Elf_Addr v) { *(u64 *)location += (u64)v; return 0; } -static int apply_r_riscv_sub16_rela(struct module *me, u32 *location, +static int apply_r_riscv_sub16_rela(struct module *me, void *location, Elf_Addr v) { *(u16 *)location -= (u16)v; return 0; } -static int apply_r_riscv_sub32_rela(struct module *me, u32 *location, +static int apply_r_riscv_sub32_rela(struct module *me, void *location, Elf_Addr v) { *(u32 *)location -= (u32)v; return 0; } -static int apply_r_riscv_sub64_rela(struct module *me, u32 *location, +static int apply_r_riscv_sub64_rela(struct module *me, void *location, Elf_Addr v) { *(u64 *)location -= (u64)v; return 0; } -static int (*reloc_handlers_rela[]) (struct module *me, u32 *location, +static int (*reloc_handlers_rela[]) (struct module *me, void *location, Elf_Addr v) = { [R_RISCV_32] = apply_r_riscv_32_rela, [R_RISCV_64] = apply_r_riscv_64_rela, @@ -342,9 +347,9 @@ int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab, struct module *me) { Elf_Rela *rel = (void *) sechdrs[relsec].sh_addr; - int (*handler)(struct module *me, u32 *location, Elf_Addr v); + int (*handler)(struct module *me, void *location, Elf_Addr v); Elf_Sym *sym; - u32 *location; + void *location; unsigned int i, type; Elf_Addr v; int res;