From f3b1582bb926a7005ba77bfaa44b1ed59a587509 Mon Sep 17 00:00:00 2001 From: Adrian Cruceru Date: Tue, 23 Jun 2020 19:18:34 +0200 Subject: [PATCH 1/3] Update libunwind build process for x86_64-fortanix-unknown-sgx target --- src/bootstrap/compile.rs | 20 ----------- src/ci/docker/dist-various-2/Dockerfile | 2 -- ...d-x86_64-fortanix-unknown-sgx-toolchain.sh | 35 ------------------ .../spec/x86_64_fortanix_unknown_sgx.rs | 6 ++-- src/libstd/sys/sgx/abi/entry.S | 14 +++++--- src/libunwind/build.rs | 36 +++++++++++++++++++ 6 files changed, 49 insertions(+), 64 deletions(-) diff --git a/src/bootstrap/compile.rs b/src/bootstrap/compile.rs index afcf0dcac7e55..5fb489cb9ac5a 100644 --- a/src/bootstrap/compile.rs +++ b/src/bootstrap/compile.rs @@ -131,28 +131,8 @@ fn copy_third_party_objects( compiler: &Compiler, target: Interned, ) -> Vec<(PathBuf, DependencyType)> { - let libdir = builder.sysroot_libdir(*compiler, target); let mut target_deps = vec![]; - // Copies libunwind.a compiled to be linked with x86_64-fortanix-unknown-sgx. - // - // This target needs to be linked to Fortanix's port of llvm's libunwind. - // libunwind requires support for rwlock and printing to stderr, - // which is provided by std for this target. - if target == "x86_64-fortanix-unknown-sgx" { - let src_path_env = "X86_FORTANIX_SGX_LIBS"; - let src = - env::var(src_path_env).unwrap_or_else(|_| panic!("{} not found in env", src_path_env)); - copy_and_stamp( - builder, - &*libdir, - Path::new(&src), - "libunwind.a", - &mut target_deps, - DependencyType::Target, - ); - } - if builder.config.sanitizers && compiler.stage != 0 { // The sanitizers are only copied in stage1 or above, // to avoid creating dependency on LLVM. diff --git a/src/ci/docker/dist-various-2/Dockerfile b/src/ci/docker/dist-various-2/Dockerfile index 43f5581f996ea..b495d8c2daeb1 100644 --- a/src/ci/docker/dist-various-2/Dockerfile +++ b/src/ci/docker/dist-various-2/Dockerfile @@ -105,8 +105,6 @@ ENV TARGETS=$TARGETS,nvptx64-nvidia-cuda ENV TARGETS=$TARGETS,armv7-unknown-linux-gnueabi ENV TARGETS=$TARGETS,armv7-unknown-linux-musleabi -ENV X86_FORTANIX_SGX_LIBS="/x86_64-fortanix-unknown-sgx/lib/" - # As per https://bugs.launchpad.net/ubuntu/+source/gcc-defaults/+bug/1300211 # we need asm in the search path for gcc-7 (for gnux32) but not in the search path of the # cross compilers. diff --git a/src/ci/docker/dist-various-2/build-x86_64-fortanix-unknown-sgx-toolchain.sh b/src/ci/docker/dist-various-2/build-x86_64-fortanix-unknown-sgx-toolchain.sh index 4294b1ef93dd8..46a0ca7290999 100755 --- a/src/ci/docker/dist-various-2/build-x86_64-fortanix-unknown-sgx-toolchain.sh +++ b/src/ci/docker/dist-various-2/build-x86_64-fortanix-unknown-sgx-toolchain.sh @@ -24,39 +24,4 @@ install_prereq() { clang-11 } -build_unwind() { - set -x - dir_name="${target}_temp" - rm -rf ${dir_name} - mkdir -p ${dir_name} - pushd ${dir_name} - - # Clone Fortanix's fork of llvm-project which has a port of libunwind - fetch_github_commit_archive "$repo_name" "$url" - cd "${repo_name}/libunwind" - - # Build libunwind - mkdir -p build - cd build - target_CC="CC_${target//-/_}" - target_CXX="CXX_${target//-/_}" - target_CFLAGS="CFLAGS_${target//-/_}" - target_CXXFLAGS="CXXFLAGS_${target//-/_}" - cmake -DCMAKE_BUILD_TYPE="RELEASE" -DRUST_SGX=1 -G "Unix Makefiles" \ - -DCMAKE_C_COMPILER="${!target_CC}" -DCMAKE_CXX_COMPILER="${!target_CXX}" \ - -DCMAKE_C_FLAGS="${!target_CFLAGS}" -DCMAKE_CXX_FLAGS="${!target_CXXFLAGS}" \ - -DCMAKE_C_COMPILER_TARGET=$target -DCMAKE_CXX_COMPILER_TARGET=$target \ - -DLLVM_ENABLE_WARNINGS=1 -DLIBUNWIND_ENABLE_WERROR=1 -DLIBUNWIND_ENABLE_PEDANTIC=0 \ - -DLLVM_PATH=../../llvm/ ../ - make unwind_static - install -D "lib/libunwind.a" "/${target}/lib/libunwind.a" - - popd - rm -rf ${dir_name} - - { set +x; } 2>/dev/null -} - -set -x hide_output install_prereq -build_unwind diff --git a/src/librustc_target/spec/x86_64_fortanix_unknown_sgx.rs b/src/librustc_target/spec/x86_64_fortanix_unknown_sgx.rs index 81974769cafb8..d01545619c8fa 100644 --- a/src/librustc_target/spec/x86_64_fortanix_unknown_sgx.rs +++ b/src/librustc_target/spec/x86_64_fortanix_unknown_sgx.rs @@ -48,8 +48,10 @@ pub fn target() -> Result { "ENCLAVE_SIZE", "CFGDATA_BASE", "DEBUG", - "EH_FRM_HDR_BASE", - "EH_FRM_HDR_SIZE", + "EH_FRM_HDR_OFFSET", + "EH_FRM_HDR_LEN", + "EH_FRM_OFFSET", + "EH_FRM_LEN", "TEXT_BASE", "TEXT_SIZE", ]; diff --git a/src/libstd/sys/sgx/abi/entry.S b/src/libstd/sys/sgx/abi/entry.S index fc6ce5770338e..f61bcf06f0815 100644 --- a/src/libstd/sys/sgx/abi/entry.S +++ b/src/libstd/sys/sgx/abi/entry.S @@ -11,7 +11,7 @@ IMAGE_BASE: .long 1 /* type = NT_VERSION */ 0: .asciz "toolchain-version" /* name */ 1: .align 4 -2: .long 0 /* desc - toolchain version number, 32-bit LE */ +2: .long 1 /* desc - toolchain version number, 32-bit LE */ 3: .align 4 .section .rodata @@ -60,10 +60,14 @@ IMAGE_BASE: globvar TEXT_BASE 8 /* The size in bytes of enclacve text section */ globvar TEXT_SIZE 8 - /* The base address (relative to enclave start) of the enclave EH_FRM_HDR section */ - globvar EH_FRM_HDR_BASE 8 - /* The size in bytes of enclacve EH_FRM_HDR section */ - globvar EH_FRM_HDR_SIZE 8 + /* The base address (relative to enclave start) of the enclave .eh_frame_hdr section */ + globvar EH_FRM_HDR_OFFSET 8 + /* The size in bytes of enclave .eh_frame_hdr section */ + globvar EH_FRM_HDR_LEN 8 + /* The base address (relative to enclave start) of the enclave .eh_frame section */ + globvar EH_FRM_OFFSET 8 + /* The size in bytes of enclacve .eh_frame section */ + globvar EH_FRM_LEN 8 .org .Lxsave_clear+512 .Lxsave_header: diff --git a/src/libunwind/build.rs b/src/libunwind/build.rs index f6f16f686e557..31d235cf679db 100644 --- a/src/libunwind/build.rs +++ b/src/libunwind/build.rs @@ -9,6 +9,8 @@ fn main() { { // Build the unwinding from libunwind C/C++ source code. llvm_libunwind::compile(); + } else if target.contains("x86_64-fortanix-unknown-sgx") { + llvm_libunwind::compile(); } else if target.contains("linux") { if target.contains("musl") { // linking for musl is handled in lib.rs @@ -55,6 +57,7 @@ mod llvm_libunwind { /// Compile the libunwind C/C++ source code. pub fn compile() { + let target = env::var("TARGET").expect("TARGET was not set"); let target_env = env::var("CARGO_CFG_TARGET_ENV").unwrap(); let target_vendor = env::var("CARGO_CFG_TARGET_VENDOR").unwrap(); let target_endian_little = env::var("CARGO_CFG_TARGET_ENDIAN").unwrap() != "big"; @@ -75,6 +78,35 @@ mod llvm_libunwind { cfg.flag("/EHsc"); cfg.define("_CRT_SECURE_NO_WARNINGS", None); cfg.define("_LIBUNWIND_DISABLE_VISIBILITY_ANNOTATIONS", None); + } else if target.contains("x86_64-fortanix-unknown-sgx") { + cfg.cpp(false); + + cfg.static_flag(true); + cfg.opt_level(3); + + cfg.flag("-nostdinc++"); + cfg.flag("-fno-exceptions"); + cfg.flag("-fno-rtti"); + cfg.flag("-fstrict-aliasing"); + cfg.flag("-funwind-tables"); + cfg.flag("-fvisibility=hidden"); + cfg.flag("-fno-stack-protector"); + cfg.flag("-ffreestanding"); + cfg.flag("-fexceptions"); + + // easiest way to undefine since no API available in cc::Build to undefine + cfg.flag("-U_FORTIFY_SOURCE"); + cfg.define("_FORTIFY_SOURCE", "0"); + + cfg.flag_if_supported("-fvisibility-global-new-delete-hidden"); + + cfg.define("_LIBUNWIND_DISABLE_VISIBILITY_ANNOTATIONS", None); + cfg.define("RUST_SGX", "1"); + cfg.define("__NO_STRING_INLINES", None); + cfg.define("__NO_MATH_INLINES", None); + cfg.define("_LIBUNWIND_IS_BAREMETAL", None); + cfg.define("__LIBUNWIND_IS_NATIVE_ONLY", None); + cfg.define("NDEBUG", None); } else { cfg.flag("-std=c99"); cfg.flag("-std=c++11"); @@ -103,6 +135,10 @@ mod llvm_libunwind { unwind_sources.push("Unwind_AppleExtras.cpp"); } + if target.contains("x86_64-fortanix-unknown-sgx") { + unwind_sources.push("UnwindRustSgx.c"); + } + let root = Path::new("../llvm-project/libunwind"); cfg.include(root.join("include")); for src in unwind_sources { From 343a9212b0421b5d81c15b2ef4ef0f02c19d2850 Mon Sep 17 00:00:00 2001 From: Adrian Cruceru Date: Thu, 25 Jun 2020 12:01:02 +0200 Subject: [PATCH 2/3] Fix comments --- src/bootstrap/compile.rs | 7 +++++++ src/ci/docker/dist-various-2/Dockerfile | 2 +- .../build-x86_64-fortanix-unknown-sgx-toolchain.sh | 7 ------- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/bootstrap/compile.rs b/src/bootstrap/compile.rs index 5fb489cb9ac5a..345741d900911 100644 --- a/src/bootstrap/compile.rs +++ b/src/bootstrap/compile.rs @@ -133,6 +133,13 @@ fn copy_third_party_objects( ) -> Vec<(PathBuf, DependencyType)> { let mut target_deps = vec![]; + // FIXME: remove this in 2021 + if target == "x86_64-fortanix-unknown-sgx" { + if env::var_os("X86_FORTANIX_SGX_LIBS").is_some() { + builder.info("Warning: X86_FORTANIX_SGX_LIBS environment variable is ignored, libunwind is now compiled as part of rustbuild"); + } + } + if builder.config.sanitizers && compiler.stage != 0 { // The sanitizers are only copied in stage1 or above, // to avoid creating dependency on LLVM. diff --git a/src/ci/docker/dist-various-2/Dockerfile b/src/ci/docker/dist-various-2/Dockerfile index b495d8c2daeb1..208d45fa83fe1 100644 --- a/src/ci/docker/dist-various-2/Dockerfile +++ b/src/ci/docker/dist-various-2/Dockerfile @@ -73,7 +73,7 @@ COPY dist-various-2/x86_64-fortanix-unknown-sgx-clang-wrap.sh /usr/bin/x86_64-fo RUN ln -s /usr/bin/x86_64-fortanix-unknown-sgx-clang-11 /usr/bin/x86_64-fortanix-unknown-sgx-clang++-11 # We pass the commit id of the port of LLVM's libunwind to the build script. # Any update to the commit id here, should cause the container image to be re-built from this point on. -RUN /tmp/build-x86_64-fortanix-unknown-sgx-toolchain.sh "800f95131fe6acd20b96b6f4723ca3c820f3d379" +RUN /tmp/build-x86_64-fortanix-unknown-sgx-toolchain.sh COPY dist-various-2/build-wasi-toolchain.sh /tmp/ RUN /tmp/build-wasi-toolchain.sh diff --git a/src/ci/docker/dist-various-2/build-x86_64-fortanix-unknown-sgx-toolchain.sh b/src/ci/docker/dist-various-2/build-x86_64-fortanix-unknown-sgx-toolchain.sh index 46a0ca7290999..78bf4527feff1 100755 --- a/src/ci/docker/dist-various-2/build-x86_64-fortanix-unknown-sgx-toolchain.sh +++ b/src/ci/docker/dist-various-2/build-x86_64-fortanix-unknown-sgx-toolchain.sh @@ -3,14 +3,7 @@ set -eu source shared.sh -if [ -z "$1" ]; then - echo "Usage: ${0} " - exit -1 -fi - target="x86_64-fortanix-unknown-sgx" -url="https://github.com/fortanix/llvm-project/archive/${1}.tar.gz" -repo_name="llvm-project" install_prereq() { curl https://apt.llvm.org/llvm-snapshot.gpg.key|apt-key add - From db6fa2fff58c1071f66d3b91e1baf626571e0bcd Mon Sep 17 00:00:00 2001 From: Adrian Cruceru Date: Thu, 25 Jun 2020 12:30:27 +0200 Subject: [PATCH 3/3] Remove deprecated comments --- src/ci/docker/dist-various-2/Dockerfile | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/ci/docker/dist-various-2/Dockerfile b/src/ci/docker/dist-various-2/Dockerfile index 208d45fa83fe1..1f1c246002352 100644 --- a/src/ci/docker/dist-various-2/Dockerfile +++ b/src/ci/docker/dist-various-2/Dockerfile @@ -71,8 +71,6 @@ RUN /tmp/build-solaris-toolchain.sh sparcv9 sparcv9 solaris-sparc COPY dist-various-2/build-x86_64-fortanix-unknown-sgx-toolchain.sh /tmp/ COPY dist-various-2/x86_64-fortanix-unknown-sgx-clang-wrap.sh /usr/bin/x86_64-fortanix-unknown-sgx-clang-11 RUN ln -s /usr/bin/x86_64-fortanix-unknown-sgx-clang-11 /usr/bin/x86_64-fortanix-unknown-sgx-clang++-11 -# We pass the commit id of the port of LLVM's libunwind to the build script. -# Any update to the commit id here, should cause the container image to be re-built from this point on. RUN /tmp/build-x86_64-fortanix-unknown-sgx-toolchain.sh COPY dist-various-2/build-wasi-toolchain.sh /tmp/