Skip to content

Commit 1c6c5f8

Browse files
committed
[RISCV][ELF] Emit .gnu.note.property for Zicfiss/Zicfilip(CFI extension)
1 parent 8fc7598 commit 1c6c5f8

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,33 @@ void RISCVTargetELFStreamer::finishAttributeSection() {
7777
ELF::SHT_RISCV_ATTRIBUTES, AttributeSection);
7878
}
7979

80+
void RISCVTargetELFStreamer::emitNoteSection(unsigned Flags) {
81+
if (Flags == 0)
82+
return;
83+
84+
MCStreamer &OutStreamer = getStreamer();
85+
MCContext &Context = OutStreamer.getContext();
86+
MCSectionELF *Nt = Context.getELFSection(".note.gnu.property", ELF::SHT_NOTE, ELF::SHF_ALLOC);
87+
MCSection *Cur = OutStreamer.getCurrentSectionOnly();
88+
OutStreamer.switchSection(Nt);
89+
90+
// Emit the note header.
91+
OutStreamer.emitValueToAlignment(Align(8));
92+
OutStreamer.emitIntValue(4, 4); // data size for note name
93+
OutStreamer.emitIntValue(4 * 4, 4); // data size
94+
OutStreamer.emitIntValue(ELF::NT_GNU_PROPERTY_TYPE_0, 4); // note type
95+
OutStreamer.emitBytes(StringRef("GNU", 4)); // note name
96+
97+
// Emit the CFI(ZICFILP/ZICFISS) properties.
98+
OutStreamer.emitIntValue(ELF::GNU_PROPERTY_RISCV_FEATURE_1_AND, 4); // and property
99+
OutStreamer.emitIntValue(4, 4); // data size
100+
OutStreamer.emitIntValue(Flags, 4); // data
101+
OutStreamer.emitIntValue(0, 4); // pad
102+
103+
OutStreamer.endSection(Nt);
104+
OutStreamer.switchSection(Cur);
105+
}
106+
80107
void RISCVTargetELFStreamer::finish() {
81108
RISCVTargetStreamer::finish();
82109
MCAssembler &MCA = getStreamer().getAssembler();
@@ -111,6 +138,19 @@ void RISCVTargetELFStreamer::finish() {
111138
}
112139

113140
MCA.setELFHeaderEFlags(EFlags);
141+
142+
unsigned GNUNoteFlags = 0;
143+
144+
// TODO check ZICFILP or ZICFISS with march
145+
// should we check with codegen enable ex. -mllvm
146+
// -riscv-hardware-shadow-stack=true ?
147+
if (Features[RISCV::FeatureStdExtZicfilp])
148+
GNUNoteFlags |= ELF::GNU_PROPERTY_RISCV_FEATURE_1_ZICFILP;
149+
150+
if (Features[RISCV::FeatureStdExtZicfiss])
151+
GNUNoteFlags |= ELF::GNU_PROPERTY_RISCV_FEATURE_1_ZICFISS;
152+
153+
emitNoteSection(GNUNoteFlags);
114154
}
115155

116156
void RISCVTargetELFStreamer::reset() {

llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ class RISCVTargetELFStreamer : public RISCVTargetStreamer {
7171
void emitDirectiveVariantCC(MCSymbol &Symbol) override;
7272

7373
void finish() override;
74+
void emitNoteSection(unsigned Flags);
7475
};
7576

7677
MCELFStreamer *createRISCVELFStreamer(MCContext &C,

0 commit comments

Comments
 (0)