@@ -82,26 +82,30 @@ void CrossDSOCFI::buildCFICheck(Module &M) {
8282 }
8383
8484 LLVMContext &Ctx = M.getContext ();
85- FunctionCallee C = M.getOrInsertFunction (
86- " __cfi_check" , Type::getVoidTy (Ctx), Type::getInt64Ty (Ctx),
87- PointerType::getUnqual (Ctx), PointerType::getUnqual (Ctx));
85+ FunctionType *CFICheckTy =
86+ FunctionType::get (Type::getVoidTy (Ctx),
87+ {Type::getInt64Ty (Ctx), PointerType::getUnqual (Ctx),
88+ PointerType::getUnqual (Ctx)},
89+ false );
90+ FunctionCallee C = Function::createWithDefaultAttr (
91+ CFICheckTy, GlobalValue::ExternalLinkage, 0 , " __cfi_check" , &M);
8892 Function *F = cast<Function>(C.getCallee ());
89- // Take over the existing function. The frontend emits a weak stub so that the
90- // linker knows about the symbol; this pass replaces the function body.
91- F->deleteBody ();
9293 F->setAlignment (Align (4096 ));
94+ if (F->getName () != " __cfi_check" ) {
95+ // The frontend might have already created a function named __cfi_check;
96+ // delete it.
97+ GlobalValue *G = M.getNamedValue (" __cfi_check" );
98+ assert (G && " cfi_check must exist after we constructed it" );
99+ if (G->getAddressSpace () != F->getAddressSpace ())
100+ report_fatal_error (" __cfi_check with unexpected address space" );
101+ G->replaceAllUsesWith (F);
102+ F->takeName (G);
103+ G->eraseFromParent ();
104+ }
93105
94106 Triple T (M.getTargetTriple ());
95107 if (T.isARM () || T.isThumb ())
96108 F->addFnAttr (" target-features" , " +thumb-mode" );
97- if (T.isAArch64 ()) {
98- if (const auto *BTE = mdconst::extract_or_null<ConstantInt>(
99- M.getModuleFlag (" branch-target-enforcement" ))) {
100- if (BTE->getZExtValue () != 0 ) {
101- F->addFnAttr (" branch-target-enforcement" );
102- }
103- }
104- }
105109
106110 auto args = F->arg_begin ();
107111 Value &CallSiteTypeId = *(args++);
0 commit comments