From 0f9f95fe9361c506c3cc9d78fb77707fdc80ca51 Mon Sep 17 00:00:00 2001 From: Peter Collingbourne Date: Thu, 29 Sep 2022 21:38:54 -0700 Subject: [PATCH] gn build: Add support for building the standalone ubsan runtime as a shared library on non-Mac. Differential Revision: https://reviews.llvm.org/D127556 --- .../secondary/compiler-rt/gen_version_script.gni | 37 ++++++ llvm/utils/gn/secondary/compiler-rt/lib/BUILD.gn | 10 +- .../gn/secondary/compiler-rt/lib/hwasan/BUILD.gn | 27 ++--- .../gn/secondary/compiler-rt/lib/ubsan/BUILD.gn | 124 +++++++++++++-------- 4 files changed, 125 insertions(+), 73 deletions(-) create mode 100644 llvm/utils/gn/secondary/compiler-rt/gen_version_script.gni diff --git a/llvm/utils/gn/secondary/compiler-rt/gen_version_script.gni b/llvm/utils/gn/secondary/compiler-rt/gen_version_script.gni new file mode 100644 index 0000000..7cfae7d --- /dev/null +++ b/llvm/utils/gn/secondary/compiler-rt/gen_version_script.gni @@ -0,0 +1,37 @@ +import("//compiler-rt/target.gni") + +template("gen_version_script") { + if (current_os != "mac" && current_os != "win") { + action(target_name) { + script = "//compiler-rt/lib/sanitizer_common/scripts/gen_dynamic_list.py" + sources = [ invoker.extra ] + deps = invoker.libs + outputs = [ invoker.output ] + args = [ + "--version-list", + "--extra", + rebase_path(invoker.extra, root_build_dir), + ] + foreach(lib_name, invoker.lib_names) { + args += [ rebase_path( + "$crt_current_out_dir/libclang_rt.$lib_name$crt_current_target_suffix.a", + root_build_dir) ] + } + args += [ + "--nm-executable", + "nm", + "-o", + rebase_path(invoker.output, root_build_dir), + ] + } + } else { + source_set(target_name) {} + not_needed(invoker, + [ + "extra", + "lib_names", + "libs", + "output", + ]) + } +} diff --git a/llvm/utils/gn/secondary/compiler-rt/lib/BUILD.gn b/llvm/utils/gn/secondary/compiler-rt/lib/BUILD.gn index acbe616..e4606bf 100644 --- a/llvm/utils/gn/secondary/compiler-rt/lib/BUILD.gn +++ b/llvm/utils/gn/secondary/compiler-rt/lib/BUILD.gn @@ -6,14 +6,14 @@ group("lib") { if (current_os == "linux") { deps += [ "//compiler-rt/lib/msan" ] } - if (current_os == "android" || current_os == "linux" || current_os == "mac") { - deps += [ "//compiler-rt/lib/ubsan" ] - } - if (current_os == "android" || current_os == "linux") { + if (current_os == "linux" || current_os == "android") { deps += [ "//compiler-rt/lib/ubsan_minimal" ] } if (current_os != "win" && current_os != "baremetal") { - deps += [ "//compiler-rt/lib/asan" ] + deps += [ + "//compiler-rt/lib/asan", + "//compiler-rt/lib/ubsan", + ] if (current_cpu == "x64" || current_cpu == "arm64") { deps += [ "//compiler-rt/lib/tsan/rtl" ] } diff --git a/llvm/utils/gn/secondary/compiler-rt/lib/hwasan/BUILD.gn b/llvm/utils/gn/secondary/compiler-rt/lib/hwasan/BUILD.gn index b91500f..2dc7435 100644 --- a/llvm/utils/gn/secondary/compiler-rt/lib/hwasan/BUILD.gn +++ b/llvm/utils/gn/secondary/compiler-rt/lib/hwasan/BUILD.gn @@ -1,3 +1,4 @@ +import("//compiler-rt/gen_version_script.gni") import("//compiler-rt/target.gni") if (current_cpu == "x64") { @@ -6,28 +7,16 @@ if (current_cpu == "x64") { hwasan_name = "hwasan" } -action("version_script") { - script = "//compiler-rt/lib/sanitizer_common/scripts/gen_dynamic_list.py" - sources = [ "hwasan.syms.extra" ] - deps = [ +gen_version_script("version_script") { + extra = "hwasan.syms.extra" + output = "$target_gen_dir/hwasan.vers" + libs = [ ":hwasan", ":hwasan_cxx", ] - outputs = [ "$target_gen_dir/hwasan.vers" ] - args = [ - "--version-list", - "--extra", - rebase_path(sources[0], root_build_dir), - rebase_path( - "$crt_current_out_dir/libclang_rt.$hwasan_name$crt_current_target_suffix.a", - root_build_dir), - rebase_path( - "$crt_current_out_dir/libclang_rt.${hwasan_name}_cxx$crt_current_target_suffix.a", - root_build_dir), - "--nm-executable", - "nm", - "-o", - rebase_path(outputs[0], root_build_dir), + lib_names = [ + "$hwasan_name", + "${hwasan_name}_cxx", ] } diff --git a/llvm/utils/gn/secondary/compiler-rt/lib/ubsan/BUILD.gn b/llvm/utils/gn/secondary/compiler-rt/lib/ubsan/BUILD.gn index fb7cb82..ab05b32 100644 --- a/llvm/utils/gn/secondary/compiler-rt/lib/ubsan/BUILD.gn +++ b/llvm/utils/gn/secondary/compiler-rt/lib/ubsan/BUILD.gn @@ -1,16 +1,29 @@ +import("//compiler-rt/gen_version_script.gni") import("//compiler-rt/target.gni") group("ubsan") { - if (current_os == "mac") { - deps = [ ":ubsan_shared_library" ] - } else { - deps = [ - ":ubsan_standalone", - ":ubsan_standalone_cxx", - ] + deps = [ + ":ubsan_shared", + ":ubsan_static", + ] + if (current_os != "mac") { + deps += [ ":ubsan_static_cxx" ] } } +gen_version_script("version_script") { + extra = "ubsan.syms.extra" + output = "$target_gen_dir/ubsan.vers" + libs = [ + ":ubsan_static", + ":ubsan_static_cxx", + ] + lib_names = [ + "ubsan_standalone", + "ubsan_standalone_cxx", + ] +} + source_set("sources") { configs -= [ "//llvm/utils/gn/build:llvm_code" ] configs += [ "//llvm/utils/gn/build:crt_code" ] @@ -69,11 +82,6 @@ source_set("standalone_sources") { "ubsan_init_standalone.cpp", "ubsan_signals_standalone.cpp", ] - deps = [ - ":sources", - "//compiler-rt/lib/interception:sources", - "//compiler-rt/lib/sanitizer_common:sources", - ] } source_set("cxx_sources") { @@ -90,18 +98,52 @@ source_set("cxx_sources") { ] } -if (current_os == "mac") { - shared_library("ubsan_shared_library") { +static_library("ubsan_static") { + output_dir = crt_current_out_dir + if (current_os == "mac") { + output_name = "clang_rt.ubsan_osx" + } else { + output_name = "clang_rt.ubsan_standalone$crt_current_target_suffix" + } + complete_static_lib = true + configs -= [ + "//llvm/utils/gn/build:llvm_code", + "//llvm/utils/gn/build:thin_archive", + ] + configs += [ "//llvm/utils/gn/build:crt_code" ] + deps = [ + ":sources", + ":standalone_sources", + ] + if (current_os == "mac") { + deps += [ ":cxx_sources" ] + } +} + +if (current_os != "mac") { + static_library("ubsan_static_cxx") { output_dir = crt_current_out_dir - output_name = "clang_rt.ubsan_osx_dynamic" - deps = [ - ":cxx_sources", - ":sources", - ":standalone_sources", - "//compiler-rt/lib/interception:sources", - "//compiler-rt/lib/sanitizer_common:sources", + output_name = "clang_rt.ubsan_standalone_cxx$crt_current_target_suffix" + complete_static_lib = true + configs -= [ + "//llvm/utils/gn/build:llvm_code", + "//llvm/utils/gn/build:thin_archive", ] - # The -U flags below correspond to the add_weak_symbols() calls in CMake. + configs += [ "//llvm/utils/gn/build:crt_code" ] + deps = [ ":cxx_sources" ] + } +} + +shared_library("ubsan_shared") { + output_dir = crt_current_out_dir + if (current_os == "mac") { + output_name = "clang_rt.ubsan_osx_dynamic" + } else { + output_name = "clang_rt.ubsan_standalone$crt_current_target_suffix" + } + configs -= [ "//llvm/utils/gn/build:llvm_code" ] + configs += [ "//llvm/utils/gn/build:crt_code" ] + if (current_os == "mac") { ldflags = [ "-lc++", "-lc++abi", @@ -125,33 +167,17 @@ if (current_os == "mac") { "-Wl,-install_name,@rpath/libclang_rt.ubsan_osx_dynamic.dylib", ] # FIXME: -Wl,-rpath + # FIXME: codesign (??) } -} else { - static_library("ubsan_standalone") { - output_dir = crt_current_out_dir - output_name = "clang_rt.ubsan_standalone$crt_current_target_suffix" - complete_static_lib = true - configs -= [ - "//llvm/utils/gn/build:llvm_code", - "//llvm/utils/gn/build:thin_archive", - ] - deps = [ - ":sources", - ":standalone_sources", - ] - configs += [ "//llvm/utils/gn/build:crt_code" ] - sources = [ "ubsan_init_standalone_preinit.cpp" ] - } - - static_library("ubsan_standalone_cxx") { - output_dir = crt_current_out_dir - output_name = "clang_rt.ubsan_standalone_cxx$crt_current_target_suffix" - complete_static_lib = true - configs -= [ - "//llvm/utils/gn/build:llvm_code", - "//llvm/utils/gn/build:thin_archive", - ] - deps = [ ":cxx_sources" ] - configs += [ "//llvm/utils/gn/build:crt_code" ] + deps = [ + ":cxx_sources", + ":sources", + ":standalone_sources", + ":version_script", + ] + if (current_os != "mac") { + inputs = [ "$target_gen_dir/ubsan.vers" ] + ldflags = + [ "-Wl,--version-script," + rebase_path(inputs[0], root_build_dir) ] } } -- 2.7.4