public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: mengqinggang <mengqinggang@loongson.cn>
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 <mengqinggang@loongson.cn>
Subject: [PATCH 1/2] LoongArch: Fix instruction immediate bug caused by sign-extend
Date: Mon, 17 Jul 2023 16:22:28 +0800	[thread overview]
Message-ID: <20230717082229.2149099-1-mengqinggang@loongson.cn> (raw)

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
-- 
2.36.0


             reply	other threads:[~2023-07-17  8:22 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-17  8:22 mengqinggang [this message]
2023-07-17  8:22 ` [PATCH 2/2] LoongArch: Fix immediate overflow check bug mengqinggang
2023-07-21  3:56   ` mengqinggang
2023-07-22  7:47     ` Xi Ruoyao
2023-07-22  7:52     ` Xi Ruoyao
2023-07-22  8:23       ` Jinyang He
2023-07-24  9:14         ` Nick Clifton

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230717082229.2149099-1-mengqinggang@loongson.cn \
    --to=mengqinggang@loongson.cn \
    --cc=binutils@sourceware.org \
    --cc=chenglulu@loongson.cn \
    --cc=hejinyang@loongson.cn \
    --cc=i.swmail@xen0n.name \
    --cc=liuzhensong@loongson.cn \
    --cc=maskray@google.com \
    --cc=xry111@xry111.site \
    --cc=xuchenghua@loongson.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).