[PATCH 3/3] LoongArch: Fix md_pcrel_from

mengqinggang mengqinggang@loongson.cn
Thu Aug 13 17:07:36 GMT 2026


On LoongArch, PC-relative offsets are relative to the start of the current
instruction. Previously, md_pcrel_from returned 0, which was incorrect.
Change it to return fixP->fx_frag->fr_address + fixP->fx_where.
---
 gas/config/tc-loongarch.c                       |  6 +++---
 gas/testsuite/gas/loongarch/reloc-abs-data.d    | 10 +++++-----
 gas/testsuite/gas/loongarch/reloc-abs-norelax.d | 10 +++++-----
 gas/testsuite/gas/loongarch/reloc-abs-pcrel.d   | 10 +++++-----
 4 files changed, 18 insertions(+), 18 deletions(-)

diff --git a/gas/config/tc-loongarch.c b/gas/config/tc-loongarch.c
index d9874e4408d..3884781f9f0 100644
--- a/gas/config/tc-loongarch.c
+++ b/gas/config/tc-loongarch.c
@@ -1556,12 +1556,12 @@ md_number_to_chars (char *buf, valueT val, int n)
   number_to_chars_littleendian (buf, val, n);
 }
 
-/* The location from which a PC relative jump should be calculated,
-   given a PC relative reloc.  */
+/* On LoongArch, PC-relative offset are relative to the start of the
+   current instruction.  */
 long
 md_pcrel_from (fixS *fixP ATTRIBUTE_UNUSED)
 {
-  return 0;
+  return fixP->fx_frag->fr_address + fixP->fx_where;
 }
 
 /* Return 1 if the relocation must be forced, and 0 if the relocation
diff --git a/gas/testsuite/gas/loongarch/reloc-abs-data.d b/gas/testsuite/gas/loongarch/reloc-abs-data.d
index 579bbbafd88..f0ef1d1b38a 100644
--- a/gas/testsuite/gas/loongarch/reloc-abs-data.d
+++ b/gas/testsuite/gas/loongarch/reloc-abs-data.d
@@ -5,14 +5,14 @@
 #...
 .*R_LARCH_ADD16	\*ABS\*\+0x2eef
 .*R_LARCH_SUB16	.L1\^B1
-.*R_LARCH_32_PCREL	\*ABS\*\+0x2eef
-.*R_LARCH_64_PCREL	\*ABS\*\+0x12345678
+.*R_LARCH_32_PCREL	\*ABS\*\+0x2ef5
+.*R_LARCH_64_PCREL	\*ABS\*\+0x12345682
 .*R_LARCH_ADD16	\*ABS\*\+0x2eef
 .*R_LARCH_SUB16	.L1\^B1
-.*R_LARCH_32_PCREL	\*ABS\*\+0x2eef
+.*R_LARCH_32_PCREL	\*ABS\*\+0x2f03
 .*R_LARCH_ADD8	\*ABS\*\+0x2e
 .*R_LARCH_SUB8	.L1\^B1
 .*R_LARCH_ADD16	\*ABS\*\+0x2eef
 .*R_LARCH_SUB16	.L1\^B1
-.*R_LARCH_32_PCREL	\*ABS\*\+0x2eef
-.*R_LARCH_64_PCREL	\*ABS\*\+0x12345678
+.*R_LARCH_32_PCREL	\*ABS\*\+0x2f0a
+.*R_LARCH_64_PCREL	\*ABS\*\+0x12345697
diff --git a/gas/testsuite/gas/loongarch/reloc-abs-norelax.d b/gas/testsuite/gas/loongarch/reloc-abs-norelax.d
index 06f3694f803..63d099166ed 100644
--- a/gas/testsuite/gas/loongarch/reloc-abs-norelax.d
+++ b/gas/testsuite/gas/loongarch/reloc-abs-norelax.d
@@ -5,14 +5,14 @@
 #...
 .*8: R_LARCH_ADD16	\*ABS\*\+0x2eef
 .*8: R_LARCH_SUB16	.L1\^B1
-.*a: R_LARCH_32_PCREL	\*ABS\*\+0x2eef
-.*e: R_LARCH_64_PCREL	\*ABS\*\+0x12345678
+.*a: R_LARCH_32_PCREL	\*ABS\*\+0x2ef9
+.*e: R_LARCH_64_PCREL	\*ABS\*\+0x12345686
 .*16: R_LARCH_ADD16	\*ABS\*\+0x2eef
 .*16: R_LARCH_SUB16	.L1\^B1
-.*18: R_LARCH_32_PCREL	\*ABS\*\+0x2eef
+.*18: R_LARCH_32_PCREL	\*ABS\*\+0x2f07
 .*1c: R_LARCH_ADD8	\*ABS\*\+0x2e
 .*1c: R_LARCH_SUB8	.L1\^B1
 .*1d: R_LARCH_ADD16	\*ABS\*\+0x2eef
 .*1d: R_LARCH_SUB16	.L1\^B1
-.*1f: R_LARCH_32_PCREL	\*ABS\*\+0x2eef
-.*23: R_LARCH_64_PCREL	\*ABS\*\+0x12345678
+.*1f: R_LARCH_32_PCREL	\*ABS\*\+0x2f0e
+.*23: R_LARCH_64_PCREL	\*ABS\*\+0x1234569b
diff --git a/gas/testsuite/gas/loongarch/reloc-abs-pcrel.d b/gas/testsuite/gas/loongarch/reloc-abs-pcrel.d
index 8403c2c4c1d..09545294b21 100644
--- a/gas/testsuite/gas/loongarch/reloc-abs-pcrel.d
+++ b/gas/testsuite/gas/loongarch/reloc-abs-pcrel.d
@@ -5,14 +5,14 @@
 #...
 .*R_LARCH_ADD16	\*ABS\*\+0x2eef
 .*R_LARCH_SUB16	.L1\^B1
-.*R_LARCH_32_PCREL	\*ABS\*\+0x2eef
-.*R_LARCH_64_PCREL	\*ABS\*\+0x12345678
+.*R_LARCH_32_PCREL	\*ABS\*\+0x2ef5
+.*R_LARCH_64_PCREL	\*ABS\*\+0x12345682
 .*R_LARCH_ADD16	\*ABS\*\+0x2eef
 .*R_LARCH_SUB16	.L1\^B1
-.*R_LARCH_32_PCREL	\*ABS\*\+0x2eef
+.*R_LARCH_32_PCREL	\*ABS\*\+0x2f03
 .*R_LARCH_ADD8	\*ABS\*\+0x2e
 .*R_LARCH_SUB8	.L1\^B1
 .*R_LARCH_ADD16	\*ABS\*\+0x2eef
 .*R_LARCH_SUB16	.L1\^B1
-.*R_LARCH_32_PCREL	\*ABS\*\+0x2eef
-.*R_LARCH_64_PCREL	\*ABS\*\+0x12345678
+.*R_LARCH_32_PCREL	\*ABS\*\+0x2f0a
+.*R_LARCH_64_PCREL	\*ABS\*\+0x12345697
-- 
2.34.1



More information about the Binutils mailing list