Skip to content

Commit a461e2b

Browse files
authored
[RISCV] Improvements to .note.gnu.property section. (#151436)
Put the emitValueToAlignment at the beginning instead of in the middle. Emit the descsz directly instead of using the difference of 2 labels. Remove the section alignment. emitValueToAlignment will increase the section alignment if necessary.
1 parent e8fc808 commit a461e2b

File tree

2 files changed

+10
-18
lines changed

2 files changed

+10
-18
lines changed

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

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -68,44 +68,37 @@ void RISCVTargetStreamer::emitNoteGnuPropertySection(
6868

6969
const Triple &Triple = Ctx.getTargetTriple();
7070
Align NoteAlign;
71+
uint64_t DescSize;
7172
if (Triple.isArch64Bit()) {
7273
NoteAlign = Align(8);
74+
DescSize = 16;
7375
} else {
7476
assert(Triple.isArch32Bit());
7577
NoteAlign = Align(4);
78+
DescSize = 12;
7679
}
7780

7881
assert(Ctx.getObjectFileType() == MCContext::Environment::IsELF);
7982
MCSection *const NoteSection =
8083
Ctx.getELFSection(".note.gnu.property", ELF::SHT_NOTE, ELF::SHF_ALLOC);
81-
NoteSection->setAlignment(NoteAlign);
8284
OutStreamer.pushSection();
8385
OutStreamer.switchSection(NoteSection);
8486

8587
// Emit the note header
86-
OutStreamer.emitIntValue(4, 4); // n_namsz
87-
88-
MCSymbol *const NDescBeginSym = Ctx.createTempSymbol();
89-
MCSymbol *const NDescEndSym = Ctx.createTempSymbol();
90-
const MCExpr *const NDescSzExpr =
91-
MCBinaryExpr::createSub(MCSymbolRefExpr::create(NDescEndSym, Ctx),
92-
MCSymbolRefExpr::create(NDescBeginSym, Ctx), Ctx);
93-
94-
OutStreamer.emitValue(NDescSzExpr, 4); // n_descsz
88+
OutStreamer.emitValueToAlignment(NoteAlign);
89+
OutStreamer.emitIntValue(4, 4); // n_namsz
90+
OutStreamer.emitIntValue(DescSize, 4); // n_descsz
9591
OutStreamer.emitIntValue(ELF::NT_GNU_PROPERTY_TYPE_0, 4); // n_type
9692
OutStreamer.emitBytes(StringRef("GNU", 4)); // n_name
9793

9894
// Emit n_desc field
99-
OutStreamer.emitLabel(NDescBeginSym);
100-
OutStreamer.emitValueToAlignment(NoteAlign);
10195

10296
// Emit the feature_1_and property
10397
OutStreamer.emitIntValue(ELF::GNU_PROPERTY_RISCV_FEATURE_1_AND, 4); // pr_type
10498
OutStreamer.emitIntValue(4, 4); // pr_datasz
10599
OutStreamer.emitIntValue(Feature1And, 4); // pr_data
106100
OutStreamer.emitValueToAlignment(NoteAlign); // pr_padding
107101

108-
OutStreamer.emitLabel(NDescEndSym);
109102
OutStreamer.popSection();
110103
}
111104

llvm/test/CodeGen/RISCV/note-gnu-property-zicfiss.ll

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,18 @@
77

88
; ASM: .section ".note.GNU-stack","",@progbits
99
; ASM-NEXT: .section .note.gnu.property,"a",@note
10+
; ASM32-NEXT: .p2align 2, 0x0
11+
; ASM64-NEXT: .p2align 3, 0x0
1012
; ASM-NEXT: .word 4
11-
; ASM-NEXT: .word .Ltmp1-.Ltmp0
13+
; ASM32-NEXT: .word 12
14+
; ASM64-NEXT: .word 16
1215
; ASM-NEXT: .word 5
1316
; ASM-NEXT: .asciz "GNU"
14-
; ASM-NEXT: .Ltmp0:
15-
; ASM32-NEXT: .p2align 2, 0x0
16-
; ASM64-NEXT: .p2align 3, 0x0
1717
; ASM-NEXT: .word 3221225472
1818
; ASM-NEXT: .word 4
1919
; ASM-NEXT: .word 2
2020
; ASM32-NEXT: .p2align 2, 0x0
2121
; ASM64-NEXT: .p2align 3, 0x0
22-
; ASM-NEXT: .Ltmp1:
2322

2423
define i32 @f() "hw-shadow-stack" {
2524
entry:

0 commit comments

Comments
 (0)