Merge #242 242: Add the .sdata section to the app binary. r=hudson-ayers a=jrvanwhy RISC-V has the `.sdata` section, which is supposed to be for commonly-accessed global variables. Previously, we discarded this section in the linker script. Discarding this section resulted in missing global variables. I originally discovered this when I tried to print a string of less than 9 bytes and it was not working. I reported it as a kernel bug at https://github.com/tock/tock/issues/2132, then later realized the issue was in the libtock-rs linker script rather than the kernel. I added a test to libtock_test that catches this issue (I verified by hand that the test fails if I don't fix the linker script). Co-authored-by: Johnathan Van Why <jrvanwhy@google.com>
diff --git a/examples-features/libtock_test.rs b/examples-features/libtock_test.rs index ac2cb48..a938f46 100644 --- a/examples-features/libtock_test.rs +++ b/examples-features/libtock_test.rs
@@ -10,6 +10,7 @@ use core::future::Future; use core::mem; use core::pin::Pin; +use core::ptr::read_volatile; use core::task::Context; use core::task::Poll; use libtock::console::ConsoleDriver; @@ -46,6 +47,7 @@ gpio: &mut GpioDriverFactory, ) -> TockResult<()> { test.console()?; + test.sdata()?; test.static_mut()?; test.dynamic_dispatch()?; test.formatting()?; @@ -72,6 +74,21 @@ self.log_success("Console") } + // RISC-V has the .sdata section, which is ostensibly for commonly-accessed + // read-write global variables. In practice, the Rust compiler tends to put + // small globals (<= 8 bytes) into it. This test verifies that small + // data symbols are accessible. + fn sdata(&mut self) -> TockResult<()> { + static mut SDATA: [u8; 4] = [1, 2, 3, 4]; + // Use volatile reads to prevent the compiler from constant-folding + // everything away. + let sdata_ptr = unsafe { &SDATA } as *const u8; + let sum = (0..4) + .map(|i| unsafe { read_volatile(sdata_ptr.offset(i)) }) + .sum::<u8>(); + self.check_if_true(sum == 10, "sdata") + } + fn static_mut(&mut self) -> TockResult<()> { self.check_if_true(increment_static_mut() == 1, "static mut") }
diff --git a/layout_generic.ld b/layout_generic.ld index dd101ab..3c5065c 100644 --- a/layout_generic.ld +++ b/layout_generic.ld
@@ -113,6 +113,7 @@ . = ALIGN(4); /* Make sure we're word-aligned here */ _data = .; KEEP(*(.data*)) + *(.sdata*) /* RISC-V small-pointer data section */ . = ALIGN(4); /* Make sure we're word-aligned at the end of flash */ } > SRAM