@@ -82,26 +82,30 @@ void CrossDSOCFI::buildCFICheck(Module &M) {
82
82
}
83
83
84
84
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);
88
92
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 ();
92
93
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
+ }
93
105
94
106
Triple T (M.getTargetTriple ());
95
107
if (T.isARM () || T.isThumb ())
96
108
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
- }
105
109
106
110
auto args = F->arg_begin ();
107
111
Value &CallSiteTypeId = *(args++);
0 commit comments