Skip to content
Merged
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
8 changes: 4 additions & 4 deletions llvm/include/llvm/MC/MCAsmInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -464,10 +464,10 @@ class LLVM_ABI MCAsmInfo {
const char *getData64bitsDirective() const { return Data64bitsDirective; }
bool supportsSignedData() const { return SupportsSignedData; }

/// Targets can implement this method to specify a section to switch to if the
/// translation unit doesn't have any trampolines that require an executable
/// stack.
virtual MCSection *getNonexecutableStackSection(MCContext &Ctx) const {
/// Targets can implement this method to specify a section to switch to
/// depending on whether the translation unit has any trampolines that require
/// an executable stack.
virtual MCSection *getStackSection(MCContext &Ctx, bool Exec) const {
return nullptr;
}

Expand Down
2 changes: 1 addition & 1 deletion llvm/include/llvm/MC/MCAsmInfoELF.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace llvm {

class MCAsmInfoELF : public MCAsmInfo {
virtual void anchor();
MCSection *getNonexecutableStackSection(MCContext &Ctx) const override;
MCSection *getStackSection(MCContext &Ctx, bool Exec) const override;
void printSwitchToSection(const MCSection &, uint32_t, const Triple &,
raw_ostream &) const final;
bool useCodeAlign(const MCSection &Sec) const final;
Expand Down
8 changes: 5 additions & 3 deletions llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2866,9 +2866,11 @@ 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");
if (!InitTrampolineIntrinsic || InitTrampolineIntrinsic->use_empty())
if (MCSection *S = MAI->getNonexecutableStackSection(OutContext))
OutStreamer->switchSection(S);
bool HasTrampolineUses =
InitTrampolineIntrinsic && !InitTrampolineIntrinsic->use_empty();
MCSection *S = MAI->getStackSection(OutContext, /*Exec=*/HasTrampolineUses);
if (S)
OutStreamer->switchSection(S);

if (TM.Options.EmitAddrsig) {
// Emit address-significance attributes for all globals.
Expand Down
5 changes: 3 additions & 2 deletions llvm/lib/MC/MCAsmInfoELF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ using namespace llvm;

void MCAsmInfoELF::anchor() {}

MCSection *MCAsmInfoELF::getNonexecutableStackSection(MCContext &Ctx) const {
MCSection *MCAsmInfoELF::getStackSection(MCContext &Ctx, bool Exec) const {
// Solaris doesn't know/doesn't care about .note.GNU-stack sections, so
// don't emit them.
if (Ctx.getTargetTriple().isOSSolaris())
return nullptr;
return Ctx.getELFSection(".note.GNU-stack", ELF::SHT_PROGBITS, 0);
return Ctx.getELFSection(".note.GNU-stack", ELF::SHT_PROGBITS,
Exec ? ELF::SHF_EXECINSTR : 0U);
}

bool MCAsmInfoELF::useCodeAlign(const MCSection &Sec) const {
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/MC/MCELFStreamer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void MCELFStreamer::initSections(bool NoExecStack, const MCSubtargetInfo &STI) {
&STI);

if (NoExecStack)
switchSection(Ctx.getAsmInfo()->getNonexecutableStackSection(Ctx));
switchSection(Ctx.getAsmInfo()->getStackSection(Ctx, /*Exec=*/false));
}

void MCELFStreamer::emitLabel(MCSymbol *S, SMLoc Loc) {
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/BPF/MCTargetDesc/BPFMCAsmInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class BPFMCAsmInfo : public MCAsmInfoELF {
DwarfUsesRelocationsAcrossSections = enable;
}

MCSection *getNonexecutableStackSection(MCContext &Ctx) const override {
MCSection *getStackSection(MCContext &Ctx, bool Exec) const override {
return nullptr;
}
};
Expand Down
6 changes: 6 additions & 0 deletions llvm/test/CodeGen/AArch64/trampoline.ll
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,9 @@ define i64 @func2() {
%fp = call ptr @llvm.adjust.trampoline(ptr @trampg)
ret i64 0
}

; Check for the explicitly emitted .note.GNU-stack section (ELF only) in the
; presence of trampolines.
; UTC_ARGS: --disable
; CHECK-LINUX: .section ".note.GNU-stack","x",@progbits
; UTC_ARGS: --enable
7 changes: 7 additions & 0 deletions llvm/test/CodeGen/RISCV/rv64-trampoline.ll
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,10 @@ define i64 @test0(i64 %n, ptr %p) nounwind {
ret i64 %ret

}

; Check for the explicitly emitted .note.GNU-stack section (ELF only) in the
; presence of trampolines.
; UTC_ARGS: --disable
; RV64-LINUX: .section ".note.GNU-stack","x",@progbits
; RV64: .section ".note.GNU-stack","x",@progbits
; UTC_ARGS: --enable
3 changes: 2 additions & 1 deletion llvm/tools/llvm-mc/llvm-mc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,8 @@ int main(int argc, char **argv) {
: MAB->createObjectWriter(*OS),
std::unique_ptr<MCCodeEmitter>(CE), *STI));
if (NoExecStack)
Str->switchSection(Ctx.getAsmInfo()->getNonexecutableStackSection(Ctx));
Str->switchSection(
Ctx.getAsmInfo()->getStackSection(Ctx, /*Exec=*/false));
Str->emitVersionForTarget(TheTriple, VersionTuple(), nullptr,
VersionTuple());
}
Expand Down