Skip to content
Snippets Groups Projects
Commit b717f6cf authored by Ahmed Abd El Mawgood's avatar Ahmed Abd El Mawgood
Browse files

Move IO Initialization to .init_array section

Most shared object in linux have their initialization in a separate
.init_array section. but glibc does not have the same behavour. Instead
the initialization is part of crt0. which (as it seams to me) run after
.init_array section. As such, this patch move IO initialization to
separate function that is marked as .init_array member and then the crt0
call this function only if init_array was never invoked (as in
statically linked binaries).
parent 6aeb2d6f
No related branches found
No related tags found
No related merge requests found
...@@ -60,6 +60,24 @@ pub unsafe fn relibc_verify_host() { ...@@ -60,6 +60,24 @@ pub unsafe fn relibc_verify_host() {
intrinsics::abort(); intrinsics::abort();
} }
} }
#[link_section = ".init_array"]
#[used]
static INIT_ARRAY: [extern "C" fn(); 1] = [init_array];
static mut init_complete: bool = false;
extern "C" fn init_array() {
io_init();
unsafe { init_complete = true };
}
fn io_init() {
unsafe {
// Initialize stdin/stdout/stderr, see https://github.com/rust-lang/rust/issues/51718
stdio::stdin = stdio::default_stdin.get();
stdio::stdout = stdio::default_stdout.get();
stdio::stderr = stdio::default_stderr.get();
}
}
#[inline(never)] #[inline(never)]
#[no_mangle] #[no_mangle]
...@@ -95,11 +113,9 @@ pub unsafe extern "C" fn relibc_start(sp: &'static Stack) -> ! { ...@@ -95,11 +113,9 @@ pub unsafe extern "C" fn relibc_start(sp: &'static Stack) -> ! {
platform::inner_environ = copy_string_array(envp, len); platform::inner_environ = copy_string_array(envp, len);
platform::environ = platform::inner_environ.as_mut_ptr(); platform::environ = platform::inner_environ.as_mut_ptr();
// Initialize stdin/stdout/stderr, see https://github.com/rust-lang/rust/issues/51718 if !init_complete {
stdio::stdin = stdio::default_stdin.get(); init_array();
stdio::stdout = stdio::default_stdout.get(); }
stdio::stderr = stdio::default_stderr.get();
pthread_init(); pthread_init();
// Run preinit array // Run preinit array
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment