Skip to content

Commit 3114954

Browse files
committed
[LLD] Add force enable Zicfiss/Zicfilip(CFI extension) option
1 parent c2c980d commit 3114954

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed

lld/ELF/Config.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ struct Config {
187187
llvm::StringRef cmseOutputLib;
188188
StringRef zBtiReport = "none";
189189
StringRef zCetReport = "none";
190+
llvm::StringRef zZicfilpReport = "none";
191+
llvm::StringRef zZicfissReport = "none";
190192
bool ltoBBAddrMap;
191193
llvm::StringRef ltoBasicBlockSections;
192194
std::pair<llvm::StringRef, llvm::StringRef> thinLTOObjectSuffixReplace;
@@ -324,6 +326,8 @@ struct Config {
324326
bool zText;
325327
bool zRetpolineplt;
326328
bool zWxneeded;
329+
bool zForceZicfilp;
330+
bool zForceZicfiss;
327331
DiscardPolicy discard;
328332
GnuStackKind zGnustack;
329333
ICFLevel icf;

lld/ELF/Driver.cpp

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,13 @@ static void checkOptions() {
463463
error("-z bti-report only supported on AArch64");
464464
}
465465

466+
if (config->emachine != EM_RISCV) {
467+
if (config->zZicfilpReport != "none")
468+
error("-z zicfilip-report only support on RISCV32/RISCV64");
469+
if (config->zZicfissReport != "none")
470+
error("-z zicfiss-report only support on RISCV32/RISCV64");
471+
}
472+
466473
if (config->emachine != EM_386 && config->emachine != EM_X86_64 &&
467474
config->zCetReport != "none")
468475
error("-z cet-report only supported on X86 and X86_64");
@@ -1455,6 +1462,8 @@ static void readConfigs(opt::InputArgList &args) {
14551462
config->zWxneeded = hasZOption(args, "wxneeded");
14561463
setUnresolvedSymbolPolicy(args);
14571464
config->power10Stubs = args.getLastArgValue(OPT_power10_stubs_eq) != "no";
1465+
config->zForceZicfilp = hasZOption(args, "force-zicfilp");
1466+
config->zForceZicfiss = hasZOption(args, "force-zicfiss");
14581467

14591468
if (opt::Arg *arg = args.getLastArg(OPT_eb, OPT_el)) {
14601469
if (arg->getOption().matches(OPT_eb))
@@ -1497,7 +1506,9 @@ static void readConfigs(opt::InputArgList &args) {
14971506
}
14981507

14991508
auto reports = {std::make_pair("bti-report", &config->zBtiReport),
1500-
std::make_pair("cet-report", &config->zCetReport)};
1509+
std::make_pair("cet-report", &config->zCetReport),
1510+
std::make_pair("zicfilp-report", &config->zZicfilpReport),
1511+
std::make_pair("zicfiss-report", &config->zZicfissReport)};
15011512
for (opt::Arg *arg : args.filtered(OPT_z)) {
15021513
std::pair<StringRef, StringRef> option =
15031514
StringRef(arg->getValue()).split('=');
@@ -2592,6 +2603,16 @@ static uint32_t getAndFeatures() {
25922603
toString(f) + ": -z cet-report: file does not have "
25932604
"GNU_PROPERTY_X86_FEATURE_1_SHSTK property");
25942605

2606+
checkAndReportMissingFeature(
2607+
config->zZicfilpReport, features, GNU_PROPERTY_RISCV_FEATURE_1_ZICFILP,
2608+
toString(f) + ": -z zicfilp-report: file does not have "
2609+
"GNU_PROPERTY_RISCV_FEATURE_1_ZICFILP property");
2610+
2611+
checkAndReportMissingFeature(
2612+
config->zZicfissReport, features, GNU_PROPERTY_RISCV_FEATURE_1_ZICFISS,
2613+
toString(f) + ": -z zicfiss-report: file does not have "
2614+
"GNU_PROPERTY_RISCV_FEATURE_1_ZICFISS property");
2615+
25952616
if (config->zForceBti && !(features & GNU_PROPERTY_AARCH64_FEATURE_1_BTI)) {
25962617
features |= GNU_PROPERTY_AARCH64_FEATURE_1_BTI;
25972618
if (config->zBtiReport == "none")
@@ -2604,6 +2625,23 @@ static uint32_t getAndFeatures() {
26042625
"GNU_PROPERTY_X86_FEATURE_1_IBT property");
26052626
features |= GNU_PROPERTY_X86_FEATURE_1_IBT;
26062627
}
2628+
2629+
if (config->zForceZicfilp &&
2630+
!(features & GNU_PROPERTY_RISCV_FEATURE_1_ZICFILP)) {
2631+
features |= GNU_PROPERTY_RISCV_FEATURE_1_ZICFILP;
2632+
if (config->zZicfilpReport == "none")
2633+
warn(toString(f) + ": -z force-zicfilp: file does not have "
2634+
"GNU_PROPERTY_RISCV_FEATURE_1_ZICFILP property");
2635+
}
2636+
2637+
if (config->zForceZicfiss &&
2638+
!(features & GNU_PROPERTY_RISCV_FEATURE_1_ZICFISS)) {
2639+
features |= GNU_PROPERTY_RISCV_FEATURE_1_ZICFISS;
2640+
if (config->zZicfissReport == "none")
2641+
warn(toString(f) + ": -z force-zicfiss: file does not have "
2642+
"GNU_PROPERTY_RISCV_FEATURE_1_ZICFISS property");
2643+
}
2644+
26072645
if (config->zPacPlt && !(features & GNU_PROPERTY_AARCH64_FEATURE_1_PAC)) {
26082646
warn(toString(f) + ": -z pac-plt: file does not have "
26092647
"GNU_PROPERTY_AARCH64_FEATURE_1_PAC property");
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# REQUIRES: riscv
2+
3+
# RUN: llvm-mc -filetype=obj -triple=riscv32-unknown-elf %s -o %t.rv32_lp.o
4+
# RUN: ld.lld %t.rv32_lp.o -zforce-zicfilp -o %t.rv32_lp | count 0
5+
# RUN: llvm-readobj -n %t.rv32_lp | FileCheck -check-prefix=CHECK -check-prefix=CHECK_ZICFILP %s
6+
7+
# RUN: llvm-mc -filetype=obj -triple=riscv64-unknown-elf %s -o %t.rv64_lp.o
8+
# RUN: ld.lld %t.rv64_lp.o -zforce-zicfilp -o %t.rv64_lp | count 0
9+
# RUN: llvm-readobj -n %t.rv64_lp | FileCheck -check-prefix=CHECK -check-prefix=CHECK_ZICFILP %s
10+
11+
# RUN: llvm-mc -filetype=obj -triple=riscv32-unknown-elf %s -o %t.rv32_ss.o
12+
# RUN: ld.lld %t.rv32_ss.o -zforce-zicfiss -o %t.rv32_ss | count 0
13+
# RUN: llvm-readobj -n %t.rv32_ss | FileCheck -check-prefix=CHECK -check-prefix=CHECK_ZICFISS %s
14+
15+
# RUN: llvm-mc -filetype=obj -triple=riscv64-unknown-elf %s -o %t.rv64_ss.o
16+
# RUN: ld.lld %t.rv64_ss.o -zforce-zicfiss -o %t.rv64_ss | count 0
17+
# RUN: llvm-readobj -n %t.rv64_ss | FileCheck -check-prefix=CHECK -check-prefix=CHECK_ZICFISS %s
18+
19+
# RUN: llvm-mc -filetype=obj -triple=riscv32-unknown-elf %s -o %t.rv32_lp_ss.o
20+
# RUN: ld.lld %t.rv32_lp_ss.o -zforce-zicfilp -zforce-zicfiss -o %t.rv32_lp_ss | count 0
21+
# RUN: llvm-readobj -n %t.rv32_lp_ss | FileCheck -check-prefix=CHECK -check-prefix=CHECK_ZICFILP_ZICFISS %s
22+
23+
# RUN: llvm-mc -filetype=obj -triple=riscv64-unknown-elf %s -o %t.rv64_lp_ss.o
24+
# RUN: ld.lld %t.rv64_lp_ss.o -zforce-zicfilp -zforce-zicfiss -o %t.rv64_lp_ss | count 0
25+
# RUN: llvm-readobj -n %t.rv64_lp_ss | FileCheck -check-prefix=CHECK -check-prefix=CHECK_ZICFILP_ZICFISS %s
26+
27+
28+
29+
// CHECK: Name: .note.gnu.property
30+
// CHECK: Type: NT_GNU_PROPERTY_TYPE_0
31+
// CHECK: Property [
32+
// CHECK_ZICFISS: riscv feature: ZICFISS
33+
// CHECK_ZICFILP: riscv feature: ZICFILP
34+
// CHECK_ZICFILP_ZICFISS: riscv feature: ZICFILP, ZICFISS
35+
// CHECK: ]

0 commit comments

Comments
 (0)