@@ -76,7 +76,7 @@ static bool InsertStackProtectors(const TargetMachine *TM, Function *F,
7676
7777// / CreateFailBB - Create a basic block to jump to when the stack protector
7878// / check fails.
79- static BasicBlock *CreateFailBB (Function *F, const Triple &Trip );
79+ static BasicBlock *CreateFailBB (Function *F, const TargetLowering &TLI );
8080
8181bool SSPLayoutInfo::shouldEmitSDCheck (const BasicBlock &BB) const {
8282 return HasPrologue && !HasIRCheck && isa<ReturnInst>(BB.getTerminator ());
@@ -673,7 +673,7 @@ bool InsertStackProtectors(const TargetMachine *TM, Function *F,
673673 // merge pass will merge together all of the various BB into one including
674674 // fail BB generated by the stack protector pseudo instruction.
675675 if (!FailBB)
676- FailBB = CreateFailBB (F, TM-> getTargetTriple () );
676+ FailBB = CreateFailBB (F, *TLI );
677677
678678 IRBuilder<> B (CheckLoc);
679679 Value *Guard = getStackGuard (TLI, M, B);
@@ -706,7 +706,7 @@ bool InsertStackProtectors(const TargetMachine *TM, Function *F,
706706 return HasPrologue;
707707}
708708
709- BasicBlock *CreateFailBB (Function *F, const Triple &Trip ) {
709+ BasicBlock *CreateFailBB (Function *F, const TargetLowering &TLI ) {
710710 auto *M = F->getParent ();
711711 LLVMContext &Context = F->getContext ();
712712 BasicBlock *FailBB = BasicBlock::Create (Context, " CallStackCheckFailBlk" , F);
@@ -716,17 +716,25 @@ BasicBlock *CreateFailBB(Function *F, const Triple &Trip) {
716716 DILocation::get (Context, 0 , 0 , F->getSubprogram ()));
717717 FunctionCallee StackChkFail;
718718 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),
722727 PointerType::getUnqual (Context));
723728 Args.push_back (B.CreateGlobalString (F->getName (), " SSH" ));
724729 } else {
725- StackChkFail =
726- M->getOrInsertFunction (" __stack_chk_fail" , Type::getVoidTy (Context));
730+ Context.emitError (" no libcall available for stack protector" );
727731 }
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+
730738 B.CreateUnreachable ();
731739 return FailBB;
732740}
0 commit comments