@@ -1179,6 +1179,7 @@ void LowerTypeTestsModule::verifyTypeMDNode(GlobalObject *GO, MDNode *Type) {
1179
1179
}
1180
1180
1181
1181
static const unsigned kX86JumpTableEntrySize = 8 ;
1182
+ static const unsigned kX86IBTJumpTableEntrySize = 16 ;
1182
1183
static const unsigned kARMJumpTableEntrySize = 4 ;
1183
1184
static const unsigned kARMBTIJumpTableEntrySize = 8 ;
1184
1185
static const unsigned kRISCVJumpTableEntrySize = 8 ;
@@ -1187,6 +1188,10 @@ unsigned LowerTypeTestsModule::getJumpTableEntrySize() {
1187
1188
switch (Arch) {
1188
1189
case Triple::x86:
1189
1190
case Triple::x86_64:
1191
+ if (const auto *MD = mdconst::extract_or_null<ConstantInt>(
1192
+ M.getModuleFlag (" cf-protection-branch" )))
1193
+ if (MD->getZExtValue ())
1194
+ return kX86IBTJumpTableEntrySize ;
1190
1195
return kX86JumpTableEntrySize ;
1191
1196
case Triple::arm:
1192
1197
case Triple::thumb:
@@ -1215,8 +1220,17 @@ void LowerTypeTestsModule::createJumpTableEntry(
1215
1220
unsigned ArgIndex = AsmArgs.size ();
1216
1221
1217
1222
if (JumpTableArch == Triple::x86 || JumpTableArch == Triple::x86_64) {
1223
+ bool Endbr = false ;
1224
+ if (const auto *MD = mdconst::extract_or_null<ConstantInt>(
1225
+ Dest->getParent ()->getModuleFlag (" cf-protection-branch" )))
1226
+ Endbr = MD->getZExtValue () != 0 ;
1227
+ if (Endbr)
1228
+ AsmOS << (JumpTableArch == Triple::x86 ? " endbr32\n " : " endbr64\n " );
1218
1229
AsmOS << " jmp ${" << ArgIndex << " :c}@plt\n " ;
1219
- AsmOS << " int3\n int3\n int3\n " ;
1230
+ if (Endbr)
1231
+ AsmOS << " .balign 16, 0xcc\n " ;
1232
+ else
1233
+ AsmOS << " int3\n int3\n int3\n " ;
1220
1234
} else if (JumpTableArch == Triple::arm) {
1221
1235
AsmOS << " b $" << ArgIndex << " \n " ;
1222
1236
} else if (JumpTableArch == Triple::aarch64) {
@@ -1389,6 +1403,9 @@ void LowerTypeTestsModule::createJumpTable(
1389
1403
// by Clang for -march=armv7.
1390
1404
F->addFnAttr (" target-cpu" , " cortex-a8" );
1391
1405
}
1406
+ // When -mbranch-protection= is used, the inline asm adds a BTI. Suppress BTI
1407
+ // for the function to avoid double BTI. This is a no-op without
1408
+ // -mbranch-protection=.
1392
1409
if (JumpTableArch == Triple::aarch64) {
1393
1410
F->addFnAttr (" branch-target-enforcement" , " false" );
1394
1411
F->addFnAttr (" sign-return-address" , " none" );
@@ -1398,6 +1415,11 @@ void LowerTypeTestsModule::createJumpTable(
1398
1415
// the linker.
1399
1416
F->addFnAttr (" target-features" , " -c,-relax" );
1400
1417
}
1418
+ // When -fcf-protection= is used, the inline asm adds an ENDBR. Suppress ENDBR
1419
+ // for the function to avoid double ENDBR. This is a no-op without
1420
+ // -fcf-protection=.
1421
+ if (JumpTableArch == Triple::x86 || JumpTableArch == Triple::x86_64)
1422
+ F->addFnAttr (Attribute::NoCfCheck);
1401
1423
// Make sure we don't emit .eh_frame for this function.
1402
1424
F->addFnAttr (Attribute::NoUnwind);
1403
1425
0 commit comments