Index: llvm/include/llvm/CodeGen/MachineModuleInfo.h =================================================================== --- llvm/include/llvm/CodeGen/MachineModuleInfo.h +++ llvm/include/llvm/CodeGen/MachineModuleInfo.h @@ -119,12 +119,6 @@ /// True if debugging information is available in this module. bool DbgInfoAvailable; - /// True if the module calls the __morestack function indirectly, as is - /// required under the large code model on x86. This is used to emit - /// a definition of a symbol, __morestack_addr, containing the address. See - /// comments in lib/Target/X86/X86FrameLowering.cpp for more details. - bool UsesMorestackAddr; - /// Maps IR Functions to their corresponding MachineFunctions. DenseMap> MachineFunctions; /// Next unique number available for a MachineFunction. @@ -187,14 +181,6 @@ /// Returns true if valid debug info is present. bool hasDebugInfo() const { return DbgInfoAvailable; } - bool usesMorestackAddr() const { - return UsesMorestackAddr; - } - - void setUsesMorestackAddr(bool b) { - UsesMorestackAddr = 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. Index: llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp =================================================================== --- llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp +++ llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp @@ -1904,23 +1904,6 @@ // Emit bytes for llvm.commandline metadata. emitModuleCommandLines(M); - // Emit __morestack address if needed for indirect calls. - if (MMI->usesMorestackAddr()) { - Align Alignment(1); - MCSection *ReadOnlySection = getObjFileLowering().getSectionForConstant( - getDataLayout(), SectionKind::getReadOnly(), - /*C=*/nullptr, Alignment); - OutStreamer->SwitchSection(ReadOnlySection); - - MCSymbol *AddrSymbol = - OutContext.getOrCreateSymbol(StringRef("__morestack_addr")); - OutStreamer->emitLabel(AddrSymbol); - - unsigned PtrSize = MAI->getCodePointerSize(); - OutStreamer->emitSymbolValue(GetExternalSymbolSymbol("__morestack"), - PtrSize); - } - // Emit .note.GNU-split-stack and .note.GNU-no-split-stack sections if // split-stack is used. if (TM.getTargetTriple().isOSBinFormatELF() && HasSplitStack) { Index: llvm/lib/CodeGen/MachineModuleInfo.cpp =================================================================== --- llvm/lib/CodeGen/MachineModuleInfo.cpp +++ llvm/lib/CodeGen/MachineModuleInfo.cpp @@ -198,7 +198,6 @@ ObjFileMMI = nullptr; CurCallSite = 0; NextFnNum = 0; - UsesMorestackAddr = false; AddrLabelSymbols = nullptr; DbgInfoAvailable = false; } @@ -225,7 +224,6 @@ Context.setObjectFileInfo(MMI.TM.getObjFileLowering()); ObjFileMMI = MMI.ObjFileMMI; CurCallSite = MMI.CurCallSite; - UsesMorestackAddr = MMI.UsesMorestackAddr; AddrLabelSymbols = MMI.AddrLabelSymbols; ExternalContext = MMI.ExternalContext; TheModule = MMI.TheModule; Index: llvm/lib/Target/X86/X86AsmPrinter.cpp =================================================================== --- llvm/lib/Target/X86/X86AsmPrinter.cpp +++ llvm/lib/Target/X86/X86AsmPrinter.cpp @@ -30,6 +30,7 @@ #include "llvm/IR/Mangler.h" #include "llvm/IR/Module.h" #include "llvm/IR/Type.h" +#include "llvm/MC/MCAsmInfo.h" #include "llvm/MC/MCCodeEmitter.h" #include "llvm/MC/MCContext.h" #include "llvm/MC/MCExpr.h" @@ -826,6 +827,22 @@ emitStackMaps(SM); FM.serializeToFaultMapSection(); } + + // Emit __morestack address if needed for indirect calls. + if (TT.getArch() == Triple::x86_64 && TM.getCodeModel() == CodeModel::Large) { + if (MCSymbol *AddrSymbol = OutContext.lookupSymbol("__morestack_addr")) { + Align Alignment(1); + MCSection *ReadOnlySection = getObjFileLowering().getSectionForConstant( + getDataLayout(), SectionKind::getReadOnly(), + /*C=*/nullptr, Alignment); + OutStreamer->SwitchSection(ReadOnlySection); + OutStreamer->emitLabel(AddrSymbol); + + unsigned PtrSize = MAI->getCodePointerSize(); + OutStreamer->emitSymbolValue(GetExternalSymbolSymbol("__morestack"), + PtrSize); + } + } } //===----------------------------------------------------------------------===// Index: llvm/lib/Target/X86/X86FrameLowering.cpp =================================================================== --- llvm/lib/Target/X86/X86FrameLowering.cpp +++ llvm/lib/Target/X86/X86FrameLowering.cpp @@ -3135,7 +3135,6 @@ .addReg(0) .addExternalSymbol("__morestack_addr") .addReg(0); - MF.getMMI().setUsesMorestackAddr(true); } else { if (Is64Bit) BuildMI(allocMBB, DL, TII.get(X86::CALL64pcrel32)) Index: llvm/test/CodeGen/X86/morestack-decl.ll =================================================================== --- /dev/null +++ llvm/test/CodeGen/X86/morestack-decl.ll @@ -0,0 +1,10 @@ +; RUN: llc -mcpu=generic -mtriple=x86_64-linux -code-model=large < %s | FileCheck %s + +; Check what happens if we have an existing declaration of __morestack_addr + +; CHECK: .section ".note.GNU-stack","",@progbits +; CHECK-NEXT: .section .rodata,"a",@progbits +; CHECK-NEXT: __morestack_addr: +; CHECK-NEXT: .quad __morestack + +declare void @__morestack_addr() Index: llvm/test/CodeGen/X86/segmented-stacks.ll =================================================================== --- llvm/test/CodeGen/X86/segmented-stacks.ll +++ llvm/test/CodeGen/X86/segmented-stacks.ll @@ -2123,7 +2123,10 @@ attributes #0 = { "split-stack" } -; X64-Linux-Large: .rodata +; X64-Linux-Large: .section ".note.GNU-split-stack","",@progbits +; X64-Linux-Large-NEXT: .section ".note.GNU-no-split-stack","",@progbits +; X64-Linux-Large-NEXT: .section ".note.GNU-stack","",@progbits +; X64-Linux-Large-NEXT: .rodata ; X64-Linux-Large-NEXT: __morestack_addr: ; X64-Linux-Large-NEXT: .quad __morestack