11# A ` main ` interface
22
3- We have a minimal working program but we need to package it in such a way that end user can build
4- safe programs on top of it. In this section we'll implement a ` main ` interface like the one standard
3+ We have a minimal working program now, but we need to package it in a way that the end user can build
4+ safe programs on top of it. In this section, we'll implement a ` main ` interface like the one standard
55Rust programs use.
66
77First, we'll convert our binary crate into a library crate:
@@ -10,7 +10,7 @@ First, we'll convert our binary crate into a library crate:
1010$ mv src/main.rs src/lib.rs
1111```
1212
13- And rename it to ` rt ` which stands for "runtime".
13+ And then rename it to ` rt ` which stands for "runtime".
1414
1515``` console
1616$ sed -i s/app/rt/ Cargo.toml
@@ -37,11 +37,11 @@ pub unsafe extern "C" fn Reset() -> ! {
3737
3838We also drop the ` #![no_main] ` attribute has it has no effect on library crates.
3939
40- > There's an orthogonal question that arises at this stage: should the ` rt ` library provide a
40+ > There's an orthogonal question that arises at this stage: Should the ` rt ` library provide a
4141> standard panicking behavior, or should it * not* provide a ` #[panic_implementation] ` function and
4242> leave the end user choose the panicking behavior? This document won't delve into that question and
43- > for simplicity will leave the dummy ` #[panic_implementation] ` function in the ` rt ` crate, but we
44- > wanted to inform the reader that there are other options.
43+ > for simplicity will leave the dummy ` #[panic_implementation] ` function in the ` rt ` crate.
44+ > However, we wanted to inform the reader that there are other options.
4545
4646The second change involves providing the linker script we wrote before to the application crate. You
4747see the linker will search for linker scripts in the library search path (` -L ` ) and in the directory
@@ -134,12 +134,12 @@ Reset:
134134
135135## Making it type safe
136136
137- The ` main ` interface works but it's easy to get it wrong: for example, the user could write ` main `
137+ The ` main ` interface works, but it's easy to get it wrong: For example, the user could write ` main `
138138as a non-divergent function, and they would get no compile time error and undefined behavior (the
139139compiler will misoptimize the program).
140140
141141We can add type safety by exposing a macro to the user instead of the symbol interface. In the
142- ` rt ` crate we can write this macro:
142+ ` rt ` crate, we can write this macro:
143143
144144``` rust
145145#[macro_export]
@@ -231,9 +231,9 @@ fn main() -> ! {
231231}
232232```
233233
234- However if you run this program on real hardware and debug it you'll observe that the ` static `
235- variables ` BSS ` and ` DATA ` don't have the values ` 0 ` and ` 1 ` by the time ` main ` have been reached.
236- Instead these variables will have junk values; the problem is that the contents of RAM are
234+ However if you run this program on real hardware and debug it, you'll observe that the ` static `
235+ variables ` BSS ` and ` DATA ` don't have the values ` 0 ` and ` 1 ` by the time ` main ` has been reached.
236+ Instead, these variables will have junk values. The problem is that the contents of RAM are
237237random after powering up the device. You won't be able to observe this effect if you run the
238238program in QEMU.
239239
@@ -283,7 +283,7 @@ later use from Rust code.
283283```
284284
285285We set the Load Memory Address (LMA) of the ` .data ` section to the end of the ` .rodata `
286- section. The ` .data ` contains ` static ` variable with a non-zero initial value; the Virtual Memory
286+ section. The ` .data ` contains ` static ` variables with a non-zero initial value; the Virtual Memory
287287Address (VMA) of the ` .data ` section is somewhere in RAM -- this is where the ` static ` variables are
288288located. The initial values of those ` static ` variables, however, must be allocated in non volatile
289289memory (Flash); the LMA is where in Flash those initial values are stored.
@@ -295,7 +295,7 @@ memory (Flash); the LMA is where in Flash those initial values are stored.
295295Finally, we associate a symbol to the LMA of ` .data ` .
296296
297297On the Rust side, we zero the ` .bss ` section and initialize the ` .data ` section. We can reference
298- the symbols we created in the linker script from the Rust code. The * addresses* of these symbols are
298+ the symbols we created in the linker script from the Rust code. The * addresses* [ ^ 1 ] of these symbols are
299299the boundaries of the ` .bss ` and ` .data ` sections.
300300
301301The updated reset handler is shown below:
@@ -339,3 +339,6 @@ undefined behavior!
339339> are interested in learning how this can be achieved check the [ ` cortex-m-rt ` ] crate.
340340
341341[ `cortex-m-rt` ] : https://github.com/japaric/cortex-m-rt/tree/v0.5.1
342+
343+ [ ^ 1 ] : The fact that the addresses of the linker script symbols must be used here can be confusing and
344+ unintuitive. An elaborate explanation for this oddity can be found [ here] ( https://stackoverflow.com/a/40392131 ) .
0 commit comments