Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[workspace]
resolver = "2"
resolver = "3"
members = [
"linker-sections",
"examples/*",
]

[workspace.package]
edition = "2021"
edition = "2024"
license = "MIT OR Apache-2.0"
repository = "https://github.com/tlevora/linker-sections-init-rs"
readme = "README.md"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use {defmt_rtt as _, panic_probe as _};

const INITIAL_VALUE: u32 = 0xDEAD_BEEF;

#[link_section = ".custom_data"]
#[unsafe(link_section = ".custom_data")]
static mut STATIC_VARIABLE: u32 = INITIAL_VALUE;

#[cortex_m_rt::pre_init]
Expand Down
4 changes: 2 additions & 2 deletions examples/demo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ const INITIAL_VALUE: u32 = 0xDEAD_BEEF;
// - Using static mut just to force compiler not to optimize it out in
// this simple example
// - linker section gets initialized because of using `linker_sections`
#[link_section = ".custom_data_a"]
#[unsafe(link_section = ".custom_data_a")]
static mut STATIC_ARRAY_A: u32 = INITIAL_VALUE;

#[allow(unsafe_code)]
// SAFETY:
// - Using static mut just to force compiler not to optimize it out in
// this simple example
// - linker section gets initialized because of using `linker_sections`
#[link_section = ".custom_data_b"]
#[unsafe(link_section = ".custom_data_b")]
static mut STATIC_ARRAY_B: [u32; 256] = [INITIAL_VALUE; 256];

#[cortex_m_rt::pre_init]
Expand Down
4 changes: 2 additions & 2 deletions examples/static-cell/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ const INITIAL_VALUE: u32 = 0xDEAD_BEEF;
// - Using static mut just to force compiler not to optimize it out in
// this simple example
// - linker section gets initialized because of using `linker_sections`
#[link_section = ".custom_data"]
#[unsafe(link_section = ".custom_data")]
static STATIC_ARRAY_A: StaticCell<u32> = StaticCell::new();

#[allow(unsafe_code)]
// SAFETY:
// - Using static mut just to force compiler not to optimize it out in
// this simple example
// - linker section gets initialized because of using `linker_sections`
#[link_section = ".custom_data"]
#[unsafe(link_section = ".custom_data")]
static STATIC_ARRAY_B: StaticCell<[u32; 256]> = StaticCell::new();

#[cortex_m_rt::pre_init]
Expand Down
10 changes: 5 additions & 5 deletions linker-sections/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
//!
//! const INITIAL_VALUE: u32 = 0xDEAD_BEEF;
//!
//! #[link_section = ".custom_data"]
//! #[unsafe(link_section = ".custom_data")]
//! static mut STATIC_VARIABLE: u32 = INITIAL_VALUE;
//! #[cortex_m_rt::pre_init]
//! unsafe fn pre_init() {
Expand Down Expand Up @@ -253,7 +253,7 @@ macro_rules! section_init_with_prefixes {
#[doc(hidden)]
macro_rules! pointer {
($name:ident) => {
extern "C" {
unsafe extern "C" {
static $name: u32;
}
};
Expand All @@ -263,7 +263,7 @@ macro_rules! pointer {
#[doc(hidden)]
macro_rules! pointer_mut {
($name:ident) => {
extern "C" {
unsafe extern "C" {
static mut $name: u32;
}
};
Expand All @@ -286,7 +286,7 @@ pub unsafe fn section_init(dst: *mut u32, end: *const u32, src: *const u32) {
assert!(end as usize % 4 == 0);
}

let len = end.offset_from(dst) as usize;
let len = unsafe { end.offset_from(dst) } as usize;

#[cfg(feature = "asserts")]
{
Expand All @@ -297,5 +297,5 @@ pub unsafe fn section_init(dst: *mut u32, end: *const u32, src: *const u32) {
assert!(src > dst + len || src + len < dst);
}

core::ptr::copy_nonoverlapping(src, dst, len);
unsafe { core::ptr::copy_nonoverlapping(src, dst, len) };
}