Skip to content

[RISCV][Zicfilp] Emit .note.gnu.property section for Zicfilp CFI unlabeled scheme #141468

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions llvm/include/llvm/Support/RISCVISAUtils.h
Original file line number Diff line number Diff line change
@@ -14,7 +14,9 @@
#define LLVM_SUPPORT_RISCVISAUTILS_H

#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/Support/Compiler.h"
#include <cassert>
#include <map>
#include <string>

@@ -43,6 +45,23 @@ struct ExtensionComparator {
typedef std::map<std::string, ExtensionVersion, ExtensionComparator>
OrderedExtensionMap;

enum class ZicfilpLabelSchemeKind {
Invalid,
Unlabeled,
FuncSig,
};

// See clang::getCFBranchLabelSchemeFlagVal() for possible CFBranchLabelScheme
inline ZicfilpLabelSchemeKind
getZicfilpLabelScheme(const StringRef CFBranchLabelScheme) {
const auto Ret = StringSwitch<ZicfilpLabelSchemeKind>(CFBranchLabelScheme)
.Case("unlabeled", ZicfilpLabelSchemeKind::Unlabeled)
.Case("func-sig", ZicfilpLabelSchemeKind::FuncSig)
.Default(ZicfilpLabelSchemeKind::Invalid);
assert(Ret != ZicfilpLabelSchemeKind::Invalid);
return Ret;
}

} // namespace RISCVISAUtils

} // namespace llvm
35 changes: 32 additions & 3 deletions llvm/lib/Target/RISCV/RISCVAsmPrinter.cpp
Original file line number Diff line number Diff line change
@@ -946,12 +946,41 @@ void RISCVAsmPrinter::EmitHwasanMemaccessSymbols(Module &M) {
}

void RISCVAsmPrinter::emitNoteGnuProperty(const Module &M) {
uint32_t Feature1And = 0;
if (const Metadata *const Flag = M.getModuleFlag("cf-protection-return");
Flag && !mdconst::extract<ConstantInt>(Flag)->isZero())
Feature1And |= ELF::GNU_PROPERTY_RISCV_FEATURE_1_CFI_SS;

if (const Metadata *const Flag = M.getModuleFlag("cf-protection-branch");
Flag && !mdconst::extract<ConstantInt>(Flag)->isZero()) {
RISCVTargetStreamer &RTS =
static_cast<RISCVTargetStreamer &>(*OutStreamer->getTargetStreamer());
RTS.emitNoteGnuPropertySection(ELF::GNU_PROPERTY_RISCV_FEATURE_1_CFI_SS);
using namespace llvm::RISCVISAUtils;
const Metadata *const CFBranchLabelSchemeFlag =
M.getModuleFlag("cf-branch-label-scheme");
assert(CFBranchLabelSchemeFlag &&
"cf-protection=branch should come with cf-branch-label-scheme=... "
"on RISC-V targets");
const StringRef CFBranchLabelScheme =
cast<MDString>(CFBranchLabelSchemeFlag)->getString();
switch (getZicfilpLabelScheme(CFBranchLabelScheme)) {
case ZicfilpLabelSchemeKind::Invalid:
llvm_unreachable("invalid RISC-V Zicfilp label scheme");
case ZicfilpLabelSchemeKind::Unlabeled:
Feature1And |= ELF::GNU_PROPERTY_RISCV_FEATURE_1_CFI_LP_UNLABELED;
break;
case ZicfilpLabelSchemeKind::FuncSig:
// TODO: Emit the func-sig bit after the feature is implemented
report_fatal_error("The complete func-sig label scheme feature had not "
"been implemented yet");
break;
}
}

if (!Feature1And)
return;

RISCVTargetStreamer &RTS =
static_cast<RISCVTargetStreamer &>(*OutStreamer->getTargetStreamer());
RTS.emitNoteGnuPropertySection(Feature1And);
}

static MCOperand lowerSymbolOperand(const MachineOperand &MO, MCSymbol *Sym,
27 changes: 27 additions & 0 deletions llvm/test/CodeGen/RISCV/note-gnu-property-zicfilp-unlabeled.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
; RUN: llc --mtriple=riscv32 --filetype=obj -o - %s | llvm-readelf -n - | FileCheck --check-prefixes=READELF %s
; RUN: llc --mtriple=riscv64 --filetype=obj -o - %s | llvm-readelf -n - | FileCheck --check-prefixes=READELF %s
; RUN: llc --mtriple=riscv32 -o - %s | FileCheck --check-prefixes=ASM,ASM32 %s
; RUN: llc --mtriple=riscv64 -o - %s | FileCheck --check-prefixes=ASM,ASM64 %s

; READELF: Properties: RISC-V feature: ZICFILP-unlabeled

; ASM: .section ".note.GNU-stack","",@progbits
; ASM-NEXT: .section .note.gnu.property,"a",@note
; ASM-NEXT: .word 4
; ASM-NEXT: .word .Ltmp1-.Ltmp0
; ASM-NEXT: .word 5
; ASM-NEXT: .asciz "GNU"
; ASM-NEXT: .Ltmp0:
; ASM32-NEXT: .p2align 2, 0x0
; ASM64-NEXT: .p2align 3, 0x0
; ASM-NEXT: .word 3221225472
; ASM-NEXT: .word 4
; ASM-NEXT: .word 1
; ASM32-NEXT: .p2align 2, 0x0
; ASM64-NEXT: .p2align 3, 0x0
; ASM-NEXT: .Ltmp1:

!llvm.module.flags = !{!0, !1}

!0 = !{i32 8, !"cf-protection-branch", i32 1}
!1 = !{i32 1, !"cf-branch-label-scheme", !"unlabeled"}