|
4 | 4 | use core::arch::asm; |
5 | 5 |
|
6 | 6 | #[cfg(target_arch = "x86_64")] |
7 | | -pub unsafe fn write(fd: usize, msg: *const u8, len: usize) -> isize { |
8 | | - let sys_nr: usize = 1; |
9 | | - let ret: isize; |
10 | | - asm!( |
11 | | - "syscall", |
12 | | - in("rax") sys_nr, |
13 | | - in("rdi") fd, |
14 | | - in("rsi") msg, |
15 | | - in("rdx") len, |
16 | | - lateout("rax") ret, |
17 | | - ); |
18 | | - ret |
| 7 | +mod consts { |
| 8 | + pub const SYS_EXIT: usize = 60; |
| 9 | + pub const SYS_WRITE: usize = 1; |
| 10 | +} |
| 11 | +#[cfg(target_arch = "aarch64")] |
| 12 | +mod consts { |
| 13 | + pub const SYS_EXIT: usize = 93; |
| 14 | + pub const SYS_WRITE: usize = 64; |
19 | 15 | } |
| 16 | +use consts::*; |
| 17 | +const HELLO: &[u8] = include_bytes!("hello.txt"); |
20 | 18 |
|
21 | | -#[cfg(target_arch = "x86_64")] |
22 | | -pub unsafe fn exit(ret: isize) -> ! { |
23 | | - let sys_nr: usize = 60; |
24 | | - asm!( |
25 | | - "syscall", |
26 | | - in("rax") sys_nr, |
27 | | - in("rdi") ret, |
28 | | - options(noreturn), |
29 | | - ); |
| 19 | +fn exit(ret: isize) -> ! { |
| 20 | + #[cfg(target_arch = "x86_64")] |
| 21 | + unsafe { |
| 22 | + asm!( |
| 23 | + "syscall", |
| 24 | + in("rax") SYS_EXIT, |
| 25 | + in("rdi") ret, |
| 26 | + options(noreturn), |
| 27 | + ); |
| 28 | + } |
| 29 | + #[cfg(target_arch = "aarch64")] |
| 30 | + unsafe { |
| 31 | + asm!( |
| 32 | + "svc #0", |
| 33 | + in("x8") SYS_EXIT, |
| 34 | + in("x0") ret, |
| 35 | + options(noreturn), |
| 36 | + ); |
| 37 | + } |
30 | 38 | } |
31 | 39 |
|
32 | | -#[cfg(target_arch = "aarch64")] |
33 | | -pub unsafe fn write(fd: usize, msg: *const u8, len: usize) -> isize { |
34 | | - let sys_nr: usize = 64; |
| 40 | +fn write(fd: usize, msg: *const u8, len: usize) -> isize { |
35 | 41 | let ret: isize; |
36 | | - asm!( |
37 | | - "svc #0", |
38 | | - in("x8") sys_nr, |
39 | | - in("x0") fd, |
40 | | - in("x1") msg, |
41 | | - in("x2") len, |
42 | | - lateout("x0") ret, |
43 | | - ); |
| 42 | + |
| 43 | + #[cfg(target_arch = "x86_64")] |
| 44 | + unsafe { |
| 45 | + asm!( |
| 46 | + "syscall", |
| 47 | + in("rax") SYS_WRITE, |
| 48 | + in("rdi") fd, |
| 49 | + in("rsi") msg, |
| 50 | + in("rdx") len, |
| 51 | + lateout("rax") ret, |
| 52 | + ); |
| 53 | + } |
| 54 | + #[cfg(target_arch = "aarch64")] |
| 55 | + unsafe { |
| 56 | + asm!( |
| 57 | + "adr x1, {msg}", // must use PC-relative addressing! which is natural in x86, but tricky here |
| 58 | + "svc #0", |
| 59 | + in("x8") SYS_WRITE, |
| 60 | + in("x0") fd, |
| 61 | + in("x2") len, |
| 62 | + lateout("x0") ret, |
| 63 | + msg = sym MSG |
| 64 | + ); |
| 65 | + } |
| 66 | + |
44 | 67 | ret |
45 | 68 | } |
46 | 69 |
|
47 | 70 | #[cfg(target_arch = "aarch64")] |
48 | | -pub unsafe fn exit(ret: isize) -> ! { |
49 | | - let sys_nr: usize = 93; |
50 | | - asm!( |
51 | | - "svc #0", |
52 | | - in("x8") sys_nr, |
53 | | - in("x0") ret, |
54 | | - options(noreturn), |
55 | | - ); |
56 | | -} |
57 | | - |
58 | | -const HELLO: &[u8] = include_bytes!("hello.txt"); |
| 71 | +#[link_section = ".text.msg"] |
| 72 | +// see linker.ld : we must control where this symbol is placed otherwise it wont be reachable when elf headers are lost (raw binary ) |
| 73 | +#[no_mangle] |
| 74 | +static MSG: [u8; HELLO.len()] = *include_bytes!("hello.txt"); |
59 | 75 |
|
60 | 76 | #[no_mangle] |
61 | | -pub extern "C" fn _start() { |
62 | | - unsafe { |
63 | | - let _ = write(1, HELLO.as_ptr(), HELLO.len()); |
64 | | - exit(0); |
65 | | - } |
| 77 | +fn _start() { |
| 78 | + let _ = write(1, HELLO.as_ptr(), HELLO.len()); |
| 79 | + exit(0); |
66 | 80 | } |
67 | 81 |
|
68 | 82 | #[panic_handler] |
|
0 commit comments