Skip to content

Commit b473157

Browse files
Rollup merge of #56067 - jethrogb:jb/sgx-target-spec, r=alexcrichton
Add SGX target to rustc This adds the `x86_64-fortanix-unknown-sgx` target specification to the Rust compiler. See #56066 for more details about this target.
2 parents 1646fc9 + 9e2e575 commit b473157

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

src/librustc_codegen_ssa/back/linker.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1050,6 +1050,10 @@ impl<'a> Linker for WasmLd<'a> {
10501050
}
10511051

10521052
fn exported_symbols(tcx: TyCtxt, crate_type: CrateType) -> Vec<String> {
1053+
if let Some(ref exports) = tcx.sess.target.target.options.override_export_symbols {
1054+
return exports.clone()
1055+
}
1056+
10531057
let mut symbols = Vec::new();
10541058

10551059
let export_threshold = symbol_export::crates_export_threshold(&[crate_type]);

src/librustc_target/spec/mod.rs

+17
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,8 @@ supported_targets! {
414414
("riscv32imac-unknown-none-elf", riscv32imac_unknown_none_elf),
415415

416416
("aarch64-unknown-none", aarch64_unknown_none),
417+
418+
("x86_64-fortanix-unknown-sgx", x86_64_fortanix_unknown_sgx),
417419
}
418420

419421
/// Everything `rustc` knows about how to compile for a specific target.
@@ -685,6 +687,10 @@ pub struct TargetOptions {
685687
/// target features. This is `true` by default, and `false` for targets like
686688
/// wasm32 where the whole program either has simd or not.
687689
pub simd_types_indirect: bool,
690+
691+
/// If set, have the linker export exactly these symbols, instead of using
692+
/// the usual logic to figure this out from the crate itself.
693+
pub override_export_symbols: Option<Vec<String>>
688694
}
689695

690696
impl Default for TargetOptions {
@@ -765,6 +771,7 @@ impl Default for TargetOptions {
765771
emit_debug_gdb_scripts: true,
766772
requires_uwtable: false,
767773
simd_types_indirect: true,
774+
override_export_symbols: None,
768775
}
769776
}
770777
}
@@ -900,6 +907,14 @@ impl Target {
900907
)
901908
);
902909
} );
910+
($key_name:ident, opt_list) => ( {
911+
let name = (stringify!($key_name)).replace("_", "-");
912+
obj.find(&name[..]).map(|o| o.as_array()
913+
.map(|v| base.options.$key_name = Some(v.iter()
914+
.map(|a| a.as_string().unwrap().to_string()).collect())
915+
)
916+
);
917+
} );
903918
($key_name:ident, optional) => ( {
904919
let name = (stringify!($key_name)).replace("_", "-");
905920
if let Some(o) = obj.find(&name[..]) {
@@ -1046,6 +1061,7 @@ impl Target {
10461061
key!(emit_debug_gdb_scripts, bool);
10471062
key!(requires_uwtable, bool);
10481063
key!(simd_types_indirect, bool);
1064+
key!(override_export_symbols, opt_list);
10491065

10501066
if let Some(array) = obj.find("abi-blacklist").and_then(Json::as_array) {
10511067
for name in array.iter().filter_map(|abi| abi.as_string()) {
@@ -1255,6 +1271,7 @@ impl ToJson for Target {
12551271
target_option_val!(emit_debug_gdb_scripts);
12561272
target_option_val!(requires_uwtable);
12571273
target_option_val!(simd_types_indirect);
1274+
target_option_val!(override_export_symbols);
12581275

12591276
if default.abi_blacklist != self.options.abi_blacklist {
12601277
d.insert("abi-blacklist".to_string(), self.options.abi_blacklist.iter()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use std::iter;
12+
13+
use super::{LinkerFlavor, Target, TargetOptions, PanicStrategy};
14+
15+
pub fn target() -> Result<Target, String> {
16+
const PRE_LINK_ARGS: &[&str] = &[
17+
"-Wl,--as-needed",
18+
"-Wl,-z,noexecstack",
19+
"-m64",
20+
"-fuse-ld=gold",
21+
"-nostdlib",
22+
"-shared",
23+
"-Wl,-e,sgx_entry",
24+
"-Wl,-Bstatic",
25+
"-Wl,--gc-sections",
26+
"-Wl,-z,text",
27+
"-Wl,-z,norelro",
28+
"-Wl,--rosegment",
29+
"-Wl,--no-undefined",
30+
"-Wl,--error-unresolved-symbols",
31+
"-Wl,--no-undefined-version",
32+
"-Wl,-Bsymbolic",
33+
"-Wl,--export-dynamic",
34+
];
35+
const EXPORT_SYMBOLS: &[&str] = &[
36+
"sgx_entry",
37+
"HEAP_BASE",
38+
"HEAP_SIZE",
39+
"RELA",
40+
"RELACOUNT",
41+
"ENCLAVE_SIZE",
42+
"CFGDATA_BASE",
43+
"DEBUG",
44+
];
45+
let opts = TargetOptions {
46+
dynamic_linking: false,
47+
executables: true,
48+
linker_is_gnu: true,
49+
max_atomic_width: Some(64),
50+
panic_strategy: PanicStrategy::Abort,
51+
cpu: "x86-64".into(),
52+
position_independent_executables: true,
53+
pre_link_args: iter::once(
54+
(LinkerFlavor::Gcc, PRE_LINK_ARGS.iter().cloned().map(String::from).collect())
55+
).collect(),
56+
override_export_symbols: Some(EXPORT_SYMBOLS.iter().cloned().map(String::from).collect()),
57+
..Default::default()
58+
};
59+
Ok(Target {
60+
llvm_target: "x86_64-unknown-linux-gnu".into(),
61+
target_endian: "little".into(),
62+
target_pointer_width: "64".into(),
63+
target_c_int_width: "32".into(),
64+
target_os: "unknown".into(),
65+
target_env: "sgx".into(),
66+
target_vendor: "fortanix".into(),
67+
data_layout: "e-m:e-i64:64-f80:128-n8:16:32:64-S128".into(),
68+
arch: "x86_64".into(),
69+
linker_flavor: LinkerFlavor::Gcc,
70+
options: opts,
71+
})
72+
}

0 commit comments

Comments
 (0)