1
1
mirror of https://github.com/rui314/mold.git synced 2024-12-24 00:43:18 +03:00

Do not embed a library fullpath if it is given by -l

This commit is contained in:
Rui Ueyama 2021-06-01 15:15:25 +09:00
parent 1f3931fc9e
commit f9e91aa391
7 changed files with 46 additions and 14 deletions

View File

@ -19,7 +19,7 @@ std::string path_dirname(std::string_view path) {
// Returns the filename part of a given path.
// Returns '/' if path reperesents the root directory.
// Returns '.' if path is empty.
std::string path_filename(std::string_view path) {
std::string_view path_filename(std::string_view path) {
if (path.empty())
return ".";
@ -31,14 +31,14 @@ std::string path_filename(std::string_view path) {
i64 pos = path.find_last_of('/');
if (pos == path.npos)
return std::string(path);
return std::string(path.substr(pos + 1));
return path;
return path.substr(pos + 1);
}
// Returns the filename part of a given path without the file
// extension.
std::string path_basename(std::string_view path) {
std::string name = path_filename(path);
std::string_view path_basename(std::string_view path) {
std::string_view name = path_filename(path);
i64 pos = name.find_last_of('.');
if (pos == name.npos)

View File

@ -264,6 +264,7 @@ static void read_input_files(Context<E> &ctx, std::span<std::string_view> args)
state.pop_back();
} else if (read_arg(ctx, args, arg, "l")) {
MemoryMappedFile<E> *mb = find_library(ctx, std::string(arg));
mb->given_fullpath = false;
read_file(ctx, mb);
} else {
read_file(ctx, MemoryMappedFile<E>::must_open(ctx, std::string(args[0])));

5
mold.h
View File

@ -883,6 +883,7 @@ public:
std::string name;
i64 mtime = 0;
bool given_fullpath = true;
private:
MemoryMappedFile(std::string name, u8 *data, u64 size, u64 mtime = 0)
@ -1097,8 +1098,8 @@ protected:
// These are various utility functions to deal with file pathnames.
std::string get_current_dir();
std::string path_dirname(std::string_view path);
std::string path_filename(std::string_view path);
std::string path_basename(std::string_view path);
std::string_view path_filename(std::string_view path);
std::string_view path_basename(std::string_view path);
std::string path_to_absolute(std::string_view path);
std::string path_clean(std::string_view path);

View File

@ -1287,7 +1287,9 @@ std::string_view SharedFile<E>::get_soname(Context<E> &ctx) {
for (ElfDyn<E> &dyn : this->template get_data<ElfDyn<E>>(ctx, *sec))
if (dyn.d_tag == DT_SONAME)
return symbol_strtab.data() + dyn.d_val;
return this->name;
if (this->mb->given_fullpath)
return this->name;
return path_filename(this->name);
}
template <typename E>

31
test/dt_needed.sh Executable file
View File

@ -0,0 +1,31 @@
#!/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 | clang -c -o $t/a.o -xc -
void foo() {}
EOF
clang -fuse-ld=`pwd`/../mold -shared -o $t/libfoo.so $t/a.o -Wl,--soname,libfoo
clang -fuse-ld=`pwd`/../mold -shared -o $t/libbar.so $t/a.o
cat <<EOF | clang -c -o $t/b.o -xc -
int main() {}
EOF
clang -fuse-ld=`pwd`/../mold -o $t/exe $t/b.o $t/libfoo.so
readelf --dynamic $t/exe | fgrep -q 'Shared library: [libfoo]'
clang -fuse-ld=`pwd`/../mold -o $t/exe $t/b.o -L $t -lfoo
readelf --dynamic $t/exe | fgrep -q 'Shared library: [libfoo]'
clang -fuse-ld=`pwd`/../mold -o $t/exe $t/b.o $t/libbar.so
readelf --dynamic $t/exe | grep -Pq 'Shared library: \[.*dt_needed/libbar\.so\]'
clang -fuse-ld=`pwd`/../mold -o $t/exe $t/b.o -L$t -lbar
readelf --dynamic $t/exe | fgrep -q 'Shared library: [libbar.so]'
echo OK

View File

@ -35,7 +35,7 @@ mkdir -p $t/script
echo 'OUTPUT_FORMAT(elf32-i386)' > $t/script/libfoo.so
clang -fuse-ld=`pwd`/../mold -o $t/exe -L$t/script -L$t/lib32 -L$t/lib64 \
$t/e.o -lfoo >& $t/log
$t/e.o -lfoo -rpath $t/lib64 >& $t/log
grep -q 'script/libfoo.so: skipping incompatible file' $t/log
grep -q 'lib32/libfoo.so: skipping incompatible file' $t/log

View File

@ -6,13 +6,10 @@ t=$(pwd)/tmp/$(basename -s .sh $0)
mkdir -p $t
cat <<EOF | clang -fPIC -c -o $t/a.o -xc -
int foo() {
return 3;
}
void foo() {}
EOF
clang -fuse-ld=`pwd`/../mold -o $t/b.so -shared $t/a.o -Wl,-soname,foo
readelf --dynamic $t/b.so > $t/log
fgrep -q 'Library soname: [foo]' $t/log
readelf --dynamic $t/b.so | fgrep -q 'Library soname: [foo]'
echo OK