[PATCH] D25469: [ELF] - Added support of PT_OPENBSD_RANDOMIZE

George Rimar via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 11 05:43:45 PDT 2016


grimar created this revision.
grimar added reviewers: ruiu, rafael.
grimar added subscribers: llvm-commits, grimar, evgeny777.

This is 30646.

PT_OPENBSD_RANDOMIZE
The array element specifies the location and size of a part of the memory image of the program that must be filled with random data before any code in the object is executed. The memory region specified by a segment of this type may overlap the region specified by a PT_GNU_RELRO segment, in which case the intersection will be filled with random data before being marked read-only.

Reference links:
http://man.openbsd.org/OpenBSD-current/man5/elf.5
https://github.com/openbsd/src/commit/c494713c450d98da3f2e1451ee8c7fb675a7c461


https://reviews.llvm.org/D25469

Files:
  ELF/LinkerScript.cpp
  ELF/Writer.cpp
  test/ELF/linkerscript/openbsd-randomize.s
  test/ELF/openbsd_randomize.s


Index: test/ELF/openbsd_randomize.s
===================================================================
--- test/ELF/openbsd_randomize.s
+++ test/ELF/openbsd_randomize.s
@@ -0,0 +1,20 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t
+# RUN: ld.lld %t -o %t.out
+# RUN: llvm-readobj --program-headers %t.out | FileCheck %s
+
+# CHECK:      ProgramHeader {
+# CHECK:        Type:  (0x65A3DBE6)
+# CHECK-NEXT:   Offset: 0x158
+# CHECK-NEXT:   VirtualAddress: 0x10158
+# CHECK-NEXT:   PhysicalAddress: 0x10158
+# CHECK-NEXT:   FileSize: 8
+# CHECK-NEXT:   MemSize: 8
+# CHECK-NEXT:   Flags [ (0x4)
+# CHECK-NEXT:     PF_R (0x4)
+# CHECK-NEXT:   ]
+# CHECK-NEXT:   Alignment: 1
+# CHECK-NEXT: }
+
+.section .openbsd.randomdata, "a"
+.quad 0
Index: test/ELF/linkerscript/openbsd-randomize.s
===================================================================
--- test/ELF/linkerscript/openbsd-randomize.s
+++ test/ELF/linkerscript/openbsd-randomize.s
@@ -0,0 +1,22 @@
+# RUN: llvm-mc -filetype=obj -triple=i686-unknown-linux %s -o %t.o
+# RUN: echo "PHDRS { text PT_LOAD FILEHDR PHDRS; rand PT_OPENBSD_RANDOMIZE; } \
+# RUN:       SECTIONS { . = SIZEOF_HEADERS; \
+# RUN:         .openbsd.randomdata : { *(.openbsd.randomdata) } : rand }" > %t.script
+# RUN: ld.lld --script %t.script %t.o -o %t
+# RUN: llvm-readobj --program-headers -s %t | FileCheck %s
+
+# CHECK:      ProgramHeader {
+# CHECK:        Type:  (0x65A3DBE6)
+# CHECK-NEXT:   Offset: 0x94
+# CHECK-NEXT:   VirtualAddress: 0x94
+# CHECK-NEXT:   PhysicalAddress: 0x94
+# CHECK-NEXT:   FileSize: 8
+# CHECK-NEXT:   MemSize: 8
+# CHECK-NEXT:   Flags [ (0x4)
+# CHECK-NEXT:     PF_R (0x4)
+# CHECK-NEXT:   ]
+# CHECK-NEXT:   Alignment: 1
+# CHECK-NEXT: }
+
+.section .openbsd.randomdata, "a"
+.quad 0
Index: ELF/Writer.cpp
===================================================================
--- ELF/Writer.cpp
+++ ELF/Writer.cpp
@@ -1097,6 +1097,14 @@
     Hdr.add(Out<ELFT>::EhFrameHdr);
   }
 
+  // PT_OPENBSD_RANDOMIZE specifies the location and size of a part of the
+  // memory image of the program that must be filled with random data before any
+  // code in the object is executed.
+  if (OutputSectionBase<ELFT> *Sec = findSection(".openbsd.randomdata")) {
+    Phdr &Hdr = *AddHdr(PT_OPENBSD_RANDOMIZE, Sec->getPhdrFlags());
+    Hdr.add(Sec);
+  }
+
   // PT_ARM_EXIDX is the ARM EHABI equivalent of PT_GNU_EH_FRAME
   if (ARMExidx.First)
     Ret.push_back(std::move(ARMExidx));
Index: ELF/LinkerScript.cpp
===================================================================
--- ELF/LinkerScript.cpp
+++ ELF/LinkerScript.cpp
@@ -1714,6 +1714,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_RANDOMIZE", PT_OPENBSD_RANDOMIZE)
                      .Default(-1);
 
   if (Ret == (unsigned)-1) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D25469.74236.patch
Type: text/x-patch
Size: 3036 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20161011/d3e3cf0d/attachment.bin>


More information about the llvm-commits mailing list