1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#![deny(missing_docs)]
use {
crate::error::LxResult,
crate::fs::INodeExt,
alloc::{collections::BTreeMap, string::String, sync::Arc, vec::Vec},
rcore_fs::vfs::INode,
xmas_elf::{program::ProgramHeader, ElfFile},
zircon_object::{util::elf_loader::*, vm::*, ZxError},
};
mod abi;
pub struct LinuxElfLoader {
pub syscall_entry: usize,
pub stack_pages: usize,
pub root_inode: Arc<dyn INode>,
}
impl LinuxElfLoader {
pub fn load(
&self,
vmar: &Arc<VmAddressRegion>,
data: &[u8],
args: Vec<String>,
envs: Vec<String>,
path: String,
) -> LxResult<(VirtAddr, VirtAddr)> {
debug!(
"load: vmar.addr & size: {:#x?}, data {:#x?}, args: {:?}, envs: {:?}",
vmar.get_info(),
data.as_ptr(),
args,
envs
);
let elf = ElfFile::new(data).map_err(|_| ZxError::INVALID_ARGS)?;
debug!("elf info: {:#x?}", elf.header.pt2);
if let Ok(interp) = elf.get_interpreter() {
info!("interp: {:?}, path: {:?}", interp, path);
let inode = self.root_inode.lookup(interp)?;
let data = inode.read_as_vec()?;
let mut new_args = vec![interp.into(), path.clone()];
new_args.extend_from_slice(&args[1..]);
return self.load(vmar, &data, new_args, envs, path);
}
let size = elf.load_segment_size();
let image_vmar = vmar.allocate(None, size, VmarFlags::CAN_MAP_RXW, PAGE_SIZE)?;
let mut base = image_vmar.addr();
let vmo = image_vmar.load_from_elf(&elf)?;
let entry = base + elf.header.pt2.entry_point() as usize;
let ph: ProgramHeader = elf.program_iter().next().unwrap();
let static_prog_base = ph.virtual_addr() as usize / PAGE_SIZE * PAGE_SIZE;
debug!(
"load: vmar.addr & size: {:#x?}, base: {:#x?}, entry: {:#x?}",
vmar.get_info(),
base,
entry
);
if let Some(offset) = elf.get_symbol_address("rcore_syscall_entry") {
vmo.write(offset as usize, &self.syscall_entry.to_ne_bytes())?;
}
match elf.relocate(image_vmar) {
Ok(()) => info!("elf relocate passed !"),
Err(error) => {
base = static_prog_base;
warn!("elf relocate Err:{:?}, base {:x?}", error, base);
}
}
let stack_vmo = VmObject::new_paged(self.stack_pages);
let flags = MMUFlags::READ | MMUFlags::WRITE | MMUFlags::USER;
let stack_bottom = vmar.map(None, stack_vmo.clone(), 0, stack_vmo.len(), flags)?;
let mut sp = stack_bottom + stack_vmo.len();
debug!("load stack bottom: {:#x}", stack_bottom);
let info = abi::ProcInitInfo {
args,
envs,
auxv: {
let mut map = BTreeMap::new();
#[cfg(target_arch = "x86_64")]
{
map.insert(abi::AT_BASE, base);
map.insert(abi::AT_PHDR, base + elf.header.pt2.ph_offset() as usize);
map.insert(abi::AT_ENTRY, entry);
}
#[cfg(target_arch = "riscv64")]
if let Some(phdr_vaddr) = elf.get_phdr_vaddr() {
map.insert(abi::AT_PHDR, phdr_vaddr as usize);
}
#[cfg(target_arch = "aarch64")]
{
map.insert(abi::AT_BASE, base);
map.insert(abi::AT_ENTRY, entry);
if let Some(phdr_vaddr) = elf.get_phdr_vaddr() {
map.insert(abi::AT_PHDR, phdr_vaddr as usize);
}
}
map.insert(abi::AT_PHENT, elf.header.pt2.ph_entry_size() as usize);
map.insert(abi::AT_PHNUM, elf.header.pt2.ph_count() as usize);
map.insert(abi::AT_PAGESZ, PAGE_SIZE);
map
},
};
let init_stack = info.push_at(sp);
stack_vmo.write(self.stack_pages * PAGE_SIZE - init_stack.len(), &init_stack)?;
sp -= init_stack.len();
debug!(
"ProcInitInfo auxv: {:#x?}\nentry:{:#x}, sp:{:#x}",
info.auxv, entry, sp
);
Ok((entry, sp))
}
}