From 1efd2ee2b9bf15b8f36fcfb3df006ad3ec4c1d5c Mon Sep 17 00:00:00 2001 From: Youjie Zheng Date: Wed, 20 Nov 2024 23:04:58 +0800 Subject: [PATCH] [chore] Rename the crate as `ctor_base` --- .gitignore | 4 ++ Cargo.toml | 12 ++-- README.md | 18 ++--- constructor_array/README.md | 70 ------------------- constructor_array_macros/README.md | 70 ------------------- {constructor_array => ctor_bare}/Cargo.toml | 6 +- ctor_bare/README.md | 1 + {constructor_array => ctor_bare}/src/lib.rs | 4 +- .../tests/test_ctor.rs | 4 +- .../tests/test_empty.rs | 4 +- .../Cargo.toml | 4 +- ctor_bare_macros/README.md | 1 + .../src/lib.rs | 8 +-- 13 files changed, 36 insertions(+), 170 deletions(-) create mode 100644 .gitignore delete mode 100644 constructor_array/README.md delete mode 100644 constructor_array_macros/README.md rename {constructor_array => ctor_bare}/Cargo.toml (69%) create mode 120000 ctor_bare/README.md rename {constructor_array => ctor_bare}/src/lib.rs (92%) rename {constructor_array => ctor_bare}/tests/test_ctor.rs (94%) rename {constructor_array => ctor_bare}/tests/test_empty.rs (66%) rename {constructor_array_macros => ctor_bare_macros}/Cargo.toml (82%) create mode 120000 ctor_bare_macros/README.md rename {constructor_array_macros => ctor_bare_macros}/src/lib.rs (85%) diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d478c63 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/target +/.vscode +.DS_Store +Cargo.lock \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index e3984d7..df1c29e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,8 +2,8 @@ resolver = "2" members = [ - "constructor_array", - "constructor_array_macros", + "ctor_bare", + "ctor_bare_macros", ] [workspace.package] @@ -11,11 +11,11 @@ version = "0.1.0" authors = ["Youjie Zheng "] license = "GPL-3.0-or-later OR Apache-2.0 OR MulanPSL-2.0" homepage = "https://github.com/arceos-org/arceos" -documentation = "https://arceos-org.github.io/constructor_array" -repository = "https://github.com/arceos-org/constructor_array" +documentation = "https://arceos-org.github.io/ctor_bare" +repository = "https://github.com/arceos-org/ctor_bare" keywords = ["arceos", "constructor"] categories = ["development-tools::procedural-macro-helpers", "no-std"] [packages] -constructor_array = { path = "constructor_array" } -constructor_array_macros = { path = "constructor_array_macros" } \ No newline at end of file +ctor_bare = { path = "ctor_bare" } +ctor_bare_macros = { path = "ctor_bare_macros" } \ No newline at end of file diff --git a/README.md b/README.md index aea0366..d1a026b 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ -# constructor_array +# ctor_bare -[![Crates.io](https://img.shields.io/crates/v/constructor_array)](https://crates.io/crates/constructor_array) -[![Docs.rs](https://docs.rs/constructor_array/badge.svg)](https://docs.rs/constructor_array) -[![CI](https://github.com/arceos-org/constructor_array/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/arceos-org/constructor_array/actions/workflows/ci.yml) +[![Crates.io](https://img.shields.io/crates/v/ctor_bare)](https://crates.io/crates/ctor_bare) +[![Docs.rs](https://docs.rs/ctor_bare/badge.svg)](https://docs.rs/ctor_bare) +[![CI](https://github.com/arceos-org/ctor_bare/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/arceos-org/ctor_bare/actions/workflows/ci.yml) Module initialization functions for Rust (like __attribute__((constructor)) in C/C++) under no_std. @@ -17,12 +17,12 @@ It can support Linux, Windows, MacOS and other systems, and can be also used in 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. -When you are running your own operating system, you can call `constructor_array::invoke_ctors` to invoke all registered constructor functions. +When you are running your own operating system, you can call `ctor_bare::call_ctors` to invoke all registered constructor functions. ## Usage ```rust -use constructor_array::register_ctor; +use ctor_bare::register_ctor; #[register_ctor] fn hello_world() { println!("Hello, world!"); @@ -49,10 +49,10 @@ Because the `.init_array` section is a default section to store initialization f .text : ALIGN(4K) { # other sections in the `.text` section - _init_array_start = .; - _init_array_end = _init_array_start + SIZEOF(.init_array); + __init_array_start = .; + __init_array_end = __init_array_start + SIZEOF(.init_array); *(.init_array .init_array.*) - . = _init_array_end; + . = __init_array_end; # other sections in the `.text` section } diff --git a/constructor_array/README.md b/constructor_array/README.md deleted file mode 100644 index aea0366..0000000 --- a/constructor_array/README.md +++ /dev/null @@ -1,70 +0,0 @@ -# constructor_array - -[![Crates.io](https://img.shields.io/crates/v/constructor_array)](https://crates.io/crates/constructor_array) -[![Docs.rs](https://docs.rs/constructor_array/badge.svg)](https://docs.rs/constructor_array) -[![CI](https://github.com/arceos-org/constructor_array/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/arceos-org/constructor_array/actions/workflows/ci.yml) - - -Module initialization functions for Rust (like __attribute__((constructor)) in C/C++) under no_std. - - -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. - - -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. - - -When you are running your own operating system, you can call `constructor_array::invoke_ctors` to invoke all registered constructor functions. - -## Usage - -```rust -use constructor_array::register_ctor; -#[register_ctor] -fn hello_world() { - println!("Hello, world!"); -} - -static MAX_NUM: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); - -#[register_ctor] -fn set_max_num() { - MAX_NUM.store(20, std::sync::atomic::Ordering::Relaxed); -} - -fn main() { - assert_eq!(MAX_NUM.load(std::sync::atomic::Ordering::Relaxed), 20); -} -``` - -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: - -```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 .init_array.*) - . = _init_array_end; - - # other sections in the `.text` section -} -``` - -## Notes -To avoid section-related symbols being optimized by the compiler, you need to add "-z nostart-stop-gc" to the compile flags (see ). - - -For example, in `.cargo/config.toml`: -```toml -[build] -rustflags = ["-C", "link-arg=-z", "link-arg=nostart-stop-gc"] -rustdocflags = ["-C", "link-arg=-z", "-C", "link-arg=nostart-stop-gc"] -``` \ No newline at end of file diff --git a/constructor_array_macros/README.md b/constructor_array_macros/README.md deleted file mode 100644 index aea0366..0000000 --- a/constructor_array_macros/README.md +++ /dev/null @@ -1,70 +0,0 @@ -# constructor_array - -[![Crates.io](https://img.shields.io/crates/v/constructor_array)](https://crates.io/crates/constructor_array) -[![Docs.rs](https://docs.rs/constructor_array/badge.svg)](https://docs.rs/constructor_array) -[![CI](https://github.com/arceos-org/constructor_array/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/arceos-org/constructor_array/actions/workflows/ci.yml) - - -Module initialization functions for Rust (like __attribute__((constructor)) in C/C++) under no_std. - - -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. - - -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. - - -When you are running your own operating system, you can call `constructor_array::invoke_ctors` to invoke all registered constructor functions. - -## Usage - -```rust -use constructor_array::register_ctor; -#[register_ctor] -fn hello_world() { - println!("Hello, world!"); -} - -static MAX_NUM: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); - -#[register_ctor] -fn set_max_num() { - MAX_NUM.store(20, std::sync::atomic::Ordering::Relaxed); -} - -fn main() { - assert_eq!(MAX_NUM.load(std::sync::atomic::Ordering::Relaxed), 20); -} -``` - -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: - -```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 .init_array.*) - . = _init_array_end; - - # other sections in the `.text` section -} -``` - -## Notes -To avoid section-related symbols being optimized by the compiler, you need to add "-z nostart-stop-gc" to the compile flags (see ). - - -For example, in `.cargo/config.toml`: -```toml -[build] -rustflags = ["-C", "link-arg=-z", "link-arg=nostart-stop-gc"] -rustdocflags = ["-C", "link-arg=-z", "-C", "link-arg=nostart-stop-gc"] -``` \ No newline at end of file diff --git a/constructor_array/Cargo.toml b/ctor_bare/Cargo.toml similarity index 69% rename from constructor_array/Cargo.toml rename to ctor_bare/Cargo.toml index 2604b54..4cf40c8 100644 --- a/constructor_array/Cargo.toml +++ b/ctor_bare/Cargo.toml @@ -1,8 +1,8 @@ [package] -name = "constructor_array" +name = "ctor_bare" edition = "2021" description = "Register constructor functions for Rust at complie time under no_std." -documentation = "https://docs.rs/constructor_array" +documentation = "https://docs.rs/ctor_bare" version.workspace = true authors.workspace = true license.workspace = true @@ -13,4 +13,4 @@ categories.workspace = true readme = "README.md" [dependencies] -constructor_array_macros = { path = "../constructor_array_macros" } \ No newline at end of file +ctor_bare_macros = { path = "../ctor_bare_macros" } \ No newline at end of file diff --git a/ctor_bare/README.md b/ctor_bare/README.md new file mode 120000 index 0000000..0837aa2 --- /dev/null +++ b/ctor_bare/README.md @@ -0,0 +1 @@ +/home/zyj/Code_file/ctor_bare/README.md \ No newline at end of file diff --git a/constructor_array/src/lib.rs b/ctor_bare/src/lib.rs similarity index 92% rename from constructor_array/src/lib.rs rename to ctor_bare/src/lib.rs index c4d85ec..94cc257 100644 --- a/constructor_array/src/lib.rs +++ b/ctor_bare/src/lib.rs @@ -1,7 +1,7 @@ #![no_std] #![doc = include_str!("../README.md")] -pub use constructor_array_macros::register_ctor; +pub use ctor_bare_macros::register_ctor; /// Placeholder for the `.init_array` section, so that /// the `__init_array_start` and `__init_array_end` symbols can be generated. @@ -18,7 +18,7 @@ extern "C" { /// /// # Notes /// Caller should ensure that the `.init_array` section will not be disturbed by other sections. -pub fn invoke_ctors() { +pub fn call_ctors() { for ctor_ptr in (__init_array_start as usize..__init_array_end as usize) .step_by(core::mem::size_of::<*const core::ffi::c_void>()) { diff --git a/constructor_array/tests/test_ctor.rs b/ctor_bare/tests/test_ctor.rs similarity index 94% rename from constructor_array/tests/test_ctor.rs rename to ctor_bare/tests/test_ctor.rs index d6abdd0..ccade11 100644 --- a/constructor_array/tests/test_ctor.rs +++ b/ctor_bare/tests/test_ctor.rs @@ -1,6 +1,6 @@ use std::sync::{atomic::AtomicUsize, Mutex}; -use constructor_array::*; +use ctor_bare::*; static INIT_NUM: AtomicUsize = AtomicUsize::new(0); @@ -20,7 +20,7 @@ fn init_vector() { } #[test] -fn test_constructor_array() { +fn test_ctor_bare() { // The constructor functions will be called before the main function. assert!(INIT_NUM.load(std::sync::atomic::Ordering::Relaxed) == 20); let vec = INIT_VEC.lock().unwrap(); diff --git a/constructor_array/tests/test_empty.rs b/ctor_bare/tests/test_empty.rs similarity index 66% rename from constructor_array/tests/test_empty.rs rename to ctor_bare/tests/test_empty.rs index 812dab2..86f266b 100644 --- a/constructor_array/tests/test_empty.rs +++ b/ctor_bare/tests/test_empty.rs @@ -1,8 +1,8 @@ #[test] fn test_empty() { // Sometimes under certain conditions, we may not have any constructor functions. - // But the `invoke_ctors` function should still work, and the `__init_array_start` and + // But the `call_ctors` function should still work, and the `__init_array_start` and // `__init_array_end` symbols should be valid. - constructor_array::invoke_ctors(); + ctor_bare::call_ctors(); println!("It should exit successfully when we don't specify any constructor functions."); } diff --git a/constructor_array_macros/Cargo.toml b/ctor_bare_macros/Cargo.toml similarity index 82% rename from constructor_array_macros/Cargo.toml rename to ctor_bare_macros/Cargo.toml index 559eafa..ee85059 100644 --- a/constructor_array_macros/Cargo.toml +++ b/ctor_bare_macros/Cargo.toml @@ -1,8 +1,8 @@ [package] -name = "constructor_array_macros" +name = "ctor_bare_macros" edition = "2021" description = "Macros for registering constructor functions for Rust under no_std." -documentation = "https://docs.rs/constructor_array_macros" +documentation = "https://docs.rs/ctor_bare_macros" version.workspace = true authors.workspace = true license.workspace = true diff --git a/ctor_bare_macros/README.md b/ctor_bare_macros/README.md new file mode 120000 index 0000000..0837aa2 --- /dev/null +++ b/ctor_bare_macros/README.md @@ -0,0 +1 @@ +/home/zyj/Code_file/ctor_bare/README.md \ No newline at end of file diff --git a/constructor_array_macros/src/lib.rs b/ctor_bare_macros/src/lib.rs similarity index 85% rename from constructor_array_macros/src/lib.rs rename to ctor_bare_macros/src/lib.rs index 3485709..b2f3421 100644 --- a/constructor_array_macros/src/lib.rs +++ b/ctor_bare_macros/src/lib.rs @@ -1,12 +1,12 @@ //!Macros for registering constructor functions for Rust under no_std, which is like __attribute__((constructor)) in C/C++. //! -//! **DO NOT** use this crate directly. Use the [constructor_array](https://docs.rs/constructor_array) crate instead. +//! **DO NOT** use this crate directly. Use the [ctor_bare](https://docs.rs/ctor_bare) crate instead. //! //! After attching the `register_ctor` macro to the given function, a pointer pointing to it will be stored in the `.init_array` section. -//! When the program is loaded, this section will be linked into the binary. The `invoke_ctors` function in the `constructor_array` +//! When the program is loaded, this section will be linked into the binary. The `call_ctors` function in the `ctor_bare` //! crate will call all the constructor functions in the `.init_array` section. //! -//! See the documentation of the [constructor_array](https://docs.rs/constructor_array) crate for more details. +//! See the documentation of the [ctor_bare](https://docs.rs/ctor_bare) crate for more details. use proc_macro::TokenStream; use proc_macro2::Span; @@ -17,7 +17,7 @@ use syn::{parse_macro_input, Error, Item}; /// /// The function should have no input arguments and return nothing. /// -/// See the documentation of the [constructor_array](https://docs.rs/constructor_array) crate for more details. +/// See the documentation of the [ctor_bare](https://docs.rs/ctor_bare) crate for more details. #[proc_macro_attribute] pub fn register_ctor(attr: TokenStream, function: TokenStream) -> TokenStream { if !attr.is_empty() {