[PATCH] D19519: Enable "Optimized Debugging" and Enable "Control Flow Guard" in MSVC builds

Alexander Riccio via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 25 18:29:29 PDT 2016


ariccio created this revision.
ariccio added a subscriber: llvm-commits.

This patch turns on two of VS2015's new features: Optimized Debugging & Control Flow Guard.

Optimized Debugging makes it much easier to debug/profile optimized MSVC binaries, and it is a bit like [[ https://gcc.gnu.org/onlinedocs/gcc/Debugging-Options.html | GCC's `-g3` option ]]. I've enabled it for all builds, so that we can enable some optimizations for debug builds in the future without worrying about debuggability.

[[ https://msdn.microsoft.com/en-us/library/windows/desktop/mt637065.aspx | Control Flow Guard ]] is a security feature that hardens against memory corruption, has negligible performance impact, and is backwards compatible. Enabling it is like enabling DEP, ASLR, or /GS stack checking.

http://reviews.llvm.org/D19519

Files:
  llvm/cmake/modules/HandleLLVMOptions.cmake

Index: llvm/cmake/modules/HandleLLVMOptions.cmake
===================================================================
--- llvm/cmake/modules/HandleLLVMOptions.cmake
+++ llvm/cmake/modules/HandleLLVMOptions.cmake
@@ -360,6 +360,38 @@
     append("/Zc:strictStrings" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
   endif(NOT (MSVC_VERSION LESS 1900))
 
+  # /Zo (Enhance Optimized Debugging) was introduced with Visual Studio 
+  # 2013 update 3. Instead of only enabling them in VS2013 update 3, we'll just
+  # enable them for Visual Studio 2015 (VS 14, MSVC_VERSION 1900) and up.
+  if (NOT (MSVC_VERSION LESS 1900))
+    # "The /Zo compiler switch generates enhanced debugging information for
+    # optimized code. Optimization may use registers for local variables,
+    # reorder code, vectorize loops, and inline function calls. These
+    # optimizations can obscure the relationship between the source code and
+    # the compiled object code. The /Zo switch tells the compiler to generate
+    # additional debugging information for local variables and inlined
+    # functions. It also enables stack traces to show inlined functions in the
+    # WinDBG debugger.
+    # Debug builds that have disabled optimizations (/Od) do not need the
+    # additional debugging information generated when /Zo is specified."
+    append("/Zo" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
+  endif(NOT (MSVC_VERSION LESS 1900))
+
+  # /guard (Enable Control Flow Guard) was introduced with Visual Studio 
+  # 2015.
+  if (NOT (MSVC_VERSION LESS 1900))
+    # "The /guard:cf option causes the compiler to analyze control flow for
+    # indirect call targets at compile time, and then to insert code to
+    # verify the targets at runtime."
+    append("/guard:cf" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
+    # "The /guard:cf option must be passed to both the compiler and linker
+    # to build code that uses the CFG exploit mitigation technique."
+    append("/guard:cf"
+      CMAKE_EXE_LINKER_FLAGS
+      CMAKE_MODULE_LINKER_FLAGS
+      CMAKE_SHARED_LINKER_FLAGS)
+  endif(NOT (MSVC_VERSION LESS 1900))
+
   # "Generate Intrinsic Functions".
   append("/Oi" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D19519.54934.patch
Type: text/x-patch
Size: 2160 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160426/a916385a/attachment.bin>


More information about the llvm-commits mailing list