[lld] r189159 - Separates /fixed (no .reloc section) and /dynamicbase (enable ASLR)

Rui Ueyama ruiu at google.com
Fri Aug 23 17:39:11 PDT 2013


Author: ruiu
Date: Fri Aug 23 19:39:10 2013
New Revision: 189159

URL: http://llvm.org/viewvc/llvm-project?rev=189159&view=rev
Log:
Separates /fixed (no .reloc section) and /dynamicbase (enable ASLR)
in order to match link.exe's behaviour.

Patch by Ron Ofir.

Added:
    lld/trunk/test/pecoff/dynamicbase.test
Modified:
    lld/trunk/include/lld/ReaderWriter/PECOFFLinkingContext.h
    lld/trunk/lib/Driver/WinLinkDriver.cpp
    lld/trunk/lib/Driver/WinLinkOptions.td
    lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp
    lld/trunk/test/pecoff/base-reloc.test
    lld/trunk/unittests/DriverTests/WinLinkDriverTest.cpp

Modified: lld/trunk/include/lld/ReaderWriter/PECOFFLinkingContext.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/include/lld/ReaderWriter/PECOFFLinkingContext.h?rev=189159&r1=189158&r2=189159&view=diff
==============================================================================
--- lld/trunk/include/lld/ReaderWriter/PECOFFLinkingContext.h (original)
+++ lld/trunk/include/lld/ReaderWriter/PECOFFLinkingContext.h Fri Aug 23 19:39:10 2013
@@ -29,7 +29,8 @@ public:
         _heapReserve(1024 * 1024), _heapCommit(4096),
         _subsystem(llvm::COFF::IMAGE_SUBSYSTEM_UNKNOWN), _minOSVersion(6, 0),
         _nxCompat(true), _largeAddressAware(false),
-        _baseRelocationEnabled(true), _terminalServerAware(true) {}
+        _baseRelocationEnabled(true), _terminalServerAware(true),
+        _dynamicBaseEnabled(true) {}
 
   struct OSVersion {
     OSVersion(int v1, int v2) : majorVersion(v1), minorVersion(v2) {}
@@ -92,6 +93,9 @@ public:
   void setTerminalServerAware(bool val) { _terminalServerAware = val; }
   bool isTerminalServerAware() const { return _terminalServerAware; }
 
+  void setDynamicBaseEnabled(bool val) { _dynamicBaseEnabled = val; }
+  bool getDynamicBaseEnabled() const { return _dynamicBaseEnabled; }
+
   virtual ErrorOr<Reference::Kind> relocKindFromString(StringRef str) const;
   virtual ErrorOr<std::string> stringFromRelocKind(Reference::Kind kind) const;
 
@@ -123,6 +127,7 @@ private:
   bool _largeAddressAware;
   bool _baseRelocationEnabled;
   bool _terminalServerAware;
+  bool _dynamicBaseEnabled;
 
   std::vector<StringRef> _inputSearchPaths;
   mutable std::unique_ptr<Reader> _reader;

Modified: lld/trunk/lib/Driver/WinLinkDriver.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/Driver/WinLinkDriver.cpp?rev=189159&r1=189158&r2=189159&view=diff
==============================================================================
--- lld/trunk/lib/Driver/WinLinkDriver.cpp (original)
+++ lld/trunk/lib/Driver/WinLinkDriver.cpp Fri Aug 23 19:39:10 2013
@@ -350,7 +350,20 @@ bool WinLinkDriver::parse(int argc, cons
 
     case OPT_fixed:
       // handle /fixed
+
+      // make sure /dynamicbase wasn't specified
+      if (parsedArgs->getLastArg(OPT_dynamicbase)) {
+        diagnostics << "/dynamicbase must not be specified with /fixed\n";
+        return true;
+      }
+
       ctx.setBaseRelocationEnabled(false);
+      ctx.setDynamicBaseEnabled(false);
+      break;
+
+    case OPT_no_dynamicbase:
+      // handle /dynamicbase:no
+      ctx.setDynamicBaseEnabled(false);
       break;
 
     case OPT_tsaware:

Modified: lld/trunk/lib/Driver/WinLinkOptions.td
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/Driver/WinLinkOptions.td?rev=189159&r1=189158&r2=189159&view=diff
==============================================================================
--- lld/trunk/lib/Driver/WinLinkOptions.td (original)
+++ lld/trunk/lib/Driver/WinLinkOptions.td Fri Aug 23 19:39:10 2013
@@ -42,6 +42,9 @@ def no_largeaddressaware : F<"largeaddre
 def fixed : F<"fixed">;
 def no_fixed : F<"fixed:no">, HelpText<"Enable base relocations">;
 
+def dynamicbase : F<"dynamicbase">;
+def no_dynamicbase : F<"dynamicbase:no">, HelpText<"Disable address space layout randomization">;
+
 def tsaware : F<"tsaware">;
 def no_tsaware : F<"tsaware:no">,
     HelpText<"Create non-Terminal Server aware executable">;

Modified: lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp?rev=189159&r1=189158&r2=189159&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp (original)
+++ lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp Fri Aug 23 19:39:10 2013
@@ -204,7 +204,7 @@ public:
           llvm::COFF::IMAGE_DLL_CHARACTERISTICS_TERMINAL_SERVER_AWARE;
     if (context.isNxCompat())
       dllCharacteristics |= llvm::COFF::IMAGE_DLL_CHARACTERISTICS_NX_COMPAT;
-    if (context.getBaseRelocationEnabled())
+    if (context.getDynamicBaseEnabled())
       dllCharacteristics |= llvm::COFF::IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE;
     _peHeader.DLLCharacteristics = dllCharacteristics;
 

Modified: lld/trunk/test/pecoff/base-reloc.test
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/pecoff/base-reloc.test?rev=189159&r1=189158&r2=189159&view=diff
==============================================================================
--- lld/trunk/test/pecoff/base-reloc.test (original)
+++ lld/trunk/test/pecoff/base-reloc.test Fri Aug 23 19:39:10 2013
@@ -19,14 +19,12 @@ NOBASEREL-SECTION-NOT: Contents of secti
 
 # RUN: lld -flavor link /out:%t1 /subsystem:console /force -- %t.obj \
 # RUN:   && llvm-readobj -file-headers %t1 \
-# RUN:   | FileCheck %s --check-prefix=BASEREL1
+# RUN:   | FileCheck %s --check-prefix=BASEREL-HEADER
 #
 # RUN: lld -flavor link /out:%t1 /subsystem:console /force /fixed -- %t.obj \
 # RUN:   && llvm-readobj -file-headers %t1 \
-# RUN:   | FileCheck %s --check-prefix=BASEREL2
+# RUN:   | FileCheck %s --check-prefix=NOBASEREL-HEADER
 
-BASEREL1-NOT: IMAGE_FILE_RELOCS_STRIPPED
-BASEREL1: IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE
+BASEREL-HEADER-NOT: IMAGE_FILE_RELOCS_STRIPPED
 
-BASEREL2: IMAGE_FILE_RELOCS_STRIPPED
-BASEREL2-NOT: IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE
+NOBASEREL-HEADER: IMAGE_FILE_RELOCS_STRIPPED

Added: lld/trunk/test/pecoff/dynamicbase.test
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/pecoff/dynamicbase.test?rev=189159&view=auto
==============================================================================
--- lld/trunk/test/pecoff/dynamicbase.test (added)
+++ lld/trunk/test/pecoff/dynamicbase.test Fri Aug 23 19:39:10 2013
@@ -0,0 +1,22 @@
+# RUN: yaml2obj %p/Inputs/hello.obj.yaml > %t.obj
+#
+# RUN: lld -flavor link /out:%t1 /subsystem:console /force -- %t.obj \
+# RUN:   && llvm-readobj -file-headers %t1 \
+# RUN:   | FileCheck %s --check-prefix=DYNAMICBASE
+#
+# RUN: lld -flavor link /out:%t1 /subsystem:console /force /dynamicbase:no \
+# RUN:   -- %t.obj && llvm-readobj -file-headers %t1 \
+# RUN:   | FileCheck %s --check-prefix=NODYNAMICBASE
+#
+# RUN: lld -flavor link /out:%t1 /subsystem:console /force /fixed -- %t.obj \
+# RUN:   && llvm-readobj -file-headers %t1 \
+# RUN:   | FileCheck %s --check-prefix=NODYNAMICBASE
+#
+# RUN: not lld -flavor link /out:%t1 /subsystem:console /force /fixed \
+# RUN:   /dynamicbase -- %t.obj 2>&1 | FileCheck %s --check-prefix=DYNAMIC-AND-FIXED
+
+DYNAMICBASE: IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE
+
+NODYNAMICBASE-NOT: IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE
+
+DYNAMIC-AND-FIXED: /dynamicbase must not be specified with /fixed

Modified: lld/trunk/unittests/DriverTests/WinLinkDriverTest.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/unittests/DriverTests/WinLinkDriverTest.cpp?rev=189159&r1=189158&r2=189159&view=diff
==============================================================================
--- lld/trunk/unittests/DriverTests/WinLinkDriverTest.cpp (original)
+++ lld/trunk/unittests/DriverTests/WinLinkDriverTest.cpp Fri Aug 23 19:39:10 2013
@@ -53,6 +53,7 @@ TEST_F(WinLinkParserTest, Basic) {
   EXPECT_FALSE(_context.getLargeAddressAware());
   EXPECT_TRUE(_context.getBaseRelocationEnabled());
   EXPECT_TRUE(_context.isTerminalServerAware());
+  EXPECT_TRUE(_context.getDynamicBaseEnabled());
   EXPECT_TRUE(_context.initialUndefinedSymbols().empty());
 }
 
@@ -171,6 +172,7 @@ TEST_F(WinLinkParserTest, NoLargeAddress
 TEST_F(WinLinkParserTest, Fixed) {
   EXPECT_FALSE(parse("link.exe", "/fixed", "a.out", nullptr));
   EXPECT_FALSE(_context.getBaseRelocationEnabled());
+  EXPECT_FALSE(_context.getDynamicBaseEnabled());
 }
 
 TEST_F(WinLinkParserTest, NoFixed) {
@@ -188,6 +190,16 @@ TEST_F(WinLinkParserTest, NoTerminalServ
   EXPECT_FALSE(_context.isTerminalServerAware());
 }
 
+TEST_F(WinLinkParserTest, DynamicBase) {
+  EXPECT_FALSE(parse("link.exe", "/dynamicbase", "a.out", nullptr));
+  EXPECT_TRUE(_context.getDynamicBaseEnabled());
+}
+
+TEST_F(WinLinkParserTest, NoDynamicBase) {
+  EXPECT_FALSE(parse("link.exe", "/dynamicbase:no", "a.out", nullptr));
+  EXPECT_FALSE(_context.getDynamicBaseEnabled());
+}
+
 TEST_F(WinLinkParserTest, Include) {
   EXPECT_FALSE(parse("link.exe", "/include:foo", "a.out", nullptr));
   auto symbols = _context.initialUndefinedSymbols();





More information about the llvm-commits mailing list