1
1
mirror of https://github.com/rui314/mold.git synced 2024-10-04 16:48:04 +03:00

[ELF] Skip incompatible files specified by linker script GROUP command

Fixes https://github.com/rui314/mold/issues/260
This commit is contained in:
Rui Ueyama 2022-01-13 10:42:10 +09:00
parent 299078aefe
commit 9d27ee0839
4 changed files with 55 additions and 3 deletions

View File

@ -172,12 +172,12 @@ static MappedFile<Context<E>> *resolve_path(Context<E> &ctx, std::string_view to
if (str.starts_with("-l"))
return find_library(ctx, str.substr(2));
if (MappedFile<Context<E>> *mf = MappedFile<Context<E>>::open(ctx, str))
if (MappedFile<Context<E>> *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<Context<E>> *mf = MappedFile<Context<E>>::open(ctx, path))
if (MappedFile<Context<E>> *mf = open_library(ctx, path))
return mf;
}

View File

@ -159,7 +159,7 @@ deduce_machine_type(Context<E> &ctx, std::span<std::string_view> args) {
}
template <typename E>
static MappedFile<Context<E>> *open_library(Context<E> &ctx, std::string path) {
MappedFile<Context<E>> *open_library(Context<E> &ctx, std::string path) {
MappedFile<Context<E>> *mf = MappedFile<Context<E>>::open(ctx, path);
if (!mf)
return nullptr;

View File

@ -1438,6 +1438,9 @@ struct Context {
Symbol<E> *etext = nullptr;
};
template <typename E>
MappedFile<Context<E>> *open_library(Context<E> &ctx, std::string path);
template <typename E>
MappedFile<Context<E>> *find_library(Context<E> &ctx, std::string path);

49
test/elf/incompatible-libs2.sh Executable file
View File

@ -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 <<EOF | $CC -m32 -c -o $t/a.o -xc -
char hello[] = "Hello world";
EOF
mkdir -p $t/lib32
$CC -m32 -shared -o $t/lib32/libfoo.so $t/a.o
cat <<EOF | $CC -c -o $t/d.o -xc -
char hello[] = "Hello world";
EOF
mkdir -p $t/lib64
$CC -shared -o $t/lib64/libfoo.so $t/d.o
cat <<EOF | $CC -c -o $t/e.o -xc -
#include <stdio.h>
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