Received: by 10.213.65.68 with SMTP id h4csp218898imn; Tue, 13 Mar 2018 01:52:32 -0700 (PDT) X-Google-Smtp-Source: AG47ELsS+0PUznmQxCtza9n6aspT1FI3imuRlKLaF0LartGlLggCWC49k6pq6qS0YjxE26RtVOZt X-Received: by 10.98.75.18 with SMTP id y18mr10983443pfa.124.1520931152129; Tue, 13 Mar 2018 01:52:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1520931152; cv=none; d=google.com; s=arc-20160816; b=D9I0f/Dru19jdG3O4zvaLrUdO2vFnpLpWV8qWJIlC7ep1u3EEyY4HjzO+5EuMBx/dM Su3slsVrknMSngLQHo9B60VwR0HlKbmgNHDtdSIVYXVW4T4nEDQVGWCUKWjYHtMis0Zf c21UCA8u5zLwNI7LjfOPoPoHu+3z74wREavY71Pg6dn9OasjnEBOGh1OP0av4nDU4VeC zbd7X+MIxpTHBO1LePC4YNaUoxNrEnWe6m+QEhiUnMZHX2YAOZJ4xKBjuQHdk+xFsMAG n+K2k3fiO6IkUJmmwOQ5kBHSqFLdnsBMlBcRbp8oe+KJv4SaAHhKhxbTvd6RmKiFUY2G VBcA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:arc-authentication-results; bh=k1pbJwZa8g+C3630VOu9YNxttNhBekmzd0ZfZUi5oSU=; b=bkopX7LAZKk9ZBawLuCeFeSu/Y1Tto/vvfSKX5obkfSQ66KYtLUA5r8fU1aKcPT4iN u0OSQLBgbsvLZLKO+18YFPBzADGXFnr1DuV9gDfsw+DdOgKYFIDDoXHg5nJ8eGYvV+bj bEfEaBOLnnz7d9MlEs0GltD6NCfa3dM8Q/AT0oS6xM1CDWmjhgAdIYrGrY8fwD9gtTOy /XOCUoso9xtdM+887eK3v5H5hn7UsVQ/Uun5hPtnGAeTzsRbtGopcUsL/9isxVN1wSr1 9b8dz4CqQIKBIPWBDukvI1J4OPLV+mMUBQHtIFnTn1hVs6ir9X5i4eal/MZ7cP42Mrqp MZkg== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id y25si7029225pfe.206.2018.03.13.01.52.18; Tue, 13 Mar 2018 01:52:32 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752463AbeCMIsb (ORCPT + 99 others); Tue, 13 Mar 2018 04:48:31 -0400 Received: from exmail.andestech.com ([59.124.169.137]:15890 "EHLO ATCSQR.andestech.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752022AbeCMIs3 (ORCPT ); Tue, 13 Mar 2018 04:48:29 -0400 Received: from ATCSQR.andestech.com (localhost [127.0.0.2] (may be forged)) by ATCSQR.andestech.com with ESMTP id w2D8Xa3B025131 for ; Tue, 13 Mar 2018 16:33:36 +0800 (GMT-8) (envelope-from zong@andestech.com) Received: from mail.andestech.com (atcpcs16.andestech.com [10.0.1.222]) by ATCSQR.andestech.com with ESMTP id w2D8XK8j025074; Tue, 13 Mar 2018 16:33:20 +0800 (GMT-8) (envelope-from zong@andestech.com) Received: from atcsqa06.andestech.com (10.0.1.85) by ATCPCS16.andestech.com (10.0.1.222) with Microsoft SMTP Server id 14.3.123.3; Tue, 13 Mar 2018 16:36:46 +0800 From: Zong Li To: , , , , , CC: Subject: [PATCH 05/11] RISC-V: Support HI20/LO12_I/LO12_S relocation type in kernel module Date: Tue, 13 Mar 2018 16:35:10 +0800 Message-ID: <390fb9c030aa83ba978070bf1df163bc4f5633e1.1520928420.git.zong@andestech.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.0.1.85] X-DNSRBL: X-MAIL: ATCSQR.andestech.com w2D8XK8j025074 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org HI20 and LO12_I/LO12_S relocate the absolute address, the range of offset must in 32-bit. Signed-off-by: Zong Li --- arch/riscv/kernel/module.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/arch/riscv/kernel/module.c b/arch/riscv/kernel/module.c index 7e85e5840b4d..654fe7dcd38d 100644 --- a/arch/riscv/kernel/module.c +++ b/arch/riscv/kernel/module.c @@ -92,6 +92,45 @@ static int apply_r_riscv_pcrel_lo12_s_rela(struct module *me, u32 *location, return 0; } +static int apply_r_riscv_hi20_rela(struct module *me, u32 *location, + Elf_Addr v) +{ + s32 hi20; + + if (IS_ENABLED(CMODEL_MEDLOW)) { + pr_err( + "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n", + me->name, v, location); + return -EINVAL; + } + + hi20 = ((s32)v + 0x800) & 0xfffff000; + *location = (*location & 0xfff) | hi20; + return 0; +} + +static int apply_r_riscv_lo12_i_rela(struct module *me, u32 *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; +} + +static int apply_r_riscv_lo12_s_rela(struct module *me, u32 *location, + Elf_Addr v) +{ + /* Skip medlow checking because of filtering by HI20 already */ + s32 hi20 = ((s32)v + 0x800) & 0xfffff000; + 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; +} + static int apply_r_riscv_got_hi20_rela(struct module *me, u32 *location, Elf_Addr v) { @@ -176,6 +215,9 @@ static int (*reloc_handlers_rela[]) (struct module *me, u32 *location, [R_RISCV_PCREL_HI20] = apply_r_riscv_pcrel_hi20_rela, [R_RISCV_PCREL_LO12_I] = apply_r_riscv_pcrel_lo12_i_rela, [R_RISCV_PCREL_LO12_S] = apply_r_riscv_pcrel_lo12_s_rela, + [R_RISCV_HI20] = apply_r_riscv_hi20_rela, + [R_RISCV_LO12_I] = apply_r_riscv_lo12_i_rela, + [R_RISCV_LO12_S] = apply_r_riscv_lo12_s_rela, [R_RISCV_GOT_HI20] = apply_r_riscv_got_hi20_rela, [R_RISCV_CALL_PLT] = apply_r_riscv_call_plt_rela, [R_RISCV_CALL] = apply_r_riscv_call_rela, -- 2.16.1