Skip to content

Commit 06ce316

Browse files
committed
Remove the pre-init feature and provide a default for the __pre_init symbol
Signed-off-by: Gabriel Smith <[email protected]>
1 parent e6a6769 commit 06ce316

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

cortex-m-rt/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,3 @@ panic-abort = "0.2.0"
2121

2222
[features]
2323
device = []
24-
pre_init = []

cortex-m-rt/link.x.in

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ PROVIDE(UserHardFault = DefaultUserHardFault);
5151
/* # Interrupt vectors */
5252
EXTERN(__INTERRUPTS); /* `static` variable similar to `__EXCEPTIONS` */
5353

54+
/* # Pre-initialization function */
55+
/* If the user overrides this using the `pre_init!` macro or by creating a `__pre_init` function,
56+
then the function this points to will be called before the RAM is initialized. */
57+
PROVIDE(__pre_init = 0);
58+
5459
/* # Sections */
5560
SECTIONS
5661
{

cortex-m-rt/src/lib.rs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -169,13 +169,6 @@
169169
//! conjunction with crates generated using `svd2rust`. Those *device crates* will populate the
170170
//! missing part of the vector table when their `"rt"` feature is enabled.
171171
//!
172-
//! ## `pre_init`
173-
//!
174-
//! If this feature is enabled then a user-defined function will be run at the start of the reset
175-
//! handler, before RAM is initialized. If this feature is enabled then the macro `pre_init!` needs
176-
//! to be called to set the function to be run. This feature is intended to perform actions that
177-
//! cannot wait the time it takes for RAM to be initialized, such as disabling a watchdog.
178-
//!
179172
//! # Inspection
180173
//!
181174
//! This section covers how to inspect a binary that builds on top of `cortex-m-rt`.
@@ -244,6 +237,10 @@
244237
//! have a size of 32 vectors (on ARMv6-M) or 240 vectors (on ARMv7-M). This array is located after
245238
//! `__EXCEPTIONS` in the `.vector_table` section.
246239
//!
240+
//! - `__pre_init`. This is a function to be run before RAM is initialized. It defaults pointing at
241+
//! `0` and if not changed to point to another address, usually by calling the `pre_init!` macro,
242+
//! the `_pre_init` function is skipped.
243+
//!
247244
//! If you override any exception handler you'll find it as an unmangled symbol, e.g. `SysTick` or
248245
//! `SVCall`, in the output of `objdump`,
249246
//!
@@ -385,6 +382,14 @@
385382
//! println!("cargo:rustc-link-search={}", out.display());
386383
//! }
387384
//! ```
385+
//!
386+
//! ## `pre_init!`
387+
//!
388+
//! A user-defined function can be run at the start of the reset handler, before RAM is
389+
//! initialized. The macro `pre_init!` can be called to set the function to be run. The function is
390+
//! intended to perform actions that cannot wait the time it takes for RAM to be initialized, such
391+
//! as disabling a watchdog. As the function is called before RAM is initialized, any access of
392+
//! static variables will result in undefined behavior.
388393
389394
// # Developer notes
390395
//
@@ -482,12 +487,13 @@ pub unsafe extern "C" fn Reset() -> ! {
482487
static mut __edata: u32;
483488
static __sidata: u32;
484489

485-
#[cfg(feature = "pre_init")]
486-
fn _pre_init();
490+
fn __pre_init();
487491
}
488492

489-
#[cfg(feature = "pre_init")]
490-
_pre_init();
493+
let pre_init: unsafe extern "C" fn() = __pre_init;
494+
if pre_init as usize != 0 {
495+
pre_init();
496+
}
491497

492498
// Initialize RAM
493499
r0::zero_bss(&mut __sbss, &mut __ebss);
@@ -904,14 +910,13 @@ macro_rules! exception {
904910
/// }
905911
/// }
906912
/// ```
907-
#[cfg(feature = "pre_init")]
908913
#[macro_export]
909914
macro_rules! pre_init {
910915
($handler:path) => {
911916
#[allow(unsafe_code)]
912917
#[deny(private_no_mangle_fns)] // raise an error if this item is not accessible
913918
#[no_mangle]
914-
pub unsafe extern "C" fn _pre_init() {
919+
pub unsafe extern "C" fn __pre_init() {
915920
// validate user handler
916921
let f: unsafe fn() = $handler;
917922
f();

0 commit comments

Comments
 (0)