Skip to content

Commit

Permalink
Fix relro, now and PIE for host and libraries (#685)
Browse files Browse the repository at this point in the history
* Fix relro, now and PIE for host and libraries

The former core-setup and corefx native code build was missing the
-z,relro and -z,now options and also the position independent related
settings.

* Reflect PR feedback
  • Loading branch information
janvorli committed Dec 10, 2019
1 parent a128330 commit 51470b8
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 14 deletions.
1 change: 0 additions & 1 deletion src/installer/corehost/cli/common.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ if(WIN32)
add_compile_options($<$<CONFIG:Release>:/MT>)
add_compile_options($<$<CONFIG:Debug>:/MTd>)
else()
add_compile_options(-fPIC)
add_compile_options(-fvisibility=hidden)
endif()

Expand Down
3 changes: 3 additions & 0 deletions src/installer/corehost/cli/exe.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

project (${DOTNET_PROJECT_NAME})

cmake_policy(SET CMP0011 NEW)
cmake_policy(SET CMP0083 NEW)

include(${CMAKE_CURRENT_LIST_DIR}/common.cmake)

# Include directories
Expand Down
1 change: 0 additions & 1 deletion src/installer/corehost/cli/test_fx_ver/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ if(WIN32)
add_compile_options($<$<CONFIG:Release>:/MT>)
add_compile_options($<$<CONFIG:Debug>:/MTd>)
else()
add_compile_options(-fPIE)
add_compile_options(-fvisibility=hidden)
endif()

Expand Down
19 changes: 14 additions & 5 deletions src/installer/settings.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@

set (CMAKE_CXX_STANDARD 11)

include(CheckPIESupported)

# All code we build should be compiled as position independent
check_pie_supported(OUTPUT_VARIABLE PIE_SUPPORT_OUTPUT LANGUAGES CXX)
if(NOT MSVC AND NOT CMAKE_CXX_LINK_PIE_SUPPORTED)
message(WARNING "PIE is not supported at link time: ${PIE_SUPPORT_OUTPUT}.\n"
"PIE link options will not be passed to linker.")
endif()

set(CMAKE_POSITION_INDEPENDENT_CODE ON)

if(CMAKE_SYSTEM_NAME STREQUAL Linux)
set(CLR_CMAKE_PLATFORM_UNIX 1)
message("System name Linux")
Expand Down Expand Up @@ -174,7 +185,7 @@ if(WIN32)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /GUARD:CF")

# Debug build specific flags
set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "/NOVCFEATURE")
set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} /NOVCFEATURE")

# Release build specific flags
set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /DEBUG /OPT:REF /OPT:ICF")
Expand Down Expand Up @@ -216,14 +227,12 @@ endif()
# containing the reference instead of using definitions from other modules.
if(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Xlinker -Bsymbolic -Bsymbolic-functions")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--build-id=sha1")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--build-id=sha1")
add_link_options(-Wl,--build-id=sha1 -Wl,-z,relro,-z,now)
add_compile_options(-fstack-protector-strong)
elseif(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
add_compile_options(-fstack-protector)
elseif(${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fuse-ld=lld -Xlinker --build-id=sha1")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=lld -Xlinker --build-id=sha1")
add_link_options(-fuse-ld=lld -Wl,--build-id=sha1 -Wl,-z,relro,-z,now)
add_compile_options(-fstack-protector)
endif()

Expand Down
26 changes: 19 additions & 7 deletions src/libraries/Native/Unix/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
cmake_minimum_required(VERSION 2.8.12)
project(CoreFX C)

cmake_policy(SET CMP0083 NEW)

include(CheckPIESupported)

# All code we build should be compiled as position independent
check_pie_supported(OUTPUT_VARIABLE PIE_SUPPORT_OUTPUT LANGUAGES C)
if(NOT MSVC AND NOT CMAKE_C_LINK_PIE_SUPPORTED)
message(WARNING "PIE is not supported at link time: ${PIE_SUPPORT_OUTPUT}.\n"
"PIE link options will not be passed to linker.")
endif()
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

set(CMAKE_MACOSX_RPATH ON)
set(CMAKE_INSTALL_PREFIX $ENV{__CMakeBinDir})
set(CMAKE_INCLUDE_CURRENT_DIR ON)
Expand Down Expand Up @@ -28,7 +40,6 @@ endif()
add_compile_options(-Werror)

if(CMAKE_SYSTEM_NAME STREQUAL Emscripten)
# Build a static library so no -fPIC
set(CLR_CMAKE_PLATFORM_WASM 1)
add_definitions(-D_WASM_)
# The emscripten build has additional warnings so -Werror breaks
Expand All @@ -37,7 +48,6 @@ if(CMAKE_SYSTEM_NAME STREQUAL Emscripten)
add_compile_options(-Wno-alloca)
add_compile_options(-Wno-implicit-int-float-conversion)
else()
add_compile_options(-fPIC)
set(GEN_SHARED_LIB 1)
endif(CMAKE_SYSTEM_NAME STREQUAL Emscripten)

Expand Down Expand Up @@ -125,9 +135,6 @@ endif ()

if (CMAKE_SYSTEM_NAME STREQUAL Linux)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_GNU_SOURCE")
set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -Wa,--noexecstack")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--build-id=sha1")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--build-id=sha1")
endif ()

if(CMAKE_SYSTEM_NAME STREQUAL Linux)
Expand All @@ -141,8 +148,7 @@ endif(CMAKE_SYSTEM_NAME STREQUAL Darwin)
if(CMAKE_SYSTEM_NAME STREQUAL FreeBSD)
set(CLR_CMAKE_PLATFORM_UNIX 1)
add_definitions(-D_BSD_SOURCE) # required for getline
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fuse-ld=lld -Xlinker --build-id=sha1")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=lld -Xlinker --build-id=sha1")
add_link_options(-fuse-ld=lld)
endif(CMAKE_SYSTEM_NAME STREQUAL FreeBSD)

if(CMAKE_SYSTEM_NAME STREQUAL OpenBSD)
Expand All @@ -164,6 +170,12 @@ endif(CMAKE_SYSTEM_NAME STREQUAL SunOS)
# ./build-native.sh cmakeargs -DCLR_ADDITIONAL_COMPILER_OPTIONS=<...> cmakeargs -DCLR_ADDITIONAL_LINKER_FLAGS=<...>
#
if(CLR_CMAKE_PLATFORM_UNIX)
if (CMAKE_SYSTEM_NAME STREQUAL Darwin)
add_link_options(-Wl,-bind_at_load)
else (CMAKE_SYSTEM_NAME STREQUAL Darwin)
add_compile_options($<$<COMPILE_LANGUAGE:ASM>:-Wa,--noexecstack>)
add_link_options(-Wl,--build-id=sha1 -Wl,-z,relro,-z,now)
endif(CMAKE_SYSTEM_NAME STREQUAL Darwin)
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${CLR_ADDITIONAL_LINKER_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${CLR_ADDITIONAL_LINKER_FLAGS}" )
add_compile_options(${CLR_ADDITIONAL_COMPILER_OPTIONS})
Expand Down

0 comments on commit 51470b8

Please sign in to comment.