Skip to content

Commit

Permalink
将入口点改为链接器;修正链接器加载地址
Browse files Browse the repository at this point in the history
  • Loading branch information
MemoryShore committed Aug 28, 2024
1 parent bd48a3d commit 1a627e4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 21 deletions.
30 changes: 12 additions & 18 deletions kernel/src/libs/elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,23 +361,22 @@ impl ElfLoader {
let mut last_bss: VirtAddr = VirtAddr::new(0);
let mut bss_prot: Option<ProtFlags> = 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::<usize>::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,
&section,
load_addr,
addr_to_map,
&elf_prot,
&elf_type,
total_size,
Expand Down Expand Up @@ -452,9 +451,8 @@ impl ElfLoader {
_ => return ExecError::InvalidParemeter,
})?;
}
if load_addr + TryInto::<usize>::try_into(interp_hdr.e_entry).unwrap()
> MMArch::USER_END_VADDR
{
load_addr += TryInto::<usize>::try_into(interp_hdr.e_entry).unwrap();
if load_addr > MMArch::USER_END_VADDR {
return Err(ExecError::BadAddress(Some(
load_addr + TryInto::<usize>::try_into(interp_hdr.e_entry).unwrap(),
)));
Expand Down Expand Up @@ -868,6 +866,7 @@ impl BinaryLoader for ElfLoader {

// 加载这个段到用户空间

log::debug!("bias: {load_bias}");
let e = Self::load_elf_segment(
&mut user_vm,
param,
Expand All @@ -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)));
Expand Down Expand Up @@ -1006,21 +1005,16 @@ 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));
user_vm.end_code = end_code.unwrap_or(VirtAddr::new(0));
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);
}
}
Expand Down
8 changes: 7 additions & 1 deletion kernel/src/process/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions kernel/src/syscall/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down

0 comments on commit 1a627e4

Please sign in to comment.