diff --git a/kernel/src/libs/elf.rs b/kernel/src/libs/elf.rs index 4abbba61c..6c74dec1a 100644 --- a/kernel/src/libs/elf.rs +++ b/kernel/src/libs/elf.rs @@ -361,23 +361,22 @@ impl ElfLoader { let mut last_bss: VirtAddr = VirtAddr::new(0); let mut bss_prot: Option = None; for section in phdr_table { - log::debug!("loading {:?}", section); if section.p_type == PT_LOAD { + log::debug!("loading {:?}", section); let mut elf_type = MapFlags::MAP_PRIVATE; let elf_prot = Self::make_prot(section.p_flags, true, true); let vaddr = TryInto::::try_into(section.p_vaddr).unwrap(); + let mut addr_to_map = load_addr + vaddr; if interp_hdr.e_type == ET_EXEC || load_addr_set { elf_type.insert(MapFlags::MAP_FIXED) //TODO 应当为MapFlags::MAP_FIXED,暂时未支持 - } - load_addr += vaddr; - if load_bias != 0 && interp_hdr.e_type == ET_DYN { - load_addr -= vaddr; + } else if load_bias != 0 && interp_hdr.e_type == ET_DYN { + addr_to_map = VirtAddr::new(0); } let map_addr = Self::load_elf_segment( &mut interp_elf_ex.vm().clone().write(), interp_elf_ex, §ion, - load_addr, + addr_to_map, &elf_prot, &elf_type, total_size, @@ -452,9 +451,8 @@ impl ElfLoader { _ => return ExecError::InvalidParemeter, })?; } - if load_addr + TryInto::::try_into(interp_hdr.e_entry).unwrap() - > MMArch::USER_END_VADDR - { + load_addr += TryInto::::try_into(interp_hdr.e_entry).unwrap(); + if load_addr > MMArch::USER_END_VADDR { return Err(ExecError::BadAddress(Some( load_addr + TryInto::::try_into(interp_hdr.e_entry).unwrap(), ))); @@ -868,6 +866,7 @@ impl BinaryLoader for ElfLoader { // 加载这个段到用户空间 + log::debug!("bias: {load_bias}"); let e = Self::load_elf_segment( &mut user_vm, param, @@ -882,7 +881,7 @@ impl BinaryLoader for ElfLoader { SystemError::ENOMEM => ExecError::OutOfMemory, _ => ExecError::Other(format!("load_elf_segment failed: {:?}", e)), })?; - + log::debug!("e.0={:?}", e.0); // 如果地址不对,那么就报错 if !e.1 { return Err(ExecError::BadAddress(Some(e.0))); @@ -1006,12 +1005,7 @@ impl BinaryLoader for ElfLoader { } // debug!("to create auxv"); let mut user_vm = binding.write(); - self.create_auxv( - param, - interp_load_addr.unwrap_or(program_entrypoint), - phdr_vaddr, - &ehdr, - )?; + self.create_auxv(param, program_entrypoint, phdr_vaddr, &ehdr)?; // debug!("auxv create ok"); user_vm.start_code = start_code.unwrap_or(VirtAddr::new(0)); @@ -1019,8 +1013,8 @@ impl BinaryLoader for ElfLoader { user_vm.start_data = start_data.unwrap_or(VirtAddr::new(0)); user_vm.end_data = end_data.unwrap_or(VirtAddr::new(0)); - let result = BinaryLoaderResult::new(program_entrypoint); - // debug!("elf load OK!!!"); + let result = BinaryLoaderResult::new(interp_load_addr.unwrap_or(program_entrypoint)); + // kdebug!("elf load OK!!!"); return Ok(result); } } diff --git a/kernel/src/process/exec.rs b/kernel/src/process/exec.rs index 909234641..a8f1e73f0 100644 --- a/kernel/src/process/exec.rs +++ b/kernel/src/process/exec.rs @@ -228,9 +228,15 @@ impl ProcInitInfo { &self, ustack: &mut UserStack, ) -> Result<(VirtAddr, VirtAddr), SystemError> { + // TODO: 实现栈的16字节对齐 + // self.push_slice( + // ustack, + // vec![0u8; 0x10 - (ustack.sp().data() & 0b1111)].as_slice(), + // )?; + self.push_slice(ustack, vec![0u8; 8].as_slice())?; + // 先把程序的名称压入栈中 self.push_str(ustack, &self.proc_name)?; - // 然后把环境变量压入栈中 let envps = self .envs diff --git a/kernel/src/syscall/mod.rs b/kernel/src/syscall/mod.rs index c9600fdf0..b41169b9b 100644 --- a/kernel/src/syscall/mod.rs +++ b/kernel/src/syscall/mod.rs @@ -860,8 +860,10 @@ impl Syscall { } SYS_EXIT_GROUP => { - warn!("SYS_EXIT_GROUP has not yet been implemented"); - Ok(0) + let exit_code = args[0]; + Self::exit(exit_code) + // warn!("SYS_EXIT_GROUP has not yet been implemented"); + // Ok(0) } SYS_MADVISE => {