Skip to content

Commit

Permalink
[LLD] Add force enable Zicfiss/Zicfilip(CFI extension) option
Browse files Browse the repository at this point in the history
  • Loading branch information
SuHo-llrr committed May 14, 2024
1 parent 1db55d6 commit 9363ff1
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lld/ELF/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ struct Config {
StringRef zBtiReport = "none";
StringRef zCetReport = "none";
StringRef zPauthReport = "none";
StringRef zZicfilpReport = "none";
StringRef zZicfissReport = "none";
bool ltoBBAddrMap;
llvm::StringRef ltoBasicBlockSections;
std::pair<llvm::StringRef, llvm::StringRef> thinLTOObjectSuffixReplace;
Expand Down Expand Up @@ -331,6 +333,8 @@ struct Config {
bool zText;
bool zRetpolineplt;
bool zWxneeded;
bool zForceZicfilp;
bool zForceZicfiss;
DiscardPolicy discard;
GnuStackKind zGnustack;
ICFLevel icf;
Expand Down
42 changes: 41 additions & 1 deletion lld/ELF/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,13 @@ static void checkOptions() {
error("-z pauth-report only supported on AArch64");
}

if (config->emachine != EM_RISCV) {
if (config->zZicfilpReport != "none")
error("-z zicfilip-report only supported on RISC-V");
if (config->zZicfissReport != "none")
error("-z zicfiss-report only supported on RISC-V");
}

if (config->emachine != EM_386 && config->emachine != EM_X86_64 &&
config->zCetReport != "none")
error("-z cet-report only supported on X86 and X86_64");
Expand Down Expand Up @@ -1465,6 +1472,8 @@ static void readConfigs(opt::InputArgList &args) {
config->zWxneeded = hasZOption(args, "wxneeded");
setUnresolvedSymbolPolicy(args);
config->power10Stubs = args.getLastArgValue(OPT_power10_stubs_eq) != "no";
config->zForceZicfilp = hasZOption(args, "force-zicfilp");
config->zForceZicfiss = hasZOption(args, "force-zicfiss");

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

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

checkAndReportMissingFeature(
config->zZicfilpReport, features,
GNU_PROPERTY_RISCV_FEATURE_1_CFI_LP_SIMPLE,
toString(f) + ": -z zicfilp-report: file does not have "
"GNU_PROPERTY_RISCV_FEATURE_1_CFI_LP_SIMPLE property");

checkAndReportMissingFeature(
config->zZicfissReport, features, GNU_PROPERTY_RISCV_FEATURE_1_CFI_SS,
toString(f) + ": -z zicfiss-report: file does not have "
"GNU_PROPERTY_RISCV_FEATURE_1_CFI_SS property");

if (config->zForceBti && !(features & GNU_PROPERTY_AARCH64_FEATURE_1_BTI)) {
features |= GNU_PROPERTY_AARCH64_FEATURE_1_BTI;
if (config->zBtiReport == "none")
Expand All @@ -2697,6 +2719,24 @@ static void readSecurityNotes() {
"GNU_PROPERTY_X86_FEATURE_1_IBT property");
features |= GNU_PROPERTY_X86_FEATURE_1_IBT;
}

if (config->zForceZicfilp &&
!(features & GNU_PROPERTY_RISCV_FEATURE_1_CFI_LP_SIMPLE)) {
features |= GNU_PROPERTY_RISCV_FEATURE_1_CFI_LP_SIMPLE;
if (config->zZicfilpReport == "none")
warn(toString(f) +
": -z force-zicfilp: file does not have "
"GNU_PROPERTY_RISCV_FEATURE_1_CFI_LP_SIMPLE property");
}

if (config->zForceZicfiss &&
!(features & GNU_PROPERTY_RISCV_FEATURE_1_CFI_SS)) {
features |= GNU_PROPERTY_RISCV_FEATURE_1_CFI_SS;
if (config->zZicfissReport == "none")
warn(toString(f) + ": -z force-zicfiss: file does not have "
"GNU_PROPERTY_RISCV_FEATURE_1_CFI_SS property");
}

if (config->zPacPlt && !(features & GNU_PROPERTY_AARCH64_FEATURE_1_PAC)) {
warn(toString(f) + ": -z pac-plt: file does not have "
"GNU_PROPERTY_AARCH64_FEATURE_1_PAC property");
Expand Down
36 changes: 36 additions & 0 deletions lld/test/ELF/riscv-force-cfi-property.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# REQUIRES: riscv

# RUN: llvm-mc -filetype=obj -triple=riscv32-unknown-elf %s -o %t.rv32_lp.o
# RUN: ld.lld %t.rv32_lp.o -zforce-zicfilp -o %t.rv32_lp | count 0
# RUN: llvm-readobj -n %t.rv32_lp | FileCheck -check-prefix=CHECK -check-prefix=CHECK_ZICFILP %s

# RUN: llvm-mc -filetype=obj -triple=riscv64-unknown-elf %s -o %t.rv64_lp.o
# RUN: ld.lld %t.rv64_lp.o -zforce-zicfilp -o %t.rv64_lp | count 0
# RUN: llvm-readobj -n %t.rv64_lp | FileCheck -check-prefix=CHECK -check-prefix=CHECK_ZICFILP %s

# RUN: llvm-mc -filetype=obj -triple=riscv32-unknown-elf %s -o %t.rv32_ss.o
# RUN: ld.lld %t.rv32_ss.o -zforce-zicfiss -o %t.rv32_ss | count 0
# RUN: llvm-readobj -n %t.rv32_ss | FileCheck -check-prefix=CHECK -check-prefix=CHECK_ZICFISS %s

# RUN: llvm-mc -filetype=obj -triple=riscv64-unknown-elf %s -o %t.rv64_ss.o
# RUN: ld.lld %t.rv64_ss.o -zforce-zicfiss -o %t.rv64_ss | count 0
# RUN: llvm-readobj -n %t.rv64_ss | FileCheck -check-prefix=CHECK -check-prefix=CHECK_ZICFISS %s

# RUN: llvm-mc -filetype=obj -triple=riscv32-unknown-elf %s -o %t.rv32_lp_ss.o
# RUN: ld.lld %t.rv32_lp_ss.o -zforce-zicfilp -zforce-zicfiss -o %t.rv32_lp_ss | count 0
# RUN: llvm-readobj -n %t.rv32_lp_ss | FileCheck -check-prefix=CHECK -check-prefix=CHECK_ZICFILP_ZICFISS %s

# RUN: llvm-mc -filetype=obj -triple=riscv64-unknown-elf %s -o %t.rv64_lp_ss.o
# RUN: ld.lld %t.rv64_lp_ss.o -zforce-zicfilp -zforce-zicfiss -o %t.rv64_lp_ss | count 0
# RUN: llvm-readobj -n %t.rv64_lp_ss | FileCheck -check-prefix=CHECK -check-prefix=CHECK_ZICFILP_ZICFISS %s



// CHECK: Name: .note.gnu.property
// CHECK: Type: NT_GNU_PROPERTY_TYPE_0 (property note)
// CHECK: Property [
// CHECK_ZICFISS: riscv feature: Zicfiss
// CHECK_ZICFILP: riscv feature: Zicfilp
// CHECK_ZICFILP_ZICFISS: riscv feature: Zicfilp, Zicfiss
// CHECK: ]

0 comments on commit 9363ff1

Please sign in to comment.