[libc] Replace -nostdlib++ flag when building with gcc and add placement new operator to HermeticTestUtils.cpp. (#78906)

`-nostdlib++` is a clang-only flag. Replacing it with `-nostdlib` when
building with gcc.
This commit is contained in:
lntue 2024-01-22 19:27:27 -05:00 committed by GitHub
parent 7117a4e840
commit 029bfd6329
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 26 additions and 2 deletions

View File

@ -62,3 +62,6 @@ message(STATUS "Compiler features available: ${AVAILABLE_COMPILER_FEATURES}")
# clang-8+, gcc-12+
check_cxx_compiler_flag("-ftrivial-auto-var-init=pattern" LIBC_CC_SUPPORTS_PATTERN_INIT)
# clang-6+, gcc-13+
check_cxx_compiler_flag("-nostdlib++" LIBC_CC_SUPPORTS_NOSTDLIBPP)

View File

@ -435,6 +435,13 @@ function(add_libc_fuzzer target_name)
endfunction(add_libc_fuzzer)
# Get libgcc_s to be used in hermetic and integration tests.
if(NOT LIBC_CC_SUPPORTS_NOSTDLIBPP)
execute_process(COMMAND ${CMAKE_CXX_COMPILER} -print-file-name=libgcc_s.so.1
OUTPUT_VARIABLE LIBGCC_S_LOCATION)
string(STRIP ${LIBGCC_S_LOCATION} LIBGCC_S_LOCATION)
endif()
# DEPRECATED: Use add_hermetic_test instead.
#
# Rule to add an integration test. An integration test is like a unit test
@ -564,8 +571,13 @@ function(add_integration_test test_name)
if(LIBC_TARGET_ARCHITECTURE_IS_GPU)
target_link_options(${fq_build_target_name} PRIVATE -nostdlib -static)
else()
elseif(LIBC_CC_SUPPORTS_NOSTDLIBPP)
target_link_options(${fq_build_target_name} PRIVATE -nolibc -nostartfiles -nostdlib++ -static)
else()
# Older version of gcc does not support `nostdlib++` flag. We use
# `nostdlib` and link against libgcc_s, which cannot be linked statically.
target_link_options(${fq_build_target_name} PRIVATE -nolibc -nostartfiles -nostdlib)
list(APPEND link_libraries ${LIBGCC_S_LOCATION})
endif()
target_link_libraries(
${fq_build_target_name}
@ -741,8 +753,13 @@ function(add_libc_hermetic_test test_name)
if(LIBC_TARGET_ARCHITECTURE_IS_GPU)
target_link_options(${fq_build_target_name} PRIVATE -nostdlib -static)
else()
elseif(LIBC_CC_SUPPORTS_NOSTDLIBPP)
target_link_options(${fq_build_target_name} PRIVATE -nolibc -nostartfiles -nostdlib++ -static)
else()
# Older version of gcc does not support `nostdlib++` flag. We use
# `nostdlib` and link against libgcc_s, which cannot be linked statically.
target_link_options(${fq_build_target_name} PRIVATE -nolibc -nostartfiles -nostdlib)
list(APPEND link_libraries ${LIBGCC_S_LOCATION})
endif()
target_link_libraries(
${fq_build_target_name}

View File

@ -104,6 +104,8 @@ void *__dso_handle = nullptr;
} // extern "C"
void *operator new(unsigned long size, void *ptr) { return ptr; }
void *operator new(size_t size) { return malloc(size); }
void *operator new[](size_t size) { return malloc(size); }
@ -113,3 +115,5 @@ void operator delete(void *) {
// we just trap here to catch any such accidental usages.
__builtin_trap();
}
void operator delete(void *ptr, size_t size) { __builtin_trap(); }