1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use std::borrow::Cow;

use super::{cvs, Cc, LinkerFlavor, Lld, Target, TargetOptions};

pub fn target() -> Target {
    let pre_link_args = TargetOptions::link_args(
        LinkerFlavor::Gnu(Cc::No, Lld::No),
        &[
            "-e",
            "elf_entry",
            "-Bstatic",
            "--gc-sections",
            "-z",
            "text",
            "-z",
            "norelro",
            "--no-undefined",
            "--error-unresolved-symbols",
            "--no-undefined-version",
            "-Bsymbolic",
            "--export-dynamic",
            // The following symbols are needed by libunwind, which is linked after
            // libstd. Make sure they're included in the link.
            "-u",
            "__rust_abort",
            "-u",
            "__rust_c_alloc",
            "-u",
            "__rust_c_dealloc",
            "-u",
            "__rust_print_err",
            "-u",
            "__rust_rwlock_rdlock",
            "-u",
            "__rust_rwlock_unlock",
            "-u",
            "__rust_rwlock_wrlock",
        ],
    );

    const EXPORT_SYMBOLS: &[&str] = &[
        "sgx_entry",
        "HEAP_BASE",
        "HEAP_SIZE",
        "RELA",
        "RELACOUNT",
        "ENCLAVE_SIZE",
        "CFGDATA_BASE",
        "DEBUG",
        "EH_FRM_HDR_OFFSET",
        "EH_FRM_HDR_LEN",
        "EH_FRM_OFFSET",
        "EH_FRM_LEN",
        "TEXT_BASE",
        "TEXT_SIZE",
    ];
    let opts = TargetOptions {
        os: "unknown".into(),
        env: "sgx".into(),
        vendor: "fortanix".into(),
        abi: "fortanix".into(),
        linker_flavor: LinkerFlavor::Gnu(Cc::No, Lld::Yes),
        linker: Some("rust-lld".into()),
        max_atomic_width: Some(64),
        cpu: "x86-64".into(),
        plt_by_default: false,
        features: "+rdrnd,+rdseed,+lvi-cfi,+lvi-load-hardening".into(),
        llvm_args: cvs!["--x86-experimental-lvi-inline-asm-hardening"],
        position_independent_executables: true,
        pre_link_args,
        override_export_symbols: Some(EXPORT_SYMBOLS.iter().cloned().map(Cow::from).collect()),
        relax_elf_relocations: true,
        ..Default::default()
    };
    Target {
        llvm_target: "x86_64-elf".into(),
        pointer_width: 64,
        data_layout: "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
            .into(),
        arch: "x86_64".into(),
        options: opts,
    }
}