Index: clang/lib/CodeGen/CGExpr.cpp =================================================================== --- clang/lib/CodeGen/CGExpr.cpp +++ clang/lib/CodeGen/CGExpr.cpp @@ -2928,6 +2928,18 @@ llvm::CallInst::Create( llvm::Intrinsic::getDeclaration(M, llvm::Intrinsic::trap), "", BB); llvm::ReturnInst::Create(Ctx, nullptr, BB); + + // Set default function attributes, but force thumb encoding if applicable. + const CGFunctionInfo &FI = + CGM.getTypes().arrangeBuiltinFunctionDeclaration(getContext().VoidTy, {}); + FunctionDecl *FD = + FunctionDecl::Create(getContext(), getContext().getTranslationUnitDecl(), + {}, {}, {}, getContext().VoidTy, nullptr, SC_Extern); + const auto &Triple = getTarget().getTriple(); + if (Triple.isARM() || Triple.isThumb()) { + FD->addAttr(TargetAttr::CreateImplicit(getContext(), "thumb")); + } + CGM.SetLLVMFunctionAttributes(FD, FI, F); } // This function is basically a switch over the CFI failure kind, which is @@ -3011,6 +3023,9 @@ } FinishFunction(); + + CGM.SetLLVMFunctionAttributes(nullptr, FI, F); + // The only reference to this function will be created during LTO link. // Make sure it survives until then. CGM.addUsedGlobal(F); Index: clang/test/CodeGen/cfi-check-thumb.c =================================================================== --- /dev/null +++ clang/test/CodeGen/cfi-check-thumb.c @@ -0,0 +1,18 @@ +// Test that __cfi_check and __cfi_check_fail have common attributes and calling convention. +// Also __cfi_check is always using Thumb encoding. +// RUN: %clang_cc1 -triple armv7-linux-android -O0 -fsanitize-cfi-cross-dso \ +// RUN: -fsanitize=cfi-vcall \ +// RUN: -emit-llvm -o - %s | FileCheck --check-prefixes=CHECK,CHECK-ARM %s +// +// RUN: %clang_cc1 -triple thumbv7-linux-android -O0 -fsanitize-cfi-cross-dso \ +// RUN: -fsanitize=cfi-vcall \ +// RUN: -emit-llvm -o - %s | FileCheck --check-prefixes=CHECK,CHECK-THUMB %s +// +// REQUIRES: arm-registered-target + +// CHECK-ARM: define weak_odr hidden void @__cfi_check_fail(i8*, i8*) #[[ARM:.*]] { +// CHECK-THUMB: define weak_odr hidden void @__cfi_check_fail(i8*, i8*) #[[THUMB:.*]] { +// CHECK: define weak void @__cfi_check(i64, i8*, i8*) #[[THUMB:.*]] { + +// CHECK-ARM: attributes #[[ARM]] = {{.*}}"target-features"="{{.*}}-thumb-mode +// CHECK: attributes #[[THUMB]] = {{.*}}"target-features"="{{.*}}+thumb-mode Index: llvm/lib/Transforms/IPO/CrossDSOCFI.cpp =================================================================== --- llvm/lib/Transforms/IPO/CrossDSOCFI.cpp +++ llvm/lib/Transforms/IPO/CrossDSOCFI.cpp @@ -117,10 +117,6 @@ F->deleteBody(); F->setAlignment(4096); - Triple T(M.getTargetTriple()); - if (T.isARM() || T.isThumb()) - F->addFnAttr("target-features", "+thumb-mode"); - auto args = F->arg_begin(); Value &CallSiteTypeId = *(args++); CallSiteTypeId.setName("CallSiteTypeId"); Index: llvm/test/Transforms/CrossDSOCFI/thumb.ll =================================================================== --- llvm/test/Transforms/CrossDSOCFI/thumb.ll +++ /dev/null @@ -1,22 +0,0 @@ -; RUN: opt -mtriple=armv7-linux-android -S -cross-dso-cfi < %s | FileCheck --check-prefix=THUMB %s -; RUN: opt -mtriple=thumbv7-linux-android -S -cross-dso-cfi < %s | FileCheck --check-prefix=THUMB %s -; RUN: opt -mtriple=i386-linux -S -cross-dso-cfi < %s | FileCheck --check-prefix=NOTHUMB %s -; RUN: opt -mtriple=x86_64-linux -S -cross-dso-cfi < %s | FileCheck --check-prefix=NOTHUMB %s - -target datalayout = "e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64" - -define signext i8 @f() !type !0 !type !1 { -entry: - ret i8 1 -} - -!llvm.module.flags = !{!2} - -!0 = !{i64 0, !"_ZTSFcvE"} -!1 = !{i64 0, i64 111} -!2 = !{i32 4, !"Cross-DSO CFI", i32 1} - -; THUMB: define void @__cfi_check({{.*}} #[[A:.*]] align 4096 -; THUMB: attributes #[[A]] = { {{.*}}"target-features"="+thumb-mode" - -; NOTHUMB: define void @__cfi_check({{.*}} align 4096