diff --git a/lld/MachO/Writer.cpp b/lld/MachO/Writer.cpp --- a/lld/MachO/Writer.cpp +++ b/lld/MachO/Writer.cpp @@ -348,10 +348,15 @@ } } - // TODO: dyld requires libSystem to be loaded. libSystem is a universal - // binary and we don't have support for that yet, so mock it out here. - headerSection->addLoadCommand( - make("/usr/lib/libSystem.B.dylib")); + if (dylibOrdinal == 1 && config->outputType == MH_EXECUTE) { + // Used in tests, as we also run those tests on non-OSX systems. + if (StringRef(getenv("LLD_FORCE_LOAD_LIBSYSTEM")) == "1") { + headerSection->addLoadCommand( + make("/usr/lib/libSystem.B.dylib")); + } else { + error("executables must link with libSystem.dylib"); + } + } } void Writer::scanRelocations() { diff --git a/lld/test/MachO/lit.local.cfg b/lld/test/MachO/lit.local.cfg --- a/lld/test/MachO/lit.local.cfg +++ b/lld/test/MachO/lit.local.cfg @@ -1,3 +1,4 @@ +config.environment['LLD_FORCE_LOAD_LIBSYSTEM'] = '1' config.substitutions += [ ('%lld', 'lld -flavor darwinnew'), ] diff --git a/lld/test/MachO/no-libsystem.s b/lld/test/MachO/no-libsystem.s new file mode 100644 --- /dev/null +++ b/lld/test/MachO/no-libsystem.s @@ -0,0 +1,25 @@ +# REQUIRES: x86 +# RUN: mkdir -p %t +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-darwin %s -o %t/no-libsystem.o +# RUN: LLD_FORCE_LOAD_LIBSYSTEM=0 not %lld -o /dev/null %t/no-libsystem.o 2>&1 \ +# RUN: | FileCheck %s --check-prefix=ERROR +# ERROR: executables must link with libSystem.dylib + +## Even though we are not loading libSystem here -- just a random dylib -- the +## error doesn't show up. This mimics ld64's behavior, which only cares that +## *some* dylib is loaded. +# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %p/Inputs/libhello.s \ +# RUN: -o %t/libhello.o +# RUN: %lld -dylib -install_name @executable_path/libhello.dylib \ +# RUN: %t/libhello.o -o %t/libhello.dylib +# RUN: LLD_FORCE_LOAD_LIBSYSTEM=0 %lld -Z -L%t -lhello -o %t/no-libsystem %t/no-libsystem.o +# RUN: llvm-readobj --needed-libs %t/no-libsystem | FileCheck %s +# CHECK: NeededLibraries [ +# CHECK=NEXT: @executable_path/libhello.dylib +# CHECK=NEXT: ] + +.text +.global _main +_main: + mov $0, %rax + ret