1
1
mirror of https://github.com/rui314/mold.git synced 2024-09-22 02:20:51 +03:00

DSOs referenced by used DSOs are considered used

This is needed by Gentoo's sys-cluster/ampi-0_pre20140616 package.
This commit is contained in:
Rui Ueyama 2021-06-21 13:17:52 +09:00
parent de668b125a
commit 81dbbdda62
3 changed files with 60 additions and 9 deletions

View File

@ -98,12 +98,12 @@ void resolve_symbols(Context<E> &ctx) {
for (std::string_view name : ctx.arg.undefined)
if (InputFile<E> *file = Symbol<E>::intern(ctx, name)->file)
if (!file->is_alive.exchange(true))
if (!file->is_dso)
roots.push_back((ObjectFile<E> *)file);
if (!file->is_alive.exchange(true) && !file->is_dso)
roots.push_back((ObjectFile<E> *)file);
tbb::parallel_do(roots, [&](ObjectFile<E> *file,
tbb::parallel_do_feeder<ObjectFile<E> *> &feeder) {
tbb::parallel_do(roots,
[&](ObjectFile<E> *file,
tbb::parallel_do_feeder<ObjectFile<E> *> &feeder) {
file->mark_live_objects(ctx, [&](ObjectFile<E> *obj) {
feeder.add(obj);
});
@ -130,15 +130,29 @@ void resolve_symbols(Context<E> &ctx) {
for (i64 i = file->first_global; i < file->elf_syms.size(); i++) {
const ElfSym<E> &esym = file->elf_syms[i];
Symbol<E> &sym = *file->symbols[i];
if (esym.is_undef() && esym.st_bind != STB_WEAK &&
sym.file && sym.file->is_dso) {
sym.file->is_alive = true;
if (esym.is_undef_strong() && sym.file && sym.file->is_dso) {
std::lock_guard lock(sym.mu);
sym.file->is_alive = true;
sym.is_weak = false;
}
}
});
// DSOs referenced by live DSOs are also alive.
std::vector<SharedFile<E> *> live_dsos;
for (SharedFile<E> *file : ctx.dsos)
if (file->is_alive)
live_dsos.push_back(file);
tbb::parallel_do(live_dsos,
[&](SharedFile<E> *file,
tbb::parallel_do_feeder<SharedFile<E> *> &feeder) {
for (Symbol<E> *sym : file->globals)
if (sym->file && sym->file != file && sym->file->is_dso &&
!sym->file->is_alive.exchange(true))
feeder.add(file);
});
// Remove symbols of unreferenced DSOs.
tbb::parallel_for_each(ctx.dsos, [](SharedFile<E> *file) {
if (!file->is_alive)

37
test/as-needed2.sh Executable file
View File

@ -0,0 +1,37 @@
#!/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 | cc -shared -fPIC -o $t/a.so -xc -
int foo() { return 3; }
EOF
cat <<EOF | cc -shared -fPIC -o $t/b.so -xc -
int bar() { return 3; }
EOF
cat <<EOF | cc -shared -fPIC -o $t/c.so -xc -
int foo();
int baz() { return foo(); }
EOF
cat <<EOF | cc -c -o $t/d.o -xc -
#include <stdio.h>
int baz();
int main() {
printf("%d\n", baz());
}
EOF
clang -fuse-ld=`pwd`/../mold -o $t/exe $t/d.o -Wl,--as-needed \
$t/c.so $t/b.so $t/a.so
readelf --dynamic $t/exe > $t/log
grep -q /a.so $t/log
grep -q /c.so $t/log
! grep -q /b.so $t/log || false
echo OK

View File

@ -9,7 +9,7 @@ echo '.globl main; main:' | cc -o $t/a.o -c -x assembler -
clang -fuse-ld=`pwd`/../mold -o $t/exe $t/a.o
readelf --wide --sections $t/exe > $t/log
grep -Pq '\.dynamic.*\b0001a0\b' $t/log
grep -Pq '\.dynamic.*\b0001b0\b' $t/log
clang -fuse-ld=`pwd`/../mold -o $t/exe $t/a.o -Wl,-spare-dynamic-tags=100
readelf --wide --sections $t/exe > $t/log