Skip to content

Commit

Permalink
[ELF] Add support for PT_OPENBSD_NOBTCFI (llvm#120005)
Browse files Browse the repository at this point in the history
  • Loading branch information
brad0 authored Dec 20, 2024
1 parent c2aee50 commit 52574b5
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 2 deletions.
1 change: 1 addition & 0 deletions lld/ELF/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ struct Config {
bool zInterpose;
bool zKeepTextSectionPrefix;
bool zLrodataAfterBss;
bool zNoBtCfi = false;
bool zNodefaultlib;
bool zNodelete;
bool zNodlopen;
Expand Down
3 changes: 3 additions & 0 deletions lld/ELF/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1897,6 +1897,9 @@ static void setConfigs(Ctx &ctx, opt::InputArgList &args) {
ErrAlways(ctx) << "cannot open --why-extract= file " << ctx.arg.whyExtract
<< ": " << e.message();
}

if (ctx.arg.osabi == ELFOSABI_OPENBSD)
ctx.arg.zNoBtCfi = hasZOption(args, "nobtcfi");
}

static bool isFormatBinary(Ctx &ctx, StringRef s) {
Expand Down
5 changes: 5 additions & 0 deletions lld/ELF/Writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2405,6 +2405,11 @@ Writer<ELFT>::createPhdrs(Partition &part) {
addHdr(PT_GNU_STACK, perm)->p_memsz = ctx.arg.zStackSize;
}

// PT_OPENBSD_NOBTCFI is an OpenBSD-specific header to mark that the
// executable is expected to violate branch-target CFI checks.
if (ctx.arg.zNoBtCfi)
addHdr(PT_OPENBSD_NOBTCFI, PF_X);

// 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
Expand Down
5 changes: 5 additions & 0 deletions lld/docs/ld.lld.1
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,11 @@ Let __start_/__stop_ references retain the associated C identifier name sections
Do not allow relocations against read-only segments.
This is the default.
.Pp
.It Cm nobtcfi
Create a
.Dv PT_OPENBSD_NOBTCFI
segment.
.Pp
.It Cm wxneeded
Create a
.Dv PT_OPENBSD_WXNEEDED
Expand Down
10 changes: 8 additions & 2 deletions lld/test/ELF/openbsd-phdr.s
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
# RUN: ld.lld randomdata.o -o randomdata
# RUN: llvm-readelf -S -l randomdata | FileCheck %s --check-prefix=RANDOMDATA

# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-openbsd /dev/null -o wxneeded.o
# RUN: ld.lld -z wxneeded wxneeded.o -o wxneeded
# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-openbsd /dev/null -o empty.o
# RUN: ld.lld -z nobtcfi empty.o -o nobtcfi
# RUN: llvm-readelf -l nobtcfi | FileCheck %s --check-prefix=NOBTCFI

# RUN: ld.lld -z wxneeded empty.o -o wxneeded
# RUN: llvm-readelf -l wxneeded | FileCheck %s --check-prefix=WXNEEDED

# RUN: ld.lld -T lds randomdata.o -o out
Expand All @@ -14,6 +17,9 @@
# RANDOMDATA: Name Type Address Off Size ES Flg Lk Inf Al
# RANDOMDATA: .openbsd.randomdata PROGBITS [[ADDR:[0-9a-f]+]] [[O:[0-9a-f]+]] 000008 00 A 0 0 1

# NOBTCFI: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
# NOBTCFI: OPENBSD_NOBTCFI 0x000000 0x0000000000000000 0x0000000000000000 0x000000 0x000000 E 0

# WXNEEDED: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
# WXNEEDED: OPENBSD_WXNEEDED 0x000000 0x0000000000000000 0x0000000000000000 0x000000 0x000000 E 0

Expand Down

0 comments on commit 52574b5

Please sign in to comment.