I’ve double-checked that when building RISC-V binaries using g++-8-riscv64-linux-gnu
on Focal (20.04), the stack isn’t being marked as executable. I did the following inside a new Ubuntu Focal Docker container:
0# binutils for readelf
1apt update && apt install g++-8-riscv64-linux-gnu binutils-riscv64-linux-gnu binutils
2
3# riscv64-linux-gnu-gcc-8 --version
4riscv64-linux-gnu-gcc-8 (Ubuntu 8.4.0-3ubuntu1) 8.4.0
5
6# compile minimal binary
7echo "int main() { return 0; }" > test.c
8riscv64-linux-gnu-gcc-8 test.c -o test
9
10# check stack
11readelf -lW test|grep GNU_STACK
12 GNU_STACK 0x000000 0x0000000000000000 0x0000000000000000 0x000000 0x000000 RW 0x10
13
14
15# recompile, tell linker to mark stack as executable
16riscv64-linux-gnu-gcc-8 test.c -o test -Wl,-z,execstack
17
18# recheck stack
19readelf -lW test|grep GNU_STACK
20 GNU_STACK 0x000000 0x0000000000000000 0x0000000000000000 0x000000 0x000000 RWE 0x10
If you perform the same inside a Ubuntu Bionic (18.04) container:
0# binutils for readelf
1apt update && apt install g++-8-riscv64-linux-gnu binutils-riscv64-linux-gnu binutils
2
3# riscv64-linux-gnu-gcc-8 --version
4riscv64-linux-gnu-gcc-8 (Ubuntu 8.4.0-1ubuntu1~18.04) 8.4.0
5
6# compile minimal binary
7echo "int main() { return 0; }" > test.c
8riscv64-linux-gnu-gcc-8 test.c -o test
9
10# check stack
11readelf -lW test|grep GNU_STACK
12< no output >
13
14# recompile, tell linker to not mark stack as executable
15riscv64-linux-gnu-gcc-8 test.c -o test -Wl,-z,noexecstack
16
17# recheck stack
18readelf -lW test|grep GNU_STACK
19 GNU_STACK 0x000000 0x0000000000000000 0x0000000000000000 0x000000 0x000000 RW 0x10
So it’s less that the stack is being marked as executable when building on Bionic, and more that the GNU_STACK
section is just missing entirely, unless you explicitly tell the linker you want either a executable or non-executable stack. This makes sense when combined with the change linked by @dongcarl above (more context here), as the GNU_STACK
section only started being added by default in GCC 9+. However it doesn’t explain why the section is being added when using GCC 8 on Focal (and no additional linker flags). My assumption was that the change had been backported / applied by Debian/Ubuntu, however I can’t seem to find evidence of that so far.