Skip to content

Commit

Permalink
[fix] Add used attr for function pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
Azure-stars committed Nov 20, 2024
1 parent 332dbc8 commit 231c391
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
16 changes: 6 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}
```

Expand Down
1 change: 1 addition & 0 deletions ctor_bare_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 231c391

Please sign in to comment.