Skip to content

[MC] Make .note.GNU-stack explicit for the trampoline case #151754

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions llvm/include/llvm/MC/MCAsmInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,12 @@ class LLVM_ABI MCAsmInfo {
return nullptr;
}

/// Targets can implement this method to specify a section to switch to if the
/// translation has trampolines that require an executable stack.
virtual MCSection *getExecutableStackSection(MCContext &Ctx) const {
return nullptr;
}

virtual const MCExpr *getExprForPersonalitySymbol(const MCSymbol *Sym,
unsigned Encoding,
MCStreamer &Streamer) const;
Expand Down
1 change: 1 addition & 0 deletions llvm/include/llvm/MC/MCAsmInfoELF.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace llvm {
class MCAsmInfoELF : public MCAsmInfo {
virtual void anchor();
MCSection *getNonexecutableStackSection(MCContext &Ctx) const final;
MCSection *getExecutableStackSection(MCContext &Ctx) const final;
void printSwitchToSection(const MCSection &, uint32_t, const Triple &,
raw_ostream &) const final;
bool useCodeAlign(const MCSection &Sec) const final;
Expand Down
8 changes: 6 additions & 2 deletions llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2840,9 +2840,13 @@ bool AsmPrinter::doFinalization(Module &M) {
// If we don't have any trampolines, then we don't require stack memory
// to be executable. Some targets have a directive to declare this.
Function *InitTrampolineIntrinsic = M.getFunction("llvm.init.trampoline");
MCSection *S = nullptr;
if (!InitTrampolineIntrinsic || InitTrampolineIntrinsic->use_empty())
if (MCSection *S = MAI->getNonexecutableStackSection(OutContext))
OutStreamer->switchSection(S);
S = MAI->getNonexecutableStackSection(OutContext);
else
S = MAI->getExecutableStackSection(OutContext);
if (S)
OutStreamer->switchSection(S);

if (TM.Options.EmitAddrsig) {
// Emit address-significance attributes for all globals.
Expand Down
7 changes: 7 additions & 0 deletions llvm/lib/MC/MCAsmInfoELF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ MCSection *MCAsmInfoELF::getNonexecutableStackSection(MCContext &Ctx) const {
return Ctx.getELFSection(".note.GNU-stack", ELF::SHT_PROGBITS, 0);
}

MCSection *MCAsmInfoELF::getExecutableStackSection(MCContext &Ctx) const {
MCSectionELF *section =
static_cast<MCSectionELF *>(getNonexecutableStackSection(Ctx));
section->setFlags(section->getFlags() | ELF::SHF_EXECINSTR);
return section;
}

bool MCAsmInfoELF::useCodeAlign(const MCSection &Sec) const {
return static_cast<const MCSectionELF &>(Sec).getFlags() & ELF::SHF_EXECINSTR;
}
Expand Down
1 change: 1 addition & 0 deletions llvm/test/CodeGen/AArch64/trampoline.ll
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,4 @@ define i64 @func2() {
%fp = call ptr @llvm.adjust.trampoline(ptr @trampg)
ret i64 0
}
; CHECK-LINUX: .section ".note.GNU-stack","x",@progbits
1 change: 1 addition & 0 deletions llvm/test/CodeGen/RISCV/rv64-trampoline.ll
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ define i64 @test0(i64 %n, ptr %p) nounwind {
; RV64-LINUX-NEXT: ld ra, 56(sp) # 8-byte Folded Reload
; RV64-LINUX-NEXT: addi sp, sp, 64
; RV64-LINUX-NEXT: ret
; RV64-LINUX: .section ".note.GNU-stack","x",@progbits
%alloca = alloca [32 x i8], align 8
call void @llvm.init.trampoline(ptr %alloca, ptr @f, ptr %p)
%tramp = call ptr @llvm.adjust.trampoline(ptr %alloca)
Expand Down