Skip to content

Commit 51470b8

Browse files
authored
Fix relro, now and PIE for host and libraries (#685)
* 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
1 parent a128330 commit 51470b8

File tree

5 files changed

+36
-14
lines changed

5 files changed

+36
-14
lines changed

src/installer/corehost/cli/common.cmake

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ if(WIN32)
99
add_compile_options($<$<CONFIG:Release>:/MT>)
1010
add_compile_options($<$<CONFIG:Debug>:/MTd>)
1111
else()
12-
add_compile_options(-fPIC)
1312
add_compile_options(-fvisibility=hidden)
1413
endif()
1514

src/installer/corehost/cli/exe.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
project (${DOTNET_PROJECT_NAME})
66

7+
cmake_policy(SET CMP0011 NEW)
8+
cmake_policy(SET CMP0083 NEW)
9+
710
include(${CMAKE_CURRENT_LIST_DIR}/common.cmake)
811

912
# Include directories

src/installer/corehost/cli/test_fx_ver/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ if(WIN32)
2929
add_compile_options($<$<CONFIG:Release>:/MT>)
3030
add_compile_options($<$<CONFIG:Debug>:/MTd>)
3131
else()
32-
add_compile_options(-fPIE)
3332
add_compile_options(-fvisibility=hidden)
3433
endif()
3534

src/installer/settings.cmake

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@
44

55
set (CMAKE_CXX_STANDARD 11)
66

7+
include(CheckPIESupported)
8+
9+
# All code we build should be compiled as position independent
10+
check_pie_supported(OUTPUT_VARIABLE PIE_SUPPORT_OUTPUT LANGUAGES CXX)
11+
if(NOT MSVC AND NOT CMAKE_CXX_LINK_PIE_SUPPORTED)
12+
message(WARNING "PIE is not supported at link time: ${PIE_SUPPORT_OUTPUT}.\n"
13+
"PIE link options will not be passed to linker.")
14+
endif()
15+
16+
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
17+
718
if(CMAKE_SYSTEM_NAME STREQUAL Linux)
819
set(CLR_CMAKE_PLATFORM_UNIX 1)
920
message("System name Linux")
@@ -174,7 +185,7 @@ if(WIN32)
174185
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /GUARD:CF")
175186

176187
# Debug build specific flags
177-
set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "/NOVCFEATURE")
188+
set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} /NOVCFEATURE")
178189

179190
# Release build specific flags
180191
set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /DEBUG /OPT:REF /OPT:ICF")
@@ -216,14 +227,12 @@ endif()
216227
# containing the reference instead of using definitions from other modules.
217228
if(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
218229
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Xlinker -Bsymbolic -Bsymbolic-functions")
219-
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--build-id=sha1")
220-
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--build-id=sha1")
230+
add_link_options(-Wl,--build-id=sha1 -Wl,-z,relro,-z,now)
221231
add_compile_options(-fstack-protector-strong)
222232
elseif(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
223233
add_compile_options(-fstack-protector)
224234
elseif(${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD")
225-
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fuse-ld=lld -Xlinker --build-id=sha1")
226-
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=lld -Xlinker --build-id=sha1")
235+
add_link_options(-fuse-ld=lld -Wl,--build-id=sha1 -Wl,-z,relro,-z,now)
227236
add_compile_options(-fstack-protector)
228237
endif()
229238

src/libraries/Native/Unix/CMakeLists.txt

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
cmake_minimum_required(VERSION 2.8.12)
22
project(CoreFX C)
33

4+
cmake_policy(SET CMP0083 NEW)
5+
6+
include(CheckPIESupported)
7+
8+
# All code we build should be compiled as position independent
9+
check_pie_supported(OUTPUT_VARIABLE PIE_SUPPORT_OUTPUT LANGUAGES C)
10+
if(NOT MSVC AND NOT CMAKE_C_LINK_PIE_SUPPORTED)
11+
message(WARNING "PIE is not supported at link time: ${PIE_SUPPORT_OUTPUT}.\n"
12+
"PIE link options will not be passed to linker.")
13+
endif()
14+
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
15+
416
set(CMAKE_MACOSX_RPATH ON)
517
set(CMAKE_INSTALL_PREFIX $ENV{__CMakeBinDir})
618
set(CMAKE_INCLUDE_CURRENT_DIR ON)
@@ -28,7 +40,6 @@ endif()
2840
add_compile_options(-Werror)
2941

3042
if(CMAKE_SYSTEM_NAME STREQUAL Emscripten)
31-
# Build a static library so no -fPIC
3243
set(CLR_CMAKE_PLATFORM_WASM 1)
3344
add_definitions(-D_WASM_)
3445
# The emscripten build has additional warnings so -Werror breaks
@@ -37,7 +48,6 @@ if(CMAKE_SYSTEM_NAME STREQUAL Emscripten)
3748
add_compile_options(-Wno-alloca)
3849
add_compile_options(-Wno-implicit-int-float-conversion)
3950
else()
40-
add_compile_options(-fPIC)
4151
set(GEN_SHARED_LIB 1)
4252
endif(CMAKE_SYSTEM_NAME STREQUAL Emscripten)
4353

@@ -125,9 +135,6 @@ endif ()
125135

126136
if (CMAKE_SYSTEM_NAME STREQUAL Linux)
127137
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_GNU_SOURCE")
128-
set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -Wa,--noexecstack")
129-
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--build-id=sha1")
130-
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--build-id=sha1")
131138
endif ()
132139

133140
if(CMAKE_SYSTEM_NAME STREQUAL Linux)
@@ -141,8 +148,7 @@ endif(CMAKE_SYSTEM_NAME STREQUAL Darwin)
141148
if(CMAKE_SYSTEM_NAME STREQUAL FreeBSD)
142149
set(CLR_CMAKE_PLATFORM_UNIX 1)
143150
add_definitions(-D_BSD_SOURCE) # required for getline
144-
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fuse-ld=lld -Xlinker --build-id=sha1")
145-
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=lld -Xlinker --build-id=sha1")
151+
add_link_options(-fuse-ld=lld)
146152
endif(CMAKE_SYSTEM_NAME STREQUAL FreeBSD)
147153

148154
if(CMAKE_SYSTEM_NAME STREQUAL OpenBSD)
@@ -164,6 +170,12 @@ endif(CMAKE_SYSTEM_NAME STREQUAL SunOS)
164170
# ./build-native.sh cmakeargs -DCLR_ADDITIONAL_COMPILER_OPTIONS=<...> cmakeargs -DCLR_ADDITIONAL_LINKER_FLAGS=<...>
165171
#
166172
if(CLR_CMAKE_PLATFORM_UNIX)
173+
if (CMAKE_SYSTEM_NAME STREQUAL Darwin)
174+
add_link_options(-Wl,-bind_at_load)
175+
else (CMAKE_SYSTEM_NAME STREQUAL Darwin)
176+
add_compile_options($<$<COMPILE_LANGUAGE:ASM>:-Wa,--noexecstack>)
177+
add_link_options(-Wl,--build-id=sha1 -Wl,-z,relro,-z,now)
178+
endif(CMAKE_SYSTEM_NAME STREQUAL Darwin)
167179
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${CLR_ADDITIONAL_LINKER_FLAGS}")
168180
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${CLR_ADDITIONAL_LINKER_FLAGS}" )
169181
add_compile_options(${CLR_ADDITIONAL_COMPILER_OPTIONS})

0 commit comments

Comments
 (0)