diff --git a/lld/lib/ReaderWriter/MachO/MachOLinkingContext.cpp b/lld/lib/ReaderWriter/MachO/MachOLinkingContext.cpp index 8697b00d3707..4ef1b625c4cc 100644 --- a/lld/lib/ReaderWriter/MachO/MachOLinkingContext.cpp +++ b/lld/lib/ReaderWriter/MachO/MachOLinkingContext.cpp @@ -455,6 +455,22 @@ bool MachOLinkingContext::validateImpl(raw_ostream &diagnostics) { addInitialUndefinedSymbol(symbol.getKey()); } + // If -dead_strip, set up initial live symbols. + if (deadStrip()) { + // Entry point is live. + if (outputTypeHasEntry()) + addDeadStripRoot(entrySymbolName()); + // Lazy binding helper is live. + if (needsStubsPass()) + addDeadStripRoot(binderSymbolName()); + // If using -exported_symbols_list, make all exported symbols live. + if (_exportMode == ExportMode::whiteList) { + _globalsAreDeadStripRoots = false; + for (const auto &symbol : _exportedSymbols) + addDeadStripRoot(symbol.getKey()); + } + } + return true; } diff --git a/lld/test/mach-o/exported_symbols_list-dylib.yaml b/lld/test/mach-o/exported_symbols_list-dylib.yaml index d75458a8acca..71121d7400f6 100644 --- a/lld/test/mach-o/exported_symbols_list-dylib.yaml +++ b/lld/test/mach-o/exported_symbols_list-dylib.yaml @@ -13,6 +13,11 @@ # RUN: -unexported_symbol _bar -unexported_symbol _a && \ # RUN: llvm-nm -m %t3 | FileCheck %s # +# RUN: lld -flavor darwin -arch x86_64 -macosx_version_min 10.8 -dylib \ +# RUN: %s %p/Inputs/libSystem.yaml -dead_strip -o %t \ +# RUN: -exported_symbols_list %p/Inputs/exported_symbols_list.exp && \ +# RUN: llvm-nm -m %t | FileCheck -check-prefix=CHECK_DEAD %s +# # Test -exported_symbols_list and -exported_symbol properly changes visibility. # @@ -65,3 +70,8 @@ global-symbols: # CHECK: (__DATA,__data) external _b # CHECK: (__TEXT,__text) non-external (was a private external) _bar # CHECK: (__TEXT,__text) external _foo + +# CHECK_DEAD-NOT: (__DATA,__data) non-external (was a private external) _a +# CHECK_DEAD: (__DATA,__data) external _b +# CHECK_DEAD-NOT: (__TEXT,__text) non-external (was a private external) _bar +# CHECK_DEAD: (__TEXT,__text) external _foo diff --git a/lld/test/mach-o/hello-world-x86_64.yaml b/lld/test/mach-o/hello-world-x86_64.yaml index 689a5d2020cc..9cb7a1a97e29 100644 --- a/lld/test/mach-o/hello-world-x86_64.yaml +++ b/lld/test/mach-o/hello-world-x86_64.yaml @@ -1,6 +1,9 @@ # RUN: lld -flavor darwin -arch x86_64 -macosx_version_min 10.8 %s -o %t && \ # RUN: llvm-nm -m -n %t | FileCheck %s # +# RUN: lld -flavor darwin -arch x86_64 -macosx_version_min 10.8 %s -dead_strip \ +# RUN: -o %t2 && llvm-nm -m -n %t2 | FileCheck %s +# # Test that x86_64 hello-world can be linked into a mach-o executable # diff --git a/lld/unittests/DriverTests/DarwinLdDriverTest.cpp b/lld/unittests/DriverTests/DarwinLdDriverTest.cpp index 3c17ca9e4cb1..cf0da9dd099a 100644 --- a/lld/unittests/DriverTests/DarwinLdDriverTest.cpp +++ b/lld/unittests/DriverTests/DarwinLdDriverTest.cpp @@ -79,17 +79,18 @@ TEST_F(DarwinLdParserTest, OutputPath) { } TEST_F(DarwinLdParserTest, DeadStrip) { - EXPECT_TRUE(parse("ld", "-dead_strip", "foo.o", nullptr)); + EXPECT_TRUE(parse("ld", "-arch", "x86_64", "-dead_strip", "foo.o", nullptr)); EXPECT_TRUE(_context.deadStrip()); } TEST_F(DarwinLdParserTest, DeadStripRootsExe) { - EXPECT_TRUE(parse("ld", "-dead_strip", "foo.o", nullptr)); + EXPECT_TRUE(parse("ld", "-arch", "x86_64", "-dead_strip", "foo.o", nullptr)); EXPECT_FALSE(_context.globalsAreDeadStripRoots()); } TEST_F(DarwinLdParserTest, DeadStripRootsDylib) { - EXPECT_TRUE(parse("ld", "-dylib", "-dead_strip", "foo.o", nullptr)); + EXPECT_TRUE(parse("ld", "-arch", "x86_64", "-dylib", "-dead_strip", "foo.o", + nullptr)); EXPECT_TRUE(_context.globalsAreDeadStripRoots()); }