Skip to content

Commit

Permalink
Weak undefs should not keep DSOs alive
Browse files Browse the repository at this point in the history
Fixes #1067
  • Loading branch information
rui314 committed Jul 28, 2023
1 parent 7101279 commit 1582b72
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
3 changes: 2 additions & 1 deletion elf/input-files.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1396,7 +1396,8 @@ SharedFile<E>::mark_live_objects(Context<E> &ctx,
if (sym.is_traced)
print_trace_symbol(ctx, *this, esym, sym);

if (esym.is_undef() && sym.file && !sym.file->is_alive.test_and_set()) {
if (esym.is_undef() && !esym.is_weak() && sym.file &&
!sym.file->is_alive.test_and_set()) {
feeder(sym.file);

if (sym.is_traced)
Expand Down
33 changes: 33 additions & 0 deletions test/elf/as-needed-dso2.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/bin/bash
. $(dirname $0)/common.inc

cat <<EOF | $CC -c -fPIC -o $t/a.o -xc -
int foo() {
return 0;
}
EOF

cat <<EOF | $CC -c -fPIC -o $t/b.o -xc -
__attribute__((weak)) int foo();
int bar() {
if (foo) return foo();
return 0;
}
EOF

cat <<EOF | $CC -xc -c -o $t/c.o -
int bar();
int main() {
return bar();
}
EOF

$CC -B. -shared -o $t/libfoo.so $t/a.o
$CC -B. -shared -o $t/libbar.so $t/b.o
$CC -B. -o $t/exe $t/c.o -L$t -Wl,--as-needed -lfoo -lbar

readelf --dynamic $t/exe > $t/log
! grep libfoo.so $t/log || false
grep -q libbar.so $t/log

0 comments on commit 1582b72

Please sign in to comment.