Index: cmake/Modules/AddCompilerRT.cmake =================================================================== --- cmake/Modules/AddCompilerRT.cmake +++ cmake/Modules/AddCompilerRT.cmake @@ -89,7 +89,7 @@ # Adds static or shared runtime for a list of architectures and operating # systems and puts it in the proper directory in the build and install trees. # add_compiler_rt_runtime( -# {STATIC|SHARED} +# {OBJECT|STATIC|SHARED} # ARCHS # OS # SOURCES @@ -100,8 +100,8 @@ # OBJECT_LIBS # PARENT_TARGET ) function(add_compiler_rt_runtime name type) - if(NOT type MATCHES "^(STATIC|SHARED)$") - message(FATAL_ERROR "type argument must be STATIC or SHARED") + if(NOT type MATCHES "^(OBJECT|STATIC|SHARED)$") + message(FATAL_ERROR "type argument must be OBJECT, STATIC or SHARED") return() endif() cmake_parse_arguments(LIB @@ -133,7 +133,10 @@ message(FATAL_ERROR "Architecture ${arch} can't be targeted") return() endif() - if(type STREQUAL "STATIC") + if(type STREQUAL "OBJECT") + set(libname "${name}-${arch}") + set(output_name_${libname} ${libname}${COMPILER_RT_OS_SUFFIX}) + elseif(type STREQUAL "STATIC") set(libname "${name}-${arch}") set(output_name_${libname} ${libname}${COMPILER_RT_OS_SUFFIX}) else() @@ -186,12 +189,34 @@ set(COMPONENT_OPTION COMPONENT ${libname}) endif() - add_library(${libname} ${type} ${sources_${libname}}) - set_target_compile_flags(${libname} ${extra_cflags_${libname}}) - set_target_link_flags(${libname} ${extra_link_flags_${libname}}) - set_property(TARGET ${libname} APPEND PROPERTY - COMPILE_DEFINITIONS ${LIB_DEFS}) - set_target_output_directories(${libname} ${COMPILER_RT_LIBRARY_OUTPUT_DIR}) + if(type STREQUAL "OBJECT") + string(TOUPPER ${CMAKE_BUILD_TYPE} config) + set(cflags "${CMAKE_C_FLAGS} ${CMAKE_C_FLAGS_${config}}") + separate_arguments(cflags) + add_custom_command( + OUTPUT ${COMPILER_RT_LIBRARY_OUTPUT_DIR}/${libname}.o + COMMAND ${CMAKE_C_COMPILER} ${sources_${libname}} ${cflags} ${extra_cflags_${libname}} -c -o ${COMPILER_RT_LIBRARY_OUTPUT_DIR}/${libname}.o + DEPENDS ${sources_${libname}} + COMMENT "Building C object ${libname}.o") + add_custom_target(${libname} DEPENDS ${COMPILER_RT_LIBRARY_OUTPUT_DIR}/${libname}.o) + install(FILES ${COMPILER_RT_LIBRARY_OUTPUT_DIR}/${libname}.o + DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR} + ${COMPONENT_OPTION}) + else() + add_library(${libname} ${type} ${sources_${libname}}) + set_target_compile_flags(${libname} ${extra_cflags_${libname}}) + set_target_link_flags(${libname} ${extra_link_flags_${libname}}) + set_property(TARGET ${libname} APPEND PROPERTY + COMPILE_DEFINITIONS ${LIB_DEFS}) + set_target_output_directories(${libname} ${COMPILER_RT_LIBRARY_OUTPUT_DIR}) + install(TARGETS ${libname} + ARCHIVE DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR} + ${COMPONENT_OPTION} + LIBRARY DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR} + ${COMPONENT_OPTION} + RUNTIME DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR} + ${COMPONENT_OPTION}) + endif() set_target_properties(${libname} PROPERTIES OUTPUT_NAME ${output_name_${libname}}) set_target_properties(${libname} PROPERTIES FOLDER "Compiler-RT Runtime") @@ -204,13 +229,6 @@ set_target_properties(${libname} PROPERTIES IMPORT_SUFFIX ".lib") endif() endif() - install(TARGETS ${libname} - ARCHIVE DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR} - ${COMPONENT_OPTION} - LIBRARY DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR} - ${COMPONENT_OPTION} - RUNTIME DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR} - ${COMPONENT_OPTION}) # We only want to generate per-library install targets if you aren't using # an IDE because the extra targets get cluttered in IDEs. Index: cmake/config-ix.cmake =================================================================== --- cmake/config-ix.cmake +++ cmake/config-ix.cmake @@ -163,6 +163,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} ${X86_64} ${ARM32} ${ARM64}) set(ALL_DFSAN_SUPPORTED_ARCH ${X86_64} ${MIPS64} ${ARM64}) set(ALL_LSAN_SUPPORTED_ARCH ${X86} ${X86_64} ${MIPS64} ${ARM64}) set(ALL_MSAN_SUPPORTED_ARCH ${X86_64} ${MIPS64} ${ARM64} ${PPC64}) @@ -381,6 +382,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}) @@ -463,6 +465,12 @@ # TODO: Add builtins support. +if (CRT_SUPPORTED_ARCH AND OS_NAME MATCHES "Fuchsia") + 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,30 @@ +add_compiler_rt_component(crt) + +set(SHARED_CFLAGS + -DSHARED +) + +foreach(arch ${CRT_SUPPORTED_ARCH}) + add_compiler_rt_runtime(crtbegin + OBJECT + ARCHS ${arch} + SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/crtbegin.c + PARENT_TARGET crt) + add_compiler_rt_runtime(crtbeginS + OBJECT + ARCHS ${arch} + SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/crtbegin.c + CFLAGS ${SHARED_CFLAGS} + PARENT_TARGET crt) + add_compiler_rt_runtime(crtend + OBJECT + ARCHS ${arch} + SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/crtend.c + PARENT_TARGET crt) + add_compiler_rt_runtime(crtendS + OBJECT + ARCHS ${arch} + SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/crtend.c + CFLAGS ${SHARED_CFLAGS} + PARENT_TARGET crt) +endforeach() Index: lib/crt/crtbegin.c =================================================================== --- /dev/null +++ lib/crt/crtbegin.c @@ -0,0 +1,35 @@ +/* ===-- 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. + * + * ===----------------------------------------------------------------------=== + */ + +__attribute__((visibility("hidden"))) +#ifdef SHARED + void *__dso_handle = &__dso_handle; +#else + void *__dso_handle = (void *)0; +#endif + +extern void __cxa_finalize(void *) __attribute__((weak)); + +static void __attribute__((used)) __do_fini() { + static _Bool finalized; + + if (__builtin_expect(finalized, 0)) + return; + +#ifdef SHARED + if (__cxa_finalize) + __cxa_finalize(__dso_handle); +#endif + + finalized = 1; +} + +__attribute__((section(".fini_array"), used)) + void (*__fini)(void) = __do_fini; Index: lib/crt/crtend.c =================================================================== --- /dev/null +++ lib/crt/crtend.c @@ -0,0 +1,14 @@ +/* ===-- 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. + * + * ===----------------------------------------------------------------------=== + */ + +#include + +static const int32_t __FRAME_END__[] + __attribute__((section(".eh_frame"), aligned(4), used)) = { 0 }; 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,32 @@ +// RUN: %clangxx -DSHARED -c %s -fPIC -o %tshared.o +// RUN: %clangxx -c %s -fPIC -o %t.o +// RUN: %clangxx -shared -o %t.so -nostartfiles -nostdlib -nodefaultlibs %crti %shared_crtbegin %tshared.o -lc -lm -lgcc_s %shared_crtend %crtn +// RUN: %clangxx -o %t -nostartfiles -nostdlib -nodefaultlibs %crt1 %crti %crtbegin %t.o -lc -lm -lgcc %t.so %crtend %crtn +// RUN: %run %t 2>&1 | FileCheck %s + +#include + +#ifdef SHARED +bool G; +void C() { + // CHECK: 1 + printf("%d\n", G); +} + +struct A { + A() { G = true; } + ~A() { + // CHECK: ~A() + printf("~A()\n"); + } +}; + +A a; +#else +void C(); + +int main() { + C(); + return 0; +} +#endif Index: test/crt/lit.cfg =================================================================== --- /dev/null +++ test/crt/lit.cfg @@ -0,0 +1,58 @@ +# -*- 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(file): + cmd = subprocess.Popen([config.clang, + config.target_cflags, + '-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-%s.o" % config.target_arch) +config.substitutions.append(('%crtbegin', base_lib % "crtbegin")) +config.substitutions.append(('%shared_crtbegin', base_lib % "crtbeginS")) +config.substitutions.append(('%crtend', base_lib % "crtend")) +config.substitutions.append(('%shared_crtend', base_lib % "crtendS")) + +config.substitutions.append( + ('%crt1', get_library_path('crt1.o'))) +config.substitutions.append( + ('%crti', get_library_path('crti.o'))) +config.substitutions.append( + ('%crtn', get_library_path('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")