@@ -76,7 +76,7 @@ static bool InsertStackProtectors(const TargetMachine *TM, Function *F,
76
76
77
77
// / CreateFailBB - Create a basic block to jump to when the stack protector
78
78
// / check fails.
79
- static BasicBlock *CreateFailBB (Function *F, const Triple &Trip );
79
+ static BasicBlock *CreateFailBB (Function *F, const TargetLowering &TLI );
80
80
81
81
bool SSPLayoutInfo::shouldEmitSDCheck (const BasicBlock &BB) const {
82
82
return HasPrologue && !HasIRCheck && isa<ReturnInst>(BB.getTerminator ());
@@ -673,7 +673,7 @@ bool InsertStackProtectors(const TargetMachine *TM, Function *F,
673
673
// merge pass will merge together all of the various BB into one including
674
674
// fail BB generated by the stack protector pseudo instruction.
675
675
if (!FailBB)
676
- FailBB = CreateFailBB (F, TM-> getTargetTriple () );
676
+ FailBB = CreateFailBB (F, *TLI );
677
677
678
678
IRBuilder<> B (CheckLoc);
679
679
Value *Guard = getStackGuard (TLI, M, B);
@@ -706,7 +706,7 @@ bool InsertStackProtectors(const TargetMachine *TM, Function *F,
706
706
return HasPrologue;
707
707
}
708
708
709
- BasicBlock *CreateFailBB (Function *F, const Triple &Trip ) {
709
+ BasicBlock *CreateFailBB (Function *F, const TargetLowering &TLI ) {
710
710
auto *M = F->getParent ();
711
711
LLVMContext &Context = F->getContext ();
712
712
BasicBlock *FailBB = BasicBlock::Create (Context, " CallStackCheckFailBlk" , F);
@@ -716,17 +716,25 @@ BasicBlock *CreateFailBB(Function *F, const Triple &Trip) {
716
716
DILocation::get (Context, 0 , 0 , F->getSubprogram ()));
717
717
FunctionCallee StackChkFail;
718
718
SmallVector<Value *, 1 > Args;
719
- if (Trip.isOSOpenBSD ()) {
720
- StackChkFail = M->getOrInsertFunction (" __stack_smash_handler" ,
721
- Type::getVoidTy (Context),
719
+
720
+ if (const char *ChkFailName =
721
+ TLI.getLibcallName (RTLIB::STACKPROTECTOR_CHECK_FAIL)) {
722
+ StackChkFail =
723
+ M->getOrInsertFunction (ChkFailName, Type::getVoidTy (Context));
724
+ } else if (const char *SSHName =
725
+ TLI.getLibcallName (RTLIB::STACK_SMASH_HANDLER)) {
726
+ StackChkFail = M->getOrInsertFunction (SSHName, Type::getVoidTy (Context),
722
727
PointerType::getUnqual (Context));
723
728
Args.push_back (B.CreateGlobalString (F->getName (), " SSH" ));
724
729
} else {
725
- StackChkFail =
726
- M->getOrInsertFunction (" __stack_chk_fail" , Type::getVoidTy (Context));
730
+ Context.emitError (" no libcall available for stack protector" );
727
731
}
728
- cast<Function>(StackChkFail.getCallee ())->addFnAttr (Attribute::NoReturn);
729
- B.CreateCall (StackChkFail, Args);
732
+
733
+ if (StackChkFail) {
734
+ cast<Function>(StackChkFail.getCallee ())->addFnAttr (Attribute::NoReturn);
735
+ B.CreateCall (StackChkFail, Args);
736
+ }
737
+
730
738
B.CreateUnreachable ();
731
739
return FailBB;
732
740
}
0 commit comments