Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 3908bfa

Browse files
committed
Merged PR 15832: Port from 5.0: Fix Position Independent Code in CMake files (#28143)
Port from 5.0: Fix Position Independent Code in CMake files (#28143) * CoreCLR PR26323 Port: Fix PIE options * Added missing PIE and RELRO compilation flags.
1 parent 242948f commit 3908bfa

File tree

46 files changed

+12
-132
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+12
-132
lines changed

configurecompiler.cmake

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ set(CMAKE_C_STANDARD_REQUIRED ON)
66
set(CMAKE_CXX_STANDARD 11)
77
set(CMAKE_CXX_STANDARD_REQUIRED ON)
88

9+
# All code we build should be compiled as position independent.
10+
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
11+
912
set(CLR_DEFINES_DEBUG_INIT DEBUG _DEBUG _DBG URTBLDENV_FRIENDLY=Checked BUILDENV_CHECKED=1)
1013
set(CLR_DEFINES_CHECKED_INIT DEBUG _DEBUG _DBG URTBLDENV_FRIENDLY=Checked BUILDENV_CHECKED=1)
1114
set(CLR_DEFINES_RELEASE_INIT NDEBUG URTBLDENV_FRIENDLY=Retail)
@@ -322,10 +325,9 @@ elseif (CLR_CMAKE_PLATFORM_UNIX)
322325
endif ()
323326

324327
# -fdata-sections -ffunction-sections: each function has own section instead of one per .o file (needed for --gc-sections)
325-
# -fPIC: enable Position Independent Code normally just for shared libraries but required when linking with address sanitizer
326328
# -O1: optimization level used instead of -O0 to avoid compile error "invalid operand for inline asm constraint"
327-
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${CLR_SANITIZE_CXX_FLAGS} -fdata-sections -ffunction-sections -fPIC -O1")
328-
set(CMAKE_CXX_FLAGS_CHECKED "${CMAKE_CXX_FLAGS_CHECKED} ${CLR_SANITIZE_CXX_FLAGS} -fdata-sections -ffunction-sections -fPIC -O1")
329+
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${CLR_SANITIZE_CXX_FLAGS} -fdata-sections -ffunction-sections -O1")
330+
set(CMAKE_CXX_FLAGS_CHECKED "${CMAKE_CXX_FLAGS_CHECKED} ${CLR_SANITIZE_CXX_FLAGS} -fdata-sections -ffunction-sections -O1")
329331

330332
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} ${CLR_SANITIZE_LINK_FLAGS}")
331333
set(CMAKE_EXE_LINKER_FLAGS_CHECKED "${CMAKE_EXE_LINKER_FLAGS_CHECKED} ${CLR_SANITIZE_LINK_FLAGS}")
@@ -335,6 +337,13 @@ elseif (CLR_CMAKE_PLATFORM_UNIX)
335337
set(CMAKE_SHARED_LINKER_FLAGS_CHECKED "${CMAKE_SHARED_LINKER_FLAGS_CHECKED} ${CLR_SANITIZE_LINK_FLAGS} -Wl,--gc-sections")
336338
endif ()
337339
endif(UPPERCASE_CMAKE_BUILD_TYPE STREQUAL DEBUG OR UPPERCASE_CMAKE_BUILD_TYPE STREQUAL CHECKED)
340+
341+
# This linker option causes executables we build to be marked as containing
342+
# position independent code.
343+
# It is necessary to make ASLR work for executables.
344+
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--build-id=sha1 -Wl,-z,relro,-z,now")
345+
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pie -Wl,-z,relro,-z,now")
346+
338347
endif(WIN32)
339348

340349
# CLR_ADDITIONAL_LINKER_FLAGS - used for passing additional arguments to linker

src/binder/CMakeLists.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,5 @@ endif(WIN32)
9191
convert_to_absolute_path(BINDER_SOURCES ${BINDER_SOURCES})
9292
convert_to_absolute_path(BINDER_CROSSGEN_SOURCES ${BINDER_CROSSGEN_SOURCES})
9393

94-
if(CLR_CMAKE_PLATFORM_UNIX)
95-
add_compile_options(-fPIC)
96-
endif(CLR_CMAKE_PLATFORM_UNIX)
97-
9894
add_subdirectory(v3binder)
9995
add_subdirectory(v3binder_crossgen)

src/classlibnative/bcltype/CMakeLists.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ set(BCLTYPE_SOURCES
1111
variant.cpp
1212
)
1313

14-
if(CLR_CMAKE_PLATFORM_UNIX)
15-
add_compile_options(-fPIC)
16-
endif(CLR_CMAKE_PLATFORM_UNIX)
17-
1814
add_library_clr(bcltype
1915
STATIC
2016
${BCLTYPE_SOURCES}

src/classlibnative/float/CMakeLists.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ set(FLOAT_SOURCES
77
floatsingle.cpp
88
)
99

10-
if(CLR_CMAKE_PLATFORM_UNIX)
11-
add_compile_options(-fPIC)
12-
endif(CLR_CMAKE_PLATFORM_UNIX)
13-
1410
add_library_clr(comfloat_wks
1511
STATIC
1612
${FLOAT_SOURCES}

src/coreclr/hosts/osxbundlerun/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ project(osxbundlerun)
22

33
include_directories(../unixcoreruncommon)
44

5-
add_compile_options(-fPIE)
6-
75
set(CORERUN_SOURCES
86
osxbundlerun.cpp
97
)

src/coreclr/hosts/unixcoreconsole/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ project(unixcoreconsole)
22

33
include_directories(../unixcoreruncommon)
44

5-
add_compile_options(-fPIE)
6-
75
set(CORECONSOLE_SOURCES
86
coreconsole.cpp
97
)

src/coreclr/hosts/unixcorerun/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ project(unixcorerun)
22

33
include_directories(../unixcoreruncommon)
44

5-
add_compile_options(-fPIE)
6-
75
set(CORERUN_SOURCES
86
corerun.cpp
97
)

src/coreclr/hosts/unixcoreruncommon/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
project(unixcoreruncommon)
22

3-
add_compile_options(-fPIC)
4-
53
set(CMAKE_INCLUDE_CURRENT_DIR ON)
64

75
include(configure.cmake)

src/corefx/System.Globalization.Native/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ endif()
3737

3838
include(configure.cmake)
3939

40-
add_compile_options(-fPIC)
41-
4240
set(NATIVEGLOBALIZATION_SOURCES
4341
pal_calendarData.c
4442
pal_casing.c

src/debug/createdump/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ include_directories(BEFORE ${VM_DIR})
2020

2121
add_definitions(-DPAL_STDCPP_COMPAT)
2222

23-
add_compile_options(-fPIE)
24-
2523
set(CREATEDUMP_SOURCES
2624
createdump.cpp
2725
crashinfo.cpp

0 commit comments

Comments
 (0)