diff --git a/lld/MachO/Writer.cpp b/lld/MachO/Writer.cpp index d9856a46e8cb8..f312828819dcc 100644 --- a/lld/MachO/Writer.cpp +++ b/lld/MachO/Writer.cpp @@ -938,17 +938,20 @@ template void Writer::createLoadCommands() { } ordinal = dylibFile->ordinal = dylibOrdinal++; - LoadCommandType lcType = - dylibFile->forceWeakImport || dylibFile->refState == RefState::Weak - ? LC_LOAD_WEAK_DYLIB - : LC_LOAD_DYLIB; + LoadCommandType lcType = LC_LOAD_DYLIB; + if (dylibFile->reexport) { + if (dylibFile->forceWeakImport) + warn(path::filename(dylibFile->getName()) + + " is re-exported so cannot be weak-linked"); + + lcType = LC_REEXPORT_DYLIB; + } else if (dylibFile->forceWeakImport || + dylibFile->refState == RefState::Weak) { + lcType = LC_LOAD_WEAK_DYLIB; + } in.header->addLoadCommand(make(lcType, dylibFile->installName, dylibFile->compatibilityVersion, dylibFile->currentVersion)); - - if (dylibFile->reexport) - in.header->addLoadCommand( - make(LC_REEXPORT_DYLIB, dylibFile->installName)); } for (const auto &dyldEnv : config->dyldEnvs) diff --git a/lld/test/MachO/sub-library.s b/lld/test/MachO/sub-library.s index ed0a96aa58f90..d799b8d54c01a 100644 --- a/lld/test/MachO/sub-library.s +++ b/lld/test/MachO/sub-library.s @@ -42,6 +42,17 @@ # REEXPORT-HEADERS-NOT: Load command # REEXPORT-HEADERS: name [[PATH]] +## Check that specifying a library both with `l` and `reexport_library` +## doesn't emit two load commands. +# RUN: %lld -dylib -reexport_library %t/libgoodbye.dylib \ +# RUN: -L%t -lgoodbye %t/libsuper.o -o %t/libsuper.dylib +# RUN: llvm-otool -L %t/libsuper.dylib | FileCheck %s \ +# RUN: --check-prefix=REEXPORT-DOUBLE -DPATH=%t/libgoodbye.dylib + +# REEXPORT-DOUBLE: [[PATH]] +# REEXPORT-DOUBLE-SAME: reexport +# REEXPORT-DOUBLE-NOT: [[PATH]] + # RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t/sub-library.o # RUN: %lld -o %t/sub-library -L%t -lsuper %t/sub-library.o