Skip to content

Commit c7c8d6b

Browse files
committed
[ELF] Add support for PT_OPENBSD_NOBTCFI
1 parent 8652608 commit c7c8d6b

File tree

5 files changed

+19
-0
lines changed

5 files changed

+19
-0
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;
366367
bool zNodefaultlib;
367368
bool zNodelete;
368369
bool zNodlopen;

lld/ELF/Driver.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1487,6 +1487,7 @@ static void readConfigs(Ctx &ctx, opt::InputArgList &args) {
14871487
args, "keep-text-section-prefix", "nokeep-text-section-prefix", false);
14881488
ctx.arg.zLrodataAfterBss =
14891489
getZFlag(args, "lrodata-after-bss", "nolrodata-after-bss", false);
1490+
ctx.arg.zNoBtCfi = hasZOption(args, "nobtcfi");
14901491
ctx.arg.zNodefaultlib = hasZOption(args, "nodefaultlib");
14911492
ctx.arg.zNodelete = hasZOption(args, "nodelete");
14921493
ctx.arg.zNodlopen = hasZOption(args, "nodlopen");

lld/ELF/Writer.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2412,6 +2412,11 @@ Writer<ELFT>::createPhdrs(Partition &part) {
24122412
if (ctx.arg.zWxneeded)
24132413
addHdr(PT_OPENBSD_WXNEEDED, PF_X);
24142414

2415+
// PT_OPENBSD_NOBTCFI is an OpenBSD-specific header to mark that the
2416+
// executable is expected to violate branch-target CFI checks.
2417+
if (ctx.arg.zNoBtCfi)
2418+
addHdr(PT_OPENBSD_NOBTCFI, PF_X);
2419+
24152420
if (OutputSection *cmd = findSection(ctx, ".note.gnu.property", partNo))
24162421
addHdr(PT_GNU_PROPERTY, PF_R)->add(cmd);
24172422

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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
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 nobtcfi.o
8+
# RUN: ld.lld -z nobtcfi nobtcfi.o -o nobtcfi
9+
# RUN: llvm-readelf -l nobtcfi | FileCheck %s --check-prefix=NOBTCFI
10+
711
# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-openbsd /dev/null -o wxneeded.o
812
# RUN: ld.lld -z wxneeded wxneeded.o -o wxneeded
913
# RUN: llvm-readelf -l wxneeded | FileCheck %s --check-prefix=WXNEEDED
@@ -14,6 +18,9 @@
1418
# RANDOMDATA: Name Type Address Off Size ES Flg Lk Inf Al
1519
# RANDOMDATA: .openbsd.randomdata PROGBITS [[ADDR:[0-9a-f]+]] [[O:[0-9a-f]+]] 000008 00 A 0 0 1
1620

21+
# NOBTCFI: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
22+
# NOBTCFI: OPENBSD_NOBTCFI 0x000000 0x0000000000000000 0x0000000000000000 0x000000 0x000000 E 0
23+
1724
# WXNEEDED: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
1825
# WXNEEDED: OPENBSD_WXNEEDED 0x000000 0x0000000000000000 0x0000000000000000 0x000000 0x000000 E 0
1926

0 commit comments

Comments
 (0)