[llvm] [BOLT][BTI] Refactor: move applyBTIFixup under MCPlusBuilder (PR #177164)

Paschalis Mpeis via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 5 06:31:13 PST 2026


================
@@ -1733,6 +1733,69 @@ class AArch64MCPlusBuilder : public MCPlusBuilder {
     BC.createInstructionPatch(PLTFunction.getAddress(), NewPLTSeq);
   }
 
+  void applyBTIFixupToSymbol(BinaryContext &BC, const MCSymbol *TargetSymbol,
+                             MCInst &Call) override {
+    BinaryFunction *TargetFunction = BC.getFunctionForSymbol(TargetSymbol);
+    applyBTIFixupCommon(TargetSymbol, TargetFunction, nullptr, Call);
+  }
+
+  void applyBTIFixupToTarget(BinaryBasicBlock &StubBB) override {
+    BinaryFunction &Func = *StubBB.getFunction();
+    BinaryContext &BC = Func.getBinaryContext();
+    const MCSymbol *RealTargetSym = BC.MIB->getTargetSymbol(*StubBB.begin());
+    BinaryFunction *TargetFunction = BC.getFunctionForSymbol(RealTargetSym);
+    BinaryBasicBlock *TgtBB = Func.getBasicBlockForLabel(RealTargetSym);
+    applyBTIFixupCommon(RealTargetSym, TargetFunction, TgtBB,
+                        *StubBB.getLastNonPseudoInstr());
+  }
+
+  void applyBTIFixupCommon(const MCSymbol *RealTargetSym,
+                           BinaryFunction *TargetFunction,
+                           BinaryBasicBlock *TargetBB, MCInst &Call) override {
+    // TODO: add support for editing each type, and remove errors.
+    if (!TargetFunction && !TargetBB) {
+      errs() << "BOLT-ERROR: Cannot add BTI to function with symbol "
+             << RealTargetSym->getName() << "\n";
+      exit(1);
+    }
+    if (TargetFunction && TargetFunction->isPLTFunction()) {
+      patchPLTEntryForBTI(*TargetFunction, Call);
+      return;
+    }
+    if (TargetFunction && TargetFunction->isIgnored()) {
+      errs() << "BOLT-ERROR: Cannot add BTI landing pad to ignored function "
+             << TargetFunction->getPrintName() << "\n";
+      exit(1);
+    }
+    if (TargetFunction && !TargetFunction->hasCFG()) {
+      if (TargetFunction->hasInstructions()) {
+        auto FirstII = TargetFunction->instrs().begin();
+        MCInst FirstInst = FirstII->second;
+        if (isCallCoveredByBTI(Call, FirstInst))
+          return;
+      }
+      errs()
+          << "BOLT-ERROR: Cannot add BTI landing pad to function without CFG: "
+          << TargetFunction->getPrintName() << "\n";
+      exit(1);
+    }
+    if (!TargetBB)
+      // !TargetBB -> TargetFunction is not a nullptr
----------------
paschalis-mpeis wrote:

nitpick suggestion to put the comment above?
```suggestion
    // ... If target block not provided get the first one from function
    if (!TargetBB)
```

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


More information about the llvm-commits mailing list