[lld] [ldd] Do not implicitly link non "public" libraries (PR #97639)
Daniel RodrÃguez Troitiño via llvm-commits
llvm-commits at lists.llvm.org
Wed Jul 3 14:17:55 PDT 2024
https://github.com/drodriguez created https://github.com/llvm/llvm-project/pull/97639
The LC_SUB_CLIENT Mach-O command and the `allowable-clients` TBD entry specify that the given framework (or library?) can only be linked directly from the specified names, even if it is sitting in `/usr/lib` or `/System/Library/Frameworks`.
Add a check for those conditions before checking if a library should be implicitly linked, and link against their umbrella if they have allowable clients. The code needs to be in both the binary libraries and the interface libraries.
Add a test that reproduces the scenario in which a framework reexports a private framework that sits in `/System/Library/Frameworks`, and check for the symbols of the reexported framework to be associated with the public framework, and not the private one.
>From 460b85663a6b94b0f31b60ca9032fb92f43ca0a5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Daniel=20Rodr=C3=ADguez=20Troiti=C3=B1o?=
<danielrodriguez at meta.com>
Date: Mon, 24 Jun 2024 17:53:12 -0700
Subject: [PATCH] [ldd] Do not implicitly link non "public" libraries
The LC_SUB_CLIENT Mach-O command and the `allowable-clients` TBD entry
specify that the given framework (or library?) can only be linked
directly from the specified names, even if it is sitting in `/usr/lib` or
`/System/Library/Frameworks`.
Add a check for those conditions before checking if a library should be
implicitly linked, and link against their umbrella if they have
allowable clients. The code needs to be in both the binary libraries and
the interface libraries.
Add a test that reproduces the scenario in which a framework reexports
a private framework that sits in `/System/Library/Frameworks`, and check
for the symbols of the reexported framework to be associated with the
public framework, and not the private one.
---
lld/MachO/InputFiles.cpp | 10 +++-
.../MachO/implicit-and-allowable-clients.test | 49 +++++++++++++++++++
2 files changed, 57 insertions(+), 2 deletions(-)
create mode 100644 lld/test/MachO/implicit-and-allowable-clients.test
diff --git a/lld/MachO/InputFiles.cpp b/lld/MachO/InputFiles.cpp
index 0cee1a84d0b55..b40a812f30bd3 100644
--- a/lld/MachO/InputFiles.cpp
+++ b/lld/MachO/InputFiles.cpp
@@ -1725,7 +1725,10 @@ DylibFile::DylibFile(MemoryBufferRef mb, DylibFile *umbrella,
}
// Initialize symbols.
- exportingFile = isImplicitlyLinked(installName) ? this : this->umbrella;
+ bool canBeImplicitlyLinked = findCommand(hdr, LC_SUB_CLIENT) == nullptr;
+ exportingFile = (canBeImplicitlyLinked && isImplicitlyLinked(installName))
+ ? this
+ : this->umbrella;
const auto *dyldInfo = findCommand<dyld_info_command>(hdr, LC_DYLD_INFO_ONLY);
const auto *exportsTrie =
@@ -1884,7 +1887,10 @@ DylibFile::DylibFile(const InterfaceFile &interface, DylibFile *umbrella,
checkAppExtensionSafety(interface.isApplicationExtensionSafe());
- exportingFile = isImplicitlyLinked(installName) ? this : umbrella;
+ bool canBeImplicitlyLinked = interface.allowableClients().size() == 0;
+ exportingFile = (canBeImplicitlyLinked && isImplicitlyLinked(installName))
+ ? this
+ : umbrella;
auto addSymbol = [&](const llvm::MachO::Symbol &symbol,
const Twine &name) -> void {
StringRef savedName = saver().save(name);
diff --git a/lld/test/MachO/implicit-and-allowable-clients.test b/lld/test/MachO/implicit-and-allowable-clients.test
new file mode 100644
index 0000000000000..97123ab9c5dce
--- /dev/null
+++ b/lld/test/MachO/implicit-and-allowable-clients.test
@@ -0,0 +1,49 @@
+# REQUIRES: arm
+# RUN: rm -rf %t; split-file %s %t
+# RUN: ln -s Versions/A/Framework1.tbd %t/System/Library/Frameworks/Framework1.framework/
+# RUN: ln -s Versions/A/Framework11.tbd %t/System/Library/Frameworks/Framework11.framework/
+# RUN: llvm-mc -filetype obj -triple arm64-apple-macos11.0 %t/test.s -o %t/test.o
+# RUN: %lld -arch arm64 -platform_version macos 11.0 11.0 -o %t/test -syslibroot %t -framework Framework1 %t/test.o
+
+# RUN: llvm-objdump --bind --no-show-raw-insn -d %t/test | FileCheck %s
+# CHECK: Bind table:
+# CHECK-DAG: __DATA __data {{.*}} pointer 0 Framework1 _func1
+# CHECK-DAG: __DATA __data {{.*}} pointer 0 Framework1 _func11
+
+#--- System/Library/Frameworks/Framework1.framework/Versions/A/Framework1.tbd
+--- !tapi-tbd
+tbd-version: 4
+targets: [ arm64-macos ]
+install-name: '/System/Library/Frameworks/Framework1.framework/Versions/A/Framework1'
+current-version: 1.0.0
+reexported-libraries:
+ - targets: [ arm64-macos ]
+ libraries: [ '/System/Library/Frameworks/Framework11.framework/Versions/A/Framework11' ]
+exports:
+ - targets: [ arm64-macos ]
+ symbols: [ '_func1' ]
+...
+#--- System/Library/Frameworks/Framework11.framework/Versions/A/Framework11.tbd
+--- !tapi-tbd
+tbd-version: 4
+targets: [ arm64-macos ]
+install-name: '/System/Library/Frameworks/Framework11.framework/Versions/A/Framework11'
+current-version: 1.0.0
+allowable-clients:
+ - targets: [ arm64-macos ]
+ clients: [ Framework1 ]
+exports:
+ - targets: [ arm64-macos ]
+ symbols: [ '_func11' ]
+...
+#--- test.s
+.text
+.globl _main
+
+_main:
+ ret
+
+.data
+ .quad _func1
+ .quad _func11
+
More information about the llvm-commits
mailing list