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

[Mach-O] Add -search_paths_first and -search_dylibs_first

This commit is contained in:
Rui Ueyama 2022-05-12 16:42:46 +08:00
parent 5cf365b4f5
commit 4bc287ff48
4 changed files with 74 additions and 10 deletions

View File

@ -54,6 +54,8 @@ Options:
Set platform, platform version and SDK version
-rpath <PATH> Add PATH to the runpath search path list
-syslibroot <DIR> Prepend DIR to library search paths
-search_paths_first
-search_dylibs_first
-t Print out each file the linker loads
-v Report version information)";
@ -265,11 +267,12 @@ std::vector<std::string> parse_nonpositional_args(Context<E> &ctx) {
ctx.arg.platform_sdk_version = parse_version(ctx, arg3);
} else if (read_arg("-rpath")) {
ctx.arg.rpath.push_back(std::string(arg));
} else if (read_flag("-search_dylibs_first")) {
Fatal(ctx) << "-search_dylibs_first is not supported";
} else if (read_flag("-search_paths_first")) {
} else if (read_arg("-syslibroot")) {
ctx.arg.syslibroot.push_back(std::string(arg));
} else if (read_flag("-search_paths_first")) {
ctx.arg.search_paths_first = true;
} else if (read_flag("-search_dylibs_first")) {
ctx.arg.search_paths_first = false;
} else if (read_flag("-t")) {
ctx.arg.trace = true;
} else if (read_flag("-v")) {

View File

@ -269,14 +269,25 @@ MappedFile<Context<E>> *find_framework(Context<E> &ctx, std::string name) {
template <typename E>
MappedFile<Context<E>> *find_library(Context<E> &ctx, std::string name) {
auto search = [&](std::vector<std::string> extn) -> MappedFile<Context<E>> * {
for (std::string dir : ctx.arg.library_paths) {
for (std::string ext : {".tbd", ".dylib", ".a"}) {
std::string path = dir + "/lib" + name + ext;
for (std::string e : extn) {
std::string path = dir + "/lib" + name + e;
if (MappedFile<Context<E>> *mf = MappedFile<Context<E>>::open(ctx, path))
return mf;
}
}
return nullptr;
};
// -search_paths_first
if (ctx.arg.search_paths_first)
return search({".tbd", ".dylib", ".a"});
// -search_dylibs_first
if (MappedFile<Context<E>> *mf = search({".tbd", ".dylib"}))
return mf;
return search({".a"});
}
template <typename E>

View File

@ -800,6 +800,7 @@ struct Context {
bool dynamic = true;
bool fatal_warnings = false;
bool noinhibit_exec = false;
bool search_paths_first = true;
bool trace = false;
i64 arch = CPU_TYPE_ARM64;
i64 headerpad = 256;

View File

@ -0,0 +1,49 @@
#!/bin/bash
export LC_ALL=C
set -e
CC="${TEST_CC:-cc}"
CXX="${TEST_CXX:-c++}"
GCC="${TEST_GCC:-gcc}"
GXX="${TEST_GXX:-g++}"
OBJDUMP="${OBJDUMP:-objdump}"
MACHINE="${MACHINE:-$(uname -m)}"
testname=$(basename "$0" .sh)
echo -n "Testing $testname ... "
cd "$(dirname "$0")"/../..
t=out/test/macho/$testname
mkdir -p $t
cat <<EOF | $CC -o $t/a.o -c -xc -
#include <stdio.h>
void say() {
printf("Hello\n");
}
EOF
cat <<EOF | $CC -o $t/b.o -c -xc -
#include <stdio.h>
void say() {
printf("Howdy\n");
}
EOF
cat <<EOF | $CC -o $t/c.o -c -xc -
void say();
int main() {
say();
}
EOF
mkdir -p $t/x $t/y
ar rcs $t/x/libfoo.a $t/a.o
$CC -shared -o $t/y/libfoo.dylib $t/b.o
clang --ld-path=./ld64 -o $t/exe $t/c.o -Wl,-L$t/x -Wl,-L$t/y -lfoo
$t/exe | grep -q Hello
clang --ld-path=./ld64 -o $t/exe $t/c.o -Wl,-L$t/x -Wl,-L$t/y -lfoo \
-Wl,-search_dylibs_first
$t/exe | grep -q Howdy
echo OK