Analyse ELF.
This commit is contained in:
parent
b6f4327a3f
commit
d38b24a9cb
4 changed files with 43 additions and 3 deletions
|
@ -12,6 +12,7 @@ lazy_static = { version = "1.4.0", features = ["spin_no_std"] }
|
||||||
buddy_system_allocator = "0.6"
|
buddy_system_allocator = "0.6"
|
||||||
spin = "0.7.0"
|
spin = "0.7.0"
|
||||||
bitflags = "1.2.1"
|
bitflags = "1.2.1"
|
||||||
|
xmas-elf = "0.7.0"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
board_qemu = []
|
board_qemu = []
|
||||||
|
|
|
@ -29,9 +29,9 @@ _num_app:
|
||||||
.quad {}"#, apps.len())?;
|
.quad {}"#, apps.len())?;
|
||||||
|
|
||||||
for i in 0..apps.len() {
|
for i in 0..apps.len() {
|
||||||
writeln!(f, r#".quad app_{}_start"#, i)?;
|
writeln!(f, r#" .quad app_{}_start"#, i)?;
|
||||||
}
|
}
|
||||||
writeln!(f, r#".quad app_{}_end"#, apps.len() - 1)?;
|
writeln!(f, r#" .quad app_{}_end"#, apps.len() - 1)?;
|
||||||
|
|
||||||
for (idx, app) in apps.iter().enumerate() {
|
for (idx, app) in apps.iter().enumerate() {
|
||||||
println!("app_{}: {}", idx, app);
|
println!("app_{}: {}", idx, app);
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
use crate::trap::TrapContext;
|
use crate::trap::TrapContext;
|
||||||
use crate::task::TaskContext;
|
use crate::task::TaskContext;
|
||||||
use crate::config::*;
|
use crate::config::*;
|
||||||
|
use xmas_elf::ElfFile;
|
||||||
|
|
||||||
#[repr(align(4096))]
|
#[repr(align(4096))]
|
||||||
struct KernelStack {
|
struct KernelStack {
|
||||||
|
@ -52,6 +53,32 @@ pub fn get_num_app() -> usize {
|
||||||
unsafe { (_num_app as usize as *const usize).read_volatile() }
|
unsafe { (_num_app as usize as *const usize).read_volatile() }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn debug_elf(start_addr: usize, end_addr: usize) {
|
||||||
|
let data_array = unsafe {
|
||||||
|
core::slice::from_raw_parts(start_addr as *const u8, end_addr - start_addr)
|
||||||
|
};
|
||||||
|
let elf = ElfFile::new(data_array).unwrap();
|
||||||
|
let elf_header = elf.header;
|
||||||
|
let magic = elf_header.pt1.magic;
|
||||||
|
assert_eq!(magic, [0x7f, 0x45, 0x4c, 0x46], "invalid elf!");
|
||||||
|
let ph_count = elf_header.pt2.ph_count();
|
||||||
|
println!("ph_count = {}", ph_count);
|
||||||
|
for i in 0..ph_count {
|
||||||
|
let ph = elf.program_header(i).unwrap();
|
||||||
|
if ph.get_type().unwrap() == xmas_elf::program::Type::Load {
|
||||||
|
println!(
|
||||||
|
"offset={:#x},va={:#x},pa={:#x},filesz={:#x},memsz={:#x},align={:#x}",
|
||||||
|
ph.offset(),
|
||||||
|
ph.virtual_addr(),
|
||||||
|
ph.physical_addr(),
|
||||||
|
ph.file_size(),
|
||||||
|
ph.mem_size(),
|
||||||
|
ph.align(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn load_apps() {
|
pub fn load_apps() {
|
||||||
extern "C" { fn _num_app(); }
|
extern "C" { fn _num_app(); }
|
||||||
let num_app_ptr = _num_app as usize as *const usize;
|
let num_app_ptr = _num_app as usize as *const usize;
|
||||||
|
@ -59,6 +86,18 @@ pub fn load_apps() {
|
||||||
let app_start = unsafe {
|
let app_start = unsafe {
|
||||||
core::slice::from_raw_parts(num_app_ptr.add(1), num_app + 1)
|
core::slice::from_raw_parts(num_app_ptr.add(1), num_app + 1)
|
||||||
};
|
};
|
||||||
|
println!("num_app = {}", num_app);
|
||||||
|
for i in 0..num_app {
|
||||||
|
println!(
|
||||||
|
"app_{} [{:#x},{:#x}) size={:#x}",
|
||||||
|
i,
|
||||||
|
app_start[i],
|
||||||
|
app_start[i + 1],
|
||||||
|
app_start[i + 1] - app_start[i]
|
||||||
|
);
|
||||||
|
debug_elf(app_start[i], app_start[i + 1]);
|
||||||
|
}
|
||||||
|
loop {}
|
||||||
// clear i-cache first
|
// clear i-cache first
|
||||||
unsafe { llvm_asm!("fence.i" :::: "volatile"); }
|
unsafe { llvm_asm!("fence.i" :::: "volatile"); }
|
||||||
// load apps
|
// load apps
|
||||||
|
|
|
@ -43,9 +43,9 @@ pub fn rust_main() -> ! {
|
||||||
mm::init();
|
mm::init();
|
||||||
println!("[kernel] back to world!");
|
println!("[kernel] back to world!");
|
||||||
mm::remap_test();
|
mm::remap_test();
|
||||||
loop {}
|
|
||||||
trap::init();
|
trap::init();
|
||||||
loader::load_apps();
|
loader::load_apps();
|
||||||
|
loop {}
|
||||||
trap::enable_interrupt();
|
trap::enable_interrupt();
|
||||||
trap::enable_timer_interrupt();
|
trap::enable_timer_interrupt();
|
||||||
timer::set_next_trigger();
|
timer::set_next_trigger();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue