1# Copyright (c) 2021-2022 Huawei Device Co., Ltd. 2# Licensed under the Apache License, Version 2.0 (the "License"); 3# you may not use this file except in compliance with the License. 4# You may obtain a copy of the License at 5# 6# http://www.apache.org/licenses/LICENSE-2.0 7# 8# Unless required by applicable law or agreed to in writing, software 9# distributed under the License is distributed on an "AS IS" BASIS, 10# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11# See the License for the specific language governing permissions and 12# limitations under the License. 13 14# 32-bits pointers optimization in Panda for objects => addresses usage low 4GB 15# We need use linker scripts for section replacement above 4GB 16# Note: AddressSanitizer reserves (mmap) addresses for its own needs, 17# so we have difference start addresses for asan and default buildings 18function(panda_add_executable target) 19 # When using rapidcheck we should use linker scripts with 20 # definitions for exception handling sections 21 cmake_parse_arguments(ARG "RAPIDCHECK_ON" "" "" ${ARGN}) 22 23 add_executable(${target} ${ARG_UNPARSED_ARGUMENTS}) 24 25 if(PANDA_USE_32_BIT_POINTER AND NOT (PANDA_TARGET_MOBILE OR PANDA_TARGET_WINDOWS OR PANDA_TARGET_MACOS OR PANDA_TARGET_OHOS)) 26 if(ARG_RAPIDCHECK_ON) 27 set(LINKER_SCRIPT_ARG "-Wl,-T,${PANDA_ROOT}/ldscripts/panda_test_asan.ld") 28 else() 29 set(LINKER_SCRIPT_ARG "-Wl,-T,${PANDA_ROOT}/ldscripts/panda.ld") 30 endif() 31 if(PANDA_ENABLE_ADDRESS_SANITIZER) 32 # For cross-aarch64 with ASAN with linker script we need use additional path-link 33 # Here we use default addresses space (without ASAN flag). It is nuance of cross-building. 34 if(PANDA_TARGET_ARM64) 35 set(LINKER_SCRIPT_ARG "-Wl,-rpath-link,${PANDA_SYSROOT}/lib ${LINKER_SCRIPT_ARG}") 36 else() 37 set(LINKER_SCRIPT_ARG "-Wl,--defsym,LSFLAG_ASAN=1 ${LINKER_SCRIPT_ARG}") 38 endif() 39 endif() 40 # We need use specific options for AMD64 building with Clang compiler 41 # because it is necessary for placement of sections above 4GB 42 if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND PANDA_TARGET_AMD64) 43 set(LINKER_SCRIPT_ARG "${LINKER_SCRIPT_ARG} -pie") 44 # -mcmodel=large with clang and asan on x64 leads to bugs in rapidcheck with high-mem mappings 45 # so for rapidcheck test binaries panda_test_asan.ld is used which does not require 46 # -mcmodel=large option 47 if(NOT ARG_RAPIDCHECK_ON) 48 target_compile_options(${target} PUBLIC "-mcmodel=large") 49 endif() 50 endif() 51 target_link_libraries(${target} ${LINKER_SCRIPT_ARG}) 52 endif() 53endfunction() 54 55# This function need for non-SHARED libraries 56# It is necessary for 32-bits pointers via amd64 building with Clang compiler 57function(panda_set_lib_32bit_property target) 58 if(PANDA_USE_32_BIT_POINTER AND PANDA_TARGET_AMD64 AND CMAKE_CXX_COMPILER_ID STREQUAL "Clang") 59 set_property(TARGET ${target} PROPERTY POSITION_INDEPENDENT_CODE ON) 60 endif() 61endfunction() 62