[PATCH] D94371: [lld/mac] Implement -u flag

Nico Weber via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Jan 9 19:23:00 PST 2021


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

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).


https://reviews.llvm.org/D94371

Files:
  lld/MachO/Config.h
  lld/MachO/Driver.cpp
  lld/MachO/Options.td
  lld/test/MachO/u.s


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
+
Index: lld/MachO/Options.td
===================================================================
--- lld/MachO/Options.td
+++ lld/MachO/Options.td
@@ -400,7 +400,6 @@
 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>">,
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>(undefined)) {
+      error("undefined symbol: " + toString(*undefined) +
+            "\n>>> referenced by flag -u " + toString(*undefined));
+      return false;
+    }
+  }
 
   createSyntheticSections();
   symtab->addDSOHandle(in.header);
Index: lld/MachO/Config.h
===================================================================
--- lld/MachO/Config.h
+++ lld/MachO/Config.h
@@ -67,6 +67,7 @@
   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;
 };
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D94371.315647.patch
Type: text/x-patch
Size: 2977 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210110/3f9c1f84/attachment.bin>


More information about the llvm-commits mailing list