Index: lld/trunk/ELF/Config.h =================================================================== --- lld/trunk/ELF/Config.h +++ lld/trunk/ELF/Config.h @@ -126,6 +126,7 @@ bool ZNow; bool ZOrigin; bool ZRelro; + bool ZWxneeded; DiscardPolicy Discard; SortSectionPolicy SortSection; StripPolicy Strip = StripPolicy::None; Index: lld/trunk/ELF/Driver.cpp =================================================================== --- lld/trunk/ELF/Driver.cpp +++ lld/trunk/ELF/Driver.cpp @@ -494,6 +494,7 @@ Config->ZNow = hasZOption(Args, "now"); Config->ZOrigin = hasZOption(Args, "origin"); Config->ZRelro = !hasZOption(Args, "norelro"); + Config->ZWxneeded = hasZOption(Args, "wxneeded"); if (!Config->Relocatable) Config->Strip = getStripOption(Args); Index: lld/trunk/ELF/LinkerScript.cpp =================================================================== --- lld/trunk/ELF/LinkerScript.cpp +++ lld/trunk/ELF/LinkerScript.cpp @@ -1720,6 +1720,7 @@ .Case("PT_GNU_EH_FRAME", PT_GNU_EH_FRAME) .Case("PT_GNU_STACK", PT_GNU_STACK) .Case("PT_GNU_RELRO", PT_GNU_RELRO) + .Case("PT_OPENBSD_WXNEEDED", PT_OPENBSD_WXNEEDED) .Default(-1); if (Ret == (unsigned)-1) { Index: lld/trunk/ELF/Writer.cpp =================================================================== --- lld/trunk/ELF/Writer.cpp +++ lld/trunk/ELF/Writer.cpp @@ -1109,6 +1109,13 @@ Hdr.H.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 + // mmap(2) with PROT_WRITE | PROT_EXEC, which is prohibited by default on + // OpenBSD. + if (Config->ZWxneeded) + AddHdr(PT_OPENBSD_WXNEEDED, PF_X); + if (Note.First) Ret.push_back(std::move(Note)); return Ret; Index: lld/trunk/test/ELF/linkerscript/openbsd-wxneeded.s =================================================================== --- lld/trunk/test/ELF/linkerscript/openbsd-wxneeded.s +++ lld/trunk/test/ELF/linkerscript/openbsd-wxneeded.s @@ -0,0 +1,17 @@ +# RUN: llvm-mc -filetype=obj -triple=i686-unknown-linux %s -o %t.o +# RUN: echo "PHDRS { text PT_LOAD FILEHDR PHDRS; wxneeded PT_OPENBSD_WXNEEDED; }" > %t.script +# RUN: ld.lld -z wxneeded --script %t.script %t.o -o %t +# RUN: llvm-readobj --program-headers %t | FileCheck %s + +# CHECK: ProgramHeader { +# CHECK: Type: (0x65A3DBE7) +# CHECK-NEXT: Offset: 0x0 +# CHECK-NEXT: VirtualAddress: 0x0 +# CHECK-NEXT: PhysicalAddress: 0x0 +# CHECK-NEXT: FileSize: 0 +# CHECK-NEXT: MemSize: 0 +# CHECK-NEXT: Flags [ +# CHECK-NEXT: PF_R +# CHECK-NEXT: ] +# CHECK-NEXT: Alignment: 0 +# CHECK-NEXT: } Index: lld/trunk/test/ELF/openbsd-wxneeded.s =================================================================== --- lld/trunk/test/ELF/openbsd-wxneeded.s +++ lld/trunk/test/ELF/openbsd-wxneeded.s @@ -0,0 +1,17 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t +# RUN: ld.lld -z wxneeded %t -o %t.out +# RUN: llvm-readobj --program-headers %t.out | FileCheck %s + +# CHECK: ProgramHeader { +# CHECK: Type: (0x65A3DBE7) +# CHECK-NEXT: Offset: 0x0 +# CHECK-NEXT: VirtualAddress: 0x0 +# CHECK-NEXT: PhysicalAddress: 0x0 +# CHECK-NEXT: FileSize: 0 +# CHECK-NEXT: MemSize: 0 +# CHECK-NEXT: Flags [ +# CHECK-NEXT: PF_X +# CHECK-NEXT: ] +# CHECK-NEXT: Alignment: 0 +# CHECK-NEXT: }