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.

llvm-svn: 295945
This commit is contained in:
Rui Ueyama 2017-02-23 08:09:51 +00:00
parent fd939c0f6c
commit a7e87252ce
2 changed files with 31 additions and 21 deletions

View File

@ -1334,9 +1334,15 @@ template <class ELFT> std::vector<PhdrEntry> Writer<ELFT>::createPhdrs() {
AddHdr(PT_OPENBSD_RANDOMIZE, Sec->getPhdrFlags())->add(Sec); AddHdr(PT_OPENBSD_RANDOMIZE, Sec->getPhdrFlags())->add(Sec);
// PT_GNU_STACK is a special section to tell the loader to make the // PT_GNU_STACK is a special section to tell the loader to make the
// pages for the stack non-executable. // pages for the stack non-executable. If you really want an executable
if (!Config->ZExecstack) // stack, you can pass -z execstack, but that's not recommended for
AddHdr(PT_GNU_STACK, PF_R | PF_W)->p_memsz = Config->ZStackSize; // 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 // 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 // is expected to perform W^X violations, such as calling mprotect(2) or

View File

@ -5,26 +5,30 @@
# RUN: ld.lld %t1 -o %t # RUN: ld.lld %t1 -o %t
# RUN: llvm-readobj --program-headers -s %t | FileCheck --check-prefix=RW %s # RUN: llvm-readobj --program-headers -s %t | FileCheck --check-prefix=RW %s
# RW: Sections [ # RW: Type: PT_GNU_STACK
# RW-NOT: Name: .note.GNU-stack # RW-NEXT: Offset: 0x0
# RW: ProgramHeaders [ # RW-NEXT: VirtualAddress: 0x0
# RW: ProgramHeader { # RW-NEXT: PhysicalAddress: 0x0
# RW: Type: PT_GNU_STACK # RW-NEXT: FileSize: 0
# RW-NEXT: Offset: 0x0 # RW-NEXT: MemSize: 0
# RW-NEXT: VirtualAddress: 0x0 # RW-NEXT: Flags [
# RW-NEXT: PhysicalAddress: 0x0 # RW-NEXT: PF_R
# RW-NEXT: FileSize: 0 # RW-NEXT: PF_W
# RW-NEXT: MemSize: 0
# RW-NEXT: Flags [
# RW-NEXT: PF_R
# RW-NEXT: PF_W
# RW-NEXT: ]
# RW-NEXT: Alignment: 0
# RW-NEXT: }
# RW-NEXT: ] # RW-NEXT: ]
# RW-NEXT: Alignment: 0
# RWX-NOT: Name: .note.GNU-stack # RWX: Type: PT_GNU_STACK
# RWX-NOT: 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 .globl _start
_start: _start: