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 Feb 15, 2024
1 parent c2c980d commit 3114954
Show file tree
Hide file tree
Showing 3 changed files with 78 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 @@ -187,6 +187,8 @@ struct Config {
llvm::StringRef cmseOutputLib;
StringRef zBtiReport = "none";
StringRef zCetReport = "none";
llvm::StringRef zZicfilpReport = "none";
llvm::StringRef zZicfissReport = "none";
bool ltoBBAddrMap;
llvm::StringRef ltoBasicBlockSections;
std::pair<llvm::StringRef, llvm::StringRef> thinLTOObjectSuffixReplace;
Expand Down Expand Up @@ -324,6 +326,8 @@ struct Config {
bool zText;
bool zRetpolineplt;
bool zWxneeded;
bool zForceZicfilp;
bool zForceZicfiss;
DiscardPolicy discard;
GnuStackKind zGnustack;
ICFLevel icf;
Expand Down
40 changes: 39 additions & 1 deletion lld/ELF/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,13 @@ static void checkOptions() {
error("-z bti-report only supported on AArch64");
}

if (config->emachine != EM_RISCV) {
if (config->zZicfilpReport != "none")
error("-z zicfilip-report only support on RISCV32/RISCV64");
if (config->zZicfissReport != "none")
error("-z zicfiss-report only support on RISCV32/RISCV64");
}

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 @@ -1455,6 +1462,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 @@ -1497,7 +1506,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("cet-report", &config->zCetReport),
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 @@ -2592,6 +2603,16 @@ static uint32_t getAndFeatures() {
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_ZICFILP,
toString(f) + ": -z zicfilp-report: file does not have "
"GNU_PROPERTY_RISCV_FEATURE_1_ZICFILP property");

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

if (config->zForceBti && !(features & GNU_PROPERTY_AARCH64_FEATURE_1_BTI)) {
features |= GNU_PROPERTY_AARCH64_FEATURE_1_BTI;
if (config->zBtiReport == "none")
Expand All @@ -2604,6 +2625,23 @@ static uint32_t getAndFeatures() {
"GNU_PROPERTY_X86_FEATURE_1_IBT property");
features |= GNU_PROPERTY_X86_FEATURE_1_IBT;
}

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

if (config->zForceZicfiss &&
!(features & GNU_PROPERTY_RISCV_FEATURE_1_ZICFISS)) {
features |= GNU_PROPERTY_RISCV_FEATURE_1_ZICFISS;
if (config->zZicfissReport == "none")
warn(toString(f) + ": -z force-zicfiss: file does not have "
"GNU_PROPERTY_RISCV_FEATURE_1_ZICFISS 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
35 changes: 35 additions & 0 deletions lld/test/ELF/riscv-force-cfi-property.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# 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
// CHECK: Property [
// CHECK_ZICFISS: riscv feature: ZICFISS
// CHECK_ZICFILP: riscv feature: ZICFILP
// CHECK_ZICFILP_ZICFISS: riscv feature: ZICFILP, ZICFISS
// CHECK: ]

0 comments on commit 3114954

Please sign in to comment.