[lld] 0f8854f - [lld-macho] Don't reference entry symbol for non-executables

Jez Ng via llvm-commits llvm-commits at lists.llvm.org
Sun May 9 17:30:34 PDT 2021


Author: Jez Ng
Date: 2021-05-09T20:30:26-04:00
New Revision: 0f8854f7f5d3e98f32eef7cfd09d5b8915a7d301

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

LOG: [lld-macho] Don't reference entry symbol for non-executables

This would cause us to pull in symbols (and code) that should
be unused.

Reviewed By: #lld-macho, thakis

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

Added: 
    

Modified: 
    lld/MachO/Config.h
    lld/MachO/Driver.cpp
    lld/MachO/Writer.cpp
    lld/test/MachO/bundle-loader.s
    lld/test/MachO/entry-symbol.s

Removed: 
    


################################################################################
diff  --git a/lld/MachO/Config.h b/lld/MachO/Config.h
index eff3ff53f3bc..077762c85084 100644
--- a/lld/MachO/Config.h
+++ b/lld/MachO/Config.h
@@ -73,7 +73,7 @@ class SymbolPatterns {
 };
 
 struct Configuration {
-  Symbol *entry;
+  Symbol *entry = nullptr;
   bool hasReexports = false;
   bool allLoad = false;
   bool forceLoadObjC = false;

diff  --git a/lld/MachO/Driver.cpp b/lld/MachO/Driver.cpp
index dfc92e8e1c9e..8a03e584945b 100644
--- a/lld/MachO/Driver.cpp
+++ b/lld/MachO/Driver.cpp
@@ -927,9 +927,6 @@ bool macho::link(ArrayRef<const char *> argsArr, bool canExitEarly,
   if (!get_threadpool_strategy(config->thinLTOJobs))
     error("--thinlto-jobs: invalid job count: " + config->thinLTOJobs);
 
-  config->entry = symtab->addUndefined(args.getLastArgValue(OPT_e, "_main"),
-                                       /*file=*/nullptr,
-                                       /*isWeakRef=*/false);
   for (const Arg *arg : args.filtered(OPT_u)) {
     config->explicitUndefineds.push_back(symtab->addUndefined(
         arg->getValue(), /*file=*/nullptr, /*isWeakRef=*/false));
@@ -1002,6 +999,11 @@ bool macho::link(ArrayRef<const char *> argsArr, bool canExitEarly,
 
   config->undefinedSymbolTreatment = getUndefinedSymbolTreatment(args);
 
+  if (config->outputType == MH_EXECUTE)
+    config->entry = symtab->addUndefined(args.getLastArgValue(OPT_e, "_main"),
+                                         /*file=*/nullptr,
+                                         /*isWeakRef=*/false);
+
   config->librarySearchPaths =
       getLibrarySearchPaths(args, config->systemLibraryRoots);
   config->frameworkSearchPaths =

diff  --git a/lld/MachO/Writer.cpp b/lld/MachO/Writer.cpp
index 257d3bde9308..8929b524bf9a 100644
--- a/lld/MachO/Writer.cpp
+++ b/lld/MachO/Writer.cpp
@@ -534,6 +534,8 @@ static void prepareBranchTarget(Symbol *sym) {
                                  sym->stubsIndex * target->wordSize);
       }
     }
+  } else {
+    assert(false && "invalid symbol type for branch");
   }
 }
 
@@ -1053,7 +1055,8 @@ void Writer::writeOutputFile() {
 }
 
 template <class LP> void Writer::run() {
-  prepareBranchTarget(config->entry);
+  if (config->entry)
+    prepareBranchTarget(config->entry);
   scanRelocations();
   if (in.stubHelper->isNeeded())
     in.stubHelper->setup();

diff  --git a/lld/test/MachO/bundle-loader.s b/lld/test/MachO/bundle-loader.s
index 29ce230009b0..43b81ec8e4b9 100644
--- a/lld/test/MachO/bundle-loader.s
+++ b/lld/test/MachO/bundle-loader.s
@@ -5,23 +5,19 @@
 # RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/3.s -o %t/3.o
 # RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/main.s -o %t/main.o
 
-# RUN: %lld  -lSystem -dylib -install_name %t/my_lib.dylib -o %t/mylib.dylib %t/2.o
-# RUN: %lld %t/2.o %t/main.o -o %t/main
+# RUN: %lld -lSystem -dylib -install_name %t/my_lib.dylib -o %t/mylib.dylib %t/2.o
+# RUN: %lld -lSystem %t/2.o %t/main.o -o %t/main
 # RUN: %lld -lSystem -bundle -bundle_loader %t/main -o %t/bundle.bundle %t/3.o %t/mylib.dylib
-# Check bundle.bundle to ensure the `my_func` symbol is from executable
-# RUN: llvm-nm -m %t/bundle.bundle |  FileCheck %s --check-prefix BUNDLE
-# BUNDLE: (undefined) external _main (from executable)
+## Check bundle.bundle to ensure the `my_func` symbol is from executable
+# RUN: llvm-nm -m %t/bundle.bundle | FileCheck %s --check-prefix BUNDLE
 # BUNDLE: (undefined) external my_func (from executable)
 # RUN: llvm-objdump  --macho --lazy-bind %t/bundle.bundle | FileCheck %s --check-prefix BUNDLE-OBJ
 # BUNDLE-OBJ: segment  section             address            dylib                 symbol
 # BUNDLE-OBJ: __DATA   __la_symbol_ptr     0x{{[0-9a-f]*}}    main-executable       my_fun
 
-
 # RUN: %lld -lSystem -bundle -bundle_loader %t/main -o %t/bundle2.bundle %t/3.o %t/2.o
-# Check bundle2.bundle to ensure that _main is still from executable
-# but my_func is not.
+## Check bundle.bundle to ensure the `my_func` symbol is not from executable
 # RUN: llvm-nm -m %t/bundle2.bundle | FileCheck %s --check-prefix BUNDLE2
-# BUNDLE2: (undefined) external _main (from executable)
 # BUNDLE2: (__TEXT,__text) external my_func
 
 # Test that bundle_loader can only be used with MachO bundle output.

diff  --git a/lld/test/MachO/entry-symbol.s b/lld/test/MachO/entry-symbol.s
index 05796d6b4b73..f4ead9cb8556 100644
--- a/lld/test/MachO/entry-symbol.s
+++ b/lld/test/MachO/entry-symbol.s
@@ -4,10 +4,12 @@
 # RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/libfoo.s -o %t/libfoo.o
 # RUN: %lld -lSystem -dylib %t/libfoo.o -o %t/libfoo.dylib
 
-# RUN: %lld -o %t/not-main %t/not-main.o -e _not_main
+# RUN: %lld -lSystem -o %t/not-main %t/not-main.o -e _not_main
 # RUN: llvm-objdump --macho --all-headers --syms %t/not-main | FileCheck %s
+
 # CHECK-LABEL: SYMBOL TABLE
-# CHECK-NEXT: {{0*}}[[#%x, ENTRY_ADDR:]] {{.*}} __TEXT,__text _not_main
+# CHECK:       {{0*}}[[#%x, ENTRY_ADDR:]] {{.*}} __TEXT,__text _not_main
+
 # CHECK:      sectname __text
 # CHECK-NEXT: segname __TEXT
 ## Note: the following checks assume that the entry symbol is right at the
@@ -46,6 +48,10 @@
 # WEAK-DYSYM-NEXT:  segment  section          address  type     addend  symbol
 # WEAK-DYSYM-NEXT:  __DATA   __la_symbol_ptr  {{.*}}   pointer       0  _weak_dysym_main
 
+# RUN: %lld -dylib -lSystem -o %t/dysym-main.dylib %t/not-main.o %t/libfoo.dylib -e _dysym_main
+# RUN: llvm-objdump --macho --indirect-symbols --lazy-bind %t/dysym-main.dylib | FileCheck %s --check-prefix=DYLIB-NO-MAIN
+# DYLIB-NO-MAIN-NOT: _dysym_main
+
 # RUN: not %lld -o /dev/null %t/not-main.o -e _missing 2>&1 | FileCheck %s --check-prefix=UNDEFINED
 # UNDEFINED: error: undefined symbol: _missing
 # RUN: not %lld -o /dev/null %t/not-main.o 2>&1 | FileCheck %s --check-prefix=DEFAULT-ENTRY


        


More information about the llvm-commits mailing list