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
84
85
86
87
88
89
90
91
92
use arch::debug;
use core::fmt::Write;
use core::mem;
use libc::{c_char,c_int,c_void,size_t,ssize_t,off_t};
use logging::Writer;

extern {
    static init_array_start: extern "C" fn();
    static init_array_end: extern "C" fn();
}

static mut errno: c_int = 0;

#[no_mangle]
pub unsafe extern fn __errno() -> *mut c_int {
    &mut errno
}

#[no_mangle]
pub unsafe extern fn __assert_fail(assertion: *const c_char, file: *const c_char, line: c_int, function: *const c_char) -> ! {
    let mut writer = Writer::get(module_path!());
    debug::put_cstr(file);
    let _ = write!(&mut writer, "({}): in function ", line);
    debug::put_cstr(function);
    let _ = write!(&mut writer, ": ");
    debug::put_cstr(assertion);
    mem::drop(writer);
    panic!("assertion failed in C code");
}

#[no_mangle]
pub extern fn __stack_chk_fail() -> ! {
    panic!("__stack_chk_fail was called")
}

#[no_mangle]
pub extern fn _fputwc_r(_ptr: *mut c_void, _wc: char, _fp: *mut c_void) -> c_int {
    panic!("_fputwc_r was called")
}

#[no_mangle]
pub extern fn _exit(_n: c_int) -> ! {
    panic!("_exit was called")
}

#[no_mangle]
pub extern fn kill(_pid: c_int, _sig: c_int) -> c_int {
    panic!("kill was called")
}

#[no_mangle]
pub extern fn getpid() -> c_int {
    panic!("getpid was called")
}

#[no_mangle]
pub extern fn write(_fd: c_int, _buf: *const c_void, _count: size_t) -> ssize_t {
    panic!("write was called")
}

#[no_mangle]
pub extern fn close(_fd: c_int) -> c_int {
    panic!("close was called")
}

#[no_mangle]
pub extern fn fstat(_fd: c_int, _buf: *mut c_void) -> c_int {
    panic!("fstat was called")
}

#[no_mangle]
pub extern fn isatty(_fd: c_int) -> c_int {
    panic!("isatty was called")
}

#[no_mangle]
pub extern fn lseek(_fd: c_int, _offset: off_t, _whence: c_int) -> off_t {
    panic!("lseek was called")
}

#[no_mangle]
pub extern fn read(_fd: c_int, _buf: *mut c_void, _count: size_t) -> ssize_t {
    panic!("read was called")
}

pub unsafe fn init() {
    let mut ptr = &init_array_start as *const _;
    while ptr < &init_array_end {
        (*ptr)();
        ptr = ptr.offset(1);
    }
}