[PATCH] D105449: [lld/mac] Implement -final_output

Nico Weber via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 5 16:50:54 PDT 2021


thakis created this revision.
thakis added a reviewer: lld-macho.
Herald added a subscriber: dang.
Herald added a reviewer: int3.
Herald added a reviewer: gkm.
Herald added a project: lld-macho.
thakis requested review of this revision.

This is one of two flags clang passes to the linker when giving calling
clang with multiple -arch flags.

I think it'd make sense to also use finalOutput instead of outputFile
in CodeSignatureSection() and when replacing @executable_path, but
ld64 doesn't do that, so I'll at least put those in separate commits.


https://reviews.llvm.org/D105449

Files:
  lld/MachO/Config.h
  lld/MachO/Driver.cpp
  lld/MachO/Options.td
  lld/test/MachO/final-output.s


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
Index: lld/MachO/Options.td
===================================================================
--- lld/MachO/Options.td
+++ lld/MachO/Options.td
@@ -873,8 +873,7 @@
     Group<grp_rare>;
 def final_output : Separate<["-"], "final_output">,
     MetaVarName<"<name>">,
-    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<grp_rare>;
 def arch_multiple : Flag<["-"], "arch_multiple">,
     HelpText<"Augment error and warning messages with the architecture name">,
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/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;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D105449.356579.patch
Type: text/x-patch
Size: 3254 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210705/e0a22166/attachment.bin>


More information about the llvm-commits mailing list