[][src]Crate r0

Memory initialization code ("crt0") written in Rust.

This crate is meant for bare metal systems where there is no ELF loader or OS to take care of initializing RAM for the program. It provides functions for initializing the .data and .bss sections.

Initializing RAM

On the linker script side, we must assign names (symbols) to the boundaries of the .bss and .data sections. For example:

.bss : ALIGN(4)
{
    _sbss = .;
    *(.bss.*);
    _ebss = ALIGN(4);
} > RAM

.data : ALIGN(4)
{
    _sdata = .;
    *(.data.*);
    _edata = ALIGN(4);
} > RAM AT > FLASH

_sidata = LOADADDR(.data);

This script defines symbols _sbss/_ebss, and _sdata/_edata to point at the boundaries of the .bss and .data sections in RAM, respectively. The AT > FLASH directive places the actual contents of the .data section in the FLASH memory region (which needs to be defined separately from this linker script snippet). Then _sidata is set to the address of that data in flash.

Note that while _sbss, _ebss, _sdata and _edata are Virtual Memory Addresses (VMAs), _sidata is the Load Memory Address (LMA) of the .data section.

On the Rust side, we must bind to those symbols using an extern block, and can then call into this crate to perform RAM initialization:

unsafe fn before_main() {
    // The type, `u32`, indicates that the memory is 4-byte aligned
    extern "C" {
        static mut _sbss: u32;
        static mut _ebss: u32;

        static mut _sdata: u32;
        static mut _edata: u32;

        static _sidata: u32;
    }

    zero_bss(&mut _sbss, &mut _ebss);
    init_data(&mut _sdata, &mut _edata, &_sidata);
}

Minimum Supported Rust Version (MSRV)

The MSRV of this release is Rust 1.31.0

Traits

Word

Trait for machine word types.

Functions

init_data

Initializes the .data section by copying it from the location indicated by sidata.

zero_bss

Zeroes the .bss section.