diff --git a/compiler-rt/CMakeLists.txt b/compiler-rt/CMakeLists.txt --- a/compiler-rt/CMakeLists.txt +++ b/compiler-rt/CMakeLists.txt @@ -45,6 +45,8 @@ mark_as_advanced(COMPILER_RT_BUILD_MEMPROF) option(COMPILER_RT_BUILD_XRAY_NO_PREINIT "Build xray with no preinit patching" OFF) mark_as_advanced(COMPILER_RT_BUILD_XRAY_NO_PREINIT) +option(COMPILER_RT_BUILD_ORC "Build ORC runtime" ON) +mark_as_advanced(COMPILER_RT_BUILD_ORC) set(COMPILER_RT_ASAN_SHADOW_SCALE "" CACHE STRING "Override the shadow scale to be used in ASan runtime") diff --git a/compiler-rt/cmake/config-ix.cmake b/compiler-rt/cmake/config-ix.cmake --- a/compiler-rt/cmake/config-ix.cmake +++ b/compiler-rt/cmake/config-ix.cmake @@ -343,6 +343,10 @@ endif() set(ALL_SHADOWCALLSTACK_SUPPORTED_ARCH ${ARM64}) +if (APPLE) +set(ALL_ORC_SUPPORTED_ARCH ${X86_64}) +endif() + if(APPLE) include(CompilerRTDarwinUtils) @@ -399,6 +403,7 @@ set(TSAN_SUPPORTED_OS osx) set(XRAY_SUPPORTED_OS osx) set(FUZZER_SUPPORTED_OS osx) + set(ORC_SUPPORTED_OS osx) # Note: In order to target x86_64h on OS X the minimum deployment target must # be 10.8 or higher. @@ -587,6 +592,9 @@ list_intersect(SHADOWCALLSTACK_SUPPORTED_ARCH ALL_SHADOWCALLSTACK_SUPPORTED_ARCH SANITIZER_COMMON_SUPPORTED_ARCH) + list_intersect(ORC_SUPPORTED_ARCH + ALL_ORC_SUPPORTED_ARCH + SANITIZER_COMMON_SUPPORTED_ARCH) else() filter_available_targets(CRT_SUPPORTED_ARCH ${ALL_CRT_SUPPORTED_ARCH}) @@ -618,6 +626,7 @@ filter_available_targets(SHADOWCALLSTACK_SUPPORTED_ARCH ${ALL_SHADOWCALLSTACK_SUPPORTED_ARCH}) filter_available_targets(GWP_ASAN_SUPPORTED_ARCH ${ALL_GWP_ASAN_SUPPORTED_ARCH}) + filter_available_targets(ORC_SUPPORTED_ARCH ${ALL_ORC_SUPPORTED_ARCH}) endif() if (MSVC) @@ -778,6 +787,12 @@ set(COMPILER_RT_HAS_XRAY FALSE) endif() +if (ORC_SUPPORTED_ARCH) + set(COMPILER_RT_HAS_ORC TRUE) +else() + set(COMPILER_RT_HAS_ORC FALSE) +endif() + if (COMPILER_RT_HAS_SANITIZER_COMMON AND FUZZER_SUPPORTED_ARCH AND OS_NAME MATCHES "Android|Darwin|Linux|NetBSD|FreeBSD|Fuchsia|Windows") set(COMPILER_RT_HAS_FUZZER TRUE) diff --git a/compiler-rt/lib/CMakeLists.txt b/compiler-rt/lib/CMakeLists.txt --- a/compiler-rt/lib/CMakeLists.txt +++ b/compiler-rt/lib/CMakeLists.txt @@ -66,6 +66,10 @@ compiler_rt_build_runtime(memprof) endif() +if(COMPILER_RT_BUILD_ORC) + compiler_rt_build_runtime(orc) +endif() + # It doesn't normally make sense to build runtimes when a sanitizer is enabled, # so we don't add_subdirectory the runtimes in that case. However, the opposite # is true for fuzzers that exercise parts of the runtime. So we add the fuzzer diff --git a/compiler-rt/lib/orc/CMakeLists.txt b/compiler-rt/lib/orc/CMakeLists.txt new file mode 100644 --- /dev/null +++ b/compiler-rt/lib/orc/CMakeLists.txt @@ -0,0 +1,84 @@ +# Build for all components of the ORC runtime support library. + +# ORC runtime library implementation files. +set(ORC_SOURCES + placeholder.cpp + ) + +# Implementation files for all ORC architectures. +set(x86_64_SOURCES +# x86-64 specific assembly files will go here. +) + +set(ORC_IMPL_HEADERS +# Implementation headers will go here. +) + +# Create list of all source files for +# consumption by tests. +set(ORC_ALL_SOURCE_FILES + ${ORC_SOURCES} + ${x86_64_SOURCES} + ${ORC_IMPL_HEADERS} + ) + +list(REMOVE_DUPLICATES ORC_ALL_SOURCE_FILES) + +# Now put it all together... +include_directories(..) +include_directories(../../include) + +set(ORC_CFLAGS ${COMPILER_RT_COMMON_CFLAGS}) + +# Allow the ORC runtime to reference LLVM headers. +foreach (DIR ${LLVM_INCLUDE_DIR} ${LLVM_MAIN_INCLUDE_DIR}) + list(APPEND ORC_CFLAGS -I${DIR}) +endforeach() + +add_compiler_rt_component(orc) + +# ORC uses C++ standard library headers. +if (TARGET cxx-headers OR HAVE_LIBCXX) + set(ORC_DEPS cxx-headers) +endif() + +if (APPLE) + add_compiler_rt_object_libraries(RTOrc + OS ${ORC_SUPPORTED_OS} + ARCHS ${ORC_SUPPORTED_ARCH} + SOURCES ${ORC_SOURCES} ${x86_64_SOURCES} + ADDITIONAL_HEADERS ${ORC_IMPL_HEADERS} + CFLAGS ${ORC_CFLAGS} + DEPS ${ORC_DEPS}) + + # We only support running on osx for now. + add_compiler_rt_runtime(clang_rt.orc + STATIC + OS ${ORC_SUPPORTED_OS} + ARCHS ${ORC_SUPPORTED_ARCH} + OBJECT_LIBS RTOrc + CFLAGS ${ORC_CFLAGS} + LINK_FLAGS ${SANITIZER_COMMON_LINK_FLAGS} ${WEAK_SYMBOL_LINK_FLAGS} + LINK_LIBS ${ORC_LINK_LIBS} + PARENT_TARGET orc) +else() # not Apple + foreach(arch ${ORC_SUPPORTED_ARCH}) + if(NOT CAN_TARGET_${arch}) + continue() + endif() + add_compiler_rt_object_libraries(RTOrc + ARCHS ${arch} + SOURCES ${ORC_SOURCES} ${${arch}_SOURCES} + ADDITIONAL_HEADERS ${ORC_IMPL_HEADERS} + CFLAGS $ORC_CFLAGS} + DEPS ${ORC_DEPS}) + + # Common ORC archive for instrumented binaries. + add_compiler_rt_runtime(clang_rt.orc + STATIC + ARCHS ${arch} + CFLAGS ${ORC_CFLAGS} + OBJECT_LIBS ${ORC_COMMON_RUNTIME_OBJECT_LIBS} RTOrc + PARENT_TARGET orc) + endforeach() +endif() # not Apple diff --git a/compiler-rt/lib/orc/placeholder.cpp b/compiler-rt/lib/orc/placeholder.cpp new file mode 100644 --- /dev/null +++ b/compiler-rt/lib/orc/placeholder.cpp @@ -0,0 +1 @@ +void placeholder(void) {} diff --git a/compiler-rt/test/CMakeLists.txt b/compiler-rt/test/CMakeLists.txt --- a/compiler-rt/test/CMakeLists.txt +++ b/compiler-rt/test/CMakeLists.txt @@ -78,6 +78,9 @@ if(COMPILER_RT_BUILD_XRAY) compiler_rt_test_runtime(xray) endif() + if(COMPILER_RT_BUILD_ORC) + compiler_rt_Test_runtime(orc) + endif() if(COMPILER_RT_BUILD_CRT AND COMPILER_RT_HAS_CRT) add_subdirectory(crt) endif() diff --git a/compiler-rt/test/orc/CMakeLists.txt b/compiler-rt/test/orc/CMakeLists.txt new file mode 100644 --- /dev/null +++ b/compiler-rt/test/orc/CMakeLists.txt @@ -0,0 +1,26 @@ +set(ORC_LIT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}) +set(ORC_LIT_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}) + +set(ORC_TESTSUITES) +set(ORC_TEST_DEPS ${SANITIZER_COMMON_LIT_TEST_DEPS} clang llvm-jitlink) + +set(ORC_TEST_ARCH ${ORC_SUPPORTED_ARCH}) +if (COMPILER_RT_BUILD_ORC AND COMPILER_RT_HAS_ORC) + foreach(arch ${ORC_TEST_ARCH}) + set(ORC_TEST_TARGET_ARCH ${arch}) + string(TOLOWER "-${arch}-${OS_NAME}" ORC_TEST_CONFIG_SUFFIX) + get_test_cc_for_arch(${arch} ORC_TEST_TARGET_CC ORC_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.py.in + ${CMAKE_CURRENT_BINARY_DIR}/${CONFIG_NAME}/lit.site.cfg.py) + list(APPEND ORC_TESTSUITES ${CMAKE_CURRENT_BINARY_DIR}/${CONFIG_NAME}) + endforeach() +endif() + +add_lit_testsuite(check-orc "Running the ORC runtime tests" + ${ORC_TESTSUITES} + DEPENDS ${ORC_TEST_DEPS}) +set_target_properties(check-orc PROPERTIES FOLDER "Compiler-RT Misc") diff --git a/compiler-rt/test/orc/TestCases/Darwin/lit.local.cfg.py b/compiler-rt/test/orc/TestCases/Darwin/lit.local.cfg.py new file mode 100644 --- /dev/null +++ b/compiler-rt/test/orc/TestCases/Darwin/lit.local.cfg.py @@ -0,0 +1,2 @@ +if config.root.host_os != 'Darwin': + config.unsupported = True diff --git a/compiler-rt/test/orc/TestCases/Darwin/x86-64/lit.local.cfg.py b/compiler-rt/test/orc/TestCases/Darwin/x86-64/lit.local.cfg.py new file mode 100644 --- /dev/null +++ b/compiler-rt/test/orc/TestCases/Darwin/x86-64/lit.local.cfg.py @@ -0,0 +1,2 @@ +if config.root.host_arch != 'x86_64': + config.unsupported = True diff --git a/compiler-rt/test/orc/TestCases/Darwin/x86-64/placeholder_test.S b/compiler-rt/test/orc/TestCases/Darwin/x86-64/placeholder_test.S new file mode 100644 --- /dev/null +++ b/compiler-rt/test/orc/TestCases/Darwin/x86-64/placeholder_test.S @@ -0,0 +1,13 @@ +// RUN: %clang -c -o %t %s +// RUN: %llvm_jitlink %t + + .section __TEXT,__text,regular,pure_instructions + .build_version macos, 11, 0 sdk_version 11, 3 + + .globl _main + .p2align 4, 0x90 +_main: + xorl %eax, %eax + retq + +.subsections_via_symbols diff --git a/compiler-rt/test/orc/lit.cfg.py b/compiler-rt/test/orc/lit.cfg.py new file mode 100644 --- /dev/null +++ b/compiler-rt/test/orc/lit.cfg.py @@ -0,0 +1,33 @@ +# -*- Python -*- + +import os + +# Setup config name. +config.name = 'ORC' + config.name_suffix + +# Setup source root. +config.test_source_root = os.path.dirname(__file__) + +def build_invocation(compile_flags): + return ' ' + ' '.join([config.clang] + compile_flags) + ' ' + +# Assume that llvm-jitlink is in the config.llvm_tools_dir. +llvm_jitlink = os.path.join(config.llvm_tools_dir, 'llvm-jitlink') + +config.substitutions.append( + ('%clang ', build_invocation([config.target_cflags]))) +config.substitutions.append( + ('%clangxx ', + build_invocation(config.cxx_mode_flags + [config.target_cflags]))) +config.substitutions.append( + ('%llvm_jitlink', llvm_jitlink)) +config.substitutions.append( + ('%orc_rt', + ('-L%s -lclang_rt.orc%s') + % (config.compiler_rt_libdir, config.target_suffix))) + +# Default test suffixes. +config.suffixes = ['.c', '.cpp', '.S'] + +if config.host_os not in ['Darwin']: + config.unsupported = True diff --git a/compiler-rt/test/orc/lit.site.cfg.py.in b/compiler-rt/test/orc/lit.site.cfg.py.in new file mode 100644 --- /dev/null +++ b/compiler-rt/test/orc/lit.site.cfg.py.in @@ -0,0 +1,14 @@ +@LIT_SITE_CFG_IN_HEADER@ + +# Tool-specific config options. +config.name_suffix = "@ORC_TEST_CONFIG_SUFFIX@" +config.orc_lit_source_dir = "@ORC_LIT_SOURCE_DIR@" +config.target_cflags = "@ORC_TEST_TARGET_CFLAGS@" +config.target_arch = "@ORC_TEST_TARGET_ARCH@" +config.built_with_llvm = ("@COMPILER_RT_STANDALONE_BUILD@" != "TRUE") + +# 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, "@CMAKE_CURRENT_SOURCE_DIR@/lit.cfg.py")