Skip to content

Commit e00f1f8

Browse files
authored
[ELF] Error for executable .note.GNU-stack unless -z execstack or -r
.note.GNU-stack with the SHF_EXECINSTR flag requires an executable stack. This is exceedingly rare. We report an error to force the user to explicitly request an executable stack. Close #121234 Pull Request: #124068
1 parent a245309 commit e00f1f8

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-11
lines changed

lld/ELF/InputFiles.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,9 +1025,18 @@ InputSectionBase *ObjFile<ELFT>::createInputSection(uint32_t idx,
10251025
// Therefore, we make LLD always add PT_GNU_STACK unless it is
10261026
// explicitly told to do otherwise (by -z execstack). Because the stack
10271027
// executable-ness is controlled solely by command line options,
1028-
// .note.GNU-stack sections are simply ignored.
1029-
if (name == ".note.GNU-stack")
1028+
// .note.GNU-stack sections are, with one exception, ignored. Report
1029+
// an error if we encounter an executable .note.GNU-stack to force the
1030+
// user to explicitly request an executable stack.
1031+
if (name == ".note.GNU-stack") {
1032+
if ((sec.sh_flags & SHF_EXECINSTR) && !ctx.arg.relocatable &&
1033+
ctx.arg.zGnustack != GnuStackKind::Exec) {
1034+
Err(ctx) << this
1035+
<< ": requires an executable stack, but -z execstack is not "
1036+
"specified";
1037+
}
10301038
return &InputSection::discarded;
1039+
}
10311040

10321041
// Object files that use processor features such as Intel Control-Flow
10331042
// Enforcement (CET) or AArch64 Branch Target Identification BTI, use a

lld/test/ELF/gnustack.s

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
# REQUIRES: x86
2-
# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t1
2+
# RUN: rm -rf %t && split-file %s %t && cd %t
3+
# RUN: llvm-mc -filetype=obj -triple=x86_64 a.s -o a.o
4+
# RUN: llvm-mc -filetype=obj -triple=x86_64 x.s -o x.o
5+
# RUN: llvm-mc -filetype=obj -triple=x86_64 nox.s -o nox.o
36

4-
# RUN: ld.lld %t1 -z execstack -o %t
5-
# RUN: llvm-readobj --program-headers -S %t | FileCheck --check-prefix=RWX %s
7+
# RUN: ld.lld a.o -z execstack -o out
8+
# RUN: llvm-readobj --program-headers -S out | FileCheck --check-prefix=RWX %s
69

7-
# RUN: ld.lld %t1 -o %t
8-
# RUN: llvm-readobj --program-headers -S %t | FileCheck --check-prefix=RW %s
10+
# RUN: ld.lld a.o -o out
11+
# RUN: llvm-readobj --program-headers -S out | FileCheck --check-prefix=RW %s
912

10-
# RUN: ld.lld %t1 -o %t -z noexecstack
11-
# RUN: llvm-readobj --program-headers -S %t | FileCheck --check-prefix=RW %s
13+
# RUN: ld.lld a.o -o out -z noexecstack
14+
# RUN: llvm-readobj --program-headers -S out | FileCheck --check-prefix=RW %s
1215

13-
# RUN: ld.lld %t1 -o %t -z nognustack
14-
# RUN: llvm-readobj --program-headers -s %t | FileCheck --check-prefix=NOGNUSTACK %s
16+
# RUN: ld.lld a.o -o out -z nognustack
17+
# RUN: llvm-readobj --program-headers -s out | FileCheck --check-prefix=NOGNUSTACK %s
1518

1619
# RW: Type: PT_GNU_STACK
1720
# RW-NEXT: Offset: 0x0
@@ -40,5 +43,19 @@
4043

4144
# NOGNUSTACK-NOT: Type: PT_GNU_STACK
4245

46+
# RUN: not ld.lld a.o x.o nox.o x.o 2>&1 | FileCheck %s --check-prefix=ERR --implicit-check-not=error:
47+
# RUN: not ld.lld a.o x.o nox.o x.o -z nognustack 2>&1 | FileCheck %s --check-prefix=ERR --implicit-check-not=error:
48+
# ERR-COUNT-2: error: x.o: requires an executable stack, but -z execstack is not specified
49+
50+
# RUN: ld.lld a.o x.o nox.o x.o -z execstack --fatal-warnings
51+
# RUN: ld.lld -r x.o --fatal-warnings
52+
53+
#--- a.s
4354
.globl _start
4455
_start:
56+
57+
#--- x.s
58+
.section .note.GNU-stack,"x"
59+
60+
#--- nox.s
61+
.section .note.GNU-stack,""

0 commit comments

Comments
 (0)