[PATCH] D77087: [lld-macho] Require executables to link against libSystem.dylib

Jez Ng via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 30 18:03:10 PDT 2020


int3 updated this revision to Diff 253758.
int3 added a comment.

rebase


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77087/new/

https://reviews.llvm.org/D77087

Files:
  lld/MachO/Writer.cpp
  lld/test/MachO/lit.local.cfg
  lld/test/MachO/no-libsystem.s


Index: lld/test/MachO/no-libsystem.s
===================================================================
--- /dev/null
+++ lld/test/MachO/no-libsystem.s
@@ -0,0 +1,25 @@
+# REQUIRES: x86
+# RUN: mkdir -p %t
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-darwin %s -o %t/no-libsystem.o
+# RUN: LLD_FORCE_LOAD_LIBSYSTEM=0 not %lld -o /dev/null %t/no-libsystem.o 2>&1 \
+# RUN:   | FileCheck %s --check-prefix=ERROR
+# ERROR: executables must link with libSystem.dylib
+
+## Even though we are not loading libSystem here -- just a random dylib -- the
+## error doesn't show up. This mimics ld64's behavior, which only cares that
+## *some* dylib is loaded.
+# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %p/Inputs/libhello.s \
+# RUN:   -o %t/libhello.o
+# RUN: %lld -dylib -install_name @executable_path/libhello.dylib \
+# RUN:   %t/libhello.o -o %t/libhello.dylib
+# RUN: LLD_FORCE_LOAD_LIBSYSTEM=0 %lld -Z -L%t -lhello -o %t/no-libsystem %t/no-libsystem.o
+# RUN: llvm-readobj --needed-libs %t/no-libsystem | FileCheck %s
+# CHECK:      NeededLibraries [
+# CHECK=NEXT:   @executable_path/libhello.dylib
+# CHECK=NEXT: ]
+
+.text
+.global _main
+_main:
+  mov $0, %rax
+  ret
Index: lld/test/MachO/lit.local.cfg
===================================================================
--- lld/test/MachO/lit.local.cfg
+++ lld/test/MachO/lit.local.cfg
@@ -1,3 +1,4 @@
+config.environment['LLD_FORCE_LOAD_LIBSYSTEM'] = '1'
 config.substitutions += [
     ('%lld', 'lld -flavor darwinnew'),
 ]
Index: lld/MachO/Writer.cpp
===================================================================
--- lld/MachO/Writer.cpp
+++ lld/MachO/Writer.cpp
@@ -356,10 +356,15 @@
     }
   }
 
-  // TODO: dyld requires libSystem to be loaded. libSystem is a universal
-  // binary and we don't have support for that yet, so mock it out here.
-  headerSection->addLoadCommand(
-      make<LCLoadDylib>("/usr/lib/libSystem.B.dylib"));
+  if (dylibOrdinal == 1 && config->outputType == MH_EXECUTE) {
+    // Used in tests, as we also run those tests on non-OSX systems.
+    if (StringRef(getenv("LLD_FORCE_LOAD_LIBSYSTEM")) == "1") {
+      headerSection->addLoadCommand(
+          make<LCLoadDylib>("/usr/lib/libSystem.B.dylib"));
+    } else {
+      error("executables must link with libSystem.dylib");
+    }
+  }
 }
 
 void Writer::scanRelocations() {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D77087.253758.patch
Type: text/x-patch
Size: 2349 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200331/7c8fd80b/attachment-0001.bin>


More information about the llvm-commits mailing list