@@ -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+
80107void 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
116156void RISCVTargetELFStreamer::reset () {
0 commit comments