Skip to content

Commit

Permalink
[ELF] Add -z ibt
Browse files Browse the repository at this point in the history
Fixes #229
  • Loading branch information
rui314 committed Jan 9, 2022
1 parent 17bdd95 commit 9ca6a9d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
3 changes: 3 additions & 0 deletions elf/cmdline.cc
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,9 @@ void parse_nonpositional_args(Context<E> &ctx,
ctx.arg.z_initfirst = true;
} else if (read_z_flag(args, "interpose")) {
ctx.arg.z_interpose = true;
} else if (read_z_flag(args, "ibt")) {
ctx.arg.z_ibt = true;
ctx.arg.z_ibtplt = true;
} else if (read_z_flag(args, "ibtplt")) {
ctx.arg.z_ibtplt = true;
} else if (read_z_flag(args, "muldefs")) {
Expand Down
1 change: 1 addition & 0 deletions elf/mold.h
Original file line number Diff line number Diff line change
Expand Up @@ -1270,6 +1270,7 @@ struct Context {
bool z_dlopen = true;
bool z_dump = true;
bool z_execstack = false;
bool z_ibt = false;
bool z_ibtplt = false;
bool z_initfirst = false;
bool z_interpose = false;
Expand Down
2 changes: 2 additions & 0 deletions elf/output-chunks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1768,6 +1768,8 @@ void NotePropertySection<E>::update_shdr(Context<E> &ctx) {
for (ObjectFile<E> *file : ctx.objs)
features &= file->features;

if (ctx.arg.z_ibt)
features |= GNU_PROPERTY_X86_FEATURE_1_IBT;
if (ctx.arg.z_shstk)
features |= GNU_PROPERTY_X86_FEATURE_1_SHSTK;

Expand Down
27 changes: 27 additions & 0 deletions test/elf/z-ibt.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash
export LANG=
set -e
CC="${CC:-cc}"
CXX="${CXX:-c++}"
testname=$(basename -s .sh "$0")
echo -n "Testing $testname ... "
cd "$(dirname "$0")"/../..
mold="$(pwd)/mold"
t=out/test/elf/$testname
mkdir -p $t

cat <<EOF | $CC -o $t/a.o -c -x assembler -
.globl main
main:
call _exit@PLT
EOF

$CC -B. -o $t/exe $t/a.o
readelf --notes $t/exe > $t/log
! grep -qw SHSTK $t/log

$CC -B. -o $t/exe $t/a.o -Wl,-z,ibt
readelf --notes $t/exe | grep -qw IBT
objdump -d -j .plt $t/exe | grep -q 'bnd jmp'

echo OK

0 comments on commit 9ca6a9d

Please sign in to comment.