[lld] r295945 - Always add PT_GNU_STACK.

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 23 00:09:52 PST 2017


Author: ruiu
Date: Thu Feb 23 02:09:51 2017
New Revision: 295945

URL: http://llvm.org/viewvc/llvm-project?rev=295945&view=rev
Log:
Always add PT_GNU_STACK.

If -z stack-size is given, we need to add PT_GNU_STACK even if
-z execstack is not given.

Modified:
    lld/trunk/ELF/Writer.cpp
    lld/trunk/test/ELF/gnustack.s

Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=295945&r1=295944&r2=295945&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Thu Feb 23 02:09:51 2017
@@ -1334,9 +1334,15 @@ template <class ELFT> std::vector<PhdrEn
     AddHdr(PT_OPENBSD_RANDOMIZE, Sec->getPhdrFlags())->add(Sec);
 
   // PT_GNU_STACK is a special section to tell the loader to make the
-  // pages for the stack non-executable.
-  if (!Config->ZExecstack)
-    AddHdr(PT_GNU_STACK, PF_R | PF_W)->p_memsz = Config->ZStackSize;
+  // pages for the stack non-executable. If you really want an executable
+  // stack, you can pass -z execstack, but that's not recommended for
+  // security reasons.
+  unsigned Perm;
+  if (Config->ZExecstack)
+    Perm = PF_R | PF_W | PF_X;
+  else
+    Perm = PF_R | PF_W;
+  AddHdr(PT_GNU_STACK, Perm)->p_memsz = Config->ZStackSize;
 
   // PT_OPENBSD_WXNEEDED is a OpenBSD-specific header to mark the executable
   // is expected to perform W^X violations, such as calling mprotect(2) or

Modified: lld/trunk/test/ELF/gnustack.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/gnustack.s?rev=295945&r1=295944&r2=295945&view=diff
==============================================================================
--- lld/trunk/test/ELF/gnustack.s (original)
+++ lld/trunk/test/ELF/gnustack.s Thu Feb 23 02:09:51 2017
@@ -5,26 +5,30 @@
 # RUN: ld.lld %t1 -o %t
 # RUN: llvm-readobj --program-headers -s %t | FileCheck --check-prefix=RW %s
 
-# RW:       Sections [
-# RW-NOT:   Name: .note.GNU-stack
-# RW:       ProgramHeaders [
-# RW:        ProgramHeader {
-# RW:        Type: PT_GNU_STACK
-# RW-NEXT:   Offset: 0x0
-# RW-NEXT:   VirtualAddress: 0x0
-# RW-NEXT:   PhysicalAddress: 0x0
-# RW-NEXT:   FileSize: 0
-# RW-NEXT:   MemSize: 0
-# RW-NEXT:   Flags [
-# RW-NEXT:     PF_R
-# RW-NEXT:     PF_W
-# RW-NEXT:   ]
-# RW-NEXT:   Alignment: 0
-# RW-NEXT:   }
+# RW:      Type: PT_GNU_STACK
+# RW-NEXT: Offset: 0x0
+# RW-NEXT: VirtualAddress: 0x0
+# RW-NEXT: PhysicalAddress: 0x0
+# RW-NEXT: FileSize: 0
+# RW-NEXT: MemSize: 0
+# RW-NEXT: Flags [
+# RW-NEXT:   PF_R
+# RW-NEXT:   PF_W
 # RW-NEXT: ]
+# RW-NEXT: Alignment: 0
 
-# RWX-NOT: Name: .note.GNU-stack
-# RWX-NOT: Type: PT_GNU_STACK
+# RWX:      Type: PT_GNU_STACK
+# RWX-NEXT: Offset: 0x0
+# RWX-NEXT: VirtualAddress: 0x0
+# RWX-NEXT: PhysicalAddress: 0x0
+# RWX-NEXT: FileSize: 0
+# RWX-NEXT: MemSize: 0
+# RWX-NEXT: Flags [
+# RWX-NEXT:   PF_R
+# RWX-NEXT:   PF_W
+# RWX-NEXT:   PF_X
+# RWX-NEXT: ]
+# RWX-NEXT: Alignment: 0
 
 .globl _start
 _start:




More information about the llvm-commits mailing list