[LoongArch] Port over minimal applyFixup from RISCV
authorWANG Xuerui <git@xen0n.name>
Fri, 26 Aug 2022 04:12:26 +0000 (12:12 +0800)
committerWeining Lu <luweining@loongson.cn>
Fri, 26 Aug 2022 09:36:29 +0000 (17:36 +0800)
Many DebugInfo tests now pass with native builds.

Differential Revision: https://reviews.llvm.org/D132323

llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.cpp

index dc98475..f9a024f 100644 (file)
@@ -73,8 +73,27 @@ void LoongArchAsmBackend::applyFixup(const MCAssembler &Asm,
                                      MutableArrayRef<char> Data, uint64_t Value,
                                      bool IsResolved,
                                      const MCSubtargetInfo *STI) const {
-  // TODO: Apply the Value for given Fixup into the provided data fragment.
-  return;
+  if (!Value)
+    return; // Doesn't change encoding.
+
+  MCFixupKind Kind = Fixup.getKind();
+  if (Kind >= FirstLiteralRelocationKind)
+    return;
+  MCFixupKindInfo Info = getFixupKindInfo(Kind);
+  // TODO: Apply any target-specific value adjustments.
+
+  // Shift the value into position.
+  Value <<= Info.TargetOffset;
+
+  unsigned Offset = Fixup.getOffset();
+  unsigned NumBytes = alignTo(Info.TargetSize + Info.TargetOffset, 8) / 8;
+
+  assert(Offset + NumBytes <= Data.size() && "Invalid fixup offset!");
+  // For each byte of the fragment that the fixup touches, mask in the
+  // bits from the fixup value.
+  for (unsigned I = 0; I != NumBytes; ++I) {
+    Data[Offset + I] |= uint8_t((Value >> (I * 8)) & 0xff);
+  }
 }
 
 bool LoongArchAsmBackend::shouldForceRelocation(const MCAssembler &Asm,