Skip to content

Commit 38cbf15

Browse files
authored
Rollup merge of #72937 - AdrianCX:master, r=nikomatsakis
Fortanix SGX target libunwind build process changes Ticket: fortanix/rust-sgx#174 LLVM related changes (merged): rust-lang/llvm-project#57 Description: libunwind changes needed to run code in sgx environment via rust-sgx. Target that uses this in rust: x86_64-fortanix-unknown-sgx. Without this change, rust std for this toolchain is forced to use a precompiled library loaded via environment variable. With this change we act the same as musl target.
2 parents 8adc781 + db6fa2f commit 38cbf15

File tree

6 files changed

+54
-71
lines changed

6 files changed

+54
-71
lines changed

src/bootstrap/compile.rs

+4-17
Original file line numberDiff line numberDiff line change
@@ -131,26 +131,13 @@ fn copy_third_party_objects(
131131
compiler: &Compiler,
132132
target: Interned<String>,
133133
) -> Vec<(PathBuf, DependencyType)> {
134-
let libdir = builder.sysroot_libdir(*compiler, target);
135134
let mut target_deps = vec![];
136135

137-
// Copies libunwind.a compiled to be linked with x86_64-fortanix-unknown-sgx.
138-
//
139-
// This target needs to be linked to Fortanix's port of llvm's libunwind.
140-
// libunwind requires support for rwlock and printing to stderr,
141-
// which is provided by std for this target.
136+
// FIXME: remove this in 2021
142137
if target == "x86_64-fortanix-unknown-sgx" {
143-
let src_path_env = "X86_FORTANIX_SGX_LIBS";
144-
let src =
145-
env::var(src_path_env).unwrap_or_else(|_| panic!("{} not found in env", src_path_env));
146-
copy_and_stamp(
147-
builder,
148-
&*libdir,
149-
Path::new(&src),
150-
"libunwind.a",
151-
&mut target_deps,
152-
DependencyType::Target,
153-
);
138+
if env::var_os("X86_FORTANIX_SGX_LIBS").is_some() {
139+
builder.info("Warning: X86_FORTANIX_SGX_LIBS environment variable is ignored, libunwind is now compiled as part of rustbuild");
140+
}
154141
}
155142

156143
if builder.config.sanitizers && compiler.stage != 0 {

src/ci/docker/dist-various-2/Dockerfile

+1-5
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,7 @@ RUN /tmp/build-solaris-toolchain.sh sparcv9 sparcv9 solaris-sparc
7171
COPY dist-various-2/build-x86_64-fortanix-unknown-sgx-toolchain.sh /tmp/
7272
COPY dist-various-2/x86_64-fortanix-unknown-sgx-clang-wrap.sh /usr/bin/x86_64-fortanix-unknown-sgx-clang-11
7373
RUN ln -s /usr/bin/x86_64-fortanix-unknown-sgx-clang-11 /usr/bin/x86_64-fortanix-unknown-sgx-clang++-11
74-
# We pass the commit id of the port of LLVM's libunwind to the build script.
75-
# Any update to the commit id here, should cause the container image to be re-built from this point on.
76-
RUN /tmp/build-x86_64-fortanix-unknown-sgx-toolchain.sh "800f95131fe6acd20b96b6f4723ca3c820f3d379"
74+
RUN /tmp/build-x86_64-fortanix-unknown-sgx-toolchain.sh
7775

7876
COPY dist-various-2/build-wasi-toolchain.sh /tmp/
7977
RUN /tmp/build-wasi-toolchain.sh
@@ -105,8 +103,6 @@ ENV TARGETS=$TARGETS,nvptx64-nvidia-cuda
105103
ENV TARGETS=$TARGETS,armv7-unknown-linux-gnueabi
106104
ENV TARGETS=$TARGETS,armv7-unknown-linux-musleabi
107105

108-
ENV X86_FORTANIX_SGX_LIBS="/x86_64-fortanix-unknown-sgx/lib/"
109-
110106
# As per https://bugs.launchpad.net/ubuntu/+source/gcc-defaults/+bug/1300211
111107
# we need asm in the search path for gcc-7 (for gnux32) but not in the search path of the
112108
# cross compilers.

src/ci/docker/dist-various-2/build-x86_64-fortanix-unknown-sgx-toolchain.sh

-42
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,7 @@
33
set -eu
44
source shared.sh
55

6-
if [ -z "$1" ]; then
7-
echo "Usage: ${0} <commit_id>"
8-
exit -1
9-
fi
10-
116
target="x86_64-fortanix-unknown-sgx"
12-
url="https://github.com/fortanix/llvm-project/archive/${1}.tar.gz"
13-
repo_name="llvm-project"
147

158
install_prereq() {
169
curl https://apt.llvm.org/llvm-snapshot.gpg.key|apt-key add -
@@ -24,39 +17,4 @@ install_prereq() {
2417
clang-11
2518
}
2619

27-
build_unwind() {
28-
set -x
29-
dir_name="${target}_temp"
30-
rm -rf ${dir_name}
31-
mkdir -p ${dir_name}
32-
pushd ${dir_name}
33-
34-
# Clone Fortanix's fork of llvm-project which has a port of libunwind
35-
fetch_github_commit_archive "$repo_name" "$url"
36-
cd "${repo_name}/libunwind"
37-
38-
# Build libunwind
39-
mkdir -p build
40-
cd build
41-
target_CC="CC_${target//-/_}"
42-
target_CXX="CXX_${target//-/_}"
43-
target_CFLAGS="CFLAGS_${target//-/_}"
44-
target_CXXFLAGS="CXXFLAGS_${target//-/_}"
45-
cmake -DCMAKE_BUILD_TYPE="RELEASE" -DRUST_SGX=1 -G "Unix Makefiles" \
46-
-DCMAKE_C_COMPILER="${!target_CC}" -DCMAKE_CXX_COMPILER="${!target_CXX}" \
47-
-DCMAKE_C_FLAGS="${!target_CFLAGS}" -DCMAKE_CXX_FLAGS="${!target_CXXFLAGS}" \
48-
-DCMAKE_C_COMPILER_TARGET=$target -DCMAKE_CXX_COMPILER_TARGET=$target \
49-
-DLLVM_ENABLE_WARNINGS=1 -DLIBUNWIND_ENABLE_WERROR=1 -DLIBUNWIND_ENABLE_PEDANTIC=0 \
50-
-DLLVM_PATH=../../llvm/ ../
51-
make unwind_static
52-
install -D "lib/libunwind.a" "/${target}/lib/libunwind.a"
53-
54-
popd
55-
rm -rf ${dir_name}
56-
57-
{ set +x; } 2>/dev/null
58-
}
59-
60-
set -x
6120
hide_output install_prereq
62-
build_unwind

src/librustc_target/spec/x86_64_fortanix_unknown_sgx.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,10 @@ pub fn target() -> Result<Target, String> {
4848
"ENCLAVE_SIZE",
4949
"CFGDATA_BASE",
5050
"DEBUG",
51-
"EH_FRM_HDR_BASE",
52-
"EH_FRM_HDR_SIZE",
51+
"EH_FRM_HDR_OFFSET",
52+
"EH_FRM_HDR_LEN",
53+
"EH_FRM_OFFSET",
54+
"EH_FRM_LEN",
5355
"TEXT_BASE",
5456
"TEXT_SIZE",
5557
];

src/libstd/sys/sgx/abi/entry.S

+9-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ IMAGE_BASE:
1111
.long 1 /* type = NT_VERSION */
1212
0: .asciz "toolchain-version" /* name */
1313
1: .align 4
14-
2: .long 0 /* desc - toolchain version number, 32-bit LE */
14+
2: .long 1 /* desc - toolchain version number, 32-bit LE */
1515
3: .align 4
1616

1717
.section .rodata
@@ -60,10 +60,14 @@ IMAGE_BASE:
6060
globvar TEXT_BASE 8
6161
/* The size in bytes of enclacve text section */
6262
globvar TEXT_SIZE 8
63-
/* The base address (relative to enclave start) of the enclave EH_FRM_HDR section */
64-
globvar EH_FRM_HDR_BASE 8
65-
/* The size in bytes of enclacve EH_FRM_HDR section */
66-
globvar EH_FRM_HDR_SIZE 8
63+
/* The base address (relative to enclave start) of the enclave .eh_frame_hdr section */
64+
globvar EH_FRM_HDR_OFFSET 8
65+
/* The size in bytes of enclave .eh_frame_hdr section */
66+
globvar EH_FRM_HDR_LEN 8
67+
/* The base address (relative to enclave start) of the enclave .eh_frame section */
68+
globvar EH_FRM_OFFSET 8
69+
/* The size in bytes of enclacve .eh_frame section */
70+
globvar EH_FRM_LEN 8
6771

6872
.org .Lxsave_clear+512
6973
.Lxsave_header:

src/libunwind/build.rs

+36
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ fn main() {
99
{
1010
// Build the unwinding from libunwind C/C++ source code.
1111
llvm_libunwind::compile();
12+
} else if target.contains("x86_64-fortanix-unknown-sgx") {
13+
llvm_libunwind::compile();
1214
} else if target.contains("linux") {
1315
if target.contains("musl") {
1416
// linking for musl is handled in lib.rs
@@ -55,6 +57,7 @@ mod llvm_libunwind {
5557

5658
/// Compile the libunwind C/C++ source code.
5759
pub fn compile() {
60+
let target = env::var("TARGET").expect("TARGET was not set");
5861
let target_env = env::var("CARGO_CFG_TARGET_ENV").unwrap();
5962
let target_vendor = env::var("CARGO_CFG_TARGET_VENDOR").unwrap();
6063
let target_endian_little = env::var("CARGO_CFG_TARGET_ENDIAN").unwrap() != "big";
@@ -75,6 +78,35 @@ mod llvm_libunwind {
7578
cfg.flag("/EHsc");
7679
cfg.define("_CRT_SECURE_NO_WARNINGS", None);
7780
cfg.define("_LIBUNWIND_DISABLE_VISIBILITY_ANNOTATIONS", None);
81+
} else if target.contains("x86_64-fortanix-unknown-sgx") {
82+
cfg.cpp(false);
83+
84+
cfg.static_flag(true);
85+
cfg.opt_level(3);
86+
87+
cfg.flag("-nostdinc++");
88+
cfg.flag("-fno-exceptions");
89+
cfg.flag("-fno-rtti");
90+
cfg.flag("-fstrict-aliasing");
91+
cfg.flag("-funwind-tables");
92+
cfg.flag("-fvisibility=hidden");
93+
cfg.flag("-fno-stack-protector");
94+
cfg.flag("-ffreestanding");
95+
cfg.flag("-fexceptions");
96+
97+
// easiest way to undefine since no API available in cc::Build to undefine
98+
cfg.flag("-U_FORTIFY_SOURCE");
99+
cfg.define("_FORTIFY_SOURCE", "0");
100+
101+
cfg.flag_if_supported("-fvisibility-global-new-delete-hidden");
102+
103+
cfg.define("_LIBUNWIND_DISABLE_VISIBILITY_ANNOTATIONS", None);
104+
cfg.define("RUST_SGX", "1");
105+
cfg.define("__NO_STRING_INLINES", None);
106+
cfg.define("__NO_MATH_INLINES", None);
107+
cfg.define("_LIBUNWIND_IS_BAREMETAL", None);
108+
cfg.define("__LIBUNWIND_IS_NATIVE_ONLY", None);
109+
cfg.define("NDEBUG", None);
78110
} else {
79111
cfg.flag("-std=c99");
80112
cfg.flag("-std=c++11");
@@ -103,6 +135,10 @@ mod llvm_libunwind {
103135
unwind_sources.push("Unwind_AppleExtras.cpp");
104136
}
105137

138+
if target.contains("x86_64-fortanix-unknown-sgx") {
139+
unwind_sources.push("UnwindRustSgx.c");
140+
}
141+
106142
let root = Path::new("../llvm-project/libunwind");
107143
cfg.include(root.join("include"));
108144
for src in unwind_sources {

0 commit comments

Comments
 (0)