xref: /relibc/src/crti/src/lib.rs (revision ed19381547d66b76981ea1e4ff942c5a4da45ab7)
1 //! crti
2 
3 #![no_std]
4 #![feature(linkage)]
5 
6 use core::arch::global_asm;
7 
8 #[cfg(target_arch = "x86")]
9 global_asm!(
10     r#"
11     .section .init
12     .global _init
13     _init:
14         push ebp
15         mov ebp, esp
16         // Created a new stack frame and updated the stack pointer
17         // Body will be filled in by gcc and ended by crtn.o
18 
19     .section .fini
20     .global _fini
21     _fini:
22         push ebp
23         mov ebp, esp
24         // Created a new stack frame and updated the stack pointer
25         // Body will be filled in by gcc and ended by crtn.o
26 "#
27 );
28 
29 // https://wiki.osdev.org/Creating_a_C_Library#crtbegin.o.2C_crtend.o.2C_crti.o.2C_and_crtn.o
30 #[cfg(target_arch = "x86_64")]
31 global_asm!(
32     r#"
33     .section .init
34     .global _init
35     _init:
36         push rbp
37         mov rbp, rsp
38         // Created a new stack frame and updated the stack pointer
39         // Body will be filled in by gcc and ended by crtn.o
40 
41     .section .fini
42     .global _fini
43     _fini:
44         push rbp
45         mov rbp, rsp
46         // Created a new stack frame and updated the stack pointer
47         // Body will be filled in by gcc and ended by crtn.o
48 "#
49 );
50 
51 // https://git.musl-libc.org/cgit/musl/tree/crt/aarch64/crti.s
52 #[cfg(target_arch = "aarch64")]
53 global_asm!(
54     r#"
55     .section .init
56     .global _init
57     .type _init,%function
58     _init:
59         stp x29,x30,[sp,-16]!
60         mov x29,sp
61         // stp: "stores two doublewords from the first and second argument to memory addressed by addr"
62         // Body will be filled in by gcc and ended by crtn.o
63 
64     .section .fini
65     .global _fini
66     .type _fini,%function
67     _fini:
68         stp x29,x30,[sp,-16]!
69         mov x29,sp
70         // stp: "stores two doublewords from the first and second argument to memory addressed by addr"
71         // Body will be filled in by gcc and ended by crtn.o
72 "#
73 );
74 
75 #[linkage = "weak"]
76 #[no_mangle]
77 extern "C" fn relibc_panic(pi: &::core::panic::PanicInfo) -> ! {
78     loop {}
79 }
80 
81 #[panic_handler]
82 #[linkage = "weak"]
83 #[no_mangle]
84 pub unsafe extern "C" fn rust_begin_unwind(pi: &::core::panic::PanicInfo) -> ! {
85     relibc_panic(pi)
86 }
87