Index: lld/MachO/Config.h =================================================================== --- lld/MachO/Config.h +++ lld/MachO/Config.h @@ -67,6 +67,7 @@ std::vector librarySearchPaths; std::vector frameworkSearchPaths; std::vector runtimePaths; + std::vector explicitUndefineds; llvm::DenseMap priorities; }; Index: lld/MachO/Driver.cpp =================================================================== --- lld/MachO/Driver.cpp +++ lld/MachO/Driver.cpp @@ -723,6 +723,9 @@ config->entry = symtab->addUndefined(args.getLastArgValue(OPT_e, "_main"), /*isWeakRef=*/false); + for (auto *arg : args.filtered(OPT_u)) + config->explicitUndefineds.push_back( + symtab->addUndefined(arg->getValue(), /*isWeakRef=*/false)); config->outputFile = args.getLastArgValue(OPT_o, "a.out"); config->installName = args.getLastArgValue(OPT_install_name, config->outputFile); @@ -852,6 +855,15 @@ error("undefined symbol: " + toString(*config->entry)); return false; } + // FIXME: This prints symbols that are undefined both in input files and + // via -u flag twice. + for (const auto *undefined : config->explicitUndefineds) { + if (isa(undefined)) { + error("undefined symbol: " + toString(*undefined) + + "\n>>> referenced by flag -u " + toString(*undefined)); + return false; + } + } createSyntheticSections(); symtab->addDSOHandle(in.header); Index: lld/MachO/Options.td =================================================================== --- lld/MachO/Options.td +++ lld/MachO/Options.td @@ -400,7 +400,6 @@ def u : Separate<["-"], "u">, MetaVarName<"">, HelpText<"Require that be defined for the link to succeed">, - Flags<[HelpHidden]>, Group; def U : Separate<["-"], "U">, MetaVarName<"">, Index: lld/test/MachO/u.s =================================================================== --- /dev/null +++ lld/test/MachO/u.s @@ -0,0 +1,32 @@ +# REQUIRES: x86 +# RUN: rm -rf %t +# RUN: split-file %s %t + +# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos -o %t/foo.o %t/foo.s +# RUN: llvm-ar csr %t/lib.a %t/foo.o + +# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos -o %t/main.o %t/main.s + +# RUN: %lld %t/main.o %t/lib.a -o /dev/null -why_load | \ +# RUN: FileCheck %s --check-prefix=NOFOO --allow-empty + +# RUN: %lld %t/main.o %t/lib.a -u _foo -o /dev/null -why_load | \ +# RUN: FileCheck %s --check-prefix=FOO + +# RUN: not %lld %t/main.o %t/lib.a -u _asdf -o /dev/null 2>&1 | \ +# RUN: FileCheck %s --check-prefix=UNDEF + +# NOFOO-NOT: _foo forced load of lib.a(foo.o) +# FOO: _foo forced load of lib.a(foo.o) +# UNDEF: error: undefined symbol: _asdf + +#--- foo.s +.globl _foo +_foo: + ret + +#--- main.s +.globl _main +_main: + ret +