[PATCH] D132019: [Object] Support LoongArch in RelocationResolver

WÁNG Xuěruì via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 17 02:53:24 PDT 2022


xen0n created this revision.
xen0n added reviewers: SixWeining, wangleiat, MaskRay, xry111.
Herald added subscribers: StephenFan, hiraditya.
Herald added a project: All.
xen0n requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

With this patch applied, llvm-dwarfdump works on existing LoongArch
object files, but generation of debuginfo on LoongArch is still pending
on proper support for relocations, so no test cases this time. They will
come later.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D132019

Files:
  llvm/lib/Object/RelocationResolver.cpp


Index: llvm/lib/Object/RelocationResolver.cpp
===================================================================
--- llvm/lib/Object/RelocationResolver.cpp
+++ llvm/lib/Object/RelocationResolver.cpp
@@ -511,6 +511,60 @@
   }
 }
 
+static bool supportsLoongArch(uint64_t Type) {
+  switch (Type) {
+  case ELF::R_LARCH_NONE:
+  case ELF::R_LARCH_32:
+  case ELF::R_LARCH_32_PCREL:
+  case ELF::R_LARCH_64:
+  case ELF::R_LARCH_ADD8:
+  case ELF::R_LARCH_SUB8:
+  case ELF::R_LARCH_ADD16:
+  case ELF::R_LARCH_SUB16:
+  case ELF::R_LARCH_ADD32:
+  case ELF::R_LARCH_SUB32:
+  case ELF::R_LARCH_ADD64:
+  case ELF::R_LARCH_SUB64:
+    return true;
+  default:
+    return false;
+  }
+}
+
+static uint64_t resolveLoongArch(uint64_t Type, uint64_t Offset, uint64_t S,
+                                 uint64_t LocData, int64_t Addend) {
+  int64_t RA = Addend;
+  uint64_t A = LocData;
+  switch (Type) {
+  case ELF::R_LARCH_NONE:
+    return LocData;
+  case ELF::R_LARCH_32:
+    return (S + RA) & 0xFFFFFFFF;
+  case ELF::R_LARCH_32_PCREL:
+    return (S + RA - Offset) & 0xFFFFFFFF;
+  case ELF::R_LARCH_64:
+    return S + RA;
+  case ELF::R_LARCH_ADD8:
+    return (A + (S + RA)) & 0xFF;
+  case ELF::R_LARCH_SUB8:
+    return (A - (S + RA)) & 0xFF;
+  case ELF::R_LARCH_ADD16:
+    return (A + (S + RA)) & 0xFFFF;
+  case ELF::R_LARCH_SUB16:
+    return (A - (S + RA)) & 0xFFFF;
+  case ELF::R_LARCH_ADD32:
+    return (A + (S + RA)) & 0xFFFFFFFF;
+  case ELF::R_LARCH_SUB32:
+    return (A - (S + RA)) & 0xFFFFFFFF;
+  case ELF::R_LARCH_ADD64:
+    return (A + (S + RA));
+  case ELF::R_LARCH_SUB64:
+    return (A - (S + RA));
+  default:
+    llvm_unreachable("Invalid relocation type");
+  }
+}
+
 static bool supportsCOFFX86(uint64_t Type) {
   switch (Type) {
   case COFF::IMAGE_REL_I386_SECREL:
@@ -711,6 +765,8 @@
       case Triple::bpfel:
       case Triple::bpfeb:
         return {supportsBPF, resolveBPF};
+      case Triple::loongarch64:
+        return {supportsLoongArch, resolveLoongArch};
       case Triple::mips64el:
       case Triple::mips64:
         return {supportsMips64, resolveMips64};
@@ -747,6 +803,8 @@
       return {supportsAVR, resolveAVR};
     case Triple::lanai:
       return {supportsLanai, resolveLanai};
+    case Triple::loongarch32:
+      return {supportsLoongArch, resolveLoongArch};
     case Triple::mipsel:
     case Triple::mips:
       return {supportsMips32, resolveMips32};


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D132019.453242.patch
Type: text/x-patch
Size: 2433 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220817/e73f87eb/attachment.bin>


More information about the llvm-commits mailing list