diff --git a/elf/lto.cc b/elf/lto.cc index ee088bb2..56d56cb1 100644 --- a/elf/lto.cc +++ b/elf/lto.cc @@ -486,6 +486,7 @@ ObjectFile *read_lto_object(Context &ctx, MappedFile> *mf) { // Create mold's object instance ObjectFile *obj = new ObjectFile; + obj->filename = mf->name; obj->symbols.push_back(new Symbol); obj->first_global = 1; obj->is_lto_obj = true; @@ -563,6 +564,12 @@ void do_lto(Context &ctx) { file->is_alive = false; std::erase_if(ctx.objs, [](ObjectFile *file) { return file->is_lto_obj; }); + + // Re-compute symbol versions and import/export information + // because `resolve_symbols` may have overwrite them. + apply_version_script(ctx); + parse_symbol_version(ctx); + compute_import_export(ctx); } template diff --git a/elf/main.cc b/elf/main.cc index 9b80cd49..f57b25a4 100644 --- a/elf/main.cc +++ b/elf/main.cc @@ -469,6 +469,15 @@ static int elf_main(int argc, char **argv) { // included to the final output. resolve_symbols(ctx); + // Apply version scripts. + apply_version_script(ctx); + + // Parse symbol version suffixes (e.g. "foo@ver1"). + parse_symbol_version(ctx); + + // Set is_import and is_export bits for each symbol. + compute_import_export(ctx); + // Do LTO if (ctx.has_lto_object) do_lto(ctx); @@ -482,15 +491,6 @@ static int elf_main(int argc, char **argv) { // Create .bss sections for common symbols. convert_common_symbols(ctx); - // Apply version scripts. - apply_version_script(ctx); - - // Parse symbol version suffixes (e.g. "foo@ver1"). - parse_symbol_version(ctx); - - // Set is_import and is_export bits for each symbol. - compute_import_export(ctx); - // Garbage-collect unreachable sections. if (ctx.arg.gc_sections) gc_sections(ctx); diff --git a/test/elf/lto-dso.sh b/test/elf/lto-dso.sh new file mode 100755 index 00000000..af08b1d1 --- /dev/null +++ b/test/elf/lto-dso.sh @@ -0,0 +1,20 @@ +#!/bin/bash +export LANG= +set -e +CC="${CC:-cc}" +CXX="${CXX:-c++}" +testname=$(basename "$0" .sh) +echo -n "Testing $testname ... " +cd "$(dirname "$0")"/../.. +mold="$(pwd)/mold" +t=out/test/elf/$testname +mkdir -p $t + +cat < $t/b.script +{ + global: foo; + local: *; +}; +EOF + +$CC -B. -shared -o $t/c.so -flto $t/a.o -Wl,-version-script=$t/b.script +nm -D $t/c.so | grep -q 'T foo' +! nm -D $t/c.so | grep -q 'T bar' || false + +echo OK