Skip to content

Commit f3b1582

Browse files
committed
Update libunwind build process for x86_64-fortanix-unknown-sgx target
1 parent 3b1c08c commit f3b1582

File tree

6 files changed

+49
-64
lines changed

6 files changed

+49
-64
lines changed

src/bootstrap/compile.rs

-20
Original file line numberDiff line numberDiff line change
@@ -131,28 +131,8 @@ 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.
142-
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-
);
154-
}
155-
156136
if builder.config.sanitizers && compiler.stage != 0 {
157137
// The sanitizers are only copied in stage1 or above,
158138
// to avoid creating dependency on LLVM.

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

-2
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,6 @@ ENV TARGETS=$TARGETS,nvptx64-nvidia-cuda
105105
ENV TARGETS=$TARGETS,armv7-unknown-linux-gnueabi
106106
ENV TARGETS=$TARGETS,armv7-unknown-linux-musleabi
107107

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

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

-35
Original file line numberDiff line numberDiff line change
@@ -24,39 +24,4 @@ install_prereq() {
2424
clang-11
2525
}
2626

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
6127
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)