Skip to content

Commit 029bfd6

Browse files
authored
[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.
1 parent 7117a4e commit 029bfd6

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

libc/cmake/modules/CheckCompilerFeatures.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,6 @@ message(STATUS "Compiler features available: ${AVAILABLE_COMPILER_FEATURES}")
6262

6363
# clang-8+, gcc-12+
6464
check_cxx_compiler_flag("-ftrivial-auto-var-init=pattern" LIBC_CC_SUPPORTS_PATTERN_INIT)
65+
66+
# clang-6+, gcc-13+
67+
check_cxx_compiler_flag("-nostdlib++" LIBC_CC_SUPPORTS_NOSTDLIBPP)

libc/cmake/modules/LLVMLibCTestRules.cmake

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,13 @@ function(add_libc_fuzzer target_name)
435435

436436
endfunction(add_libc_fuzzer)
437437

438+
# Get libgcc_s to be used in hermetic and integration tests.
439+
if(NOT LIBC_CC_SUPPORTS_NOSTDLIBPP)
440+
execute_process(COMMAND ${CMAKE_CXX_COMPILER} -print-file-name=libgcc_s.so.1
441+
OUTPUT_VARIABLE LIBGCC_S_LOCATION)
442+
string(STRIP ${LIBGCC_S_LOCATION} LIBGCC_S_LOCATION)
443+
endif()
444+
438445
# DEPRECATED: Use add_hermetic_test instead.
439446
#
440447
# Rule to add an integration test. An integration test is like a unit test
@@ -564,8 +571,13 @@ function(add_integration_test test_name)
564571

565572
if(LIBC_TARGET_ARCHITECTURE_IS_GPU)
566573
target_link_options(${fq_build_target_name} PRIVATE -nostdlib -static)
567-
else()
574+
elseif(LIBC_CC_SUPPORTS_NOSTDLIBPP)
568575
target_link_options(${fq_build_target_name} PRIVATE -nolibc -nostartfiles -nostdlib++ -static)
576+
else()
577+
# Older version of gcc does not support `nostdlib++` flag. We use
578+
# `nostdlib` and link against libgcc_s, which cannot be linked statically.
579+
target_link_options(${fq_build_target_name} PRIVATE -nolibc -nostartfiles -nostdlib)
580+
list(APPEND link_libraries ${LIBGCC_S_LOCATION})
569581
endif()
570582
target_link_libraries(
571583
${fq_build_target_name}
@@ -741,8 +753,13 @@ function(add_libc_hermetic_test test_name)
741753

742754
if(LIBC_TARGET_ARCHITECTURE_IS_GPU)
743755
target_link_options(${fq_build_target_name} PRIVATE -nostdlib -static)
744-
else()
756+
elseif(LIBC_CC_SUPPORTS_NOSTDLIBPP)
745757
target_link_options(${fq_build_target_name} PRIVATE -nolibc -nostartfiles -nostdlib++ -static)
758+
else()
759+
# Older version of gcc does not support `nostdlib++` flag. We use
760+
# `nostdlib` and link against libgcc_s, which cannot be linked statically.
761+
target_link_options(${fq_build_target_name} PRIVATE -nolibc -nostartfiles -nostdlib)
762+
list(APPEND link_libraries ${LIBGCC_S_LOCATION})
746763
endif()
747764
target_link_libraries(
748765
${fq_build_target_name}

libc/test/UnitTest/HermeticTestUtils.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ void *__dso_handle = nullptr;
104104

105105
} // extern "C"
106106

107+
void *operator new(unsigned long size, void *ptr) { return ptr; }
108+
107109
void *operator new(size_t size) { return malloc(size); }
108110

109111
void *operator new[](size_t size) { return malloc(size); }
@@ -113,3 +115,5 @@ void operator delete(void *) {
113115
// we just trap here to catch any such accidental usages.
114116
__builtin_trap();
115117
}
118+
119+
void operator delete(void *ptr, size_t size) { __builtin_trap(); }

0 commit comments

Comments
 (0)