Skip to content

Commit 0a1a475

Browse files
author
Jethro Beekman
committed
SGX target: Use linker option to avoid code CGU assignment kludge
1 parent f694222 commit 0a1a475

File tree

5 files changed

+53
-41
lines changed

5 files changed

+53
-41
lines changed

src/librustc_target/spec/x86_64_fortanix_unknown_sgx.rs

+9
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ pub fn target() -> Result<Target, String> {
2121
"-Wl,--no-undefined-version",
2222
"-Wl,-Bsymbolic",
2323
"-Wl,--export-dynamic",
24+
// The following symbols are needed by libunwind, which is linked after
25+
// libstd. Make sure they're included in the link.
26+
"-Wl,-u,__rust_abort",
27+
"-Wl,-u,__rust_c_alloc",
28+
"-Wl,-u,__rust_c_dealloc",
29+
"-Wl,-u,__rust_print_err",
30+
"-Wl,-u,__rust_rwlock_rdlock",
31+
"-Wl,-u,__rust_rwlock_unlock",
32+
"-Wl,-u,__rust_rwlock_wrlock",
2433
];
2534

2635
const EXPORT_SYMBOLS: &[&str] = &[

src/libstd/sys/sgx/alloc.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::alloc::{GlobalAlloc, Layout, System};
1+
use crate::alloc::{self, GlobalAlloc, Layout, System};
22

33
use super::waitqueue::SpinMutex;
44

@@ -30,3 +30,17 @@ unsafe impl GlobalAlloc for System {
3030
DLMALLOC.lock().realloc(ptr, layout.size(), layout.align(), new_size)
3131
}
3232
}
33+
34+
// The following functions are needed by libunwind. These symbols are named
35+
// in pre-link args for the target specification, so keep that in sync.
36+
#[cfg(not(test))]
37+
#[no_mangle]
38+
pub unsafe extern "C" fn __rust_c_alloc(size: usize, align: usize) -> *mut u8 {
39+
alloc::alloc(Layout::from_size_align_unchecked(size, align))
40+
}
41+
42+
#[cfg(not(test))]
43+
#[no_mangle]
44+
pub unsafe extern "C" fn __rust_c_dealloc(ptr: *mut u8, size: usize, align: usize) {
45+
alloc::dealloc(ptr, Layout::from_size_align_unchecked(size, align))
46+
}

src/libstd/sys/sgx/mod.rs

+9
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,15 @@ pub unsafe fn abort_internal() -> ! {
130130
abi::usercalls::exit(true)
131131
}
132132

133+
// This function is needed by the panic runtime. The symbol is named in
134+
// pre-link args for the target specification, so keep that in sync.
135+
#[cfg(not(test))]
136+
#[no_mangle]
137+
// NB. used by both libunwind and libpanic_abort
138+
pub unsafe extern "C" fn __rust_abort() {
139+
abort_internal();
140+
}
141+
133142
pub fn hashmap_random_keys() -> (u64, u64) {
134143
fn rdrand64() -> u64 {
135144
unsafe {

src/libstd/sys/sgx/rwlock.rs

+2-40
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
#[cfg(not(test))]
2-
use crate::alloc::{self, Layout};
31
use crate::num::NonZeroUsize;
4-
#[cfg(not(test))]
5-
use crate::slice;
6-
#[cfg(not(test))]
7-
use crate::str;
82

93
use super::waitqueue::{
104
try_lock_or_false, NotifiedTcs, SpinMutex, SpinMutexGuard, WaitQueue, WaitVariable,
@@ -165,10 +159,11 @@ impl RWLock {
165159
pub unsafe fn destroy(&self) {}
166160
}
167161

162+
// The following functions are needed by libunwind. These symbols are named
163+
// in pre-link args for the target specification, so keep that in sync.
168164
#[cfg(not(test))]
169165
const EINVAL: i32 = 22;
170166

171-
// used by libunwind port
172167
#[cfg(not(test))]
173168
#[no_mangle]
174169
pub unsafe extern "C" fn __rust_rwlock_rdlock(p: *mut RWLock) -> i32 {
@@ -198,39 +193,6 @@ pub unsafe extern "C" fn __rust_rwlock_unlock(p: *mut RWLock) -> i32 {
198193
return 0;
199194
}
200195

201-
// the following functions are also used by the libunwind port. They're
202-
// included here to make sure parallel codegen and LTO don't mess things up.
203-
#[cfg(not(test))]
204-
#[no_mangle]
205-
pub unsafe extern "C" fn __rust_print_err(m: *mut u8, s: i32) {
206-
if s < 0 {
207-
return;
208-
}
209-
let buf = slice::from_raw_parts(m as *const u8, s as _);
210-
if let Ok(s) = str::from_utf8(&buf[..buf.iter().position(|&b| b == 0).unwrap_or(buf.len())]) {
211-
eprint!("{}", s);
212-
}
213-
}
214-
215-
#[cfg(not(test))]
216-
#[no_mangle]
217-
// NB. used by both libunwind and libpanic_abort
218-
pub unsafe extern "C" fn __rust_abort() {
219-
crate::sys::abort_internal();
220-
}
221-
222-
#[cfg(not(test))]
223-
#[no_mangle]
224-
pub unsafe extern "C" fn __rust_c_alloc(size: usize, align: usize) -> *mut u8 {
225-
alloc::alloc(Layout::from_size_align_unchecked(size, align))
226-
}
227-
228-
#[cfg(not(test))]
229-
#[no_mangle]
230-
pub unsafe extern "C" fn __rust_c_dealloc(ptr: *mut u8, size: usize, align: usize) {
231-
alloc::dealloc(ptr, Layout::from_size_align_unchecked(size, align))
232-
}
233-
234196
#[cfg(test)]
235197
mod tests {
236198
use super::*;

src/libstd/sys/sgx/stdio.rs

+18
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ use fortanix_sgx_abi as abi;
22

33
use crate::io;
44
use crate::sys::fd::FileDesc;
5+
#[cfg(not(test))]
6+
use crate::slice;
7+
#[cfg(not(test))]
8+
use crate::str;
59

610
pub struct Stdin(());
711
pub struct Stdout(());
@@ -62,3 +66,17 @@ pub fn is_ebadf(err: &io::Error) -> bool {
6266
pub fn panic_output() -> Option<impl io::Write> {
6367
super::abi::panic::SgxPanicOutput::new()
6468
}
69+
70+
// This function is needed by libunwind. The symbol is named in pre-link args
71+
// for the target specification, so keep that in sync.
72+
#[cfg(not(test))]
73+
#[no_mangle]
74+
pub unsafe extern "C" fn __rust_print_err(m: *mut u8, s: i32) {
75+
if s < 0 {
76+
return;
77+
}
78+
let buf = slice::from_raw_parts(m as *const u8, s as _);
79+
if let Ok(s) = str::from_utf8(&buf[..buf.iter().position(|&b| b == 0).unwrap_or(buf.len())]) {
80+
eprint!("{}", s);
81+
}
82+
}

0 commit comments

Comments
 (0)