From ccd27807aa87c739fcbb053b191a241eaa2c1e34 Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Mon, 10 May 2021 14:05:21 +0900 Subject: [PATCH] Do not add unreferenced DSO symbols to .dynsym --- passes.cc | 6 ++---- test/dt_init.sh | 16 ++++++++-------- test/shared.sh | 7 +++++-- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/passes.cc b/passes.cc index 7b84b8ae..00b11e39 100644 --- a/passes.cc +++ b/passes.cc @@ -417,11 +417,9 @@ void scan_rels(Context &ctx) { tbb::parallel_for((i64)0, (i64)files.size(), [&](i64 i) { for (Symbol *sym : files[i]->symbols) { - if (sym->file != files[i]) - continue; - if (sym->is_imported || sym->is_exported) + if (!files[i]->is_dso && (sym->is_imported || sym->is_exported)) sym->flags |= NEEDS_DYNSYM; - if (sym->flags) + if (sym->file == files[i] && sym->flags) vec[i].push_back(sym); } }); diff --git a/test/dt_init.sh b/test/dt_init.sh index 0a2c7f06..89dfff4c 100755 --- a/test/dt_init.sh +++ b/test/dt_init.sh @@ -18,17 +18,17 @@ EOF clang -fuse-ld=`pwd`/../mold -o $t/exe $t/a.o readelf -a $t/exe > $t/log -grep -Pq '\(INIT\)\s+0x233010' $t/log -grep -Pq '\(FINI\)\s+0x233000' $t/log -grep -Pq '0000000000233010\s+0 FUNC GLOBAL HIDDEN \d+ _init$' $t/log -grep -Pq '0000000000233000\s+0 FUNC GLOBAL HIDDEN \d+ _fini$' $t/log +grep -Pq '\(INIT\)\s+0x201010' $t/log +grep -Pq '\(FINI\)\s+0x201000' $t/log +grep -Pq '0000000000201010\s+0 FUNC GLOBAL HIDDEN \d+ _init$' $t/log +grep -Pq '0000000000201000\s+0 FUNC GLOBAL HIDDEN \d+ _fini$' $t/log clang -fuse-ld=`pwd`/../mold -o $t/exe $t/a.o -Wl,-init,init -Wl,-fini,fini readelf -a $t/exe > $t/log -grep -Pq '\(INIT\)\s+0x233119' $t/log -grep -Pq '\(FINI\)\s+0x23311a' $t/log -grep -Pq '0000000000233119\s+0 NOTYPE GLOBAL DEFAULT \d+ init$' $t/log -grep -Pq '000000000023311a\s+0 NOTYPE GLOBAL DEFAULT \d+ fini$' $t/log +grep -Pq '\(INIT\)\s+0x201119' $t/log +grep -Pq '\(FINI\)\s+0x20111a' $t/log +grep -Pq '0000000000201119\s+0 NOTYPE GLOBAL DEFAULT \d+ init$' $t/log +grep -Pq '000000000020111a\s+0 NOTYPE GLOBAL DEFAULT \d+ fini$' $t/log echo OK diff --git a/test/shared.sh b/test/shared.sh index 7ab83b27..b0fda6f9 100755 --- a/test/shared.sh +++ b/test/shared.sh @@ -6,10 +6,12 @@ t=$(pwd)/tmp/$(basename -s .sh $0) mkdir -p $t cat < $t/log grep -q '0000000000000000 0 NOTYPE GLOBAL DEFAULT UND fn2' $t/log -grep -q '000000000003311c 0 NOTYPE GLOBAL DEFAULT 15 fn1' $t/log +grep -q '000000000000111c 0 NOTYPE GLOBAL DEFAULT 15 fn1' $t/log cat < @@ -36,5 +38,6 @@ EOF clang -fuse-ld=`pwd`/../mold -o $t/exe $t/c.o $t/b.so $t/exe | grep -q hello +! readelf --symbols $t/exe | grep -q fn3 || false echo OK