diff --git a/src/header/stdlib/mod.rs b/src/header/stdlib/mod.rs index 6573ac50dd8d02cd487b7b0bda7d8e2dd56c25d3..0644dfb8a080cf8bc055969409e0239719938000 100644 --- a/src/header/stdlib/mod.rs +++ b/src/header/stdlib/mod.rs @@ -220,6 +220,9 @@ pub extern "C" fn erand(xsubi: [c_ushort; 3]) -> c_double { #[no_mangle] pub unsafe extern "C" fn exit(status: c_int) { extern "C" { + static __fini_array_start: extern "C" fn(); + static __fini_array_end: extern "C" fn(); + fn _fini(); } @@ -229,6 +232,13 @@ pub unsafe extern "C" fn exit(status: c_int) { } } + // Look for the neighbor functions in memory until the end + let mut f = &__fini_array_end as *const _; + while f > &__fini_array_start { + f = f.offset(-1); + (*f)(); + } + _fini(); Sys::exit(status); diff --git a/src/start.rs b/src/start.rs index 006b722d9cd5028069f4386a8e6947a1a89a0a35..9399eb2fe55ce7f1d0f3f7cd8e0ef8dd48958bf5 100644 --- a/src/start.rs +++ b/src/start.rs @@ -30,6 +30,11 @@ impl Stack { #[no_mangle] pub unsafe extern "C" fn relibc_start(sp: &'static Stack) -> ! { extern "C" { + static __preinit_array_start: extern "C" fn(); + static __preinit_array_end: extern "C" fn(); + static __init_array_start: extern "C" fn(); + static __init_array_end: extern "C" fn(); + fn _init(); fn main(argc: isize, argv: *const *const c_char, envp: *const *const c_char) -> c_int; } @@ -66,6 +71,18 @@ pub unsafe extern "C" fn relibc_start(sp: &'static Stack) -> ! { _init(); + // Look for the neighbor functions in memory until the end + let mut f = &__preinit_array_start as *const _; + while f < &__preinit_array_end { + (*f)(); + f = f.offset(1); + } + f = &__init_array_start as *const _; + while f < &__init_array_end { + (*f)(); + f = f.offset(1); + } + stdlib::exit(main( argc, argv,