[llvm] [RISCV] Vendor Relocations for Xqci extension (PR #135400)

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Sat May 17 09:23:43 PDT 2025


================
@@ -622,6 +598,129 @@ bool RISCVAsmBackend::evaluateTargetFixup(
   return AUIPCFixup->getTargetKind() == RISCV::fixup_riscv_pcrel_hi20;
 }
 
+StringRef
+RISCVAsmBackend::getVendorSymbolNameForRelocation(unsigned FixupKind) const {
+  switch (FixupKind) {
+  case RISCV::fixup_riscv_qc_e_branch:
+  case RISCV::fixup_riscv_qc_abs20_u:
+  case RISCV::fixup_riscv_qc_e_32:
+  case RISCV::fixup_riscv_qc_e_jump_plt:
+    return "QUALCOMM";
+  }
+
+  return "";
+}
+
+bool RISCVAsmBackend::evaluateVendorFixup(const MCAssembler &Asm,
+                                          const MCFixup &Fixup,
+                                          const MCFragment *DF,
+                                          const MCValue &Target,
+                                          const MCSubtargetInfo *STI,
+                                          uint64_t &Value, bool RecordReloc) {
+  // This is a copy of the target-independent branch of
+  // MCAssembler::evaluateFixup
+  bool IsResolved = false;
+  const MCSymbol *Add = Target.getAddSym();
+  const MCSymbol *Sub = Target.getSubSym();
+  Value = Target.getConstant();
+  if (Add && Add->isDefined())
+    Value += Asm.getSymbolOffset(*Add);
+  if (Sub && Sub->isDefined())
+    Value -= Asm.getSymbolOffset(*Sub);
+
+  unsigned FixupFlags = getFixupKindInfo(Fixup.getKind()).Flags;
+  if (FixupFlags & MCFixupKindInfo::FKF_IsPCRel) {
+    Value -= Asm.getFragmentOffset(*DF) + Fixup.getOffset();
+
+    if (Add && !Sub && !Add->isUndefined() && !Add->isAbsolute()) {
+      IsResolved = Asm.getWriter().isSymbolRefDifferenceFullyResolvedImpl(
+          Asm, *Add, *DF, false, true);
+    }
+  } else {
+    IsResolved = Target.isAbsolute();
+  }
+  // End copy of MCAssembler::evaluateFixup
+
+  // If we failed to resolve, or we need to force relocations (relaxations),
+  // then record a vendor relocation too.
+  if ((!IsResolved || shouldForceRelocation(Asm, Fixup, Target, STI)) &&
+      RecordReloc) {
+    // Here are the additions to emit a vendor relocation for fixups that need
+    // them.
+    MCContext &Ctx = Asm.getContext();
+
+    StringRef VendorIdentifier =
+        getVendorSymbolNameForRelocation(Fixup.getTargetKind());
+
+    auto It = VendorSymbols.find(VendorIdentifier);
+    if (It == VendorSymbols.end()) {
+      auto *VendorSymbol =
+          cast<MCSymbolELF>(Ctx.createLocalSymbol(VendorIdentifier));
+
+      // Vendor Symbols are Absolute, Local, NOTYPE.
+      VendorSymbol->setType(ELF::STT_NOTYPE);
----------------
MaskRay wrote:

The symbol is by default NOTYPE and LOCAL. In the rare scenario that the users changes the type/binding, we should respect that.

So setType/setBinding should be omitted.

https://github.com/llvm/llvm-project/pull/135400


More information about the llvm-commits mailing list