vmbase: Export linker-defined MemoryRegions & VAs

Group all linker-defined variables in a private module.

Export the memory regions defined by the linker as safe Range<usize>.

Export __stack_chk_guard as a safe static &u64.

Bug: 238050226
Test: atest vmbase_example.integration_test
Change-Id: I401ac4af949e283f78fbc576db19339e38ed3942
diff --git a/vmbase/example/src/layout.rs b/vmbase/example/src/layout.rs
index c0c4fab..4c3af6d 100644
--- a/vmbase/example/src/layout.rs
+++ b/vmbase/example/src/layout.rs
@@ -17,72 +17,59 @@
 use aarch64_paging::paging::{MemoryRegion, VirtualAddress};
 use core::arch::asm;
 use core::ops::Range;
+use vmbase::layout;
 use vmbase::println;
+use vmbase::STACK_CHK_GUARD;
 
 /// The first 1 GiB of memory are used for MMIO.
 pub const DEVICE_REGION: MemoryRegion = MemoryRegion::new(0, 0x40000000);
 
+fn into_va_range(r: Range<usize>) -> Range<VirtualAddress> {
+    VirtualAddress(r.start)..VirtualAddress(r.end)
+}
+
 /// Memory reserved for the DTB.
 pub fn dtb_range() -> Range<VirtualAddress> {
-    unsafe {
-        VirtualAddress(&dtb_begin as *const u8 as usize)
-            ..VirtualAddress(&dtb_end as *const u8 as usize)
-    }
+    into_va_range(layout::dtb_range())
 }
 
 /// Executable code.
 pub fn text_range() -> Range<VirtualAddress> {
-    unsafe {
-        VirtualAddress(&text_begin as *const u8 as usize)
-            ..VirtualAddress(&text_end as *const u8 as usize)
-    }
+    into_va_range(layout::text_range())
 }
 
 /// Read-only data.
 pub fn rodata_range() -> Range<VirtualAddress> {
-    unsafe {
-        VirtualAddress(&rodata_begin as *const u8 as usize)
-            ..VirtualAddress(&rodata_end as *const u8 as usize)
-    }
+    into_va_range(layout::rodata_range())
 }
 
 /// Initialised writable data.
 pub fn data_range() -> Range<VirtualAddress> {
-    unsafe {
-        VirtualAddress(&data_begin as *const u8 as usize)
-            ..VirtualAddress(&data_end as *const u8 as usize)
-    }
+    into_va_range(layout::data_range())
 }
 
 /// Zero-initialised writable data.
 pub fn bss_range() -> Range<VirtualAddress> {
-    unsafe {
-        VirtualAddress(&bss_begin as *const u8 as usize)
-            ..VirtualAddress(&bss_end as *const u8 as usize)
-    }
+    into_va_range(layout::bss_range())
 }
 
 /// Writable data region for the stack.
 pub fn boot_stack_range() -> Range<VirtualAddress> {
-    unsafe {
-        VirtualAddress(&boot_stack_begin as *const u8 as usize)
-            ..VirtualAddress(&boot_stack_end as *const u8 as usize)
-    }
+    into_va_range(layout::boot_stack_range())
 }
 
 /// Writable data, including the stack.
 pub fn writable_region() -> MemoryRegion {
-    unsafe {
-        MemoryRegion::new(&data_begin as *const u8 as usize, &boot_stack_end as *const u8 as usize)
-    }
+    let r = layout::writable_region();
+    MemoryRegion::new(r.start, r.end)
 }
 
 fn data_load_address() -> VirtualAddress {
-    unsafe { VirtualAddress(&data_lma as *const u8 as usize) }
+    VirtualAddress(layout::data_load_address())
 }
 
 fn binary_end() -> VirtualAddress {
-    unsafe { VirtualAddress(&bin_end as *const u8 as usize) }
+    VirtualAddress(layout::binary_end())
 }
 
 pub fn print_addresses() {
@@ -124,23 +111,5 @@
 
 /// Value of __stack_chk_guard.
 pub fn stack_chk_guard() -> u64 {
-    unsafe { __stack_chk_guard }
-}
-
-extern "C" {
-    static dtb_begin: u8;
-    static dtb_end: u8;
-    static text_begin: u8;
-    static text_end: u8;
-    static rodata_begin: u8;
-    static rodata_end: u8;
-    static data_begin: u8;
-    static data_end: u8;
-    static data_lma: u8;
-    static bin_end: u8;
-    static bss_begin: u8;
-    static bss_end: u8;
-    static boot_stack_begin: u8;
-    static boot_stack_end: u8;
-    static __stack_chk_guard: u64;
+    *STACK_CHK_GUARD
 }