[lld] e0b8604 - [lld/mac] Implement -u flag

Nico Weber via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 9 05:23:39 PST 2021


Author: Nico Weber
Date: 2021-02-09T08:23:06-05:00
New Revision: e0b8604e5d3c80a727a833663e765c8057ce7825

URL: https://github.com/llvm/llvm-project/commit/e0b8604e5d3c80a727a833663e765c8057ce7825
DIFF: https://github.com/llvm/llvm-project/commit/e0b8604e5d3c80a727a833663e765c8057ce7825.diff

LOG: [lld/mac] Implement -u flag

Since we emit diagnostics for undefineds in Writer::scanRelocations()
and symbols referenced by -u flags aren't referenced by any relocations,
this needs some manual code (similar to the entry point).

Differential Revision: https://reviews.llvm.org/D94371

Added: 
    lld/test/MachO/u.s

Modified: 
    lld/MachO/Config.h
    lld/MachO/Driver.cpp
    lld/MachO/Options.td

Removed: 
    


################################################################################
diff  --git a/lld/MachO/Config.h b/lld/MachO/Config.h
index f6e1f134d974..94f84b246c6f 100644
--- a/lld/MachO/Config.h
+++ b/lld/MachO/Config.h
@@ -68,6 +68,7 @@ struct Configuration {
   std::vector<llvm::StringRef> librarySearchPaths;
   std::vector<llvm::StringRef> frameworkSearchPaths;
   std::vector<llvm::StringRef> runtimePaths;
+  std::vector<Symbol *> explicitUndefineds;
   llvm::DenseMap<llvm::StringRef, SymbolPriorityEntry> priorities;
 };
 

diff  --git a/lld/MachO/Driver.cpp b/lld/MachO/Driver.cpp
index 569948244476..7c7fd869b9bf 100644
--- a/lld/MachO/Driver.cpp
+++ b/lld/MachO/Driver.cpp
@@ -734,6 +734,10 @@ bool macho::link(ArrayRef<const char *> argsArr, bool canExitEarly,
   config->entry = symtab->addUndefined(args.getLastArgValue(OPT_e, "_main"),
                                        /*file=*/nullptr,
                                        /*isWeakRef=*/false);
+  for (auto *arg : args.filtered(OPT_u)) {
+    config->explicitUndefineds.push_back(symtab->addUndefined(
+        arg->getValue(), /*file=*/nullptr, /*isWeakRef=*/false));
+  }
   config->outputFile = args.getLastArgValue(OPT_o, "a.out");
   config->installName =
       args.getLastArgValue(OPT_install_name, config->outputFile);
@@ -864,6 +868,15 @@ bool macho::link(ArrayRef<const char *> argsArr, bool canExitEarly,
     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>(undefined)) {
+      error("undefined symbol: " + toString(*undefined) +
+            "\n>>> referenced by flag -u " + toString(*undefined));
+      return false;
+    }
+  }
 
   createSyntheticSections();
   symtab->addDSOHandle(in.header);

diff  --git a/lld/MachO/Options.td b/lld/MachO/Options.td
index 97b9ed60fc3b..adff1d10e250 100644
--- a/lld/MachO/Options.td
+++ b/lld/MachO/Options.td
@@ -454,7 +454,6 @@ def flat_namespace : Flag<["-"], "flat_namespace">,
 def u : Separate<["-"], "u">,
      MetaVarName<"<symbol>">,
      HelpText<"Require that <symbol> be defined for the link to succeed">,
-     Flags<[HelpHidden]>,
      Group<grp_resolve>;
 def U : Separate<["-"], "U">,
      MetaVarName<"<symbol>">,

diff  --git a/lld/test/MachO/u.s b/lld/test/MachO/u.s
new file mode 100644
index 000000000000..f85a6ddd38f4
--- /dev/null
+++ b/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
+


        


More information about the llvm-commits mailing list