Skip to content

Commit 52574b5

Browse files
authored
[ELF] Add support for PT_OPENBSD_NOBTCFI (llvm#120005)
1 parent c2aee50 commit 52574b5

File tree

5 files changed

+22
-2
lines changed

5 files changed

+22
-2
lines changed

lld/ELF/Config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,7 @@ struct Config {
363363
bool zInterpose;
364364
bool zKeepTextSectionPrefix;
365365
bool zLrodataAfterBss;
366+
bool zNoBtCfi = false;
366367
bool zNodefaultlib;
367368
bool zNodelete;
368369
bool zNodlopen;

lld/ELF/Driver.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1897,6 +1897,9 @@ static void setConfigs(Ctx &ctx, opt::InputArgList &args) {
18971897
ErrAlways(ctx) << "cannot open --why-extract= file " << ctx.arg.whyExtract
18981898
<< ": " << e.message();
18991899
}
1900+
1901+
if (ctx.arg.osabi == ELFOSABI_OPENBSD)
1902+
ctx.arg.zNoBtCfi = hasZOption(args, "nobtcfi");
19001903
}
19011904

19021905
static bool isFormatBinary(Ctx &ctx, StringRef s) {

lld/ELF/Writer.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2405,6 +2405,11 @@ Writer<ELFT>::createPhdrs(Partition &part) {
24052405
addHdr(PT_GNU_STACK, perm)->p_memsz = ctx.arg.zStackSize;
24062406
}
24072407

2408+
// PT_OPENBSD_NOBTCFI is an OpenBSD-specific header to mark that the
2409+
// executable is expected to violate branch-target CFI checks.
2410+
if (ctx.arg.zNoBtCfi)
2411+
addHdr(PT_OPENBSD_NOBTCFI, PF_X);
2412+
24082413
// PT_OPENBSD_WXNEEDED is a OpenBSD-specific header to mark the executable
24092414
// is expected to perform W^X violations, such as calling mprotect(2) or
24102415
// mmap(2) with PROT_WRITE | PROT_EXEC, which is prohibited by default on

lld/docs/ld.lld.1

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -952,6 +952,11 @@ Let __start_/__stop_ references retain the associated C identifier name sections
952952
Do not allow relocations against read-only segments.
953953
This is the default.
954954
.Pp
955+
.It Cm nobtcfi
956+
Create a
957+
.Dv PT_OPENBSD_NOBTCFI
958+
segment.
959+
.Pp
955960
.It Cm wxneeded
956961
Create a
957962
.Dv PT_OPENBSD_WXNEEDED

lld/test/ELF/openbsd-phdr.s

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44
# RUN: ld.lld randomdata.o -o randomdata
55
# RUN: llvm-readelf -S -l randomdata | FileCheck %s --check-prefix=RANDOMDATA
66

7-
# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-openbsd /dev/null -o wxneeded.o
8-
# RUN: ld.lld -z wxneeded wxneeded.o -o wxneeded
7+
# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-openbsd /dev/null -o empty.o
8+
# RUN: ld.lld -z nobtcfi empty.o -o nobtcfi
9+
# RUN: llvm-readelf -l nobtcfi | FileCheck %s --check-prefix=NOBTCFI
10+
11+
# RUN: ld.lld -z wxneeded empty.o -o wxneeded
912
# RUN: llvm-readelf -l wxneeded | FileCheck %s --check-prefix=WXNEEDED
1013

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

20+
# NOBTCFI: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
21+
# NOBTCFI: OPENBSD_NOBTCFI 0x000000 0x0000000000000000 0x0000000000000000 0x000000 0x000000 E 0
22+
1723
# WXNEEDED: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
1824
# WXNEEDED: OPENBSD_WXNEEDED 0x000000 0x0000000000000000 0x0000000000000000 0x000000 0x000000 E 0
1925

0 commit comments

Comments
 (0)