Skip to content

Conversation

wangleiat
Copy link
Contributor

@wangleiat wangleiat commented Sep 28, 2025

Match gas behavior: generate R_LARCH_MARK_LA relocation for la.abs.

Created using spr 1.3.5-bogner
@llvmbot
Copy link
Member

llvmbot commented Sep 28, 2025

@llvm/pr-subscribers-backend-loongarch

@llvm/pr-subscribers-backend-risc-v

Author: wanglei (wangleiat)

Changes

The relocation is used for edk2 builds.


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

6 Files Affected:

  • (modified) llvm/lib/ExecutionEngine/JITLink/ELF_riscv.cpp (+4)
  • (modified) llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp (+3)
  • (modified) llvm/lib/Target/LoongArch/AsmParser/LoongArchAsmParser.cpp (+3-1)
  • (modified) llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchMCAsmInfo.cpp (+1)
  • (modified) llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchMCCodeEmitter.cpp (+6)
  • (modified) llvm/test/MC/LoongArch/Macros/macros-la.s (+1)
diff --git a/llvm/lib/ExecutionEngine/JITLink/ELF_riscv.cpp b/llvm/lib/ExecutionEngine/JITLink/ELF_riscv.cpp
index 0dc97104610bb..b3f7a443d2238 100644
--- a/llvm/lib/ExecutionEngine/JITLink/ELF_riscv.cpp
+++ b/llvm/lib/ExecutionEngine/JITLink/ELF_riscv.cpp
@@ -919,6 +919,10 @@ class ELFLinkGraphBuilder_riscv : public ELFLinkGraphBuilder<ELFT> {
     uint32_t Type = Rel.getType(false);
     int64_t Addend = Rel.r_addend;
 
+    // ignore
+    if (Type == ELF::R_LARCH_MARK_LA)
+      return Error::success();
+
     if (Type == ELF::R_RISCV_RELAX) {
       if (BlockToFix.edges_empty())
         return make_error<StringError>(
diff --git a/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp b/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp
index d6268037dea86..dd1b1d3b2e943 100644
--- a/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp
+++ b/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp
@@ -781,6 +781,9 @@ void RuntimeDyldELF::resolveLoongArch64Relocation(const SectionEntry &Section,
   default:
     report_fatal_error("Relocation type not implemented yet!");
     break;
+  case ELF::R_LARCH_MARK_LA:
+    // ignore
+    break;
   case ELF::R_LARCH_32:
     support::ulittle32_t::ref{TargetPtr} =
         static_cast<uint32_t>(Value + Addend);
diff --git a/llvm/lib/Target/LoongArch/AsmParser/LoongArchAsmParser.cpp b/llvm/lib/Target/LoongArch/AsmParser/LoongArchAsmParser.cpp
index 5be4713b349ee..9b11201d0312d 100644
--- a/llvm/lib/Target/LoongArch/AsmParser/LoongArchAsmParser.cpp
+++ b/llvm/lib/Target/LoongArch/AsmParser/LoongArchAsmParser.cpp
@@ -957,8 +957,10 @@ void LoongArchAsmParser::emitLoadAddressAbs(MCInst &Inst, SMLoc IDLoc,
                              : Inst.getOperand(2).getExpr();
   InstSeq Insts;
 
+  // To distinguish between la.abs and %abs_hi20, la.abs will generate
+  // R_LARCH_MARK_LA and R_LARCH_ABS_HI20 relocations.
   Insts.push_back(
-      LoongArchAsmParser::Inst(LoongArch::LU12I_W, ELF::R_LARCH_ABS_HI20));
+      LoongArchAsmParser::Inst(LoongArch::LU12I_W, ELF::R_LARCH_MARK_LA));
   Insts.push_back(
       LoongArchAsmParser::Inst(LoongArch::ORI, ELF::R_LARCH_ABS_LO12));
 
diff --git a/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchMCAsmInfo.cpp b/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchMCAsmInfo.cpp
index 0d7761777cb7d..8ecb62d0ea7bb 100644
--- a/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchMCAsmInfo.cpp
+++ b/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchMCAsmInfo.cpp
@@ -32,6 +32,7 @@ static StringRef getLoongArchSpecifierName(uint16_t S) {
     return "b16";
   case ELF::R_LARCH_B21:
     return "b21";
+  case ELF::R_LARCH_MARK_LA:
   case ELF::R_LARCH_ABS_HI20:
     return "abs_hi20";
   case ELF::R_LARCH_ABS_LO12:
diff --git a/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchMCCodeEmitter.cpp b/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchMCCodeEmitter.cpp
index b7ead5e61ab81..603c6f77f05dc 100644
--- a/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchMCCodeEmitter.cpp
+++ b/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchMCCodeEmitter.cpp
@@ -161,6 +161,12 @@ LoongArchMCCodeEmitter::getExprOpValue(const MCInst &MI, const MCOperand &MO,
     case ELF::R_LARCH_B26:
       FixupKind = LoongArch::fixup_loongarch_b26;
       break;
+    case ELF::R_LARCH_MARK_LA:
+      // The R_LARCH_MARCH_LA is used for LoongArch edk2 builds.
+      Fixups.push_back(
+          MCFixup::create(0, MCConstantExpr::create(0, Ctx),
+                          FirstLiteralRelocationKind + ELF::R_LARCH_MARK_LA));
+      [[fallthrough]];
     case ELF::R_LARCH_ABS_HI20:
       FixupKind = LoongArch::fixup_loongarch_abs_hi20;
       break;
diff --git a/llvm/test/MC/LoongArch/Macros/macros-la.s b/llvm/test/MC/LoongArch/Macros/macros-la.s
index a732988ef1f1a..8022d5b038880 100644
--- a/llvm/test/MC/LoongArch/Macros/macros-la.s
+++ b/llvm/test/MC/LoongArch/Macros/macros-la.s
@@ -26,6 +26,7 @@ la.abs $a0, sym_abs
 # ABS-NEXT:   lu32i.d $a0, %abs64_lo20(sym_abs)
 # ABS-NEXT:   lu52i.d $a0, $a0, %abs64_hi12(sym_abs)
 # ABS-EMPTY:
+# RELOC-NEXT: R_LARCH_MARK_LA - 0x0
 # RELOC-NEXT: R_LARCH_ABS_HI20 sym_abs 0x0
 # RELOC-NEXT: R_LARCH_ABS_LO12 sym_abs 0x0
 # RELOC-NEXT: R_LARCH_ABS64_LO20 sym_abs 0x0

Created using spr 1.3.7
Copy link
Member

@heiher heiher left a comment

Choose a reason for hiding this comment

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

LGTM.

@heiher
Copy link
Member

heiher commented Sep 29, 2025

In the la-abi-specs, R_LARCH_MARK_LA is described as being used to mark la.abs. Should this be backported to LLVM 21?

@SixWeining
Copy link
Contributor

About the comments and the commit message: I don't want to tie this relocation to specific applications (edk2) because we don't know if there is any other user.

Created using spr 1.3.7
@wangleiat
Copy link
Contributor Author

In the la-abi-specs, R_LARCH_MARK_LA is described as being used to mark la.abs. Should this be backported to LLVM 21?

This is not actually required — it’s defined in ABI 1.0 and no longer used in 2.0. The relocation is generated only to satisfy specific software requirements (not an urgent need).

@wangleiat wangleiat merged commit 6b23f4f into main Sep 30, 2025
7 of 9 checks passed
@wangleiat wangleiat deleted the users/wangleiat/spr/loongarch-add-r_larch_mark_la-relocation-for-laabs-1 branch September 30, 2025 01:58
llvm-sync bot pushed a commit to arm/arm-toolchain that referenced this pull request Sep 30, 2025
Match gas behavior: generate `R_LARCH_MARK_LA` relocation for `la.abs`.

Reviewers: heiher, SixWeining

Reviewed By: SixWeining, heiher

Pull Request: llvm/llvm-project#161062
mahesh-attarde pushed a commit to mahesh-attarde/llvm-project that referenced this pull request Oct 3, 2025
Match gas behavior: generate `R_LARCH_MARK_LA` relocation for `la.abs`.

Reviewers: heiher, SixWeining

Reviewed By: SixWeining, heiher

Pull Request: llvm#161062
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.

4 participants