From 3962d561a63fb3912c9310838793863ce5818cba Mon Sep 17 00:00:00 2001 From: Than McIntosh Date: Wed, 27 Sep 2017 19:34:00 +0000 Subject: [PATCH] [CodeGen] Emit necessary .note sections for -fsplit-stack Summary: According to https://gcc.gnu.org/wiki/SplitStacks, the linker expects a zero-sized .note.GNU-split-stack section if split-stack is used (and also .note.GNU-no-split-stack section if it also contains non-split-stack functions), so it can handle the cases where a split-stack function calls non-split-stack function. This change adds the sections if needed. Fixes PR #34670. Reviewers: thanm, rnk, luqmana Reviewed By: rnk Subscribers: llvm-commits Patch by Cherry Zhang Differential Revision: https://reviews.llvm.org/D38051 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@314335 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/CodeGen/MachineModuleInfo.h | 26 ++++++++++++++++++++++++++ lib/CodeGen/AsmPrinter/AsmPrinter.cpp | 10 ++++++++++ lib/CodeGen/MachineModuleInfo.cpp | 1 + lib/CodeGen/PrologEpilogInserter.cpp | 6 +++++- test/CodeGen/X86/segmented-stacks.ll | 19 +++++++++++++++++++ 5 files changed, 61 insertions(+), 1 deletion(-) diff --git a/include/llvm/CodeGen/MachineModuleInfo.h b/include/llvm/CodeGen/MachineModuleInfo.h index d64941a9e72..7b57a407b4b 100644 --- a/include/llvm/CodeGen/MachineModuleInfo.h +++ b/include/llvm/CodeGen/MachineModuleInfo.h @@ -125,6 +125,16 @@ class MachineModuleInfo : public ImmutablePass { /// comments in lib/Target/X86/X86FrameLowering.cpp for more details. bool UsesMorestackAddr; + /// True if the module contains split-stack functions. This is used to + /// emit .note.GNU-split-stack section as required by the linker for + /// special handling split-stack function calling no-split-stack function. + bool HasSplitStack; + + /// True if the module contains no-split-stack functions. This is used to + /// emit .note.GNU-no-split-stack section when it also contains split-stack + /// functions. + bool HasNosplitStack; + /// Maps IR Functions to their corresponding MachineFunctions. DenseMap> MachineFunctions; /// Next unique number available for a MachineFunction. @@ -194,6 +204,22 @@ public: UsesMorestackAddr = b; } + bool hasSplitStack() const { + return HasSplitStack; + } + + void setHasSplitStack(bool b) { + HasSplitStack = b; + } + + bool hasNosplitStack() const { + return HasNosplitStack; + } + + void setHasNosplitStack(bool b) { + HasNosplitStack = b; + } + /// Return the symbol to be used for the specified basic block when its /// address is taken. This cannot be its normal LBB label because the block /// may be accessed outside its containing function. diff --git a/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/lib/CodeGen/AsmPrinter/AsmPrinter.cpp index ee5cb35fa9d..26ca58a76e4 100644 --- a/lib/CodeGen/AsmPrinter/AsmPrinter.cpp +++ b/lib/CodeGen/AsmPrinter/AsmPrinter.cpp @@ -1382,6 +1382,16 @@ bool AsmPrinter::doFinalization(Module &M) { PtrSize); } + // Emit .note.GNU-split-stack and .note.GNU-no-split-stack sections if + // split-stack is used. + if (TM.getTargetTriple().isOSBinFormatELF() && MMI->hasSplitStack()) { + OutStreamer->SwitchSection( + OutContext.getELFSection(".note.GNU-split-stack", ELF::SHT_PROGBITS, 0)); + if (MMI->hasNosplitStack()) + OutStreamer->SwitchSection( + OutContext.getELFSection(".note.GNU-no-split-stack", ELF::SHT_PROGBITS, 0)); + } + // 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"); diff --git a/lib/CodeGen/MachineModuleInfo.cpp b/lib/CodeGen/MachineModuleInfo.cpp index f0dd77ef0da..4cedcd03a2d 100644 --- a/lib/CodeGen/MachineModuleInfo.cpp +++ b/lib/CodeGen/MachineModuleInfo.cpp @@ -208,6 +208,7 @@ bool MachineModuleInfo::doInitialization(Module &M) { ObjFileMMI = nullptr; CurCallSite = 0; DbgInfoAvailable = UsesVAFloatArgument = UsesMorestackAddr = false; + HasSplitStack = HasNosplitStack = false; AddrLabelSymbols = nullptr; TheModule = &M; return false; diff --git a/lib/CodeGen/PrologEpilogInserter.cpp b/lib/CodeGen/PrologEpilogInserter.cpp index 6e442065ef7..032abb441dd 100644 --- a/lib/CodeGen/PrologEpilogInserter.cpp +++ b/lib/CodeGen/PrologEpilogInserter.cpp @@ -994,7 +994,11 @@ void PEI::insertPrologEpilogCode(MachineFunction &Fn) { if (Fn.shouldSplitStack()) { for (MachineBasicBlock *SaveBlock : SaveBlocks) TFI.adjustForSegmentedStacks(Fn, *SaveBlock); - } + // Record that there are split-stack functions, so we will emit a + // special section to tell the linker. + Fn.getMMI().setHasSplitStack(true); + } else + Fn.getMMI().setHasNosplitStack(true); // Emit additional code that is required to explicitly handle the stack in // HiPE native code (if needed) when loaded in the Erlang/OTP runtime. The diff --git a/test/CodeGen/X86/segmented-stacks.ll b/test/CodeGen/X86/segmented-stacks.ll index a0cd1824629..a4861efac0e 100644 --- a/test/CodeGen/X86/segmented-stacks.ll +++ b/test/CodeGen/X86/segmented-stacks.ll @@ -636,8 +636,27 @@ define void @test_nostack() #0 { ; X64-DFlyBSD-NOT: callq __morestack } +define void @test_nosplitstck() { + ret void +} + attributes #0 = { "split-stack" } ; X64-Linux-Large: .rodata ; X64-Linux-Large-NEXT: __morestack_addr: ; X64-Linux-Large-NEXT: .quad __morestack + +; X32-Linux: .section ".note.GNU-split-stack","",@progbits +; X32-Linux: .section ".note.GNU-no-split-stack","",@progbits + +; X64-Linux: .section ".note.GNU-split-stack","",@progbits +; X64-Linux: .section ".note.GNU-no-split-stack","",@progbits + +; X64-FreeBSD: .section ".note.GNU-split-stack","",@progbits +; X64-FreeBSD: .section ".note.GNU-no-split-stack","",@progbits + +; X32-DFlyBSD: .section ".note.GNU-split-stack","",@progbits +; X32-DFlyBSD: .section ".note.GNU-no-split-stack","",@progbits + +; X64-DFlyBSD: .section ".note.GNU-split-stack","",@progbits +; X64-DFlyBSD: .section ".note.GNU-no-split-stack","",@progbits -- 2.11.0