From 6f04ef9cf2be6946cd1ed66603285b9523ce44c0 Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Mon, 6 Jun 2022 20:22:53 +0800 Subject: [PATCH] [Mach-O] Handle $ld$install_name$ symbols --- macho/tapi.cc | 11 ++++++++ test/macho/tbd-install-name.sh | 49 ++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100755 test/macho/tbd-install-name.sh diff --git a/macho/tapi.cc b/macho/tapi.cc index ac51ba69..1735b81d 100644 --- a/macho/tapi.cc +++ b/macho/tapi.cc @@ -173,6 +173,17 @@ static void interpret_ld_symbols(Context &ctx, TextDylib &tbd) { hidden_syms.insert(m[2]); continue; } + + // $ld$install_name$os_version$name changes the install name to a + // given name. + static std::regex + install_name_re(R"(\$ld\$install_name\$os([\d.]+)\$(.+))", flags); + + if (std::regex_match(name, m, install_name_re)) { + if (ctx.arg.platform_min_version == parse_version(m[1])) + tbd.install_name = save_string(ctx, m[2]); + continue; + } } for (std::string_view s : tbd.exports) diff --git a/test/macho/tbd-install-name.sh b/test/macho/tbd-install-name.sh new file mode 100755 index 00000000..680b5452 --- /dev/null +++ b/test/macho/tbd-install-name.sh @@ -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 > $t/libfoo.tbd <<'EOF' +--- !tapi-tbd +tbd-version: 4 +targets: [ x86_64-macos, arm64-macos ] +uuids: + - target: x86_64-macos + value: 00000000-0000-0000-0000-000000000000 + - target: arm64-macos + value: 00000000-0000-0000-0000-000000000000 +install-name: '/foo' +current-version: 0 +compatibility-version: 0 +exports: + - targets: [ x86_64-macos, arm64-macos ] + symbols: [ '$ld$install_name$os25.0$/bar', _foo ] +... +EOF + +cat <& /dev/null + +otool -L $t/exe1 | grep -q /foo + +clang --ld-path=./ld64 -o $t/exe2 $t/libfoo.tbd $t/a.o \ + -Wl,-platform_version,macos,25.0,21.0 >& /dev/null + +otool -L $t/exe2 | grep -q /bar + +echo OK