Skip to content

Commit

Permalink
[ELF] - Added support of PT_OPENBSD_RANDOMIZE
Browse files Browse the repository at this point in the history
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
openbsd/src@c494713

Differential revision: https://reviews.llvm.org/D25469

llvm-svn: 284234
George Rimar committed Oct 14, 2016
1 parent 220c755 commit 270173f
Showing 4 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions lld/ELF/LinkerScript.cpp
Original file line number Diff line number Diff line change
@@ -1720,6 +1720,7 @@ unsigned ScriptParser::readPhdrType() {
.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)
.Case("PT_OPENBSD_WXNEEDED", PT_OPENBSD_WXNEEDED)
.Default(-1);

8 changes: 8 additions & 0 deletions lld/ELF/Writer.cpp
Original file line number Diff line number Diff line change
@@ -1097,6 +1097,14 @@ std::vector<PhdrEntry<ELFT>> Writer<ELFT>::createPhdrs() {
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));
22 changes: 22 additions & 0 deletions lld/test/ELF/linkerscript/openbsd-randomize.s
Original file line number Diff line number Diff line change
@@ -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
20 changes: 20 additions & 0 deletions lld/test/ELF/openbsd-randomize.s
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 270173f

Please sign in to comment.