Index: lld/MachO/Config.h =================================================================== --- lld/MachO/Config.h +++ lld/MachO/Config.h @@ -121,9 +121,17 @@ uint32_t dylibCurrentVersion = 0; uint32_t timeTraceGranularity = 500; std::string progName; + + // For `clang -arch arm64 -arch x86_64`, clang will: + // 1. invoke the linker twice, to write one temporary output per arch + // 2. invoke `lipo` to merge the two outputs into a single file + // `outputFile` is the name of the temporary file the linker writes to. + // `finalOutput `is the name of the file lipo writes to after the link. + llvm::StringRef outputFile; + llvm::StringRef finalOutput; + llvm::StringRef installName; llvm::StringRef mapFile; - llvm::StringRef outputFile; llvm::StringRef ltoObjPath; llvm::StringRef thinLTOJobs; llvm::StringRef umbrella; Index: lld/MachO/Driver.cpp =================================================================== --- lld/MachO/Driver.cpp +++ lld/MachO/Driver.cpp @@ -1106,6 +1106,10 @@ config->mapFile = args.getLastArgValue(OPT_map); config->outputFile = args.getLastArgValue(OPT_o, "a.out"); + if (const Arg *arg = args.getLastArg(OPT_final_output)) + config->finalOutput = arg->getValue(); + else + config->finalOutput = config->outputFile; config->astPaths = args.getAllArgValues(OPT_add_ast_path); config->headerPad = args::getHex(args, OPT_headerpad, /*Default=*/32); config->headerPadMaxInstallNames = @@ -1169,7 +1173,7 @@ else config->installName = arg->getValue(); } else if (config->outputType == MH_DYLIB) { - config->installName = config->outputFile; + config->installName = config->finalOutput; } if (args.hasArg(OPT_mark_dead_strippable_dylib)) { Index: lld/MachO/Options.td =================================================================== --- lld/MachO/Options.td +++ lld/MachO/Options.td @@ -873,8 +873,7 @@ Group; def final_output : Separate<["-"], "final_output">, MetaVarName<"">, - HelpText<"Specify the dylib install name if -install_name is not used--used by compiler driver for multiple -arch arguments">, - Flags<[HelpHidden]>, + HelpText<"Specify dylib install name if -install_name is not used; used by compiler driver for multiple -arch arguments">, Group; def arch_multiple : Flag<["-"], "arch_multiple">, HelpText<"Augment error and warning messages with the architecture name">, Index: lld/test/MachO/final-output.s =================================================================== --- /dev/null +++ lld/test/MachO/final-output.s @@ -0,0 +1,20 @@ +# REQUIRES: x86 + +# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos -o %t.o %s + +## -final_output sets the default for -install_name, but an explicit +## -install_name wins +# RUN: %lld -dylib -o %t.dylib -final_output /lib/foo.dylib %t.o +# RUN: llvm-otool -lv %t.dylib | FileCheck -DID=/lib/foo.dylib %s + +# RUN: %lld -dylib -o %t.dylib -install_name /foo/bar.dylib \ +# RUN: -final_output /lib/foo.dylib %t.o +# RUN: llvm-otool -lv %t.dylib | FileCheck -DID=/foo/bar.dylib %s + +# CHECK: cmd LC_ID_DYLIB +# CHECK-NEXT: cmdsize +# CHECK-NEXT: name [[ID]] + +.globl __Z3foo +__Z3foo: + ret