1
1
mirror of https://github.com/rui314/mold.git synced 2024-12-24 00:43:18 +03:00

Add -z execstack and -z noexecstack

This commit is contained in:
Rui Ueyama 2021-03-12 18:09:15 +09:00
parent fefa083188
commit e8e14e4e0d
4 changed files with 35 additions and 3 deletions

View File

@ -277,6 +277,10 @@ void parse_nonpositional_args(std::span<std::string_view> args,
config.print_stats = true;
} else if (read_z_flag(args, "now")) {
config.z_now = true;
} else if (read_z_flag(args, "execstack")) {
config.z_execstack = true;
} else if (read_z_flag(args, "noexecstack")) {
config.z_execstack = false;
} else if (read_flag(args, "fork")) {
config.fork = true;
} else if (read_flag(args, "no-fork")) {

1
mold.h
View File

@ -86,6 +86,7 @@ struct Config {
bool shared = false;
bool strip_all = false;
bool trace = false;
bool z_execstack = false;
bool z_now = false;
i16 default_version = VER_NDX_GLOBAL;
std::vector<std::string_view> version_definitions;

View File

@ -151,11 +151,13 @@ std::vector<ElfPhdr> create_phdr() {
define(PT_GNU_EH_FRAME, PF_R, 1, out::eh_frame_hdr);
// Add PT_GNU_STACK, which is a marker segment that doesn't really
// contain any segments. If exists, the runtime turn on the No Exeecute
// bit for stack pages.
// contain any segments. It controls executable bit of stack area.
vec.push_back({});
vec.back().p_type = PT_GNU_STACK;
vec.back().p_flags = PF_R | PF_W;
if (config.z_execstack)
vec.back().p_flags = PF_R | PF_W | PF_X;
else
vec.back().p_flags = PF_R | PF_W;
return vec;
}

25
test/execstack.sh Executable file
View File

@ -0,0 +1,25 @@
#!/bin/bash
set -e
cd $(dirname $0)
echo -n "Testing $(basename -s .sh $0) ... "
t=$(pwd)/tmp/$(basename -s .sh $0)
mkdir -p $t
cat <<EOF | clang -c -xc -o $t/a.o -
int main() {}
EOF
clang -fuse-ld=`pwd`/../mold -o $t/exe $t/a.o -Wl,-z,execstack
readelf --segments -W $t/exe > $t/log
grep -q 'GNU_STACK.* RWE ' $t/log
clang -fuse-ld=`pwd`/../mold -o $t/exe $t/a.o -Wl,-z,execstack \
-Wl,-z,noexecstack
readelf --segments -W $t/exe > $t/log
grep -q 'GNU_STACK.* RW ' $t/log
clang -fuse-ld=`pwd`/../mold -o $t/exe $t/a.o
readelf --segments -W $t/exe > $t/log
grep -q 'GNU_STACK.* RW ' $t/log
echo OK