From bc6ef9d68968fd4a07659ba8a2d3bbd4df5acdc5 Mon Sep 17 00:00:00 2001 From: Stephen Marz Date: Sun, 26 Apr 2020 09:19:07 -0400 Subject: [PATCH] Fixed ELF loader and added offset. --- risc_v/src/test.rs | 8 ++++++-- risc_v/src/userspace/helloworld.cpp | 3 ++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/risc_v/src/test.rs b/risc_v/src/test.rs index 4fa859f..b48322a 100644 --- a/risc_v/src/test.rs +++ b/risc_v/src/test.rs @@ -25,7 +25,7 @@ pub fn test_elf() { // interrupt context right now, so we cannot pause. Usually, this would // be done by an exec system call. let files_inode = 25u32; - let files_size = 14304; + let files_size = 14440; let bytes_to_read = 1024 * 50; let mut buffer = BlockBuffer::new(bytes_to_read); // Read the file from the disk. I got the inode by mounting @@ -137,10 +137,14 @@ pub fn test_elf() { // is provided in the ELF program header. let pages = (ph.memsz + PAGE_SIZE) / PAGE_SIZE; for i in 0..pages { + // Align the offset to the nearest page for mapping. + // We might not need to do this, but not doing this could leak + // a different section into the virtual address space of another. + let off = (ph.off + PAGE_SIZE - 1) & !(PAGE_SIZE - 1); let vaddr = ph.vaddr + i * PAGE_SIZE; // The ELF specifies a paddr, but not when we // use the vaddr! - let paddr = program_mem as usize + i * PAGE_SIZE; + let paddr = program_mem as usize + off + i * PAGE_SIZE; map(table, vaddr, paddr, bits, 0); } } diff --git a/risc_v/src/userspace/helloworld.cpp b/risc_v/src/userspace/helloworld.cpp index fc5df4f..a1c6579 100644 --- a/risc_v/src/userspace/helloworld.cpp +++ b/risc_v/src/userspace/helloworld.cpp @@ -1,9 +1,10 @@ #include #include +int myarray[1000]; + int main() { - int myarray[1000]; printf("I'm a C++ program, and I'm running in user space. How about a big, Hello World\n"); printf("My array is at 0x%p\n", myarray); printf("I'm going to start crunching some numbers, so gimme a minute.\n");