Skip to content

Commit 24c1bb6

Browse files
authored
[MC] Make .note.GNU-stack explicit for the trampoline case (#151754)
In the presence of trampolines, the .note.GNU-stack section is not emitted. The absence of .note.GNU-stack results in the stack marked executable by some linkers. But others require an explict .note.GNU-stack section. The GNU ld 2.43 on x86 machines, for example, issues the following: missing .note.GNU-stack section implies executable stack NOTE: This behaviour is deprecated and will be removed in a future version of the linker On one of the ARM machines, the absence of .note.GNU-stack results in the stack marked as non-executable: STACK off 0x0000000000000000 vaddr 0x0000000000000000 paddr 0x0000000000000000 align 2**4 filesz 0x0000000000000000 memsz 0x0000000000000000 flags rw- This change just emits the explicit .note.GNU-stack and marks it executable if required.
1 parent e301a7b commit 24c1bb6

File tree

9 files changed

+30
-13
lines changed

9 files changed

+30
-13
lines changed

llvm/include/llvm/MC/MCAsmInfo.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -464,10 +464,10 @@ class LLVM_ABI MCAsmInfo {
464464
const char *getData64bitsDirective() const { return Data64bitsDirective; }
465465
bool supportsSignedData() const { return SupportsSignedData; }
466466

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

llvm/include/llvm/MC/MCAsmInfoELF.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace llvm {
1515

1616
class MCAsmInfoELF : public MCAsmInfo {
1717
virtual void anchor();
18-
MCSection *getNonexecutableStackSection(MCContext &Ctx) const override;
18+
MCSection *getStackSection(MCContext &Ctx, bool Exec) const override;
1919
void printSwitchToSection(const MCSection &, uint32_t, const Triple &,
2020
raw_ostream &) const final;
2121
bool useCodeAlign(const MCSection &Sec) const final;

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2866,9 +2866,11 @@ bool AsmPrinter::doFinalization(Module &M) {
28662866
// If we don't have any trampolines, then we don't require stack memory
28672867
// to be executable. Some targets have a directive to declare this.
28682868
Function *InitTrampolineIntrinsic = M.getFunction("llvm.init.trampoline");
2869-
if (!InitTrampolineIntrinsic || InitTrampolineIntrinsic->use_empty())
2870-
if (MCSection *S = MAI->getNonexecutableStackSection(OutContext))
2871-
OutStreamer->switchSection(S);
2869+
bool HasTrampolineUses =
2870+
InitTrampolineIntrinsic && !InitTrampolineIntrinsic->use_empty();
2871+
MCSection *S = MAI->getStackSection(OutContext, /*Exec=*/HasTrampolineUses);
2872+
if (S)
2873+
OutStreamer->switchSection(S);
28722874

28732875
if (TM.Options.EmitAddrsig) {
28742876
// Emit address-significance attributes for all globals.

llvm/lib/MC/MCAsmInfoELF.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,13 @@ using namespace llvm;
2727

2828
void MCAsmInfoELF::anchor() {}
2929

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

3839
bool MCAsmInfoELF::useCodeAlign(const MCSection &Sec) const {

llvm/lib/MC/MCELFStreamer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ void MCELFStreamer::initSections(bool NoExecStack, const MCSubtargetInfo &STI) {
5454
&STI);
5555

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

6060
void MCELFStreamer::emitLabel(MCSymbol *S, SMLoc Loc) {

llvm/lib/Target/BPF/MCTargetDesc/BPFMCAsmInfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class BPFMCAsmInfo : public MCAsmInfoELF {
4949
DwarfUsesRelocationsAcrossSections = enable;
5050
}
5151

52-
MCSection *getNonexecutableStackSection(MCContext &Ctx) const override {
52+
MCSection *getStackSection(MCContext &Ctx, bool Exec) const override {
5353
return nullptr;
5454
}
5555
};

llvm/test/CodeGen/AArch64/trampoline.ll

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,3 +263,9 @@ define i64 @func2() {
263263
%fp = call ptr @llvm.adjust.trampoline(ptr @trampg)
264264
ret i64 0
265265
}
266+
267+
; Check for the explicitly emitted .note.GNU-stack section (ELF only) in the
268+
; presence of trampolines.
269+
; UTC_ARGS: --disable
270+
; CHECK-LINUX: .section ".note.GNU-stack","x",@progbits
271+
; UTC_ARGS: --enable

llvm/test/CodeGen/RISCV/rv64-trampoline.ll

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,10 @@ define i64 @test0(i64 %n, ptr %p) nounwind {
7878
ret i64 %ret
7979

8080
}
81+
82+
; Check for the explicitly emitted .note.GNU-stack section (ELF only) in the
83+
; presence of trampolines.
84+
; UTC_ARGS: --disable
85+
; RV64-LINUX: .section ".note.GNU-stack","x",@progbits
86+
; RV64: .section ".note.GNU-stack","x",@progbits
87+
; UTC_ARGS: --enable

llvm/tools/llvm-mc/llvm-mc.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,8 @@ int main(int argc, char **argv) {
642642
: MAB->createObjectWriter(*OS),
643643
std::unique_ptr<MCCodeEmitter>(CE), *STI));
644644
if (NoExecStack)
645-
Str->switchSection(Ctx.getAsmInfo()->getNonexecutableStackSection(Ctx));
645+
Str->switchSection(
646+
Ctx.getAsmInfo()->getStackSection(Ctx, /*Exec=*/false));
646647
Str->emitVersionForTarget(TheTriple, VersionTuple(), nullptr,
647648
VersionTuple());
648649
}

0 commit comments

Comments
 (0)