Skip to content

Commit f37e25b

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

File tree

3 files changed

+81
-1
lines changed

3 files changed

+81
-1
lines changed

lld/ELF/Config.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@ struct Config {
188188
StringRef zBtiReport = "none";
189189
StringRef zCetReport = "none";
190190
StringRef zPauthReport = "none";
191+
StringRef zZicfilpReport = "none";
192+
StringRef zZicfissReport = "none";
191193
bool ltoBBAddrMap;
192194
llvm::StringRef ltoBasicBlockSections;
193195
std::pair<llvm::StringRef, llvm::StringRef> thinLTOObjectSuffixReplace;
@@ -331,6 +333,8 @@ struct Config {
331333
bool zText;
332334
bool zRetpolineplt;
333335
bool zWxneeded;
336+
bool zForceZicfilp;
337+
bool zForceZicfiss;
334338
DiscardPolicy discard;
335339
GnuStackKind zGnustack;
336340
ICFLevel icf;

lld/ELF/Driver.cpp

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,13 @@ static void checkOptions() {
468468
error("-z pauth-report only supported on AArch64");
469469
}
470470

471+
if (config->emachine != EM_RISCV) {
472+
if (config->zZicfilpReport != "none")
473+
error("-z zicfilip-report only supported on RISC-V");
474+
if (config->zZicfissReport != "none")
475+
error("-z zicfiss-report only supported on RISC-V");
476+
}
477+
471478
if (config->emachine != EM_386 && config->emachine != EM_X86_64 &&
472479
config->zCetReport != "none")
473480
error("-z cet-report only supported on X86 and X86_64");
@@ -1465,6 +1472,8 @@ static void readConfigs(opt::InputArgList &args) {
14651472
config->zWxneeded = hasZOption(args, "wxneeded");
14661473
setUnresolvedSymbolPolicy(args);
14671474
config->power10Stubs = args.getLastArgValue(OPT_power10_stubs_eq) != "no";
1475+
config->zForceZicfilp = hasZOption(args, "force-zicfilp");
1476+
config->zForceZicfiss = hasZOption(args, "force-zicfiss");
14681477

14691478
if (opt::Arg *arg = args.getLastArg(OPT_eb, OPT_el)) {
14701479
if (arg->getOption().matches(OPT_eb))
@@ -1508,7 +1517,9 @@ static void readConfigs(opt::InputArgList &args) {
15081517

15091518
auto reports = {std::make_pair("bti-report", &config->zBtiReport),
15101519
std::make_pair("cet-report", &config->zCetReport),
1511-
std::make_pair("pauth-report", &config->zPauthReport)};
1520+
std::make_pair("pauth-report", &config->zPauthReport),
1521+
std::make_pair("zicfilp-report", &config->zZicfilpReport),
1522+
std::make_pair("zicfiss-report", &config->zZicfissReport)};
15121523
for (opt::Arg *arg : args.filtered(OPT_z)) {
15131524
std::pair<StringRef, StringRef> option =
15141525
StringRef(arg->getValue()).split('=');
@@ -2685,6 +2696,17 @@ static void readSecurityNotes() {
26852696
toString(f) + ": -z cet-report: file does not have "
26862697
"GNU_PROPERTY_X86_FEATURE_1_SHSTK property");
26872698

2699+
checkAndReportMissingFeature(
2700+
config->zZicfilpReport, features,
2701+
GNU_PROPERTY_RISCV_FEATURE_1_CFI_LP_SIMPLE,
2702+
toString(f) + ": -z zicfilp-report: file does not have "
2703+
"GNU_PROPERTY_RISCV_FEATURE_1_CFI_LP_SIMPLE property");
2704+
2705+
checkAndReportMissingFeature(
2706+
config->zZicfissReport, features, GNU_PROPERTY_RISCV_FEATURE_1_CFI_SS,
2707+
toString(f) + ": -z zicfiss-report: file does not have "
2708+
"GNU_PROPERTY_RISCV_FEATURE_1_CFI_SS property");
2709+
26882710
if (config->zForceBti && !(features & GNU_PROPERTY_AARCH64_FEATURE_1_BTI)) {
26892711
features |= GNU_PROPERTY_AARCH64_FEATURE_1_BTI;
26902712
if (config->zBtiReport == "none")
@@ -2697,6 +2719,24 @@ static void readSecurityNotes() {
26972719
"GNU_PROPERTY_X86_FEATURE_1_IBT property");
26982720
features |= GNU_PROPERTY_X86_FEATURE_1_IBT;
26992721
}
2722+
2723+
if (config->zForceZicfilp &&
2724+
!(features & GNU_PROPERTY_RISCV_FEATURE_1_CFI_LP_SIMPLE)) {
2725+
features |= GNU_PROPERTY_RISCV_FEATURE_1_CFI_LP_SIMPLE;
2726+
if (config->zZicfilpReport == "none")
2727+
warn(toString(f) +
2728+
": -z force-zicfilp: file does not have "
2729+
"GNU_PROPERTY_RISCV_FEATURE_1_CFI_LP_SIMPLE property");
2730+
}
2731+
2732+
if (config->zForceZicfiss &&
2733+
!(features & GNU_PROPERTY_RISCV_FEATURE_1_CFI_SS)) {
2734+
features |= GNU_PROPERTY_RISCV_FEATURE_1_CFI_SS;
2735+
if (config->zZicfissReport == "none")
2736+
warn(toString(f) + ": -z force-zicfiss: file does not have "
2737+
"GNU_PROPERTY_RISCV_FEATURE_1_CFI_SS property");
2738+
}
2739+
27002740
if (config->zPacPlt && !(features & GNU_PROPERTY_AARCH64_FEATURE_1_PAC)) {
27012741
warn(toString(f) + ": -z pac-plt: file does not have "
27022742
"GNU_PROPERTY_AARCH64_FEATURE_1_PAC property");
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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 (property note)
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: ]
36+

0 commit comments

Comments
 (0)