Use zksync-os linker scripts (#178)

This commit is contained in:
Han
2025-10-24 19:57:11 +08:00
committed by GitHub
parent 6a2c6e1d54
commit 5c342eec44
9 changed files with 99 additions and 49 deletions

View File

@@ -1,17 +1,53 @@
use core::alloc::{GlobalAlloc, Layout};
use core::{
alloc::{GlobalAlloc, Layout},
ptr::addr_of_mut,
};
core::arch::global_asm!(include_str!("./asm_reduced.S"));
#[link_section = ".init.rust"]
#[export_name = "_start_rust"]
unsafe extern "C" {
// Boundaries of the heap
static mut _sheap: usize;
static mut _eheap: usize;
// Boundaries of the .data section (and it's part in ROM)
static mut _sidata: usize;
static mut _sdata: usize;
static mut _edata: usize;
// Boundaries of the .rodata section
static mut _sirodata: usize;
static mut _srodata: usize;
static mut _erodata: usize;
}
unsafe fn load_to_ram(src: *const u8, dst_start: *mut u8, dst_end: *mut u8) {
let offset = dst_end.addr() - dst_start.addr();
unsafe { core::ptr::copy_nonoverlapping(src, dst_start, offset) };
}
#[unsafe(link_section = ".init.rust")]
#[unsafe(export_name = "_start_rust")]
unsafe extern "C" fn start_rust() -> ! {
unsafe {
load_to_ram(
addr_of_mut!(_sirodata) as *const u8,
addr_of_mut!(_srodata) as *mut u8,
addr_of_mut!(_erodata) as *mut u8,
);
load_to_ram(
addr_of_mut!(_sidata) as *const u8,
addr_of_mut!(_sdata) as *mut u8,
addr_of_mut!(_edata) as *mut u8,
);
};
crate::main();
unsafe { core::hint::unreachable_unchecked() }
}
/// A simple heap allocator.
///
/// Allocates memory from left to right, without any deallocation.
struct SimpleAlloc;
unsafe impl GlobalAlloc for SimpleAlloc {
@@ -29,17 +65,10 @@ static mut HEAP_POS: usize = 0;
#[unsafe(no_mangle)]
pub unsafe extern "C" fn sys_alloc_aligned(bytes: usize, align: usize) -> *mut u8 {
unsafe extern "C" {
// start/end of heap defined in `link.x`.
unsafe static _sheap: u8;
unsafe static _eheap: u8;
}
// SAFETY: Single threaded, so nothing else can touch this while we're working.
let mut heap_pos = unsafe { HEAP_POS };
if heap_pos == 0 {
heap_pos = unsafe { (&_sheap) as *const u8 as usize };
heap_pos = addr_of_mut!(_sheap) as *const u8 as usize;
}
let offset = heap_pos & (align - 1);
@@ -50,11 +79,21 @@ pub unsafe extern "C" fn sys_alloc_aligned(bytes: usize, align: usize) -> *mut u
let ptr = heap_pos as *mut u8;
let (heap_pos, overflowed) = heap_pos.overflowing_add(bytes);
let eheap = unsafe { (&_eheap) as *const u8 as usize };
let eheap = addr_of_mut!(_eheap) as *const u8 as usize;
if overflowed || heap_pos > eheap {
panic!("Heap exhausted");
panic!("heap exhausted");
}
unsafe { HEAP_POS = heap_pos };
ptr
}
#[cfg(all(target_arch = "riscv32", target_feature = "a"))]
#[unsafe(no_mangle)]
fn _critical_section_1_0_acquire() -> u32 {
0
}
#[cfg(all(target_arch = "riscv32", target_feature = "a"))]
#[unsafe(no_mangle)]
fn _critical_section_1_0_release(_: u32) {}