[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,@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
This commit is contained in:
Nico Weber 2022-04-22 11:55:50 -04:00
parent ddef1ed4e7
commit 895a72111b
5 changed files with 80 additions and 9 deletions

View File

@ -151,6 +151,7 @@ struct Configuration {
bool deadStrip = false; bool deadStrip = false;
bool errorForArchMismatch = false; bool errorForArchMismatch = false;
PlatformInfo platformInfo; PlatformInfo platformInfo;
llvm::Optional<PlatformInfo> secondaryPlatformInfo;
NamespaceKind namespaceKind = NamespaceKind::twolevel; NamespaceKind namespaceKind = NamespaceKind::twolevel;
UndefinedSymbolTreatment undefinedSymbolTreatment = UndefinedSymbolTreatment undefinedSymbolTreatment =
UndefinedSymbolTreatment::error; UndefinedSymbolTreatment::error;

View File

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

View File

@ -458,9 +458,11 @@ public:
auto *c = reinterpret_cast<build_version_command *>(buf); auto *c = reinterpret_cast<build_version_command *>(buf);
c->cmd = LC_BUILD_VERSION; c->cmd = LC_BUILD_VERSION;
c->cmdsize = getSize(); c->cmdsize = getSize();
c->platform = static_cast<uint32_t>(platformInfo.target.Platform); c->platform = static_cast<uint32_t>(platformInfo.target.Platform);
c->minos = encodeVersion(platformInfo.minimum); c->minos = encodeVersion(platformInfo.minimum);
c->sdk = encodeVersion(platformInfo.sdk); c->sdk = encodeVersion(platformInfo.sdk);
c->ntools = ntools; c->ntools = ntools;
auto *t = reinterpret_cast<build_tool_version *>(&c[1]); auto *t = reinterpret_cast<build_tool_version *>(&c[1]);
t->tool = TOOL_LD; t->tool = TOOL_LD;
@ -768,6 +770,11 @@ template <class LP> void Writer::createLoadCommands() {
else else
in.header->addLoadCommand(make<LCMinVersion>(config->platformInfo)); 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. // This is down here to match ld64's load command order.
if (config->outputType == MH_EXECUTE) if (config->outputType == MH_EXECUTE)
in.header->addLoadCommand(make<LCMain>()); in.header->addLoadCommand(make<LCMain>());

View File

@ -61,10 +61,10 @@
# FAIL-PLATFORM-NOT: malformed {{minimum|sdk}} version: {{.*}} # FAIL-PLATFORM-NOT: malformed {{minimum|sdk}} version: {{.*}}
# RUN: not %no-arg-lld -arch x86_64 -o %t %t.o -fatal_warnings 2>&1 \ # 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: -platform_version macos 12.0 12.0 \
# RUN: | FileCheck --check-prefix=FAIL-TODO %s # 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: not %no-arg-lld -arch x86_64 -o %t %t.o 2>&1 \
# RUN: -platform_version bridgeOS 1 5 \ # RUN: -platform_version bridgeOS 1 5 \

View File

@ -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-macabi -o %t/test_maccatalyst.o
# RUN: echo "" | llvm-mc -filetype=obj -triple=x86_64-apple-ios13.15.0 -o %t/test_ios.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: %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.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: %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 # 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 # 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 different from target platform iOS # CHECK: test.dylib has platform macOS/macCatalyst, which is different 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 different from target platform macOS
--- !mach-o --- !mach-o
FileHeader: FileHeader:
magic: 0xFEEDFACF magic: 0xFEEDFACF