diff --git a/lld/MachO/Driver.h b/lld/MachO/Driver.h --- a/lld/MachO/Driver.h +++ b/lld/MachO/Driver.h @@ -44,7 +44,8 @@ llvm::Optional resolveDylibPath(llvm::StringRef path); llvm::Optional loadDylib(llvm::MemoryBufferRef mbref, - DylibFile *umbrella = nullptr); + DylibFile *umbrella = nullptr, + bool isBundleLoader = false); uint32_t getModTime(llvm::StringRef path); diff --git a/lld/MachO/Driver.cpp b/lld/MachO/Driver.cpp --- a/lld/MachO/Driver.cpp +++ b/lld/MachO/Driver.cpp @@ -257,7 +257,8 @@ return v; } -static InputFile *addFile(StringRef path, bool forceLoadArchive) { +static InputFile *addFile(StringRef path, bool forceLoadArchive, + bool isBundleLoader = false) { Optional buffer = readFile(path); if (!buffer) return nullptr; @@ -317,6 +318,16 @@ case file_magic::bitcode: newFile = make(mbref); break; + case file_magic::macho_executable: + case file_magic::macho_bundle: + // We only allow executable and bundle type here if it is used + // as a bundle loader. + if (!isBundleLoader) + error(path + ": unhandled file type"); + if (Optional dylibFile = + loadDylib(mbref, nullptr, isBundleLoader)) + newFile = *dylibFile; + break; default: error(path + ": unhandled file type"); } @@ -731,6 +742,11 @@ config->printEachFile = args.hasArg(OPT_t); config->printWhyLoad = args.hasArg(OPT_why_load); config->outputType = getOutputType(args); + if (const opt::Arg *arg = args.getLastArg(OPT_bundle_loader)) { + if (config->outputType != MH_BUNDLE) + error("-bundle_loader can only be used with MachO bundle output"); + addFile(arg->getValue(), false, true); + } config->ltoObjPath = args.getLastArgValue(OPT_object_path_lto); config->ltoNewPassManager = args.hasFlag(OPT_no_lto_legacy_pass_manager, OPT_lto_legacy_pass_manager, @@ -780,6 +796,7 @@ const auto &opt = arg->getOption(); warnIfDeprecatedOption(opt); warnIfUnimplementedOption(opt); + // TODO: are any of these better handled via filtered() or getLastArg()? switch (opt.getID()) { case OPT_INPUT: diff --git a/lld/MachO/DriverUtils.cpp b/lld/MachO/DriverUtils.cpp --- a/lld/MachO/DriverUtils.cpp +++ b/lld/MachO/DriverUtils.cpp @@ -173,7 +173,8 @@ static DenseMap loadedDylibs; Optional macho::loadDylib(MemoryBufferRef mbref, - DylibFile *umbrella) { + DylibFile *umbrella, + bool isBundleLoader) { StringRef path = mbref.getBufferIdentifier(); DylibFile *&file = loadedDylibs[CachedHashStringRef(path)]; if (file) @@ -187,11 +188,13 @@ ": " + toString(result.takeError())); return {}; } - file = make(**result, umbrella); + file = make(**result, umbrella, isBundleLoader); } else { assert(magic == file_magic::macho_dynamically_linked_shared_lib || - magic == file_magic::macho_dynamically_linked_shared_lib_stub); - file = make(mbref, umbrella); + magic == file_magic::macho_dynamically_linked_shared_lib_stub || + magic == file_magic::macho_executable || + magic == file_magic::macho_bundle); + file = make(mbref, umbrella, isBundleLoader); } return file; } diff --git a/lld/MachO/InputFiles.h b/lld/MachO/InputFiles.h --- a/lld/MachO/InputFiles.h +++ b/lld/MachO/InputFiles.h @@ -125,10 +125,12 @@ // the root dylib to ensure symbols in the child library are correctly bound // to the root. On the other hand, if a dylib is being directly loaded // (through an -lfoo flag), then `umbrella` should be a nullptr. - explicit DylibFile(MemoryBufferRef mb, DylibFile *umbrella = nullptr); + explicit DylibFile(MemoryBufferRef mb, DylibFile *umbrella = nullptr, + bool isBundleLoader = false); explicit DylibFile(const llvm::MachO::InterfaceFile &interface, - DylibFile *umbrella = nullptr); + DylibFile *umbrella = nullptr, + bool isBundleLoader = false); static bool classof(const InputFile *f) { return f->kind() == DylibKind; } @@ -139,6 +141,12 @@ RefState refState; bool reexport = false; bool forceWeakImport = false; + + // An executable can be used as a bundle loader that will load the output + // file being linked, and that contains symbols referenced, but not + // implemented in the bundle. When used like this, it is very similar + // to a Dylib, so we re-used the same class to represent it. + bool isBundleLoader; }; // .a file diff --git a/lld/MachO/InputFiles.cpp b/lld/MachO/InputFiles.cpp --- a/lld/MachO/InputFiles.cpp +++ b/lld/MachO/InputFiles.cpp @@ -606,8 +606,11 @@ inputFiles.insert(*reexport); } -DylibFile::DylibFile(MemoryBufferRef mb, DylibFile *umbrella) - : InputFile(DylibKind, mb), refState(RefState::Unreferenced) { +DylibFile::DylibFile(MemoryBufferRef mb, DylibFile *umbrella, + bool isBundleLoader) + : InputFile(DylibKind, mb), refState(RefState::Unreferenced), + isBundleLoader(isBundleLoader) { + assert(!isBundleLoader || !umbrella); if (umbrella == nullptr) umbrella = this; @@ -620,13 +623,16 @@ currentVersion = read32le(&c->dylib.current_version); compatibilityVersion = read32le(&c->dylib.compatibility_version); dylibName = reinterpret_cast(cmd) + read32le(&c->dylib.name); - } else { + } else if (!isBundleLoader) { + // macho_executable and macho_bundle don't have LC_ID_DYLIB, + // so it's OK. error("dylib " + toString(this) + " missing LC_ID_DYLIB load command"); return; } // Initialize symbols. - DylibFile *exportingFile = isImplicitlyLinked(dylibName) ? this : umbrella; + DylibFile *exportingFile = + (isBundleLoader || isImplicitlyLinked(dylibName)) ? this : umbrella; if (const load_command *cmd = findCommand(hdr, LC_DYLD_INFO_ONLY)) { auto *c = reinterpret_cast(cmd); parseTrie(buf + c->export_off, c->export_size, @@ -659,8 +665,12 @@ } } -DylibFile::DylibFile(const InterfaceFile &interface, DylibFile *umbrella) - : InputFile(DylibKind, interface), refState(RefState::Unreferenced) { +DylibFile::DylibFile(const InterfaceFile &interface, DylibFile *umbrella, + bool isBundleLoader) + : InputFile(DylibKind, interface), refState(RefState::Unreferenced), + isBundleLoader(isBundleLoader) { + // FIXME: Add test for the missing TBD code path. + if (umbrella == nullptr) umbrella = this; diff --git a/lld/MachO/Options.td b/lld/MachO/Options.td --- a/lld/MachO/Options.td +++ b/lld/MachO/Options.td @@ -396,7 +396,6 @@ def bundle_loader : Separate<["-"], "bundle_loader">, MetaVarName<"">, HelpText<"Resolve undefined symbols from ">, - Flags<[HelpHidden]>, Group; def grp_object : OptionGroup<"object">, HelpText<"CREATING AN OBJECT FILE">; diff --git a/lld/MachO/SyntheticSections.cpp b/lld/MachO/SyntheticSections.cpp --- a/lld/MachO/SyntheticSections.cpp +++ b/lld/MachO/SyntheticSections.cpp @@ -269,7 +269,9 @@ if (dysym->file->ordinal <= BIND_IMMEDIATE_MASK) { os << static_cast(BIND_OPCODE_SET_DYLIB_ORDINAL_IMM | dysym->file->ordinal); - } else { + } else if (!dysym->file->isBundleLoader) { + // bundle-loader "dylib" is special because its ordinal is + // EXECUTABLE_ORDINAL, which is not used here. os << static_cast(BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB); encodeULEB128(dysym->file->ordinal, os); } diff --git a/lld/MachO/Writer.cpp b/lld/MachO/Writer.cpp --- a/lld/MachO/Writer.cpp +++ b/lld/MachO/Writer.cpp @@ -502,6 +502,14 @@ uint64_t dylibOrdinal = 1; for (InputFile *file : inputFiles) { if (auto *dylibFile = dyn_cast(file)) { + if (dylibFile->isBundleLoader) { + // Set this ordinal so that llvm-objdump works correctly. + dylibFile->ordinal = llvm::MachO::BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE; + + // The bundle loader doesn't reexport the symbols. + dylibFile->reexport = false; + continue; + } LoadCommandType lcType = dylibFile->forceWeakImport || dylibFile->refState == RefState::Weak ? LC_LOAD_WEAK_DYLIB diff --git a/lld/test/MachO/bundle_loader-darwin.test b/lld/test/MachO/bundle_loader-darwin.test new file mode 100644 --- /dev/null +++ b/lld/test/MachO/bundle_loader-darwin.test @@ -0,0 +1,52 @@ +REQUIRES: x86 + +# RUN: rm -rf %t; split-file %s %t +# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/2.s -o %t/2.o +# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/3.s -o %t/3.o +# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/main.s -o %t/main.o + +# RUN: %lld -lSystem -dylib -install_name %t/my_lib.dylib -o %t/mylib.dylib %t/2.o +# RUN: %lld %t/2.o %t/main.o -o %t/main +# RUN: %lld -lSystem -bundle -bundle_loader %t/main -o %t/bundle.bundle %t/3.o %t/mylib.dylib +# Check bundle.bundle to ensure the `my_func` symbol is from executable +# RUN: llvm-nm -m %t/bundle.bundle | FileCheck %s --check-prefix BUNDLE_CHECK +# BUNDLE_CHECK (undefined) external _main (from executable) +# BUNDLE_CHECK: (undefined) external my_func (from executable) +# Check with llvm-objdump: +# RUN: llvm-objdump --macho --lazy-bind %t/bundle.bundle | FileCheck %s --check-prefix BUNDLE_OBJ_CHECK +# BUNDLE_OBJ_CHECK: __DATA __la_symbol_ptr 0x00001010 main-executable my_fun + + +# RUN: %lld -lSystem -bundle -bundle_loader %t/main -o %t/bundle2.bundle %t/3.o %t/2.o +# Check bundle2.bundle to ensure that _main is still from executable +# but my_func is not. +# RUN: llvm-nm -m %t/bundle2.bundle | FileCheck %s --check-prefix BUNDLE2_CHECK +# BUNDLE2_CHECK: (undefined) external _main (from executable) +# BUNDLE2_CHECK: (__TEXT,__text) external my_func + +# Test that bundle_loader can only be used with MachO bundle output. +# RUN: not %lld -lSystem -bundle_loader %t/main -o %t/bundle3.bundle 2>&1 | FileCheck %s --check-prefix ERROR_CHECK +# ERROR_CHECK: -bundle_loader can only be used with MachO bundle output + +#--- 2.s +# my_lib: This contains the exported function +.globl my_func +my_func: + retq + +#--- 3.s +# my_user.s: This is the user/caller of the +# exported function +.text +my_user: + callq my_func() + retq + +#--- main.s +# main.s: dummy exec/main loads the exported function. +# This is basically a way to say `my_user` should get +# `my_func` from this executable. +.globl _main +.text + _main: + retq \ No newline at end of file