Skip to content

Commit

Permalink
[ELF] Add .got.plt to PT_GNU_RELRO if -z now
Browse files Browse the repository at this point in the history
If external symbols are resolved eagerly on process startup, .got.plt
can be read-only and therefore can be in the PT_GNU_RELRO segment.
  • Loading branch information
rui314 committed Feb 7, 2022
1 parent a1287c2 commit 73159e2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
1 change: 1 addition & 0 deletions elf/output-chunks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ bool is_relro(Context<E> &ctx, Chunk<E> *chunk) {
if ((flags & SHF_TLS) || type == SHT_INIT_ARRAY ||
type == SHT_FINI_ARRAY || type == SHT_PREINIT_ARRAY ||
chunk == ctx.got.get() || chunk == ctx.dynamic.get() ||
(ctx.arg.z_now && chunk == ctx.gotplt.get()) ||
chunk->name.ends_with(".rel.ro"))
return true;
return false;
Expand Down
24 changes: 17 additions & 7 deletions test/elf/relro.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,25 @@ t=out/test/elf/$testname
mkdir -p $t

cat <<EOF | $CC -c -xc -o $t/a.o -
int main() {}
#include <stdio.h>
int main() {
printf("Hello world\n");
}
EOF

$CC -B. -o $t/exe $t/a.o
readelf --segments -W $t/exe > $t/log
grep -q 'GNU_RELRO ' $t/log
$CC -B. -o $t/exe1 $t/a.o -Wl,-z,relro,-z,lazy
$t/exe1 | grep -q 'Hello world'
readelf --segments -W $t/exe1 > $t/log1
grep -q 'GNU_RELRO ' $t/log1

$CC -B. -o $t/exe $t/a.o -Wl,-z,norelro
readelf --segments -W $t/exe > $t/log
! grep -q 'GNU_RELRO ' $t/log || false
$CC -B. -o $t/exe2 $t/a.o -Wl,-z,relro,-z,now
$t/exe2 | grep -q 'Hello world'
readelf --segments -W $t/exe2 > $t/log2
grep -q 'GNU_RELRO ' $t/log2

$CC -B. -o $t/exe3 $t/a.o -Wl,-z,norelro
$t/exe3 | grep -q 'Hello world'
readelf --segments -W $t/exe3 > $t/log3
! grep -q 'GNU_RELRO ' $t/log3 || false

echo OK

0 comments on commit 73159e2

Please sign in to comment.