diff --git a/README.md b/README.md index d1a026b..c4b5ca7 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ Module initialization functions for Rust (like __attribute__((constructor)) in C After registering a constructor function, a function pointer pointing to it will be stored in the `.init_array` section. -It can support Linux, Windows, MacOS and other systems, and can be also used in `no_std` environments when developing your own kernel. +It can support Linux, MacOS and other systems, and can be also used in `no_std` environments when developing your own kernel. In Linux, Windows, MacOS and other systems, the `.init_array` section is a default section to store initialization functions. When the program starts, the system will call all functions in the `.init_array` section in order. @@ -43,18 +43,14 @@ fn main() { Because the `.init_array` section is a default section to store initialization functions in Linux and some other systems, it will be included in the linker script of compilers like GCC and Clang. -**However**, if you are using a custom linker script, you need to **add the `.init_array` section to the `.text` section manually**, so that these functions can be mapped into the page table and executed correctly. You can add the following line to your linker script as a reference: +**However**, if you are using a custom linker script, you need to **add the `.init_array` section and map them in the page table manually**, so that these functions can be executed correctly. You can add the following line to your linker script as a reference: ```test, ignore -.text : ALIGN(4K) { - # other sections in the `.text` section - - __init_array_start = .; - __init_array_end = __init_array_start + SIZEOF(.init_array); +.init_array : ALIGN(4K) { + PROVIDE_HIDDEN (__init_array_start = .); *(.init_array .init_array.*) - . = __init_array_end; - - # other sections in the `.text` section + PROVIDE_HIDDEN (__init_array_end = .); + . = ALIGN(4K); } ``` diff --git a/ctor_bare_macros/src/lib.rs b/ctor_bare_macros/src/lib.rs index b2f3421..ac6732d 100644 --- a/ctor_bare_macros/src/lib.rs +++ b/ctor_bare_macros/src/lib.rs @@ -58,6 +58,7 @@ pub fn register_ctor(attr: TokenStream, function: TokenStream) -> TokenStream { quote! { #[link_section = ".init_array"] + #[used] #[allow(non_upper_case_globals)] static #name_ident: extern "C" fn() = #name;