[PATCH] [lld][WinLinkDriver]Add support for /dynamicbase

Ron Ofir ron.ofir at gmail.com
Fri Aug 23 14:54:59 PDT 2013


Hi ruiu,

This patch separates /fixed (no .reloc section) and /dynamicbase (enable ASLR) in order to match link.exe's behaviour.

http://llvm-reviews.chandlerc.com/D1495

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

Index: include/lld/ReaderWriter/PECOFFLinkingContext.h
===================================================================
--- include/lld/ReaderWriter/PECOFFLinkingContext.h
+++ include/lld/ReaderWriter/PECOFFLinkingContext.h
@@ -29,7 +29,8 @@
         _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) {}
@@ -91,6 +92,9 @@
 
   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 @@
   bool _largeAddressAware;
   bool _baseRelocationEnabled;
   bool _terminalServerAware;
+  bool _dynamicBaseEnabled;
 
   std::vector<StringRef> _inputSearchPaths;
   mutable std::unique_ptr<Reader> _reader;
Index: lib/Driver/WinLinkDriver.cpp
===================================================================
--- lib/Driver/WinLinkDriver.cpp
+++ lib/Driver/WinLinkDriver.cpp
@@ -350,9 +350,23 @@
 
     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:
       // handle /tsaware
       ctx.setTerminalServerAware(true);
Index: lib/Driver/WinLinkOptions.td
===================================================================
--- lib/Driver/WinLinkOptions.td
+++ lib/Driver/WinLinkOptions.td
@@ -42,6 +42,9 @@
 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">;
Index: lib/ReaderWriter/PECOFF/WriterPECOFF.cpp
===================================================================
--- lib/ReaderWriter/PECOFF/WriterPECOFF.cpp
+++ lib/ReaderWriter/PECOFF/WriterPECOFF.cpp
@@ -204,7 +204,7 @@
           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;
 
Index: test/pecoff/base-reloc.test
===================================================================
--- test/pecoff/base-reloc.test
+++ test/pecoff/base-reloc.test
@@ -19,14 +19,12 @@
 
 # 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
Index: test/pecoff/dynamicbase.test
===================================================================
--- test/pecoff/dynamicbase.test
+++ test/pecoff/dynamicbase.test
@@ -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
Index: unittests/DriverTests/WinLinkDriverTest.cpp
===================================================================
--- unittests/DriverTests/WinLinkDriverTest.cpp
+++ unittests/DriverTests/WinLinkDriverTest.cpp
@@ -53,6 +53,7 @@
   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, 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 @@
   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();
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D1495.1.patch
Type: text/x-patch
Size: 6771 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20130823/1b72e081/attachment.bin>


More information about the llvm-commits mailing list