[lld] r258554 - Add support for export_dynamic cmdline option and behaviour.
Pete Cooper via llvm-commits
llvm-commits at lists.llvm.org
Fri Jan 22 13:13:25 PST 2016
Author: pete
Date: Fri Jan 22 15:13:24 2016
New Revision: 258554
URL: http://llvm.org/viewvc/llvm-project?rev=258554&view=rev
Log:
Add support for export_dynamic cmdline option and behaviour.
This option matches the behaviour of ld64, that is it prevents globals
from being dead stripped in executables and dylibs.
Reviewed by Lang Hames
Differential Revision: http://reviews.llvm.org/D16026
Added:
lld/trunk/test/mach-o/dead-strip-globals.yaml
Modified:
lld/trunk/include/lld/ReaderWriter/MachOLinkingContext.h
lld/trunk/lib/Driver/DarwinLdDriver.cpp
lld/trunk/lib/Driver/DarwinLdOptions.td
lld/trunk/lib/ReaderWriter/MachO/MachOLinkingContext.cpp
lld/trunk/unittests/DriverTests/DarwinLdDriverTest.cpp
Modified: lld/trunk/include/lld/ReaderWriter/MachOLinkingContext.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/include/lld/ReaderWriter/MachOLinkingContext.h?rev=258554&r1=258553&r2=258554&view=diff
==============================================================================
--- lld/trunk/include/lld/ReaderWriter/MachOLinkingContext.h (original)
+++ lld/trunk/include/lld/ReaderWriter/MachOLinkingContext.h Fri Jan 22 15:13:24 2016
@@ -84,7 +84,8 @@ public:
/// Initializes the context to sane default values given the specified output
/// file type, arch, os, and minimum os version. This should be called before
/// other setXXX() methods.
- void configure(HeaderFileType type, Arch arch, OS os, uint32_t minOSVersion);
+ void configure(HeaderFileType type, Arch arch, OS os, uint32_t minOSVersion,
+ bool exportDynamicSymbols);
void addPasses(PassManager &pm) override;
bool validateImpl(raw_ostream &diagnostics) override;
Modified: lld/trunk/lib/Driver/DarwinLdDriver.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/Driver/DarwinLdDriver.cpp?rev=258554&r1=258553&r2=258554&view=diff
==============================================================================
--- lld/trunk/lib/Driver/DarwinLdDriver.cpp (original)
+++ lld/trunk/lib/Driver/DarwinLdDriver.cpp Fri Jan 22 15:13:24 2016
@@ -385,9 +385,16 @@ bool DarwinLdDriver::parse(llvm::ArrayRe
// No min-os version on command line, check environment variables
}
+ // Handle export_dynamic
+ // FIXME: Should we warn when this applies to something other than a static
+ // executable or dylib? Those are the only cases where this has an effect.
+ // Note, this has to come before ctx.configure() so that we get the correct
+ // value for _globalsAreDeadStripRoots.
+ bool exportDynamicSymbols = parsedArgs.hasArg(OPT_export_dynamic);
+
// Now that there's enough information parsed in, let the linking context
// set up default values.
- ctx.configure(fileType, arch, os, minOSVersion);
+ ctx.configure(fileType, arch, os, minOSVersion, exportDynamicSymbols);
// Handle -e xxx
if (llvm::opt::Arg *entry = parsedArgs.getLastArg(OPT_entry))
Modified: lld/trunk/lib/Driver/DarwinLdOptions.td
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/Driver/DarwinLdOptions.td?rev=258554&r1=258553&r2=258554&view=diff
==============================================================================
--- lld/trunk/lib/Driver/DarwinLdOptions.td (original)
+++ lld/trunk/lib/Driver/DarwinLdOptions.td Fri Jan 22 15:13:24 2016
@@ -84,6 +84,9 @@ def stack_size : Separate<["-"], "stack_
HelpText<"Specifies the maximum stack size for the main thread in a program. "
"Must be a page-size multiple. (default=8Mb)">,
Group<grp_main>;
+def export_dynamic : Flag<["-"], "export_dynamic">,
+ HelpText<"Preserves all global symbols in main executables during LTO">,
+ Group<grp_main>;
// dylib executable options
def grp_dylib : OptionGroup<"opts">, HelpText<"DYLIB EXECUTABLE OPTIONS">;
Modified: lld/trunk/lib/ReaderWriter/MachO/MachOLinkingContext.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/MachO/MachOLinkingContext.cpp?rev=258554&r1=258553&r2=258554&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/MachO/MachOLinkingContext.cpp (original)
+++ lld/trunk/lib/ReaderWriter/MachO/MachOLinkingContext.cpp Fri Jan 22 15:13:24 2016
@@ -155,7 +155,8 @@ MachOLinkingContext::MachOLinkingContext
MachOLinkingContext::~MachOLinkingContext() {}
void MachOLinkingContext::configure(HeaderFileType type, Arch arch, OS os,
- uint32_t minOSVersion) {
+ uint32_t minOSVersion,
+ bool exportDynamicSymbols) {
_outputMachOType = type;
_arch = arch;
_os = os;
@@ -218,9 +219,10 @@ void MachOLinkingContext::configure(Head
case OS::unknown:
break;
}
+ setGlobalsAreDeadStripRoots(exportDynamicSymbols);
break;
case llvm::MachO::MH_DYLIB:
- setGlobalsAreDeadStripRoots(true);
+ setGlobalsAreDeadStripRoots(exportDynamicSymbols);
break;
case llvm::MachO::MH_BUNDLE:
break;
Added: lld/trunk/test/mach-o/dead-strip-globals.yaml
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/mach-o/dead-strip-globals.yaml?rev=258554&view=auto
==============================================================================
--- lld/trunk/test/mach-o/dead-strip-globals.yaml (added)
+++ lld/trunk/test/mach-o/dead-strip-globals.yaml Fri Jan 22 15:13:24 2016
@@ -0,0 +1,27 @@
+# RUN: lld -flavor darwin -arch x86_64 -dead_strip -export_dynamic %s -dylib %p/Inputs/libSystem.yaml -o %t.dylib -print_atoms | FileCheck -check-prefix=CHECK1 %s
+# RUN: lld -flavor darwin -arch x86_64 -export_dynamic -dead_strip %s -dylib %p/Inputs/libSystem.yaml -o %t.dylib -print_atoms | FileCheck -check-prefix=CHECK1 %s
+# RUN: lld -flavor darwin -arch x86_64 -dead_strip %s -dylib %p/Inputs/libSystem.yaml -o %t2.dylib -print_atoms | FileCheck -check-prefix=CHECK2 %s
+
+#
+# Test that -export_dynamic -dead-strip from removing globals.
+#
+
+---
+defined-atoms:
+ - name: def
+ scope: global
+ dead-strip: never
+ - name: dead
+ scope: global
+shared-library-atoms:
+ - name: dyld_stub_binder
+ load-name: /usr/lib/libSystem.B.dylib
+ type: unknown
+...
+
+# CHECK1: name: def
+# CHECK1: name: dead
+
+# CHECK2: name: def
+# CHECK2-NOT: name: dead
+
Modified: lld/trunk/unittests/DriverTests/DarwinLdDriverTest.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/unittests/DriverTests/DarwinLdDriverTest.cpp?rev=258554&r1=258553&r2=258554&view=diff
==============================================================================
--- lld/trunk/unittests/DriverTests/DarwinLdDriverTest.cpp (original)
+++ lld/trunk/unittests/DriverTests/DarwinLdDriverTest.cpp Fri Jan 22 15:13:24 2016
@@ -84,9 +84,34 @@ TEST_F(DarwinLdParserTest, DeadStripRoot
TEST_F(DarwinLdParserTest, DeadStripRootsDylib) {
EXPECT_TRUE(parse("ld", "-arch", "x86_64", "-dylib", "-dead_strip", "foo.o",
nullptr));
+ EXPECT_FALSE(_ctx.globalsAreDeadStripRoots());
+}
+
+TEST_F(DarwinLdParserTest, DeadStripRootsRelocatable) {
+ EXPECT_TRUE(parse("ld", "-arch", "x86_64", "-r", "-dead_strip", "foo.o",
+ nullptr));
+ EXPECT_FALSE(_ctx.globalsAreDeadStripRoots());
+}
+
+TEST_F(DarwinLdParserTest, DeadStripRootsExportDynamicExe) {
+ EXPECT_TRUE(parse("ld", "-arch", "x86_64", "-dead_strip",
+ "-export_dynamic", "foo.o", nullptr));
EXPECT_TRUE(_ctx.globalsAreDeadStripRoots());
}
+TEST_F(DarwinLdParserTest, DeadStripRootsExportDynamicDylib) {
+ EXPECT_TRUE(parse("ld", "-arch", "x86_64", "-dylib", "-dead_strip",
+ "-export_dynamic", "foo.o",
+ nullptr));
+ EXPECT_TRUE(_ctx.globalsAreDeadStripRoots());
+}
+
+TEST_F(DarwinLdParserTest, DeadStripRootsExportDynamicRelocatable) {
+ EXPECT_TRUE(parse("ld", "-arch", "x86_64", "-r", "-dead_strip",
+ "-export_dynamic", "foo.o", nullptr));
+ EXPECT_FALSE(_ctx.globalsAreDeadStripRoots());
+}
+
TEST_F(DarwinLdParserTest, Arch) {
EXPECT_TRUE(parse("ld", "-arch", "x86_64", "foo.o", nullptr));
EXPECT_EQ(MachOLinkingContext::arch_x86_64, _ctx.arch());
More information about the llvm-commits
mailing list