xref: /aosp_15_r20/prebuilts/rust/linux-x86/1.80.1/src/stdlibs/library/rtstartup/rsbegin.rs (revision b40554a23088fb75aa6945dfe8e65169c8484da3)
1 // rsbegin.o and rsend.o are the so called "compiler runtime startup objects".
2 // They contain code needed to correctly initialize the compiler runtime.
3 //
4 // When an executable or dylib image is linked, all user code and libraries are
5 // "sandwiched" between these two object files, so code or data from rsbegin.o
6 // become first in the respective sections of the image, whereas code and data
7 // from rsend.o become the last ones. This effect can be used to place symbols
8 // at the beginning or at the end of a section, as well as to insert any required
9 // headers or footers.
10 //
11 // Note that the actual module entry point is located in the C runtime startup
12 // object (usually called `crtX.o`), which then invokes initialization callbacks
13 // of other runtime components (registered via yet another special image section).
14 
15 #![feature(no_core)]
16 #![feature(lang_items)]
17 #![feature(auto_traits)]
18 #![crate_type = "rlib"]
19 #![no_core]
20 #![allow(non_camel_case_types)]
21 #![allow(internal_features)]
22 
23 #[lang = "sized"]
24 trait Sized {}
25 #[lang = "sync"]
26 auto trait Sync {}
27 #[lang = "copy"]
28 trait Copy {}
29 #[lang = "freeze"]
30 auto trait Freeze {}
31 
32 #[lang = "drop_in_place"]
33 #[inline]
34 #[allow(unconditional_recursion)]
drop_in_place<T: ?Sized>(to_drop: *mut T)35 pub unsafe fn drop_in_place<T: ?Sized>(to_drop: *mut T) {
36     drop_in_place(to_drop);
37 }
38 
39 // Frame unwind info registration
40 //
41 // Each module's image contains a frame unwind info section (usually
42 // ".eh_frame").  When a module is loaded/unloaded into the process, the
43 // unwinder must be informed about the location of this section in memory. The
44 // methods of achieving that vary by the platform.  On some (e.g., Linux), the
45 // unwinder can discover unwind info sections on its own (by dynamically
46 // enumerating currently loaded modules via the dl_iterate_phdr() API and
47 // finding their ".eh_frame" sections); Others, like Windows, require modules
48 // to actively register their unwind info sections via unwinder API.
49 #[cfg(all(target_os = "windows", target_arch = "x86", target_env = "gnu"))]
50 pub mod eh_frames {
51     #[no_mangle]
52     #[link_section = ".eh_frame"]
53     // Marks beginning of the stack frame unwind info section
54     pub static __EH_FRAME_BEGIN__: [u8; 0] = [];
55 
56     // Scratch space for unwinder's internal book-keeping.
57     // This is defined as `struct object` in $GCC/libgcc/unwind-dw2-fde.h.
58     static mut OBJ: [isize; 6] = [0; 6];
59 
60     macro_rules! impl_copy {
61         ($($t:ty)*) => {
62             $(
63                 impl ::Copy for $t {}
64             )*
65         }
66     }
67 
68     impl_copy! {
69         usize u8 u16 u32 u64 u128
70         isize i8 i16 i32 i64 i128
71         f32 f64
72         bool char
73     }
74 
75     // Unwind info registration/deregistration routines.
76     extern "C" {
__register_frame_info(eh_frame_begin: *const u8, object: *mut u8)77         fn __register_frame_info(eh_frame_begin: *const u8, object: *mut u8);
__deregister_frame_info(eh_frame_begin: *const u8, object: *mut u8)78         fn __deregister_frame_info(eh_frame_begin: *const u8, object: *mut u8);
79     }
80 
init()81     unsafe extern "C" fn init() {
82         // register unwind info on module startup
83         __register_frame_info(&__EH_FRAME_BEGIN__ as *const u8, &mut OBJ as *mut _ as *mut u8);
84     }
85 
uninit()86     unsafe extern "C" fn uninit() {
87         // unregister on shutdown
88         __deregister_frame_info(&__EH_FRAME_BEGIN__ as *const u8, &mut OBJ as *mut _ as *mut u8);
89     }
90 
91     // MinGW-specific init/uninit routine registration
92     pub mod mingw_init {
93         // MinGW's startup objects (crt0.o / dllcrt0.o) will invoke global constructors in the
94         // .ctors and .dtors sections on startup and exit. In the case of DLLs, this is done when
95         // the DLL is loaded and unloaded.
96         //
97         // The linker will sort the sections, which ensures that our callbacks are located at the
98         // end of the list. Since constructors are run in reverse order, this ensures that our
99         // callbacks are the first and last ones executed.
100 
101         #[link_section = ".ctors.65535"] // .ctors.* : C initialization callbacks
102         pub static P_INIT: unsafe extern "C" fn() = super::init;
103 
104         #[link_section = ".dtors.65535"] // .dtors.* : C termination callbacks
105         pub static P_UNINIT: unsafe extern "C" fn() = super::uninit;
106     }
107 }
108