From patchwork Mon Jul 17 08:22:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: mengqinggang X-Patchwork-Id: 121117 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c923:0:b0:3e4:2afc:c1 with SMTP id j3csp975987vqt; Mon, 17 Jul 2023 01:23:09 -0700 (PDT) X-Google-Smtp-Source: APBJJlEiQzRjwzWGLDo49ZrJxvu5ktiMIeKmL5rwxeM2GBbY4CEov4YwdtyfBnRjHDTi9fT+Rtz4 X-Received: by 2002:aa7:d449:0:b0:51e:3bb0:aa44 with SMTP id q9-20020aa7d449000000b0051e3bb0aa44mr10379257edr.17.1689582188868; Mon, 17 Jul 2023 01:23:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689582188; cv=none; d=google.com; s=arc-20160816; b=skCrIEnuDXKD2wj16hXOyGlaNmju9eDHyu3lC88F6+SO2k4hbE/d52hEKDMWORZvsE bi8ZWrSdAjUCtazCGoXQUmbX6WtNG+0d10NCS48uUl9EkruYF0G9esanZ/SVzRVf2Xtz d8PgHvPRCipIasrqDa02Lit+u8tAxpLR63UJ/GbY0UfDdb2VZInnTTu9Z39ArdtWHRRH olCxPAhUPgPvEuoqECmU+K5fOKodEgD/T7MiatW+aL/Yw9VncNBDtcfteCj0lwpAh//r OiwmXXKoZYaDtz+2n51adkPdxz056OaHhZyGUHU7v+30wxN7D+Xjc6t7lyyzPlh1t6O+ UoRQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dmarc-filter :delivered-to; bh=AWj4MJzXNJpN3J92s5hQQ6xhxPZ93hF7Ie4Cp1WuWCk=; fh=kXwVnDgf0tvHCA5mYrprGjBOWzexk5p9zSuZNCICnQo=; b=IzOS2DnRa1+sve4Tc5l8F8Jq9HIYByqy/lTv9/YlMNuj6dNlko68cepaSt7pObZCn9 AzSeLd6+79CmTnPZSVIDBF4Juh9NdE/b6xPjUYQ4CNsB9Gouulqnblc2vs0chkA+rj0h wE+k+NHsm5SthWI28z+eFilv8iKsxv3dqe1sxd2qgEnqvN1hZllzaJSAVU3GbGTYm6Qo atkWpCKJnHkAuErpkqenZI8TJdQxMIwnljaevu8wN1vKt780Ijm9NBozEeb4PfI3eFwl pDRferyVdxatqQx/21+koTPDbTbCnoHVXp4WpHJpS7K7rsT4YAwt0DZNfkf5W85wIwgq 20zQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org" Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id c5-20020a056402120500b0051e1654f831si3917655edw.453.2023.07.17.01.23.08 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Jul 2023 01:23:08 -0700 (PDT) Received-SPF: pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 90856385B81D for ; Mon, 17 Jul 2023 08:22:44 +0000 (GMT) X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by sourceware.org (Postfix) with ESMTP id CB4493856DF8 for ; Mon, 17 Jul 2023 08:22:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CB4493856DF8 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=loongson.cn Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=loongson.cn Received: from loongson.cn (unknown [10.2.5.5]) by gateway (Coremail) with SMTP id _____8AxFvFH+rRkgdoFAA--.15524S3; Mon, 17 Jul 2023 16:22:31 +0800 (CST) Received: from 5.5.5 (unknown [10.2.5.5]) by localhost.localdomain (Coremail) with SMTP id AQAAf8CxF8xH+rRkf0ExAA--.30792S2; Mon, 17 Jul 2023 16:22:31 +0800 (CST) From: mengqinggang To: binutils@sourceware.org Cc: xuchenghua@loongson.cn, chenglulu@loongson.cn, liuzhensong@loongson.cn, xry111@xry111.site, i.swmail@xen0n.name, maskray@google.com, hejinyang@loongson.cn, mengqinggang Subject: [PATCH 1/2] LoongArch: Fix instruction immediate bug caused by sign-extend Date: Mon, 17 Jul 2023 16:22:28 +0800 Message-Id: <20230717082229.2149099-1-mengqinggang@loongson.cn> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-CM-TRANSID: AQAAf8CxF8xH+rRkf0ExAA--.30792S2 X-CM-SenderInfo: 5phqw15lqjwttqj6z05rqj20fqof0/ X-Coremail-Antispam: 1Uk129KBj93XoWxCFWfKw4Dtw18KFykCrWkZrc_yoW5ZrWfpr 13K395GFWrXw13tr1aqa4YgFn8tay09a47Jay2qr18uF4fWF4Dtr15JrZFkF1UuF1UAFW7 WF9rJFW2kw18AabCm3ZEXasCq-sJn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7KY7ZEXa sCq-sGcSsGvfJ3Ic02F40EFcxC0VAKzVAqx4xG6I80ebIjqfuFe4nvWSU5nxnvy29KBjDU 0xBIdaVrnRJUUUkFb4IE77IF4wAFF20E14v26r1j6r4UM7CY07I20VC2zVCF04k26cxKx2 IYs7xG6rWj6s0DM7CIcVAFz4kK6r1Y6r17M28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48v e4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Jr0_JF4l84ACjcxK6xIIjxv20xvEc7CjxVAFwI 0_Jr0_Gr1l84ACjcxK6I8E87Iv67AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVCY1x0267AK xVW8Jr0_Cr1UM2AIxVAIcxkEcVAq07x20xvEncxIr21l57IF6xkI12xvs2x26I8E6xACxx 1l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv 67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41l42xK82IYc2 Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s02 6x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r1q6r43MIIYrxkI7VAKI48JMIIF0x vE2Ix0cI8IcVAFwI0_Jr0_JF4lIxAIcVC0I7IYx2IY6xkF7I0E14v26r1j6r4UMIIF0xvE 42xK8VAvwI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxVWUJVW8JwCI42IY6I8E87Iv6x kF7I0E14v26r1j6r4UYxBIdaVFxhVjvjDU0xZFpf9x07j0FALUUUUU= X-Spam-Status: No, score=-12.3 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: binutils@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: binutils-bounces+ouuuleilei=gmail.com@sourceware.org Sender: "Binutils" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1771655333420423648 X-GMAIL-MSGID: 1771655333420423648 For extreme code mode, the instruction sequences is pcalau12i $t0, hi20 addi.d $t1, $zero, lo12 lu32i.d $t1, lo20 lu52i.d $t1, hi12 add.d $t1, $t0, $t1 If lo12 > 0x7ff, hi20 need to add 0x1, lo20 need to sub 0x1. If hi20 > 0x7ffff, lo20 need to add 0x1. bfd/ChangeLog: * elfnn-loongarch.c (RELOCATE_CALC_PC32_HI20): Redefined. (RELOCATE_CALC_PC64_HI32): Redefined. --- bfd/elfnn-loongarch.c | 59 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 49 insertions(+), 10 deletions(-) diff --git a/bfd/elfnn-loongarch.c b/bfd/elfnn-loongarch.c index d3d8419d80b..e9c408b2dff 100644 --- a/bfd/elfnn-loongarch.c +++ b/bfd/elfnn-loongarch.c @@ -2284,26 +2284,65 @@ loongarch_reloc_is_fatal (struct bfd_link_info *info, return fatal; } +/* If lo12 immediate > 0x7ff, because sign-extend caused by addi.d/ld.d, + hi20 immediate need to add 0x1. + For example: pc 0x120000000, symbol 0x120000812 + lo12 immediate is 0x812, 0x120000812 & 0xfff = 0x812 + hi20 immediate is 1, because lo12 imm > 0x7ff, symbol need to add 0x1000 + (((0x120000812 + 0x1000) & ~0xfff) - (0x120000000 & ~0xfff)) >> 12 = 0x1 + + At run: + pcalau12i $t0, hi20 (0x1) + $t0 = 0x120000000 + (0x1 << 12) = 0x120001000 + addi.d $t0, $t0, lo12 (0x812) + $t0 = 0x120001000 + 0xfffffffffffff812 (-(0x1000 - 0x812) = -0x7ee) + = 0x120001000 - 0x7ee (0x1000 - 0x7ee = 0x812) + = 0x120000812 + Without hi20 add 0x1000, the result 0x120000000 - 0x7ee = 0x11ffff812 is + error. + 0x1000 + sign-extend-to64(0x8xx) = 0x8xx. */ #define RELOCATE_CALC_PC32_HI20(relocation, pc) \ ({ \ bfd_vma __lo = (relocation) & ((bfd_vma)0xfff); \ - pc = pc & (~(bfd_vma)0xfff); \ + relocation = (relocation & ~(bfd_vma)0xfff) \ + - (pc & ~(bfd_vma)0xfff); \ if (__lo > 0x7ff) \ - { \ relocation += 0x1000; \ - } \ - relocation &= ~(bfd_vma)0xfff; \ - relocation -= pc; \ }) +/* For example: pc is 0x11000010000100, symbol is 0x1812348ffff812 + offset = (0x1812348ffff812 & ~0xfff) - (0x11000010000100 & ~0xfff) + = 0x712347ffff000 + lo12: 0x1812348ffff812 & 0xfff = 0x812 + hi20: 0x7ffff + 0x1(lo12 > 0x7ff) = 0x80000 + lo20: 0x71234 - 0x1(lo12 > 0x7ff) + 0x1(hi20 > 0x7ffff) + hi12: 0x0 + + pcalau12i $t1, hi20 (0x80000) + $t1 = 0x11000010000100 + sign-extend(0x80000 << 12) + = 0x11000010000100 + 0xffffffff80000000 + = 0x10ffff90000000 + addi.d $t0, $zero, lo12 (0x812) + $t0 = 0xfffffffffffff812 (if lo12 > 0x7ff, because sign-extend, + lo20 need to sub 0x1) + lu32i.d $t0, lo12 (0x71234) + $t0 = {0x71234, 0xfffff812} + = 0x71234fffff812 + lu52i.d $t0, hi12 (0x0) + $t0 = {0x0, 0x71234fffff812} + = 0x71234fffff812 + add.d $t1, $t1, $t0 + $t1 = 0x10ffff90000000 + 0x71234fffff812 + = 0x1812348ffff812. */ #define RELOCATE_CALC_PC64_HI32(relocation, pc) \ ({ \ - bfd_vma __lo = (relocation) & ((bfd_vma)0xfff); \ + bfd_vma __lo = (relocation & (bfd_vma)0xfff); \ + relocation = (relocation & ~(bfd_vma)0xfff) \ + - (pc & ~(bfd_vma)0xfff); \ if (__lo > 0x7ff) \ - { \ - relocation -= 0x100000000; \ - } \ - relocation -= (pc & ~(bfd_vma)0xffffffff); \ + relocation += (0x1000 - 0x100000000); \ + if (relocation & 0x80000000) \ + relocation += 0x100000000; \ }) static int