[lld] 895a721 - [lld/mac] Support writing zippered dylibs and bundles

Nico Weber via llvm-commits llvm-commits at lists.llvm.org
Wed May 4 16:23:46 PDT 2022


Author: Nico Weber
Date: 2022-05-04T19:23:35-04:00
New Revision: 895a72111b0f1b6b29683d254dae4ebda9081eb4

URL: https://github.com/llvm/llvm-project/commit/895a72111b0f1b6b29683d254dae4ebda9081eb4
DIFF: https://github.com/llvm/llvm-project/commit/895a72111b0f1b6b29683d254dae4ebda9081eb4.diff

LOG: [lld/mac] Support writing zippered dylibs and bundles

With -platform_version flags for two distinct platforms,
this writes a LC_BUILD_VERSION header for each.

The motivation is that this is needed for self-hosting with lld as linker
after D124059.

To create a zippered output at the clang driver level, pass

    -target arm64-apple-macos -darwin-target-variant arm64-apple-ios-macabi

to create a zippered dylib.

(In Xcode's clang, `-darwin-target-variant` is spelled just `-target-variant`.)

(If you pass `-target arm64-apple-ios-macabi -target-variant arm64-apple-macos`
instead, ld64 crashes!)

This results in two -platform_version flags being passed to the linker.

ld64 also verifies that the iOS SDK version is at least 13.1. We don't do that
yet. But ld64 also does that for other platforms and we don't. So we need to
do that at some point, but not in this patch.

Only dylib and bundle outputs can be zippered.

I verified that a Catalyst app linked against a dylib created with

    clang -shared foo.cc -o libfoo.dylib \
          -target arm64-apple-macos \
          -target-variant arm64-apple-ios-macabi \
          -Wl,-install_name, at rpath/libfoo.dylib \
          -fuse-ld=$PWD/out/gn/bin/ld64.lld

runs successfully. (The app calls a function `f()` in libfoo.dylib
that returns a const char* "foo", and NSLog(@"%s")s it.)

ld64 is a bit more permissive when writing zippered outputs,
see references to "unzippered twins". That's not implemented yet.
(If anybody wants to implement that, D124275 is a good start.)

Differential Revision: https://reviews.llvm.org/D124887

Added: 
    

Modified: 
    lld/MachO/Config.h
    lld/MachO/Driver.cpp
    lld/MachO/Writer.cpp
    lld/test/MachO/platform-version.s
    lld/test/MachO/zippered.yaml

Removed: 
    


################################################################################
diff  --git a/lld/MachO/Config.h b/lld/MachO/Config.h
index 823b215a3b0eb..46b3dca0c03dd 100644
--- a/lld/MachO/Config.h
+++ b/lld/MachO/Config.h
@@ -151,6 +151,7 @@ struct Configuration {
   bool deadStrip = false;
   bool errorForArchMismatch = false;
   PlatformInfo platformInfo;
+  llvm::Optional<PlatformInfo> secondaryPlatformInfo;
   NamespaceKind namespaceKind = NamespaceKind::twolevel;
   UndefinedSymbolTreatment undefinedSymbolTreatment =
       UndefinedSymbolTreatment::error;

diff  --git a/lld/MachO/Driver.cpp b/lld/MachO/Driver.cpp
index 15daac5e55128..28a31d6dc53f4 100644
--- a/lld/MachO/Driver.cpp
+++ b/lld/MachO/Driver.cpp
@@ -642,12 +642,27 @@ static PlatformType parsePlatformVersions(const ArgList &args) {
     return PLATFORM_UNKNOWN;
   }
   if (platformVersions.size() == 2) {
-    // FIXME: If you implement support for this, add a diagnostic if
-    // outputType is not dylib or bundle -- linkers shouldn't be able to
-    // write zippered executables.
-    warn("writing zippered outputs not yet implemented, "
-         "ignoring all but last -platform_version flag");
+    bool isZipperedCatalyst = platformVersions.count(PLATFORM_MACOS) &&
+                              platformVersions.count(PLATFORM_MACCATALYST);
+
+    if (!isZipperedCatalyst) {
+      error("lld supports writing zippered outputs only for "
+            "macos and mac-catalyst");
+    } else if (config->outputType != MH_DYLIB &&
+               config->outputType != MH_BUNDLE) {
+      error("writing zippered outputs only valid for -dylib and -bundle");
+    } else {
+      config->platformInfo.minimum = platformVersions[PLATFORM_MACOS].minimum;
+      config->platformInfo.sdk = platformVersions[PLATFORM_MACOS].sdk;
+      config->secondaryPlatformInfo = PlatformInfo{};
+      config->secondaryPlatformInfo->minimum =
+          platformVersions[PLATFORM_MACCATALYST].minimum;
+      config->secondaryPlatformInfo->sdk =
+          platformVersions[PLATFORM_MACCATALYST].sdk;
+    }
+    return PLATFORM_MACOS;
   }
+
   config->platformInfo.minimum = lastVersionInfo->minimum;
   config->platformInfo.sdk = lastVersionInfo->sdk;
   return lastVersionInfo->platform;
@@ -664,6 +679,10 @@ static TargetInfo *createTargetInfo(InputArgList &args) {
   PlatformType platform = parsePlatformVersions(args);
   config->platformInfo.target =
       MachO::Target(getArchitectureFromName(archName), platform);
+  if (config->secondaryPlatformInfo) {
+    config->secondaryPlatformInfo->target =
+        MachO::Target(getArchitectureFromName(archName), PLATFORM_MACCATALYST);
+  }
 
   uint32_t cpuType;
   uint32_t cpuSubtype;
@@ -1128,6 +1147,7 @@ bool macho::link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
 
   config = std::make_unique<Configuration>();
   symtab = std::make_unique<SymbolTable>();
+  config->outputType = getOutputType(args);
   target = createTargetInfo(args);
   depTracker = std::make_unique<DependencyTracker>(
       args.getLastArgValue(OPT_dependency_info));
@@ -1234,7 +1254,6 @@ bool macho::link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
   config->printEachFile = args.hasArg(OPT_t);
   config->printWhyLoad = args.hasArg(OPT_why_load);
   config->omitDebugInfo = args.hasArg(OPT_S);
-  config->outputType = getOutputType(args);
   config->errorForArchMismatch = args.hasArg(OPT_arch_errors_fatal);
   if (const Arg *arg = args.getLastArg(OPT_bundle_loader)) {
     if (config->outputType != MH_BUNDLE)

diff  --git a/lld/MachO/Writer.cpp b/lld/MachO/Writer.cpp
index 727640e1f582e..ac36630569a45 100644
--- a/lld/MachO/Writer.cpp
+++ b/lld/MachO/Writer.cpp
@@ -458,9 +458,11 @@ class LCBuildVersion final : public LoadCommand {
     auto *c = reinterpret_cast<build_version_command *>(buf);
     c->cmd = LC_BUILD_VERSION;
     c->cmdsize = getSize();
+
     c->platform = static_cast<uint32_t>(platformInfo.target.Platform);
     c->minos = encodeVersion(platformInfo.minimum);
     c->sdk = encodeVersion(platformInfo.sdk);
+
     c->ntools = ntools;
     auto *t = reinterpret_cast<build_tool_version *>(&c[1]);
     t->tool = TOOL_LD;
@@ -768,6 +770,11 @@ template <class LP> void Writer::createLoadCommands() {
   else
     in.header->addLoadCommand(make<LCMinVersion>(config->platformInfo));
 
+  if (config->secondaryPlatformInfo) {
+    in.header->addLoadCommand(
+        make<LCBuildVersion>(*config->secondaryPlatformInfo));
+  }
+
   // This is down here to match ld64's load command order.
   if (config->outputType == MH_EXECUTE)
     in.header->addLoadCommand(make<LCMain>());

diff  --git a/lld/test/MachO/platform-version.s b/lld/test/MachO/platform-version.s
index 56b566140ad6c..047aea02fcde3 100644
--- a/lld/test/MachO/platform-version.s
+++ b/lld/test/MachO/platform-version.s
@@ -61,10 +61,10 @@
 # FAIL-PLATFORM-NOT: malformed {{minimum|sdk}} version: {{.*}}
 
 # RUN: not %no-arg-lld -arch x86_64 -o %t %t.o -fatal_warnings 2>&1 \
-# RUN:        -platform_version 'mac catalyst' 14.0 15.0 \
+# RUN:        -platform_version iOS 14.0 15.0 \
 # RUN:        -platform_version macos 12.0 12.0 \
 # RUN:     | FileCheck --check-prefix=FAIL-TODO %s
-# FAIL-TODO: writing zippered outputs not yet implemented, ignoring all but last -platform_version flag
+# FAIL-TODO: lld supports writing zippered outputs only for macos and mac-catalyst
 
 # RUN: not %no-arg-lld -arch x86_64 -o %t %t.o 2>&1 \
 # RUN:        -platform_version bridgeOS 1 5 \

diff  --git a/lld/test/MachO/zippered.yaml b/lld/test/MachO/zippered.yaml
index 1972be4454a47..75a43c41a750d 100644
--- a/lld/test/MachO/zippered.yaml
+++ b/lld/test/MachO/zippered.yaml
@@ -5,10 +5,10 @@
 # RUN: echo "" | llvm-mc -filetype=obj -triple=x86_64-apple-ios13.15.0-macabi -o %t/test_maccatalyst.o
 # RUN: echo "" | llvm-mc -filetype=obj -triple=x86_64-apple-ios13.15.0 -o %t/test_ios.o
 
+## Test linking against a zippered dylib.
 # RUN: %lld -lSystem -dylib %t/test.dylib %t/test_macos.o -o /dev/null
 # RUN: %no-arg-lld -syslibroot %S/Inputs/MacOSX.sdk -lSystem -dylib -arch x86_64 -platform_version mac-catalyst 13.15.0 14.0 %t/test.dylib %t/test_maccatalyst.o -o /dev/null
 
-
 # RUN: %no-arg-lld -syslibroot %S/Inputs/MacOSX.sdk -lSystem -dylib -arch x86_64 -platform_version mac-catalyst 13.15.0 14.0 %t/test_maccatalyst.o -o /dev/null -framework MacOnly-Indirect
 
 # RUN: not %no-arg-lld -syslibroot %S/Inputs/MacOSX.sdk -lSystem -dylib -arch x86_64 -platform_version mac-catalyst 13.15.0 14.0 %t/test_maccatalyst.o -o /dev/null -framework MacOnly 2>&1 | FileCheck --check-prefix=INCOMPATIBLE %s
@@ -17,6 +17,50 @@
 # RUN: not %no-arg-lld -syslibroot %S/Inputs/MacOSX.sdk -lSystem -dylib -arch x86_64 -platform_version ios 13.15.0 14.0 %t/test.dylib %t/test_ios.o -o /dev/null 2>&1 | FileCheck %s
 # CHECK: test.dylib has platform macOS/macCatalyst, which is 
diff erent from target platform iOS
 
+## Test creating a zippered dylib.
+# RUN: %no-arg-lld -syslibroot %S/Inputs/MacOSX.sdk -lSystem -dylib -arch x86_64 -platform_version macos 12.0 13.0 -platform_version mac-catalyst 13.15.0 14.0 %t/test.dylib %t/test_macos.o -o %t/test_zippered.dylib
+# RUN: llvm-otool -l %t/test_zippered.dylib | FileCheck --check-prefix=ZIPPERED %s
+# ZIPPERED:           cmd LC_BUILD_VERSION
+# ZIPPERED-NEXT:  cmdsize
+# ZIPPERED-NEXT: platform 1
+# ZIPPERED-NEXT:      sdk 13.0
+# ZIPPERED-NEXT:    minos 12.0
+# ZIPPERED-NEXT:   ntools 1
+# ZIPPERED-NEXT:     tool
+# ZIPPERED-NEXT:  version
+# ZIPPERED-NEXT: Load command
+# ZIPPERED-NEXT:      cmd LC_BUILD_VERSION
+# ZIPPERED-NEXT:  cmdsize
+# ZIPPERED-NEXT: platform 6
+# ZIPPERED-NEXT:      sdk 14.0
+# ZIPPERED-NEXT:    minos 13.15
+# ZIPPERED-NEXT:   ntools 1
+# ZIPPERED-NEXT:     tool
+# ZIPPERED-NEXT:  version
+
+# RUN: %no-arg-lld -syslibroot %S/Inputs/MacOSX.sdk -lSystem -dylib -arch x86_64 -platform_version macos 10.8 13.0 -platform_version mac-catalyst 13.15.0 14.0 %t/test.dylib %t/test_macos.o -o %t/test_zippered.dylib
+# RUN: llvm-otool -l %t/test_zippered.dylib | FileCheck --check-prefix=ZIPPERED-OLD %s
+# ZIPPERED-OLD:           cmd LC_VERSION_MIN_MACOSX
+# ZIPPERED-OLD-NEXT:  cmdsize
+# ZIPPERED-OLD-NEXT:  version 10.8
+# ZIPPERED-OLD-NEXT:      sdk 13.0
+# ZIPPERED-OLD-NEXT: Load command
+# ZIPPERED-OLD-NEXT:      cmd LC_BUILD_VERSION
+# ZIPPERED-OLD-NEXT:  cmdsize
+# ZIPPERED-OLD-NEXT: platform 6
+# ZIPPERED-OLD-NEXT:      sdk 14.0
+# ZIPPERED-OLD-NEXT:    minos 13.15
+# ZIPPERED-OLD-NEXT:   ntools 1
+# ZIPPERED-OLD-NEXT:     tool
+# ZIPPERED-OLD-NEXT:  version
+
+# RUN: not %no-arg-lld -syslibroot %S/Inputs/MacOSX.sdk -lSystem -arch x86_64 -platform_version macos 10.8 13.0 -platform_version mac-catalyst 13.15.0 14.0 %t/test.dylib %t/test_macos.o -o %t/test_zippered.dylib 2>&1 | FileCheck --check-prefix=ZIPPERED-EXE %s
+# ZIPPERED-EXE: writing zippered outputs only valid for -dylib and -bundle
+
+## Reject .o files which only have the secondary platform.
+# RUN: not %no-arg-lld -syslibroot %S/Inputs/MacOSX.sdk -lSystem -dylib -arch x86_64 -platform_version macos 12.0 13.0 -platform_version mac-catalyst 13.15.0 14.0 %t/test.dylib %t/test_maccatalyst.o -o %t/test_zippered.dylib 2>&1 | FileCheck --check-prefix=CAT %s
+# CAT: test_maccatalyst.o has platform macCatalyst, which is 
diff erent from target platform macOS
+
 --- !mach-o
 FileHeader:
   magic:           0xFEEDFACF


        


More information about the llvm-commits mailing list