ELF: Omit PT_GNU_STACK segment if -z execstack is provided.

In the previous patch (r254003), I made the linker emit PT_GNU_STACK
unconditionally. But sometimes you want to have a control over the
presence of the segment. With this patch, you can omit the segment
by passing -z execstack option.

llvm-svn: 254039
diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp
index b24b4ed..fba35fb 100644
--- a/lld/ELF/Writer.cpp
+++ b/lld/ELF/Writer.cpp
@@ -933,12 +933,11 @@
 
   // PT_GNU_STACK is a special section to tell the loader to make the
   // pages for the stack non-executable.
-  Elf_Phdr *PH = &Phdrs[++PhdrIdx];
-  PH->p_type = PT_GNU_STACK;
-  if (Config->ZExecStack)
-    PH->p_flags = PF_R | PF_W | PF_X;
-  else
+  if (!Config->ZExecStack) {
+    Elf_Phdr *PH = &Phdrs[++PhdrIdx];
+    PH->p_type = PT_GNU_STACK;
     PH->p_flags = PF_R | PF_W;
+  }
 
   // Fix up PT_INTERP as we now know the address of .interp section.
   if (Interp) {
@@ -962,11 +961,13 @@
 // Returns the number of PHDR entries.
 template <class ELFT> int Writer<ELFT>::getPhdrsNum() const {
   bool Tls = false;
-  int I = 3; // 3 for PT_PHDR, first PT_LOAD and PT_GNU_STACK
+  int I = 2; // 2 for PT_PHDR and first PT_LOAD
   if (needsInterpSection())
     ++I;
   if (isOutputDynamic())
     ++I;
+  if (!Config->ZExecStack)
+    ++I;
   uintX_t Last = PF_R;
   for (OutputSectionBase<ELFT> *Sec : OutputSections) {
     if (!needsPhdr<ELFT>(Sec))