Skip to content

Commit 66b7ed5

Browse files
committed
enable optimization, DEP and ASLR if building with gcc
optimization, data execution prevention and address space layout randomization all seem like Good Things To Have Enabled
1 parent 84b3e80 commit 66b7ed5

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

CMakeLists.txt

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,11 +197,27 @@ if(WIN32 AND MSVC)
197197
set( CMAKE_DEBUG_POSTFIX "d" ) # little effect in unix
198198
else()
199199
# add any gcc flags
200+
201+
set ( CFLAGS "${CFLAGS} -O2")
202+
# -O2: GCC performs nearly all supported optimizations that do not involve
203+
# a space-speed tradeoff. As compared to '-O', this option increases
204+
# both compilation time and the performance of the generated code.
205+
206+
set ( LDFLAGS "${LDFLAGS} -Wl,--nxcompat")
207+
# https://en.wikipedia.org/wiki/Data_Execution_Prevention
208+
# Enable DEP with -Wl,--nxcompat
209+
210+
set ( LDFLAGS "-Wl,--dynamicbase,--export-all-symbols")
211+
# https://en.wikipedia.org/wiki/Address_space_layout_randomization
212+
# https://stackoverflow.com/questions/24283918/how-can-i-enable-aslr-dep-and-safeseh-on-an-exe-in-codeblocks-using-mingw
213+
# ASLR with gcc has a problem: -Wl,--dynamicbase doesn't emit the necessary relocation table.
214+
# As a workaround, you can pass -Wl,--dynamicbase,--export-all-symbols
215+
200216
endif()
201217

202218
set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${WARNING_FLAGS} ${MSVC_FLAGS} -D_REENTRANT" )
203219
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${WARNING_FLAGS} ${MSVC_FLAGS} -D_REENTRANT" )
204-
set( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${MSVC_LD_FLAGS}" )
220+
set( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${LDFLAGS} ${MSVC_LD_FLAGS}" )
205221

206222

207223
#------------------------------------------------------------------------

0 commit comments

Comments
 (0)