Add AsmPrinter support for emitting a directive to declare that
the code being generated does not require an executable stack.

Also, add target-specific code to make use of this on Linux
on x86. 


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@50634 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/CodeGen/AsmPrinter.cpp b/lib/CodeGen/AsmPrinter.cpp
index 65b8c43..9c4e789 100644
--- a/lib/CodeGen/AsmPrinter.cpp
+++ b/lib/CodeGen/AsmPrinter.cpp
@@ -192,6 +192,13 @@
                                          E = CMM->begin(); I != E; )
     (*--I)->finishAssembly(O, *this, *TAI);
 
+  // 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 (TAI->getNonexecutableStackDirective())
+      O << TAI->getNonexecutableStackDirective() << "\n";
+
   delete Mang; Mang = 0;
   return false;
 }
diff --git a/lib/Target/TargetAsmInfo.cpp b/lib/Target/TargetAsmInfo.cpp
index 2169945..c2504cc 100644
--- a/lib/Target/TargetAsmInfo.cpp
+++ b/lib/Target/TargetAsmInfo.cpp
@@ -26,6 +26,7 @@
   TLSDataSection("\t.section .tdata,\"awT\",@progbits"),
   TLSBSSSection("\t.section .tbss,\"awT\",@nobits"),
   ZeroFillDirective(0),
+  NonexecutableStackDirective(0),
   NeedsSet(false),
   MaxInstLength(4),
   PCSymbol("$"),
diff --git a/lib/Target/X86/X86Subtarget.cpp b/lib/Target/X86/X86Subtarget.cpp
index e0f350c..e555079 100644
--- a/lib/Target/X86/X86Subtarget.cpp
+++ b/lib/Target/X86/X86Subtarget.cpp
@@ -278,6 +278,8 @@
         DarwinVers = atoi(&TT[Pos+7]);
       else
         DarwinVers = 8;  // Minimum supported darwin is Tiger.
+    } else if (TT.find("linux") != std::string::npos) {
+      TargetType = isELFLinux;
     } else if (TT.find("cygwin") != std::string::npos) {
       TargetType = isCygwin;
     } else if (TT.find("mingw") != std::string::npos) {
@@ -302,6 +304,9 @@
     
 #elif defined(_WIN32) || defined(_WIN64)
     TargetType = isWindows;
+#elif defined(__linux__)
+    // Linux doesn't imply ELF, but we don't currently support anything else.
+    TargetType = isELFLinux;
 #endif
   }
 
diff --git a/lib/Target/X86/X86Subtarget.h b/lib/Target/X86/X86Subtarget.h
index c91088d..1e955cd 100644
--- a/lib/Target/X86/X86Subtarget.h
+++ b/lib/Target/X86/X86Subtarget.h
@@ -84,7 +84,7 @@
 
 public:
   enum {
-    isELF, isCygwin, isDarwin, isWindows, isMingw
+    isELF, isELFLinux, isCygwin, isDarwin, isWindows, isMingw
   } TargetType;
 
   /// This constructor initializes the data members to match that
@@ -132,7 +132,12 @@
   bool isFlavorIntel() const { return AsmFlavor == Intel; }
 
   bool isTargetDarwin() const { return TargetType == isDarwin; }
-  bool isTargetELF() const { return TargetType == isELF; }
+  bool isTargetELF() const {
+    return TargetType == isELF || TargetType == isELFLinux;
+  }
+  bool isTargetLinux() const {
+    return TargetType == isELFLinux;
+  }
   bool isTargetWindows() const { return TargetType == isWindows; }
   bool isTargetMingw() const { return TargetType == isMingw; }
   bool isTargetCygMing() const { return (TargetType == isMingw ||
diff --git a/lib/Target/X86/X86TargetAsmInfo.cpp b/lib/Target/X86/X86TargetAsmInfo.cpp
index 7636d77..4aa9d4c 100644
--- a/lib/Target/X86/X86TargetAsmInfo.cpp
+++ b/lib/Target/X86/X86TargetAsmInfo.cpp
@@ -128,6 +128,7 @@
     break;
 
   case X86Subtarget::isELF:
+  case X86Subtarget::isELFLinux:
     ReadOnlySection = "\t.section\t.rodata";
     FourByteConstantSection = "\t.section\t.rodata.cst4,\"aM\",@progbits,4";
     EightByteConstantSection = "\t.section\t.rodata.cst8,\"aM\",@progbits,8";
@@ -229,6 +230,10 @@
     SectionEndDirectiveSuffix = "\tends\n";
   }
 
+  // On Linux we must declare when we can use a non-executable stack.
+  if (Subtarget->isTargetLinux())
+    NonexecutableStackDirective = "\t.section\t.note.GNU-stack,\"\",@progbits";
+
   AssemblerDialect = Subtarget->getAsmFlavor();
 }