Index: cmake/config-ix.cmake =================================================================== --- cmake/config-ix.cmake +++ cmake/config-ix.cmake @@ -162,6 +162,7 @@ ${ARM32} ${ARM64} ${MIPS32} ${MIPS64} ${S390X}) set(ALL_ASAN_SUPPORTED_ARCH ${X86} ${X86_64} ${ARM32} ${ARM64} ${MIPS32} ${MIPS64} ${PPC64} ${S390X}) +set(ALL_CRT_SUPPORTED_ARCH ${X86_64} ${ARM64}) set(ALL_DFSAN_SUPPORTED_ARCH ${X86_64} ${MIPS64} ${ARM64}) set(ALL_LSAN_SUPPORTED_ARCH ${X86_64} ${MIPS64} ${ARM64}) set(ALL_MSAN_SUPPORTED_ARCH ${X86_64} ${MIPS64} ${ARM64} ${PPC64}) @@ -380,6 +381,7 @@ ALL_XRAY_SUPPORTED_ARCH SANITIZER_COMMON_SUPPORTED_ARCH) else() + filter_available_targets(CRT_SUPPORTED_ARCH ${ALL_CRT_SUPPORTED_ARCH}) # Architectures supported by compiler-rt libraries. filter_available_targets(SANITIZER_COMMON_SUPPORTED_ARCH ${ALL_SANITIZER_COMMON_SUPPORTED_ARCH}) @@ -462,6 +464,12 @@ # TODO: Add builtins support. +if (CRT_SUPPORTED_ARCH AND OS_NAME MATCHES "Linux") + set(COMPILER_RT_HAS_CRT TRUE) +else() + set(COMPILER_RT_HAS_CRT FALSE) +endif() + if (COMPILER_RT_HAS_SANITIZER_COMMON AND DFSAN_SUPPORTED_ARCH AND OS_NAME MATCHES "Linux") set(COMPILER_RT_HAS_DFSAN TRUE) Index: lib/CMakeLists.txt =================================================================== --- lib/CMakeLists.txt +++ lib/CMakeLists.txt @@ -17,6 +17,10 @@ add_subdirectory(builtins) endif() +if(COMPILER_RT_BUILD_CRT) + add_subdirectory(crt) +endif() + function(compiler_rt_build_runtime runtime) string(TOUPPER ${runtime} runtime_uppercase) if(COMPILER_RT_HAS_${runtime_uppercase}) Index: lib/crt/CMakeLists.txt =================================================================== --- /dev/null +++ lib/crt/CMakeLists.txt @@ -0,0 +1,34 @@ +add_compiler_rt_component(crt) + +function(add_compiler_rt_crt_file name) + cmake_parse_arguments(CRT + "" + "SOURCE" + "CFLAGS" + ${ARGN}) + add_custom_command( + OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${name}.o + COMMAND ${CMAKE_C_COMPILER} ${CMAKE_CURRENT_SOURCE_DIR}/${CRT_SOURCE} ${CMAKE_C_CFLAGS} ${CRT_CFLAGS} -c -o ${COMPILER_RT_LIBRARY_OUTPUT_DIR}/${name}.o + DEPENDS ${CRT_SOURCE} + COMMENT "Building C object ${name}.o") + add_custom_target(${name} DEPENDS ${name}.o) + add_custom_target(install-${name} + DEPENDS ${name} + COMMAND "${CMAKE_COMMAND}" + -DCMAKE_INSTALL_COMPONENT=${name} + -P "${CMAKE_BINARY_DIR}/cmake_install.cmake") +endfunction() + +add_compiler_rt_crt_file(crtbegin + SOURCE crtbegin.c) +add_compiler_rt_crt_file(crtbeginS + SOURCE crtbegin.c + CFLAGS -DSHARED) + +add_compiler_rt_crt_file(crtend + SOURCE crtend.c) +add_compiler_rt_crt_file(crtendS + SOURCE crtend.c + CFLAGS -DSHARED) + +add_dependencies(crt crtbegin crtbeginS crtend crtendS) Index: lib/crt/crtbegin.c =================================================================== --- /dev/null +++ lib/crt/crtbegin.c @@ -0,0 +1,15 @@ +/* ===-- crtbegin.c - Provide __dso_handle ---------------------------------=== + * + * The LLVM Compiler Infrastructure + * + * This file is dual licensed under the MIT and the University of Illinois Open + * Source Licenses. See LICENSE.TXT for details. + * + * ===----------------------------------------------------------------------=== + */ + +#ifdef SHARED +__attribute__((visibility("hidden"))) void *__dso_handle = &__dso_handle; +#else +__attribute__((visibility("hidden"))) void *__dso_handle = (void *)0; +#endif Index: lib/crt/crtend.c =================================================================== --- /dev/null +++ lib/crt/crtend.c @@ -0,0 +1,12 @@ +/* ===-- crtend.c - Provide .eh_frame --------------------------------------=== + * + * The LLVM Compiler Infrastructure + * + * This file is dual licensed under the MIT and the University of Illinois Open + * Source Licenses. See LICENSE.TXT for details. + * + * ===----------------------------------------------------------------------=== + */ + +static const int __FRAME_END__[] + __attribute__((section(".eh_frame"), aligned(4), used)) = {}; Index: test/CMakeLists.txt =================================================================== --- test/CMakeLists.txt +++ test/CMakeLists.txt @@ -42,6 +42,9 @@ if(COMPILER_RT_HAS_ASAN) add_subdirectory(asan) endif() + if(COMPILER_RT_HAS_CRT) + add_subdirectory(crt) + endif() if(COMPILER_RT_HAS_DFSAN) add_subdirectory(dfsan) endif() Index: test/crt/CMakeLists.txt =================================================================== --- /dev/null +++ test/crt/CMakeLists.txt @@ -0,0 +1,31 @@ +set(CRT_LIT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}) + +set(CRT_TESTSUITES) + +set(CRT_TEST_DEPS "") + +if(NOT COMPILER_RT_STANDALONE_BUILD AND COMPILER_RT_BUILD_CRT AND + COMPILER_RT_HAS_CRT) + list(APPEND CRT_TEST_DEPS crt) +endif() + +set(CRT_TEST_ARCH ${CRT_SUPPORTED_ARCH}) +if (COMPILER_RT_BUILD_CRT AND COMPILER_RT_HAS_CRT) + foreach(arch ${CRT_TEST_ARCH}) + set(CRT_TEST_TARGET_ARCH ${arch}) + string(TOLOWER "-${arch}-${OS_NAME}" CRT_TEST_CONFIG_SUFFIX) + get_test_cc_for_arch(${arch} CRT_TEST_TARGET_CC CRT_TEST_TARGET_CFLAGS) + string(TOUPPER ${arch} ARCH_UPPER_CASE) + set(CONFIG_NAME ${ARCH_UPPER_CASE}${OS_NAME}Config) + + configure_lit_site_cfg( + ${CMAKE_CURRENT_SOURCE_DIR}/lit.site.cfg.in + ${CMAKE_CURRENT_BINARY_DIR}/${CONFIG_NAME}/lit.site.cfg) + list(APPEND CRT_TESTSUITES ${CMAKE_CURRENT_BINARY_DIR}/${CONFIG_NAME}) + endforeach() +endif() + +add_lit_testsuite(check-crt "Running the CRT tests" + ${CRT_TESTSUITES} + DEPENDS ${CRT_TEST_DEPS}) +set_target_properties(check-crt PROPERTIES FOLDER "Compiler-RT Misc") Index: test/crt/dso_handle.cpp =================================================================== --- /dev/null +++ test/crt/dso_handle.cpp @@ -0,0 +1,19 @@ +// RUN: %clangxx -c %s -o %s.o +// RUN: %clangxx -o %t -nostartfiles -nostdlib -nodefaultlibs %crt1 %crti %crtbegin %s.o -lc -lm -lgcc %crtend %crtn +// RUN: %run %t 2>&1 | FileCheck %s + +#include + +struct A { + A() {} + ~A() { + // CHECK: ~A() + printf("~A()"); + } +}; + +A a; + +int main() { + return 0; +} Index: test/crt/lit.cfg =================================================================== --- /dev/null +++ test/crt/lit.cfg @@ -0,0 +1,55 @@ +# -*- Python -*- + +import os +import subprocess + +# Setup config name. +config.name = 'CRT' + config.name_suffix + +# Setup source root. +config.test_source_root = os.path.dirname(__file__) + + +def get_library_path(clang, file): + cmd = subprocess.Popen([clang, '-print-file-name=%s' % file], + stdout=subprocess.PIPE, + env=config.environment) + if not cmd.stdout: + lit_config.fatal("Couldn't find the library path for '%s'" % file) + dir = cmd.stdout.read().strip() + if sys.platform in ['win32'] and execute_external: + # Don't pass dosish path separator to msys bash.exe. + dir = dir.replace('\\', '/') + # Ensure the result is an ascii string, across Python2.5+ - Python3. + return str(dir.decode('ascii')) + + +def build_invocation(compile_flags): + return ' ' + ' '.join([config.clang] + compile_flags) + ' ' + + +# Setup substitutions. +config.substitutions.append( + ('%clang ', build_invocation([config.target_cflags]))) +config.substitutions.append( + ('%clangxx ', + build_invocation(config.cxx_mode_flags + [config.target_cflags]))) + +base_lib = os.path.join(config.compiler_rt_libdir, "%s.o") +config.substitutions.append(('%crtbegin', base_lib % "crtbegin")) +config.substitutions.append(('%crtbeginS', base_lib % "crtbeginS")) +config.substitutions.append(('%crtend', base_lib % "crtend")) +config.substitutions.append(('%crtendS', base_lib % "crtendS")) + +config.substitutions.append( + ('%crt1', get_library_path(config.clang, 'crt1.o'))) +config.substitutions.append( + ('%crti', get_library_path(config.clang, 'crti.o'))) +config.substitutions.append( + ('%crtn', get_library_path(config.clang, 'crtn.o'))) + +# Default test suffixes. +config.suffixes = ['.c', '.cc', '.cpp'] + +if config.host_os not in ['Linux']: + config.unsupported = True Index: test/crt/lit.site.cfg.in =================================================================== --- /dev/null +++ test/crt/lit.site.cfg.in @@ -0,0 +1,13 @@ +@LIT_SITE_CFG_IN_HEADER@ + +# Tool-specific config options. +config.name_suffix = "@CRT_TEST_CONFIG_SUFFIX@" +config.crt_lit_source_dir = "@CRT_LIT_SOURCE_DIR@" +config.target_cflags = "@CRT_TEST_TARGET_CFLAGS@" +config.target_arch = "@CRT_TEST_TARGET_ARCH@" + +# Load common config for all compiler-rt lit tests +lit_config.load_config(config, "@COMPILER_RT_BINARY_DIR@/test/lit.common.configured") + +# Load tool-specific config that would do the real work. +lit_config.load_config(config, "@CRT_LIT_SOURCE_DIR@/lit.cfg")