Skip to content

Commit 3e7543a

Browse files
committed
WIP: Cross-compilation for Redox target
1 parent f86e014 commit 3e7543a

File tree

20 files changed

+1462
-10
lines changed

20 files changed

+1462
-10
lines changed

src/liballoc_jemalloc/build.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ fn main() {
3636
// targets, which means we have to build the alloc_jemalloc crate
3737
// for targets like emscripten, even if we don't use it.
3838
if target.contains("rumprun") || target.contains("bitrig") || target.contains("openbsd") ||
39-
target.contains("msvc") || target.contains("emscripten") || target.contains("fuchsia") {
39+
target.contains("msvc") || target.contains("emscripten") || target.contains("fuchsia") ||
40+
target.contains("redox") {
4041
println!("cargo:rustc-cfg=dummy_jemalloc");
4142
return;
4243
}

src/liballoc_system/lib.rs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
issue = "27783")]
2020
#![feature(allocator)]
2121
#![feature(staged_api)]
22+
#![cfg_attr(target_os = "redox", feature(libc))]
2223
#![cfg_attr(unix, feature(libc))]
2324

2425
// The minimum alignment guaranteed by the architecture. This value is used to
@@ -71,7 +72,49 @@ pub extern "C" fn __rust_usable_size(size: usize, align: usize) -> usize {
7172
imp::usable_size(size, align)
7273
}
7374

74-
#[cfg(unix)]
75+
#[cfg(target_os = "redox")]
76+
mod imp {
77+
extern crate libc;
78+
79+
use core::cmp;
80+
use core::ptr;
81+
use MIN_ALIGN;
82+
83+
pub unsafe fn allocate(size: usize, _align: usize) -> *mut u8 {
84+
libc::malloc(size as libc::size_t) as *mut u8
85+
}
86+
87+
pub unsafe fn reallocate(ptr: *mut u8, old_size: usize, size: usize, align: usize) -> *mut u8 {
88+
if align <= MIN_ALIGN {
89+
libc::realloc(ptr as *mut libc::c_void, size as libc::size_t) as *mut u8
90+
} else {
91+
let new_ptr = allocate(size, align);
92+
if !new_ptr.is_null() {
93+
ptr::copy(ptr, new_ptr, cmp::min(size, old_size));
94+
deallocate(ptr, old_size, align);
95+
}
96+
new_ptr
97+
}
98+
}
99+
100+
pub unsafe fn reallocate_inplace(_ptr: *mut u8,
101+
old_size: usize,
102+
_size: usize,
103+
_align: usize)
104+
-> usize {
105+
old_size
106+
}
107+
108+
pub unsafe fn deallocate(ptr: *mut u8, _old_size: usize, _align: usize) {
109+
libc::free(ptr as *mut libc::c_void)
110+
}
111+
112+
pub fn usable_size(size: usize, _align: usize) -> usize {
113+
size
114+
}
115+
}
116+
117+
#[cfg(any(unix))]
75118
mod imp {
76119
extern crate libc;
77120

src/libstd/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ compiler_builtins = { path = "../libcompiler_builtins" }
2323
std_unicode = { path = "../libstd_unicode" }
2424
unwind = { path = "../libunwind" }
2525

26+
[replace]
27+
"core:0.0.0" = { path = "../libcore" }
28+
2629
[build-dependencies]
2730
build_helper = { path = "../build_helper" }
2831
gcc = "0.3.27"

src/libstd/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ fn main() {
2626
let target = env::var("TARGET").expect("TARGET was not set");
2727
let host = env::var("HOST").expect("HOST was not set");
2828
if cfg!(feature = "backtrace") && !target.contains("apple") && !target.contains("msvc") &&
29-
!target.contains("emscripten") && !target.contains("fuchsia") {
29+
!target.contains("emscripten") && !target.contains("fuchsia") && !target.contains("redox") {
3030
build_libbacktrace(&host, &target);
3131
}
3232

src/libstd/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@
299299
#![feature(unwind_attributes)]
300300
#![feature(vec_push_all)]
301301
#![feature(zero_one)]
302+
#![cfg_attr(target_os = "redox", feature(naked_functions))]
302303
#![cfg_attr(test, feature(update_panic_count))]
303304

304305
// Explicitly import the prelude. The compiler uses this same unstable attribute

src/libstd/sys/redox/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010

1111
#![allow(dead_code, missing_docs, bad_style)]
1212

13-
pub extern crate syscall;
14-
1513
use io::{self, ErrorKind};
1614

1715
pub mod args;
@@ -33,7 +31,9 @@ pub mod process;
3331
pub mod rand;
3432
pub mod rwlock;
3533
pub mod stack_overflow;
34+
pub mod start;
3635
pub mod stdio;
36+
pub mod syscall;
3737
pub mod thread;
3838
pub mod thread_local;
3939
pub mod time;

src/libstd/sys/redox/net/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ use vec::{IntoIter, Vec};
2020

2121
use self::dns::{Dns, DnsQuery};
2222

23-
pub extern crate libc as netc;
2423
pub use self::tcp::{TcpStream, TcpListener};
2524
pub use self::udp::UdpSocket;
2625

26+
pub mod netc;
27+
2728
mod dns;
2829
mod tcp;
2930
mod udp;

src/libstd/sys/redox/net/netc.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
pub type in_addr_t = u32;
2+
pub type in_port_t = u16;
3+
4+
pub type socklen_t = u32;
5+
pub type sa_family_t = u16;
6+
7+
pub const AF_INET: sa_family_t = 1;
8+
pub const AF_INET6: sa_family_t = 2;
9+
10+
#[derive(Copy, Clone)]
11+
#[repr(C)]
12+
pub struct in_addr {
13+
pub s_addr: in_addr_t,
14+
}
15+
16+
#[derive(Copy, Clone)]
17+
#[repr(C)]
18+
pub struct in6_addr {
19+
pub s6_addr: [u8; 16],
20+
__align: [u32; 0],
21+
}
22+
23+
#[derive(Copy, Clone)]
24+
#[repr(C)]
25+
pub struct sockaddr {
26+
pub sa_family: sa_family_t,
27+
pub sa_data: [u8; 14],
28+
}
29+
30+
#[derive(Copy, Clone)]
31+
#[repr(C)]
32+
pub struct sockaddr_in {
33+
pub sin_family: sa_family_t,
34+
pub sin_port: in_port_t,
35+
pub sin_addr: in_addr,
36+
pub sin_zero: [u8; 8],
37+
}
38+
39+
#[derive(Copy, Clone)]
40+
#[repr(C)]
41+
pub struct sockaddr_in6 {
42+
pub sin6_family: sa_family_t,
43+
pub sin6_port: in_port_t,
44+
pub sin6_flowinfo: u32,
45+
pub sin6_addr: in6_addr,
46+
pub sin6_scope_id: u32,
47+
}

src/libstd/sys/redox/rand.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,19 @@
99
// except according to those terms.
1010

1111
use io;
12-
use libc;
1312
use rand::Rng;
1413

15-
pub struct OsRng;
14+
// FIXME: Use rand:
15+
pub struct OsRng {
16+
state: [u64; 2]
17+
}
1618

1719
impl OsRng {
1820
/// Create a new `OsRng`.
1921
pub fn new() -> io::Result<OsRng> {
20-
Ok(OsRng)
22+
Ok(OsRng {
23+
state: [0xBADF00D1, 0xDEADBEEF]
24+
})
2125
}
2226
}
2327

@@ -26,7 +30,20 @@ impl Rng for OsRng {
2630
self.next_u64() as u32
2731
}
2832
fn next_u64(&mut self) -> u64 {
29-
unsafe { libc::random() }
33+
// Store the first and second part.
34+
let mut x = self.state[0];
35+
let y = self.state[1];
36+
37+
// Put the second part into the first slot.
38+
self.state[0] = y;
39+
// Twist the first slot.
40+
x ^= x << 23;
41+
// Update the second slot.
42+
self.state[1] = x ^ y ^ (x >> 17) ^ (y >> 26);
43+
44+
// Generate the final integer.
45+
self.state[1].wrapping_add(y)
46+
3047
}
3148
fn fill_bytes(&mut self, buf: &mut [u8]) {
3249
for chunk in buf.chunks_mut(8) {

src/libstd/sys/redox/start.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use sys::syscall::exit;
2+
3+
#[allow(private_no_mangle_fns)]
4+
#[no_mangle]
5+
#[naked]
6+
#[cfg(target_arch = "x86")]
7+
pub unsafe fn _start() {
8+
asm!("push esp
9+
call _start_stack
10+
pop esp"
11+
:
12+
:
13+
: "memory"
14+
: "intel", "volatile");
15+
let _ = exit(0);
16+
}
17+
18+
#[allow(private_no_mangle_fns)]
19+
#[no_mangle]
20+
#[naked]
21+
#[cfg(target_arch = "x86_64")]
22+
pub unsafe fn _start() {
23+
asm!("mov rdi, rsp
24+
and rsp, 0xFFFFFFFFFFFFFFF0
25+
call _start_stack"
26+
:
27+
:
28+
: "memory"
29+
: "intel", "volatile");
30+
let _ = exit(0);
31+
}
32+
33+
#[allow(private_no_mangle_fns)]
34+
#[no_mangle]
35+
pub unsafe extern "C" fn _start_stack(stack: *const usize){
36+
extern "C" {
37+
fn main(argc: usize, argv: *const *const u8) -> usize;
38+
}
39+
40+
let argc = *stack as usize;
41+
let argv = stack.offset(1) as *const *const u8;
42+
let _ = exit(main(argc, argv));
43+
}

0 commit comments

Comments
 (0)