Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp3156416rwe; Mon, 29 Aug 2022 06:50:31 -0700 (PDT) X-Google-Smtp-Source: AA6agR788FbsrexJKNrlYTYr62NOXQEiAK4LgzBYYbZ73z3uwqAgbZj0Y+DXx6BxGkRQdAdrXTN5 X-Received: by 2002:aa7:88d0:0:b0:536:e993:730d with SMTP id k16-20020aa788d0000000b00536e993730dmr16629023pff.81.1661781030838; Mon, 29 Aug 2022 06:50:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661781030; cv=none; d=google.com; s=arc-20160816; b=FCJuaxWFwDZJDhr8IQUHu7Kf+7TJCE70Kk+4lbG2WhWtbuXWwFZlGfYuNSDj8wQB1v CkSMY4srk85fF7u4tituONtcwM0Vr+5f8Q343B9DRcIcHK9DOPMV5vaP4/V11FGZPz/W MehvLbpdKp5/eanPdKJVxMSJUJzLgjUt0+FO8OXH/1fSUR501w65CVBR0X6pnoGBu3wT DWUQwKufhWbE3uk3z9leY2WjnqBEwWJUM5p8EKKmCY7zfUZ4TNrgCZoIOwJEY+eBETCq ND4wX43u7joFAIUafJ/6yJttGVzZyykjDax0eMdMHm4Kw3/na0/rgMDR+Dj5jLpW0f9h irDA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=0nLHjLZEWeoCPYQYRYYEQJ82K5QeAbLNQ5aT9VvMDsQ=; b=A8DM4dKioNU3YwG5hm4ec7MkxHplcLfHYACsU8bqqID5lpkFujKtub5zhhimpsGJy6 5JzYRvVwF4eprr4edOOQfWHcE922XLXh5Iw8fmSTGDCEUZdW/HvuiEHErpB+npzXsXLt RtfC33vbMkA8iMaeDBSzKKM3jTjW2jBKvSIrGRY6czHGdHkO9IQsxqkWz35pkcDxiWcn Hhg45RuxChZr0qRlL2NcPILlZZzNTlhjYTHxiwQ4UdHlPa6NiQkLfAP5fwM9hds6fAQW +YhrEzXuqtZWjxTzqO2HrbgiD5gwXr5T/G73YKGLX/R5HL6ggOB5EH9X5daP5D6fvxvU Z4YQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@xry111.site header.s=default header.b=YKkmU3nt; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=xry111.site Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id nv18-20020a17090b1b5200b001fddccc9214si2123993pjb.171.2022.08.29.06.50.19; Mon, 29 Aug 2022 06:50:30 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@xry111.site header.s=default header.b=YKkmU3nt; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=xry111.site Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229970AbiH2Nfp (ORCPT + 99 others); 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: 8bit X-Spam-Status: No, score=-0.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FROM_SUSPICIOUS_NTLD, PDS_OTHER_BAD_TLD,SPF_HELO_PASS,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Binutils >= 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/kernel/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) for (i = 0; i < num; i++) { type = ELF_R_TYPE(relas[i].r_info); - if (type == 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, u32 *location, Elf_Addr v, } } +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 = (void *)v - (void *)location; + union loongarch_instruction *insn = (union loongarch_instruction *)location; + + if (offset >= SZ_128M) + v = module_emit_plt_entry(mod, v); + + if (offset < -SZ_128M) + v = module_emit_plt_entry(mod, v); + + offset = (void *)v - (void *)location; + + if (offset & 3) { + pr_err("module %s: jump offset = 0x%llx unaligned! dangerous R_LARCH_B26 (%u) relocation\n", + mod->name, (long long)offset, type); + return -ENOEXEC; + } + + if (!signed_imm_check(offset, 28)) { + pr_err("module %s: jump offset = 0x%llx overflow! dangerous R_LARCH_B26 (%u) relocation\n", + mod->name, (long long)offset, type); + return -ENOEXEC; + } + + offset >>= 2; + insn->reg0i26_format.immediate_l = offset & 0xffff; + insn->reg0i26_format.immediate_h = (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 = (union loongarch_instruction *)location; + /* Use s32 for a sign-extension deliberately. */ + s32 offset_hi20 = (void *)((v + 0x800) & ~0xfff) - + (void *)((Elf_Addr)location & ~0xfff); + Elf_Addr anchor = (((Elf_Addr)location) & ~0xfff) + offset_hi20; + ptrdiff_t offset_rem = (void *)v - (void *)anchor; + + switch (type) { + case R_LARCH_PCALA_HI20: + v = offset_hi20 >> 12; + break; + case R_LARCH_PCALA64_LO20: + v = offset_rem >> 32; + break; + case R_LARCH_PCALA64_HI12: + v = offset_rem >> 52; + break; + default: + /* Do nothing. */ + } + + switch (type) { + case R_LARCH_PCALA_HI20: + case R_LARCH_PCALA64_LO20: + insn->reg1i20_format.immediate = v & 0xfffff; + break; + case R_LARCH_PCALA_LO12: + case R_LARCH_PCALA64_HI12: + insn->reg2i12_format.immediate = 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[] = { [R_LARCH_SOP_SUB ... R_LARCH_SOP_IF_ELSE] = apply_r_larch_sop, [R_LARCH_SOP_POP_32_S_10_5 ... R_LARCH_SOP_POP_32_U] = apply_r_larch_sop_imm_field, [R_LARCH_ADD32 ... R_LARCH_SUB64] = apply_r_larch_add_sub, + [R_LARCH_B26] = apply_r_larch_b26, + [R_LARCH_PCALA_HI20...R_LARCH_PCALA64_HI12] = apply_r_larch_pcala, }; int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab, -- 2.37.0