Skip to content

Conversation

@MQ-mengqing
Copy link
Contributor

@MQ-mengqing MQ-mengqing commented Nov 14, 2023

The R_LARCH_{ADD,SUB}6 relocation type are usually used by DwarfCFA to calculate a tiny offset. They appear after binutils 2.41, with GAS enabling relaxation by default.

The R_LARCH_{ADD,SUB}6 relocation type are usually used by DwarfCFA to
calculate a tiny offset. They appear after binutils 2.41, with GAS
enable relaxation by default.
@llvmbot
Copy link
Member

llvmbot commented Nov 14, 2023

@llvm/pr-subscribers-lld

@llvm/pr-subscribers-lld-elf

Author: Jinyang He (MQ-mengqing)

Changes

The R_LARCH_{ADD,SUB}6 relocation type are usually used by DwarfCFA to calculate a tiny offset. They appear after binutils 2.41, with GAS enable relaxation by default.


Full diff: https://github.com/llvm/llvm-project/pull/72190.diff

2 Files Affected:

  • (modified) lld/ELF/Arch/LoongArch.cpp (+8)
  • (modified) lld/test/ELF/loongarch-add-sub.s (+5-1)
diff --git a/lld/ELF/Arch/LoongArch.cpp b/lld/ELF/Arch/LoongArch.cpp
index 04ddb4682917b4b..d3a538577a59a5d 100644
--- a/lld/ELF/Arch/LoongArch.cpp
+++ b/lld/ELF/Arch/LoongArch.cpp
@@ -444,10 +444,12 @@ RelExpr LoongArch::getRelExpr(const RelType type, const Symbol &s,
   case R_LARCH_TLS_LE64_LO20:
   case R_LARCH_TLS_LE64_HI12:
     return R_TPREL;
+  case R_LARCH_ADD6:
   case R_LARCH_ADD8:
   case R_LARCH_ADD16:
   case R_LARCH_ADD32:
   case R_LARCH_ADD64:
+  case R_LARCH_SUB6:
   case R_LARCH_SUB8:
   case R_LARCH_SUB16:
   case R_LARCH_SUB32:
@@ -650,6 +652,9 @@ void LoongArch::relocate(uint8_t *loc, const Relocation &rel,
     write32le(loc, setK12(read32le(loc), extractBits(val, 63, 52)));
     return;
 
+  case R_LARCH_ADD6:
+    *loc = (*loc & 0xc0) | ((*loc + val) & 0x3f);
+    return;
   case R_LARCH_ADD8:
     *loc += val;
     return;
@@ -662,6 +667,9 @@ void LoongArch::relocate(uint8_t *loc, const Relocation &rel,
   case R_LARCH_ADD64:
     write64le(loc, read64le(loc) + val);
     return;
+  case R_LARCH_SUB6:
+    *loc = (*loc & 0xc0) | ((*loc - val) & 0x3f);
+    return;
   case R_LARCH_SUB8:
     *loc -= val;
     return;
diff --git a/lld/test/ELF/loongarch-add-sub.s b/lld/test/ELF/loongarch-add-sub.s
index 63a3f7de179e6b8..35f8a053d69cef3 100644
--- a/lld/test/ELF/loongarch-add-sub.s
+++ b/lld/test/ELF/loongarch-add-sub.s
@@ -6,7 +6,7 @@
 # RUN: llvm-readelf -x .rodata %t.la64 | FileCheck --check-prefix=CHECK %s
 # CHECK:      section '.rodata':
 # CHECK-NEXT: 0x9876543210 10325476 98badcfe 804602be 79ffffff
-# CHECK-NEXT: 0x9876543220 804602be 804680
+# CHECK-NEXT: 0x9876543220 804602be 80468097
 
 .text
 .global _start
@@ -34,3 +34,7 @@ quux:
     .byte 0
     .reloc quux, R_LARCH_ADD8, 1b
     .reloc quux, R_LARCH_SUB8, 2b
+qux:
+    .byte 0b10000000
+    .reloc qux, R_LARCH_ADD6, qux
+    .reloc qux, R_LARCH_SUB6, 2b

@MQ-mengqing
Copy link
Contributor Author

Add: @SixWeining @xen0n @xry111 .

Copy link
Contributor

@xen0n xen0n left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@MaskRay
Copy link
Member

MaskRay commented Nov 14, 2023

They appear after binutils 2.41, with GAS enable relaxation by default.

They appear after binutils 2.41, with GAS enabling relaxation by default.

@SixWeining
Copy link
Contributor

Seems your email setting needs some changes. I see: [email protected]

@SixWeining SixWeining merged commit 72accbf into llvm:main Nov 15, 2023
zahiraam pushed a commit to zahiraam/llvm-project that referenced this pull request Nov 20, 2023
…#72190)

The R_LARCH_{ADD,SUB}6 relocation type are usually used by DwarfCFA to
calculate a tiny offset. They appear after binutils 2.41, with GAS
enabling relaxation by default.
leecheechen pushed a commit to leecheechen/llvm-project that referenced this pull request Jun 9, 2025
…#72190)

The R_LARCH_{ADD,SUB}6 relocation type are usually used by DwarfCFA to
calculate a tiny offset. They appear after binutils 2.41, with GAS
enabling relaxation by default.

(cherry picked from commit 72accbf)
Change-Id: Iad676e522f11c52e5dc381243f1df60edcef58f5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants