From 9d27ee08390c11ba7b6bf13b3149af003b560230 Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Thu, 13 Jan 2022 10:42:10 +0900 Subject: [PATCH] [ELF] Skip incompatible files specified by linker script GROUP command Fixes https://github.com/rui314/mold/issues/260 --- elf/linker-script.cc | 4 +-- elf/main.cc | 2 +- elf/mold.h | 3 +++ test/elf/incompatible-libs2.sh | 49 ++++++++++++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 3 deletions(-) create mode 100755 test/elf/incompatible-libs2.sh diff --git a/elf/linker-script.cc b/elf/linker-script.cc index d0a5f65b..4adcf9e2 100644 --- a/elf/linker-script.cc +++ b/elf/linker-script.cc @@ -172,12 +172,12 @@ static MappedFile> *resolve_path(Context &ctx, std::string_view to if (str.starts_with("-l")) return find_library(ctx, str.substr(2)); - if (MappedFile> *mf = MappedFile>::open(ctx, str)) + if (MappedFile> *mf = open_library(ctx, str)) return mf; for (std::string_view dir : ctx.arg.library_paths) { std::string path = std::string(dir) + "/" + str; - if (MappedFile> *mf = MappedFile>::open(ctx, path)) + if (MappedFile> *mf = open_library(ctx, path)) return mf; } diff --git a/elf/main.cc b/elf/main.cc index ee23d591..a6e73300 100644 --- a/elf/main.cc +++ b/elf/main.cc @@ -159,7 +159,7 @@ deduce_machine_type(Context &ctx, std::span args) { } template -static MappedFile> *open_library(Context &ctx, std::string path) { +MappedFile> *open_library(Context &ctx, std::string path) { MappedFile> *mf = MappedFile>::open(ctx, path); if (!mf) return nullptr; diff --git a/elf/mold.h b/elf/mold.h index 2254d83f..c3367a80 100644 --- a/elf/mold.h +++ b/elf/mold.h @@ -1438,6 +1438,9 @@ struct Context { Symbol *etext = nullptr; }; +template +MappedFile> *open_library(Context &ctx, std::string path); + template MappedFile> *find_library(Context &ctx, std::string path); diff --git a/test/elf/incompatible-libs2.sh b/test/elf/incompatible-libs2.sh new file mode 100755 index 00000000..9a873c56 --- /dev/null +++ b/test/elf/incompatible-libs2.sh @@ -0,0 +1,49 @@ +#!/bin/bash +export LANG= +set -e +CC="${CC:-cc}" +CXX="${CXX:-c++}" +testname=$(basename -s .sh "$0") +echo -n "Testing $testname ... " +cd "$(dirname "$0")"/../.. +mold="$(pwd)/mold" +t=out/test/elf/$testname +mkdir -p $t + +echo 'int main() {}' | $CC -m32 -o $t/exe -xc - >& /dev/null \ + || { echo skipped; exit; } + +cat < + +extern char hello[]; + +int main() { + printf("%s\n", hello); +} +EOF + +mkdir -p $t/script +echo 'GROUP(libfoo.so)' > $t/script/libfoo.so + +$CC -B. -o $t/exe -L$t/lib32 -L$t/lib64 -lfoo $t/e.o -Wl,-rpath $t/lib64 \ + >& $t/log + +grep -q 'lib32/libfoo.so: skipping incompatible file' $t/log +$t/exe | grep -q 'Hello world' + +echo OK