[clang] [lld] [llvm] [RISCV] Support .note.gnu.property for enable Zicfiss and Zicfilp extension (PR #77414)
via cfe-commits
cfe-commits at lists.llvm.org
Thu May 9 23:29:25 PDT 2024
https://github.com/SuHo-llrr updated https://github.com/llvm/llvm-project/pull/77414
>From 61aaf8df2fbd0abfa4dfcd807f3366ce67f5603d Mon Sep 17 00:00:00 2001
From: SuHsien Ho <su-hsien.ho at mediatek.com>
Date: Thu, 23 Nov 2023 14:36:43 +0800
Subject: [PATCH 1/5] [RISCV][ELF] Add RISCV GNU property label
---
llvm/include/llvm/BinaryFormat/ELF.h | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/llvm/include/llvm/BinaryFormat/ELF.h b/llvm/include/llvm/BinaryFormat/ELF.h
index f296acc2ca4bb..c8da286fc9588 100644
--- a/llvm/include/llvm/BinaryFormat/ELF.h
+++ b/llvm/include/llvm/BinaryFormat/ELF.h
@@ -1737,6 +1737,7 @@ enum : unsigned {
GNU_PROPERTY_AARCH64_FEATURE_1_AND = 0xc0000000,
GNU_PROPERTY_AARCH64_FEATURE_PAUTH = 0xc0000001,
GNU_PROPERTY_X86_FEATURE_1_AND = 0xc0000002,
+ GNU_PROPERTY_RISCV_FEATURE_1_AND = 0xc0000000,
GNU_PROPERTY_X86_UINT32_OR_LO = 0xc0008000,
GNU_PROPERTY_X86_FEATURE_2_NEEDED = GNU_PROPERTY_X86_UINT32_OR_LO + 1,
@@ -1796,6 +1797,12 @@ enum : unsigned {
GNU_PROPERTY_X86_ISA_1_V4 = 1 << 3,
};
+// riscv processor feature bits.
+enum : unsigned {
+ GNU_PROPERTY_RISCV_FEATURE_1_CFI_LP_SIMPLE = 1 << 0,
+ GNU_PROPERTY_RISCV_FEATURE_1_CFI_SS = 1 << 1,
+};
+
// FreeBSD note types.
enum {
NT_FREEBSD_ABI_TAG = 1,
>From 40b81f066e9a7c1789e4c0c1e92715a0569a77eb Mon Sep 17 00:00:00 2001
From: SuHsien Ho <su-hsien.ho at mediatek.com>
Date: Thu, 23 Nov 2023 14:38:34 +0800
Subject: [PATCH 2/5] [RISCV][ELF] Emit .gnu.note.property for
Zicfiss/Zicfilip(CFI extension)
---
.../RISCV/MCTargetDesc/RISCVELFStreamer.cpp | 42 +++++++++++++++++++
.../RISCV/MCTargetDesc/RISCVELFStreamer.h | 1 +
.../MCTargetDesc/RISCVTargetStreamer.cpp | 2 +
.../RISCV/MCTargetDesc/RISCVTargetStreamer.h | 4 ++
4 files changed, 49 insertions(+)
diff --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.cpp b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.cpp
index ae7ce476fff22..7164a191bd41f 100644
--- a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.cpp
+++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.cpp
@@ -85,6 +85,35 @@ void RISCVTargetELFStreamer::finishAttributeSection() {
ELF::SHT_RISCV_ATTRIBUTES, AttributeSection);
}
+void RISCVTargetELFStreamer::emitNoteSection(unsigned Flags) {
+ if (Flags == 0)
+ return;
+
+ MCStreamer &OutStreamer = getStreamer();
+ MCContext &Context = OutStreamer.getContext();
+ MCSectionELF *Nt = Context.getELFSection(".note.gnu.property", ELF::SHT_NOTE,
+ ELF::SHF_ALLOC);
+ MCSection *Cur = OutStreamer.getCurrentSectionOnly();
+ OutStreamer.switchSection(Nt);
+
+ // Emit the note header.
+ OutStreamer.emitValueToAlignment(Align(8));
+ OutStreamer.emitIntValue(4, 4); // data size for note name
+ OutStreamer.emitIntValue(4 * 4, 4); // data size
+ OutStreamer.emitIntValue(ELF::NT_GNU_PROPERTY_TYPE_0, 4); // note type
+ OutStreamer.emitBytes(StringRef("GNU", 4)); // note name
+
+ // Emit the CFI(ZICFILP/ZICFISS) properties.
+ OutStreamer.emitIntValue(ELF::GNU_PROPERTY_RISCV_FEATURE_1_AND,
+ 4); // and property
+ OutStreamer.emitIntValue(4, 4); // data size
+ OutStreamer.emitIntValue(Flags, 4); // data
+ OutStreamer.emitIntValue(0, 4); // pad
+
+ OutStreamer.endSection(Nt);
+ OutStreamer.switchSection(Cur);
+}
+
void RISCVTargetELFStreamer::finish() {
RISCVTargetStreamer::finish();
MCAssembler &MCA = getStreamer().getAssembler();
@@ -118,6 +147,19 @@ void RISCVTargetELFStreamer::finish() {
}
MCA.setELFHeaderEFlags(EFlags);
+
+ unsigned GNUNoteFlags = 0;
+
+ // check ZICFILP or ZICFISS with features
+ // TODO should we check with codegen enable ex. -mllvm
+ // -riscv-hardware-shadow-stack=true ?
+ if (hasZICFILP())
+ GNUNoteFlags |= ELF::GNU_PROPERTY_RISCV_FEATURE_1_CFI_LP_SIMPLE;
+
+ if (hasZICFISS())
+ GNUNoteFlags |= ELF::GNU_PROPERTY_RISCV_FEATURE_1_CFI_SS;
+
+ emitNoteSection(GNUNoteFlags);
}
void RISCVTargetELFStreamer::reset() {
diff --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.h b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.h
index 212d731889f1a..06ee170500c59 100644
--- a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.h
+++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.h
@@ -70,6 +70,7 @@ class RISCVTargetELFStreamer : public RISCVTargetStreamer {
void emitDirectiveVariantCC(MCSymbol &Symbol) override;
void finish() override;
+ void emitNoteSection(unsigned Flags);
};
MCELFStreamer *createRISCVELFStreamer(MCContext &C,
diff --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVTargetStreamer.cpp b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVTargetStreamer.cpp
index 0f92e9ed6a64d..9f142b4d25463 100644
--- a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVTargetStreamer.cpp
+++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVTargetStreamer.cpp
@@ -52,6 +52,8 @@ void RISCVTargetStreamer::setFlagsFromFeatures(const MCSubtargetInfo &STI) {
HasRVC = STI.hasFeature(RISCV::FeatureStdExtC) ||
STI.hasFeature(RISCV::FeatureStdExtZca);
HasTSO = STI.hasFeature(RISCV::FeatureStdExtZtso);
+ HasZICFILP = STI.hasFeature(RISCV::FeatureStdExtZicfilp);
+ HasZICFISS = STI.hasFeature(RISCV::FeatureStdExtZicfiss);
}
void RISCVTargetStreamer::emitTargetAttributes(const MCSubtargetInfo &STI,
diff --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVTargetStreamer.h b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVTargetStreamer.h
index cb8bc21cb6355..541d8fc807ead 100644
--- a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVTargetStreamer.h
+++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVTargetStreamer.h
@@ -35,6 +35,8 @@ class RISCVTargetStreamer : public MCTargetStreamer {
RISCVABI::ABI TargetABI = RISCVABI::ABI_Unknown;
bool HasRVC = false;
bool HasTSO = false;
+ bool HasZICFILP = false;
+ bool HasZICFISS = false;
public:
RISCVTargetStreamer(MCStreamer &S);
@@ -63,6 +65,8 @@ class RISCVTargetStreamer : public MCTargetStreamer {
void setFlagsFromFeatures(const MCSubtargetInfo &STI);
bool hasRVC() const { return HasRVC; }
bool hasTSO() const { return HasTSO; }
+ bool hasZICFILP() const { return HasZICFILP; }
+ bool hasZICFISS() const { return HasZICFISS; }
};
// This part is for ascii assembly output
>From a2baf770c60c9e404614316d47616a7bd81a1b31 Mon Sep 17 00:00:00 2001
From: SuHsien Ho <su-hsien.ho at mediatek.com>
Date: Thu, 23 Nov 2023 16:37:08 +0800
Subject: [PATCH 3/5] [LLD] Emit .note.gnu.property in RISCV for
Zicfiss/Zicfilip(CFI extension)
---
lld/ELF/Driver.cpp | 2 +-
lld/ELF/InputFiles.cpp | 15 ++++++++++++---
lld/ELF/SyntheticSections.cpp | 15 ++++++++++++---
3 files changed, 25 insertions(+), 7 deletions(-)
diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp
index b29e1e1a67f13..8456491da77ca 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -2651,7 +2651,7 @@ static void checkAndReportMissingFeature(StringRef config, uint32_t features,
// ones can be allowed (see -z pauth-report).
static void readSecurityNotes() {
if (config->emachine != EM_386 && config->emachine != EM_X86_64 &&
- config->emachine != EM_AARCH64)
+ config->emachine != EM_AARCH64 && config->emachine != EM_RISCV)
return;
config->andFeatures = -1;
diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp
index 1f496026d3ae2..cc6c7bb7f7d57 100644
--- a/lld/ELF/InputFiles.cpp
+++ b/lld/ELF/InputFiles.cpp
@@ -954,9 +954,18 @@ void readGnuProperty(const InputSection &sec, ObjFile<ELFT> &f) {
continue;
}
- uint32_t featureAndType = config->emachine == EM_AARCH64
- ? GNU_PROPERTY_AARCH64_FEATURE_1_AND
- : GNU_PROPERTY_X86_FEATURE_1_AND;
+ uint32_t featureAndType = 0;
+ switch (config->emachine) {
+ default:
+ featureAndType = GNU_PROPERTY_X86_FEATURE_1_AND;
+ break;
+ case EM_AARCH64:
+ featureAndType = GNU_PROPERTY_AARCH64_FEATURE_1_AND;
+ break;
+ case EM_RISCV:
+ featureAndType = GNU_PROPERTY_RISCV_FEATURE_1_AND;
+ break;
+ }
// Read a body of a NOTE record, which consists of type-length-value fields.
ArrayRef<uint8_t> desc = note.getDesc(sec.addralign);
diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp
index 7b9ada40c0f67..bd6f3a3f2bb07 100644
--- a/lld/ELF/SyntheticSections.cpp
+++ b/lld/ELF/SyntheticSections.cpp
@@ -323,9 +323,18 @@ void GnuPropertySection::writeTo(uint8_t *buf) {
write32(buf + 8, NT_GNU_PROPERTY_TYPE_0); // Type
memcpy(buf + 12, "GNU", 4); // Name string
- uint32_t featureAndType = config->emachine == EM_AARCH64
- ? GNU_PROPERTY_AARCH64_FEATURE_1_AND
- : GNU_PROPERTY_X86_FEATURE_1_AND;
+ uint32_t featureAndType = 0;
+ switch (config->emachine) {
+ default:
+ featureAndType = GNU_PROPERTY_X86_FEATURE_1_AND;
+ break;
+ case EM_AARCH64:
+ featureAndType = GNU_PROPERTY_AARCH64_FEATURE_1_AND;
+ break;
+ case EM_RISCV:
+ featureAndType = GNU_PROPERTY_RISCV_FEATURE_1_AND;
+ break;
+ }
unsigned offset = 16;
if (config->andFeatures != 0) {
>From 06cc58e3cbfaddeca7a6bccabf51026f91a27b94 Mon Sep 17 00:00:00 2001
From: SuHsien Ho <su-hsien.ho at mediatek.com>
Date: Thu, 23 Nov 2023 18:21:01 +0800
Subject: [PATCH 4/5] [llvm-readobj] Parse .note.gnu.property in RISCV for
Zicfiss/Zicfilip(CFI extension)
---
clang/test/Driver/riscv-cfi-property.c | 29 +++++++++++++++
llvm/tools/llvm-readobj/ELFDumper.cpp | 49 ++++++++++++++++++--------
2 files changed, 63 insertions(+), 15 deletions(-)
create mode 100644 clang/test/Driver/riscv-cfi-property.c
diff --git a/clang/test/Driver/riscv-cfi-property.c b/clang/test/Driver/riscv-cfi-property.c
new file mode 100644
index 0000000000000..7a4044c9328dc
--- /dev/null
+++ b/clang/test/Driver/riscv-cfi-property.c
@@ -0,0 +1,29 @@
+// When -march with zicfiss0p4 or zicfilp0p4 add GNU property to file object
+
+// RUN: %clang --target=riscv32-linux-gnu -menable-experimental-extensions -march=rv32gc_zicfiss0p4 -c -o - %s | llvm-readobj -n - | FileCheck -check-prefix=CHECK -check-prefix=CHECK_ZICFISS %s
+// RUN: %clang --target=riscv64-linux-gnu -menable-experimental-extensions -march=rv64gc_zicfiss0p4 -c -o - %s | llvm-readobj -n - | FileCheck -check-prefix=CHECK -check-prefix=CHECK_ZICFISS %s
+// RUN: %clang --target=riscv32-linux-gnu -menable-experimental-extensions -march=rv32gc_zicfilp0p4 -c -o - %s | llvm-readobj -n - | FileCheck -check-prefix=CHECK -check-prefix=CHECK_ZICFILP %s
+// RUN: %clang --target=riscv64-linux-gnu -menable-experimental-extensions -march=rv64gc_zicfilp0p4 -c -o - %s | llvm-readobj -n - | FileCheck -check-prefix=CHECK -check-prefix=CHECK_ZICFILP %s
+// RUN: %clang --target=riscv32-linux-gnu -menable-experimental-extensions -march=rv32gc_zicfilp0p4_zicfiss0p4 -c -o - %s | llvm-readobj -n - | FileCheck -check-prefix=CHECK -check-prefix=CHECK_ZICFILP_ZICFISS %s
+// RUN: %clang --target=riscv64-linux-gnu -menable-experimental-extensions -march=rv64gc_zicfilp0p4_zicfiss0p4 -c -o - %s | llvm-readobj -n - | 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: ]
+
+// GNU Note Section Example
+/*.section .note.gnu.property, "a";
+.balign 8;
+.long 0x4;
+.long 0x10;
+.long 0x5
+.asciz "GNU"
+.long 0xc0000000
+.long 4
+.long 3
+.long 0*/
diff --git a/llvm/tools/llvm-readobj/ELFDumper.cpp b/llvm/tools/llvm-readobj/ELFDumper.cpp
index a752cc4015293..c1705dc55bfa9 100644
--- a/llvm/tools/llvm-readobj/ELFDumper.cpp
+++ b/llvm/tools/llvm-readobj/ELFDumper.cpp
@@ -5239,7 +5239,7 @@ static bool printAArch64PAuthABICoreInfo(raw_ostream &OS, uint32_t DataSize,
template <typename ELFT>
static std::string getGNUProperty(uint32_t Type, uint32_t DataSize,
- ArrayRef<uint8_t> Data) {
+ ArrayRef<uint8_t> Data, uint16_t Target) {
std::string str;
raw_string_ostream OS(str);
uint32_t PrData;
@@ -5272,8 +5272,16 @@ static std::string getGNUProperty(uint32_t Type, uint32_t DataSize,
return OS.str();
case GNU_PROPERTY_AARCH64_FEATURE_1_AND:
case GNU_PROPERTY_X86_FEATURE_1_AND:
- OS << ((Type == GNU_PROPERTY_AARCH64_FEATURE_1_AND) ? "aarch64 feature: "
- : "x86 feature: ");
+
+ if (Type == GNU_PROPERTY_AARCH64_FEATURE_1_AND) {
+ if (Target == ELF::EM_RISCV)
+ OS << "riscv feature: ";
+ else
+ OS << "aarch64 feature: ";
+ } else {
+ OS << "x86 feature: ";
+ }
+
if (DataSize != 4) {
OS << format("<corrupt length: 0x%x>", DataSize);
return OS.str();
@@ -5283,14 +5291,21 @@ static std::string getGNUProperty(uint32_t Type, uint32_t DataSize,
OS << "<None>";
return OS.str();
}
+
if (Type == GNU_PROPERTY_AARCH64_FEATURE_1_AND) {
- DumpBit(GNU_PROPERTY_AARCH64_FEATURE_1_BTI, "BTI");
- DumpBit(GNU_PROPERTY_AARCH64_FEATURE_1_PAC, "PAC");
- DumpBit(GNU_PROPERTY_AARCH64_FEATURE_1_GCS, "GCS");
+ if (Target == ELF::EM_RISCV) {
+ DumpBit(GNU_PROPERTY_RISCV_FEATURE_1_CFI_LP_SIMPLE, "ZICFILP");
+ DumpBit(GNU_PROPERTY_RISCV_FEATURE_1_CFI_SS, "ZICFISS");
+ } else {
+ DumpBit(GNU_PROPERTY_AARCH64_FEATURE_1_BTI, "BTI");
+ DumpBit(GNU_PROPERTY_AARCH64_FEATURE_1_PAC, "PAC");
+ DumpBit(GNU_PROPERTY_AARCH64_FEATURE_1_GCS, "GCS");
+ }
} else {
DumpBit(GNU_PROPERTY_X86_FEATURE_1_IBT, "IBT");
DumpBit(GNU_PROPERTY_X86_FEATURE_1_SHSTK, "SHSTK");
}
+
if (PrData)
OS << format("<unknown flags: 0x%x>", PrData);
return OS.str();
@@ -5347,7 +5362,8 @@ static std::string getGNUProperty(uint32_t Type, uint32_t DataSize,
}
template <typename ELFT>
-static SmallVector<std::string, 4> getGNUPropertyList(ArrayRef<uint8_t> Arr) {
+static SmallVector<std::string, 4> getGNUPropertyList(ArrayRef<uint8_t> Arr,
+ uint16_t Target) {
using Elf_Word = typename ELFT::Word;
SmallVector<std::string, 4> Properties;
@@ -5365,8 +5381,8 @@ static SmallVector<std::string, 4> getGNUPropertyList(ArrayRef<uint8_t> Arr) {
Properties.push_back(OS.str());
break;
}
- Properties.push_back(
- getGNUProperty<ELFT>(Type, DataSize, Arr.take_front(PaddedSize)));
+ Properties.push_back(getGNUProperty<ELFT>(
+ Type, DataSize, Arr.take_front(PaddedSize), Target));
Arr = Arr.drop_front(PaddedSize);
}
@@ -5418,7 +5434,7 @@ static StringRef getDescAsStringRef(ArrayRef<uint8_t> Desc) {
template <typename ELFT>
static bool printGNUNote(raw_ostream &OS, uint32_t NoteType,
- ArrayRef<uint8_t> Desc) {
+ ArrayRef<uint8_t> Desc, uint16_t Target) {
// Return true if we were able to pretty-print the note, false otherwise.
switch (NoteType) {
default:
@@ -5440,7 +5456,7 @@ static bool printGNUNote(raw_ostream &OS, uint32_t NoteType,
break;
case ELF::NT_GNU_PROPERTY_TYPE_0:
OS << " Properties:";
- for (const std::string &Property : getGNUPropertyList<ELFT>(Desc))
+ for (const std::string &Property : getGNUPropertyList<ELFT>(Desc, Target))
OS << " " << Property << "\n";
break;
}
@@ -6127,10 +6143,12 @@ template <class ELFT> void GNUELFDumper<ELFT>::printNotes() {
else
OS << "Unknown note type: (" << format_hex(Type, 10) << ")\n";
+ uint16_t Target = this->Obj.getHeader().e_machine;
+
// Print the description, or fallback to printing raw bytes for unknown
// owners/if we fail to pretty-print the contents.
if (Name == "GNU") {
- if (printGNUNote<ELFT>(OS, Type, Descriptor))
+ if (printGNUNote<ELFT>(OS, Type, Descriptor, Target))
return Error::success();
} else if (Name == "FreeBSD") {
if (std::optional<FreeBSDNote> N =
@@ -7746,7 +7764,7 @@ template <class ELFT> void LLVMELFDumper<ELFT>::printAddrsig() {
template <typename ELFT>
static bool printGNUNoteLLVMStyle(uint32_t NoteType, ArrayRef<uint8_t> Desc,
- ScopedPrinter &W) {
+ ScopedPrinter &W, uint16_t Target) {
// Return true if we were able to pretty-print the note, false otherwise.
switch (NoteType) {
default:
@@ -7771,7 +7789,7 @@ static bool printGNUNoteLLVMStyle(uint32_t NoteType, ArrayRef<uint8_t> Desc,
break;
case ELF::NT_GNU_PROPERTY_TYPE_0:
ListScope D(W, "Property");
- for (const std::string &Property : getGNUPropertyList<ELFT>(Desc))
+ for (const std::string &Property : getGNUPropertyList<ELFT>(Desc, Target))
W.printString(Property);
break;
}
@@ -7884,10 +7902,11 @@ template <class ELFT> void LLVMELFDumper<ELFT>::printNotes() {
W.printString("Type",
"Unknown (" + to_string(format_hex(Type, 10)) + ")");
+ uint16_t Target = this->Obj.getHeader().e_machine;
// Print the description, or fallback to printing raw bytes for unknown
// owners/if we fail to pretty-print the contents.
if (Name == "GNU") {
- if (printGNUNoteLLVMStyle<ELFT>(Type, Descriptor, W))
+ if (printGNUNoteLLVMStyle<ELFT>(Type, Descriptor, W, Target))
return Error::success();
} else if (Name == "FreeBSD") {
if (std::optional<FreeBSDNote> N =
>From e01a93cb192e71947f693bdd07d534912e54fbef Mon Sep 17 00:00:00 2001
From: SuHsien Ho <su-hsien.ho at mediatek.com>
Date: Thu, 23 Nov 2023 19:04:26 +0800
Subject: [PATCH 5/5] [LLD] Add force enable Zicfiss/Zicfilip(CFI extension)
option
---
lld/ELF/Config.h | 4 +++
lld/ELF/Driver.cpp | 40 ++++++++++++++++++++++++-
lld/test/ELF/riscv-force-cfi-property.s | 35 ++++++++++++++++++++++
3 files changed, 78 insertions(+), 1 deletion(-)
create mode 100644 lld/test/ELF/riscv-force-cfi-property.s
diff --git a/lld/ELF/Config.h b/lld/ELF/Config.h
index c55b547a733c7..035f5a44ba25f 100644
--- a/lld/ELF/Config.h
+++ b/lld/ELF/Config.h
@@ -188,6 +188,8 @@ struct Config {
StringRef zBtiReport = "none";
StringRef zCetReport = "none";
StringRef zPauthReport = "none";
+ llvm::StringRef zZicfilpReport = "none";
+ llvm::StringRef zZicfissReport = "none";
bool ltoBBAddrMap;
llvm::StringRef ltoBasicBlockSections;
std::pair<llvm::StringRef, llvm::StringRef> thinLTOObjectSuffixReplace;
@@ -331,6 +333,8 @@ struct Config {
bool zText;
bool zRetpolineplt;
bool zWxneeded;
+ bool zForceZicfilp;
+ bool zForceZicfiss;
DiscardPolicy discard;
GnuStackKind zGnustack;
ICFLevel icf;
diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp
index 8456491da77ca..182ca6d4dfc21 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -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 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");
@@ -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))
@@ -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('=');
@@ -2685,6 +2696,16 @@ 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")
@@ -2697,6 +2718,23 @@ 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");
diff --git a/lld/test/ELF/riscv-force-cfi-property.s b/lld/test/ELF/riscv-force-cfi-property.s
new file mode 100644
index 0000000000000..65043d1e71261
--- /dev/null
+++ b/lld/test/ELF/riscv-force-cfi-property.s
@@ -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: ]
More information about the cfe-commits
mailing list