1 use core::{ 2 cmp::min, 3 intrinsics::{likely, unlikely}, 4 ops::Range, 5 }; 6 7 use alloc::vec::Vec; 8 use elf::{endian::AnyEndian, file::FileHeader, segment::ProgramHeader}; 9 10 use crate::{ 11 arch::MMArch, 12 current_pcb, 13 io::SeekFrom, 14 kerror, 15 libs::align::page_align_up, 16 mm::{ 17 allocator::page_frame::{PageFrameCount, VirtPageFrame}, 18 syscall::{MapFlags, ProtFlags}, 19 ucontext::InnerAddressSpace, 20 MemoryManagementArch, VirtAddr, 21 }, 22 process::{ 23 abi::AtType, 24 exec::{BinaryLoader, BinaryLoaderResult, ExecError, ExecLoadMode, ExecParam}, 25 }, 26 syscall::{ 27 user_access::{clear_user, copy_to_user}, 28 SystemError, 29 }, 30 }; 31 32 use super::rwlock::RwLockWriteGuard; 33 34 #[derive(Debug)] 35 pub struct ElfLoader; 36 37 pub const ELF_LOADER: ElfLoader = ElfLoader::new(); 38 39 impl ElfLoader { 40 #[cfg(target_arch = "x86_64")] 41 pub const ELF_PAGE_SIZE: usize = MMArch::PAGE_SIZE; 42 43 /// 读取文件的缓冲区大小 44 pub const FILE_READ_BUF_SIZE: usize = 512 * 1024; 45 46 pub const fn new() -> Self { 47 Self 48 } 49 50 #[cfg(target_arch = "x86_64")] 51 pub fn probe_x86_64( 52 &self, 53 param: &ExecParam, 54 ehdr: &FileHeader<AnyEndian>, 55 ) -> Result<(), ExecError> { 56 // 只支持 64 位的 ELF 文件 57 if ehdr.class != elf::file::Class::ELF64 { 58 return Err(ExecError::WrongArchitecture); 59 } 60 61 // 判断架构是否匹配 62 if ElfMachine::from(ehdr.e_machine) != ElfMachine::X86_64 { 63 return Err(ExecError::WrongArchitecture); 64 } 65 66 // 判断是否以可执行文件的形式加载 67 if param.load_mode() == ExecLoadMode::Exec { 68 // 检查文件类型是否为可执行文件 69 if ElfType::from(ehdr.e_type) != ElfType::Executable { 70 return Err(ExecError::NotExecutable); 71 } 72 } else { 73 return Err(ExecError::NotSupported); 74 } 75 76 return Ok(()); 77 } 78 79 /// 设置用户堆空间,映射[start, end)区间的虚拟地址,并把brk指针指向end 80 /// 81 /// ## 参数 82 /// 83 /// - `user_vm_guard` - 用户虚拟地址空间 84 /// - `start` - 本次映射的起始地址 85 /// - `end` - 本次映射的结束地址(不包含) 86 /// - `prot_flags` - 本次映射的权限 87 fn set_elf_brk( 88 &self, 89 user_vm_guard: &mut RwLockWriteGuard<'_, InnerAddressSpace>, 90 start: VirtAddr, 91 end: VirtAddr, 92 prot_flags: ProtFlags, 93 ) -> Result<(), ExecError> { 94 let start = self.elf_page_start(start); 95 let end = self.elf_page_align_up(end); 96 // kdebug!("set_elf_brk: start={:?}, end={:?}", start, end); 97 if end > start { 98 let r = user_vm_guard.map_anonymous( 99 start, 100 end - start, 101 prot_flags, 102 MapFlags::MAP_ANONYMOUS | MapFlags::MAP_FIXED_NOREPLACE, 103 false, 104 ); 105 if r.is_err() { 106 kerror!("set_elf_brk: map_anonymous failed, err={:?}", r); 107 return Err(ExecError::OutOfMemory); 108 } 109 } 110 user_vm_guard.elf_brk_start = end; 111 user_vm_guard.elf_brk = end; 112 return Ok(()); 113 } 114 115 /// 计算addr在ELF PAGE内的偏移 116 fn elf_page_offset(&self, addr: VirtAddr) -> usize { 117 addr.data() & (Self::ELF_PAGE_SIZE - 1) 118 } 119 120 fn elf_page_start(&self, addr: VirtAddr) -> VirtAddr { 121 VirtAddr::new(addr.data() & (!(Self::ELF_PAGE_SIZE - 1))) 122 } 123 124 fn elf_page_align_up(&self, addr: VirtAddr) -> VirtAddr { 125 VirtAddr::new((addr.data() + Self::ELF_PAGE_SIZE - 1) & (!(Self::ELF_PAGE_SIZE - 1))) 126 } 127 128 /// 根据ELF的p_flags生成对应的ProtFlags 129 fn make_prot(&self, p_flags: u32, _has_interpreter: bool, _is_interpreter: bool) -> ProtFlags { 130 let mut prot = ProtFlags::empty(); 131 if p_flags & elf::abi::PF_R != 0 { 132 prot |= ProtFlags::PROT_READ; 133 } 134 if p_flags & elf::abi::PF_W != 0 { 135 prot |= ProtFlags::PROT_WRITE; 136 } 137 if p_flags & elf::abi::PF_X != 0 { 138 prot |= ProtFlags::PROT_EXEC; 139 } 140 141 // todo: 增加与架构相关的处理 142 // ref: https://opengrok.ringotek.cn/xref/linux-5.19.10/fs/binfmt_elf.c?r=&mo=22652&fi=824#572 143 144 return prot; 145 } 146 147 /// 加载ELF文件到用户空间 148 /// 149 /// 参考Linux的elf_map函数 150 /// https://opengrok.ringotek.cn/xref/linux-5.19.10/fs/binfmt_elf.c?r=&mo=22652&fi=824#365 151 /// ## 参数 152 /// 153 /// - `user_vm_guard`:用户空间地址空间 154 /// - `param`:执行参数 155 /// - `phent`:ELF文件的ProgramHeader 156 /// - `addr_to_map`:当前段应该被加载到的内存地址 157 /// - `prot`:保护标志 158 /// - `map_flags`:映射标志 159 /// - `total_size`:ELF文件的总大小 160 /// 161 /// ## 返回值 162 /// 163 /// - `Ok((VirtAddr, bool))`:如果成功加载,则bool值为true,否则为false. VirtAddr为加载的地址 164 fn load_elf_segment( 165 &self, 166 user_vm_guard: &mut RwLockWriteGuard<'_, InnerAddressSpace>, 167 param: &mut ExecParam, 168 phent: &ProgramHeader, 169 mut addr_to_map: VirtAddr, 170 prot: &ProtFlags, 171 map_flags: &MapFlags, 172 total_size: usize, 173 ) -> Result<(VirtAddr, bool), SystemError> { 174 // kdebug!("load_elf_segment: addr_to_map={:?}", addr_to_map); 175 176 // 映射位置的偏移量(页内偏移) 177 let beginning_page_offset = self.elf_page_offset(addr_to_map); 178 addr_to_map = self.elf_page_start(addr_to_map); 179 // 计算要映射的内存的大小 180 let map_size = phent.p_filesz as usize 181 + self.elf_page_offset(VirtAddr::new(phent.p_vaddr as usize)) 182 + beginning_page_offset; 183 let map_size = self.elf_page_align_up(VirtAddr::new(map_size)).data(); 184 // 当前段在文件中的大小 185 let seg_in_file_size = phent.p_filesz as usize; 186 // 当前段在文件中的偏移量 187 let file_offset = phent.p_offset as usize; 188 189 // 如果当前段的大小为0,则直接返回. 190 // 段在文件中的大小为0,是合法的,但是段在内存中的大小不能为0 191 if map_size == 0 { 192 return Ok((addr_to_map, true)); 193 } 194 195 let map_err_handler = |err: SystemError| { 196 if err == SystemError::EEXIST { 197 kerror!( 198 "Pid: {}, elf segment at {:p} overlaps with existing mapping", 199 current_pcb().pid, 200 addr_to_map.as_ptr::<u8>() 201 ); 202 } 203 err 204 }; 205 // 由于后面需要把ELF文件的内容加载到内存,因此暂时把当前段的权限设置为可写 206 let tmp_prot = if !prot.contains(ProtFlags::PROT_WRITE) { 207 *prot | ProtFlags::PROT_WRITE 208 } else { 209 *prot 210 }; 211 212 // 映射到的虚拟地址。请注意,这个虚拟地址是user_vm_guard这个地址空间的虚拟地址。不一定是当前进程地址空间的 213 let map_addr: VirtAddr; 214 215 // total_size is the size of the ELF (interpreter) image. 216 // The _first_ mmap needs to know the full size, otherwise 217 // randomization might put this image into an overlapping 218 // position with the ELF binary image. (since size < total_size) 219 // So we first map the 'big' image - and unmap the remainder at 220 // the end. (which unmap is needed for ELF images with holes.) 221 if total_size != 0 { 222 let total_size = self.elf_page_align_up(VirtAddr::new(total_size)).data(); 223 224 // kdebug!("total_size={}", total_size); 225 226 map_addr = user_vm_guard 227 .map_anonymous(addr_to_map, total_size, tmp_prot, *map_flags, false) 228 .map_err(map_err_handler)? 229 .virt_address(); 230 // kdebug!("map ok: addr_to_map={:?}", addr_to_map); 231 232 let to_unmap = map_addr + map_size; 233 let to_unmap_size = total_size - map_size; 234 235 // kdebug!("to_unmap={:?}, to_unmap_size={}", to_unmap, to_unmap_size); 236 user_vm_guard.munmap( 237 VirtPageFrame::new(to_unmap), 238 PageFrameCount::from_bytes(to_unmap_size).unwrap(), 239 )?; 240 241 // 加载文件到内存 242 self.do_load_file( 243 map_addr + beginning_page_offset, 244 seg_in_file_size, 245 file_offset, 246 param, 247 )?; 248 if tmp_prot != *prot { 249 user_vm_guard.mprotect( 250 VirtPageFrame::new(map_addr), 251 PageFrameCount::from_bytes(page_align_up(map_size)).unwrap(), 252 *prot, 253 )?; 254 } 255 } else { 256 // kdebug!("total size = 0"); 257 258 map_addr = user_vm_guard 259 .map_anonymous(addr_to_map, map_size, tmp_prot, *map_flags, false)? 260 .virt_address(); 261 // kdebug!( 262 // "map ok: addr_to_map={:?}, map_addr={map_addr:?},beginning_page_offset={beginning_page_offset:?}", 263 // addr_to_map 264 // ); 265 266 // 加载文件到内存 267 self.do_load_file( 268 map_addr + beginning_page_offset, 269 seg_in_file_size, 270 file_offset, 271 param, 272 )?; 273 274 if tmp_prot != *prot { 275 user_vm_guard.mprotect( 276 VirtPageFrame::new(map_addr), 277 PageFrameCount::from_bytes(page_align_up(map_size)).unwrap(), 278 *prot, 279 )?; 280 } 281 } 282 // kdebug!("load_elf_segment OK: map_addr={:?}", map_addr); 283 return Ok((map_addr, true)); 284 } 285 286 /// 加载ELF文件到用户空间 287 /// 288 /// ## 参数 289 /// 290 /// - `vaddr`:要加载到的虚拟地址 291 /// - `size`:要加载的大小 292 /// - `offset_in_file`:在文件内的偏移量 293 /// - `param`:执行参数 294 fn do_load_file( 295 &self, 296 mut vaddr: VirtAddr, 297 size: usize, 298 offset_in_file: usize, 299 param: &mut ExecParam, 300 ) -> Result<(), SystemError> { 301 let file = param.file_mut(); 302 if (file.metadata()?.size as usize) < offset_in_file + size { 303 return Err(SystemError::ENOEXEC); 304 } 305 let buf_size = min(size, Self::FILE_READ_BUF_SIZE); 306 let mut buf = vec![0u8; buf_size]; 307 308 let mut remain = size; 309 310 file.lseek(SeekFrom::SeekSet(offset_in_file as i64))?; 311 312 while remain > 0 { 313 let read_size = min(remain, buf_size); 314 file.read(read_size, &mut buf[..read_size])?; 315 // kdebug!("copy_to_user: vaddr={:?}, read_size = {read_size}", vaddr); 316 unsafe { 317 copy_to_user(vaddr, &buf[..read_size]).map_err(|_| SystemError::EFAULT)?; 318 } 319 320 vaddr += read_size; 321 remain -= read_size; 322 } 323 return Ok(()); 324 } 325 326 /// 我们需要显式的把数据段之后剩余的内存页都清零。 327 fn pad_zero(&self, elf_bss: VirtAddr) -> Result<(), SystemError> { 328 let nbyte = self.elf_page_offset(elf_bss); 329 if nbyte > 0 { 330 let nbyte = Self::ELF_PAGE_SIZE - nbyte; 331 unsafe { clear_user(elf_bss, nbyte).map_err(|_| SystemError::EFAULT) }?; 332 } 333 return Ok(()); 334 } 335 336 /// 创建auxv 337 /// 338 /// ## 参数 339 /// 340 /// - `param`:执行参数 341 /// - `entrypoint_vaddr`:程序入口地址 342 /// - `phdr_vaddr`:程序头表地址 343 /// - `elf_header`:ELF文件头 344 fn create_auxv( 345 &self, 346 param: &mut ExecParam, 347 entrypoint_vaddr: VirtAddr, 348 phdr_vaddr: Option<VirtAddr>, 349 ehdr: &elf::file::FileHeader<AnyEndian>, 350 ) -> Result<(), ExecError> { 351 let phdr_vaddr = phdr_vaddr.unwrap_or(VirtAddr::new(0)); 352 353 let init_info = param.init_info_mut(); 354 init_info 355 .auxv 356 .insert(AtType::PhEnt as u8, ehdr.e_phentsize as usize); 357 init_info 358 .auxv 359 .insert(AtType::PageSize as u8, MMArch::PAGE_SIZE); 360 init_info.auxv.insert(AtType::Phdr as u8, phdr_vaddr.data()); 361 init_info 362 .auxv 363 .insert(AtType::PhNum as u8, ehdr.e_phnum as usize); 364 init_info 365 .auxv 366 .insert(AtType::Entry as u8, entrypoint_vaddr.data()); 367 368 return Ok(()); 369 } 370 371 /// 解析文件的ehdr 372 fn parse_ehdr(data: &[u8]) -> Result<FileHeader<AnyEndian>, elf::ParseError> { 373 let ident_buf = data.get_bytes(0..elf::abi::EI_NIDENT)?; 374 let ident = elf::file::parse_ident::<AnyEndian>(ident_buf)?; 375 376 let tail_start = elf::abi::EI_NIDENT; 377 let tail_end = match ident.1 { 378 elf::file::Class::ELF32 => tail_start + elf::file::ELF32_EHDR_TAILSIZE, 379 elf::file::Class::ELF64 => tail_start + elf::file::ELF64_EHDR_TAILSIZE, 380 }; 381 let tail_buf = data.get_bytes(tail_start..tail_end)?; 382 383 let ehdr: FileHeader<_> = FileHeader::parse_tail(ident, tail_buf)?; 384 return Ok(ehdr); 385 } 386 387 /// 解析文件的program header table 388 /// 389 /// ## 参数 390 /// 391 /// - `param`:执行参数 392 /// - `ehdr`:文件头 393 /// - `data_buf`:用于缓存SegmentTable的Vec。 394 /// 这是因为SegmentTable的生命周期与data_buf一致。初始化这个Vec的大小为0即可。 395 /// 396 /// ## 说明 397 /// 398 /// 这个函数由elf库的`elf::elf_bytes::find_phdrs`修改而来。 399 fn parse_segments<'a>( 400 param: &mut ExecParam, 401 ehdr: &FileHeader<AnyEndian>, 402 data_buf: &'a mut Vec<u8>, 403 ) -> Result<Option<elf::segment::SegmentTable<'a, AnyEndian>>, elf::ParseError> { 404 // It's Ok to have no program headers 405 if ehdr.e_phoff == 0 { 406 return Ok(None); 407 } 408 let file = param.file_mut(); 409 // If the number of segments is greater than or equal to PN_XNUM (0xffff), 410 // e_phnum is set to PN_XNUM, and the actual number of program header table 411 // entries is contained in the sh_info field of the section header at index 0. 412 let mut phnum = ehdr.e_phnum as usize; 413 if phnum == elf::abi::PN_XNUM as usize { 414 let shoff: usize = ehdr.e_shoff.try_into()?; 415 416 // 从磁盘读取shdr的前2个entry 417 file.lseek(SeekFrom::SeekSet(shoff as i64)) 418 .map_err(|_| elf::ParseError::BadOffset(shoff as u64))?; 419 let shdr_buf_size = ehdr.e_shentsize * 2; 420 let mut shdr_buf = vec![0u8; shdr_buf_size as usize]; 421 file.read(shdr_buf_size as usize, &mut shdr_buf) 422 .map_err(|_| elf::ParseError::BadOffset(shoff as u64))?; 423 424 let mut offset = 0; 425 let shdr0 = <elf::section::SectionHeader as elf::parse::ParseAt>::parse_at( 426 ehdr.endianness, 427 ehdr.class, 428 &mut offset, 429 &shdr_buf, 430 )?; 431 phnum = shdr0.sh_info.try_into()?; 432 } 433 434 // Validate phentsize before trying to read the table so that we can error early for corrupted files 435 let entsize = <ProgramHeader as elf::parse::ParseAt>::validate_entsize( 436 ehdr.class, 437 ehdr.e_phentsize as usize, 438 )?; 439 let phoff: usize = ehdr.e_phoff.try_into()?; 440 let size = entsize 441 .checked_mul(phnum) 442 .ok_or(elf::ParseError::IntegerOverflow)?; 443 phoff 444 .checked_add(size) 445 .ok_or(elf::ParseError::IntegerOverflow)?; 446 447 // 读取program header table 448 449 file.lseek(SeekFrom::SeekSet(phoff as i64)) 450 .map_err(|_| elf::ParseError::BadOffset(phoff as u64))?; 451 data_buf.clear(); 452 data_buf.resize(size, 0); 453 454 file.read(size, data_buf) 455 .expect("read program header table failed"); 456 let buf = data_buf.get_bytes(0..size)?; 457 458 return Ok(Some(elf::segment::SegmentTable::new( 459 ehdr.endianness, 460 ehdr.class, 461 buf, 462 ))); 463 } 464 } 465 466 impl BinaryLoader for ElfLoader { 467 fn probe(self: &'static Self, param: &ExecParam, buf: &[u8]) -> Result<(), ExecError> { 468 // let elf_bytes = 469 // ElfBytes::<AnyEndian>::minimal_parse(buf).map_err(|_| ExecError::NotExecutable)?; 470 471 let ehdr = Self::parse_ehdr(buf).map_err(|_| ExecError::NotExecutable)?; 472 473 #[cfg(target_arch = "x86_64")] 474 return self.probe_x86_64(param, &ehdr); 475 476 #[cfg(not(target_arch = "x86_64"))] 477 unimplemented!("Unsupported architecture"); 478 } 479 480 fn load( 481 self: &'static Self, 482 param: &mut ExecParam, 483 head_buf: &[u8], 484 ) -> Result<BinaryLoaderResult, ExecError> { 485 // 解析elf文件头 486 let ehdr = Self::parse_ehdr(head_buf).map_err(|_| ExecError::NotExecutable)?; 487 488 // 参考linux-5.19的load_elf_binary函数 489 // https://opengrok.ringotek.cn/xref/linux-5.19.10/fs/binfmt_elf.c?r=&mo=22652&fi=824#1034 490 491 let elf_type = ElfType::from(ehdr.e_type); 492 // kdebug!("ehdr = {:?}", ehdr); 493 494 let binding = param.vm().clone(); 495 let mut user_vm = binding.write(); 496 497 // todo: 增加对user stack上的内存是否具有可执行权限的处理(方法:寻找phdr里面的PT_GNU_STACK段) 498 499 // todo: 增加对动态链接的处理 500 501 // kdebug!("to parse segments"); 502 // 加载ELF文件并映射到用户空间 503 let mut phdr_buf = Vec::new(); 504 let loadable_sections = Self::parse_segments(param, &ehdr, &mut phdr_buf) 505 .map_err(|_| ExecError::ParseError)? 506 .ok_or(ExecError::ParseError)? 507 .iter() 508 .filter(|seg| seg.p_type == elf::abi::PT_LOAD); 509 510 // kdebug!("loadable_sections = {:?}", loadable_sections); 511 512 let mut elf_brk = VirtAddr::new(0); 513 let mut elf_bss = VirtAddr::new(0); 514 let mut start_code: Option<VirtAddr> = None; 515 let mut end_code: Option<VirtAddr> = None; 516 let mut start_data: Option<VirtAddr> = None; 517 let mut end_data: Option<VirtAddr> = None; 518 519 // 加载的时候的偏移量(这个偏移量在加载动态链接段的时候产生,由于还没有动态链接,因此暂时不可变。) 520 // 请不要删除load_bias! 以免到时候写动态链接的时候忘记了。 521 let load_bias = 0usize; 522 let mut bss_prot_flags = ProtFlags::empty(); 523 // 是否是第一个加载的段 524 let mut first_pt_load = true; 525 // program header的虚拟地址 526 let mut phdr_vaddr: Option<VirtAddr> = None; 527 for seg_to_load in loadable_sections { 528 // kdebug!("seg_to_load = {:?}", seg_to_load); 529 if unlikely(elf_brk > elf_bss) { 530 // kdebug!( 531 // "to set brk, elf_brk = {:?}, elf_bss = {:?}", 532 // elf_brk, 533 // elf_bss 534 // ); 535 self.set_elf_brk( 536 &mut user_vm, 537 elf_bss + load_bias, 538 elf_brk + load_bias, 539 bss_prot_flags, 540 )?; 541 let nbyte = self.elf_page_offset(elf_bss); 542 if nbyte > 0 { 543 let nbyte = min(Self::ELF_PAGE_SIZE - nbyte, elf_brk - elf_bss); 544 unsafe { 545 // This bss-zeroing can fail if the ELF file specifies odd protections. 546 // So we don't check the return value. 547 clear_user(elf_bss + load_bias, nbyte).ok(); 548 } 549 } 550 } 551 552 // 生成ProtFlags. 553 // TODO: 当有了动态链接之后,需要根据情况设置这里的has_interpreter 554 let elf_prot_flags = self.make_prot(seg_to_load.p_flags, false, false); 555 556 let mut elf_map_flags = MapFlags::MAP_PRIVATE; 557 558 let vaddr = VirtAddr::new(seg_to_load.p_vaddr as usize); 559 560 if !first_pt_load { 561 elf_map_flags.insert(MapFlags::MAP_FIXED_NOREPLACE); 562 } else if elf_type == ElfType::Executable { 563 /* 564 * This logic is run once for the first LOAD Program 565 * Header for ET_EXEC binaries. No special handling 566 * is needed. 567 */ 568 elf_map_flags.insert(MapFlags::MAP_FIXED_NOREPLACE); 569 } else if elf_type == ElfType::DSO { 570 // TODO: 支持动态链接 571 unimplemented!("DragonOS currently does not support dynamic linking!"); 572 } 573 574 // 加载这个段到用户空间 575 // todo: 引入动态链接后,这里的total_size要按照实际的填写,而不一定是0 576 577 let e = self 578 .load_elf_segment( 579 &mut user_vm, 580 param, 581 &seg_to_load, 582 vaddr + load_bias, 583 &elf_prot_flags, 584 &elf_map_flags, 585 0, 586 ) 587 .map_err(|e| match e { 588 SystemError::EFAULT => ExecError::BadAddress(None), 589 SystemError::ENOMEM => ExecError::OutOfMemory, 590 _ => ExecError::Other(format!("load_elf_segment failed: {:?}", e)), 591 })?; 592 593 // 如果地址不对,那么就报错 594 if !e.1 { 595 return Err(ExecError::BadAddress(Some(e.0))); 596 } 597 598 if first_pt_load { 599 first_pt_load = false; 600 if elf_type == ElfType::DSO { 601 // todo: 在这里增加对load_bias和reloc_func_desc的更新代码 602 todo!() 603 } 604 } 605 606 // kdebug!("seg_to_load.p_offset={}", seg_to_load.p_offset); 607 // kdebug!("e_phoff={}", ehdr.e_phoff); 608 // kdebug!("seg_to_load.p_filesz={}", seg_to_load.p_filesz); 609 // Figure out which segment in the file contains the Program Header Table, 610 // and map to the associated virtual address. 611 if (seg_to_load.p_offset <= ehdr.e_phoff) 612 && (ehdr.e_phoff < (seg_to_load.p_offset + seg_to_load.p_filesz)) 613 { 614 phdr_vaddr = Some(VirtAddr::new( 615 (ehdr.e_phoff - seg_to_load.p_offset + seg_to_load.p_vaddr) as usize, 616 )); 617 } 618 619 let p_vaddr = VirtAddr::new(seg_to_load.p_vaddr as usize); 620 if (seg_to_load.p_flags & elf::abi::PF_X) != 0 { 621 if start_code.is_none() || start_code.as_ref().unwrap() > &p_vaddr { 622 start_code = Some(p_vaddr); 623 } 624 } 625 626 if start_data.is_none() 627 || (start_data.is_some() && start_data.as_ref().unwrap() > &p_vaddr) 628 { 629 start_data = Some(p_vaddr); 630 } 631 632 // 如果程序段要加载的目标地址不在用户空间内,或者是其他不合法的情况,那么就报错 633 if !p_vaddr.check_user() 634 || seg_to_load.p_filesz > seg_to_load.p_memsz 635 || seg_to_load.p_memsz > MMArch::USER_END_VADDR.data() as u64 636 { 637 // kdebug!("ERR: p_vaddr={p_vaddr:?}"); 638 return Err(ExecError::InvalidParemeter); 639 } 640 641 drop(p_vaddr); 642 643 // end vaddr of this segment(code+data+bss) 644 let seg_end_vaddr_f = self.elf_page_align_up(VirtAddr::new( 645 (seg_to_load.p_vaddr + seg_to_load.p_filesz) as usize, 646 )); 647 648 if seg_end_vaddr_f > elf_bss { 649 elf_bss = seg_end_vaddr_f; 650 } 651 652 if ((seg_to_load.p_flags & elf::abi::PF_X) != 0) 653 && (end_code.is_none() 654 || (end_code.is_some() && end_code.as_ref().unwrap() < &seg_end_vaddr_f)) 655 { 656 end_code = Some(seg_end_vaddr_f); 657 } 658 659 if end_data.is_none() 660 || (end_data.is_some() && end_data.as_ref().unwrap() < &seg_end_vaddr_f) 661 { 662 end_data = Some(seg_end_vaddr_f); 663 } 664 665 drop(seg_end_vaddr_f); 666 667 let seg_end_vaddr = VirtAddr::new((seg_to_load.p_vaddr + seg_to_load.p_memsz) as usize); 668 669 if seg_end_vaddr > elf_brk { 670 bss_prot_flags = elf_prot_flags; 671 elf_brk = seg_end_vaddr; 672 } 673 } 674 // kdebug!("elf load: phdr_vaddr={phdr_vaddr:?}"); 675 let program_entrypoint = VirtAddr::new(ehdr.e_entry as usize + load_bias); 676 let phdr_vaddr = if phdr_vaddr.is_some() { 677 Some(phdr_vaddr.unwrap() + load_bias) 678 } else { 679 None 680 }; 681 682 elf_bss += load_bias; 683 elf_brk += load_bias; 684 start_code = start_code.map(|v| v + load_bias); 685 end_code = end_code.map(|v| v + load_bias); 686 start_data = start_data.map(|v| v + load_bias); 687 end_data = end_data.map(|v| v + load_bias); 688 689 // kdebug!( 690 // "to set brk: elf_bss: {:?}, elf_brk: {:?}, bss_prot_flags: {:?}", 691 // elf_bss, 692 // elf_brk, 693 // bss_prot_flags 694 // ); 695 self.set_elf_brk(&mut user_vm, elf_bss, elf_brk, bss_prot_flags)?; 696 697 if likely(elf_bss != elf_brk) && unlikely(self.pad_zero(elf_bss).is_err()) { 698 // kdebug!("elf_bss = {elf_bss:?}, elf_brk = {elf_brk:?}"); 699 return Err(ExecError::BadAddress(Some(elf_bss))); 700 } 701 // todo: 动态链接:增加加载interpreter的代码 702 // kdebug!("to create auxv"); 703 704 self.create_auxv(param, program_entrypoint, phdr_vaddr, &ehdr)?; 705 706 // kdebug!("auxv create ok"); 707 user_vm.start_code = start_code.unwrap_or(VirtAddr::new(0)); 708 user_vm.end_code = end_code.unwrap_or(VirtAddr::new(0)); 709 user_vm.start_data = start_data.unwrap_or(VirtAddr::new(0)); 710 user_vm.end_data = end_data.unwrap_or(VirtAddr::new(0)); 711 712 let result = BinaryLoaderResult::new(program_entrypoint); 713 // kdebug!("elf load OK!!!"); 714 return Ok(result); 715 } 716 } 717 718 /// Elf机器架构,对应于e_machine字段。在ABI中,以EM_开头的常量是e_machine字段的值。 719 #[derive(Debug, Eq, PartialEq)] 720 pub enum ElfMachine { 721 I386, 722 AArch32, 723 AArch64, 724 X86_64, 725 RiscV, 726 /// 龙芯架构 727 LoongArch, 728 /// 未知架构 729 Unknown, 730 } 731 732 impl From<u16> for ElfMachine { 733 fn from(machine: u16) -> Self { 734 match machine { 735 0x03 => Self::I386, 736 0x28 => Self::AArch32, 737 0xb7 => Self::AArch64, 738 0x3e => Self::X86_64, 739 0xf3 => Self::RiscV, 740 0x102 => Self::LoongArch, 741 // 未知架构 742 _ => Self::Unknown, 743 } 744 } 745 } 746 747 /// Elf文件类型,对应于e_type字段。在ABI中,以ET_开头的常量是e_type字段的值。 748 #[derive(Debug, Eq, PartialEq)] 749 pub enum ElfType { 750 /// 可重定位文件 751 Relocatable, 752 /// 可执行文件 753 Executable, 754 /// 动态链接库 755 DSO, 756 /// 核心转储文件 757 Core, 758 /// 未知类型 759 Unknown, 760 } 761 762 impl From<u16> for ElfType { 763 fn from(elf_type: u16) -> Self { 764 match elf_type { 765 0x01 => Self::Relocatable, 766 0x02 => Self::Executable, 767 0x03 => Self::DSO, 768 0x04 => Self::Core, 769 _ => Self::Unknown, 770 } 771 } 772 } 773 774 // Simple convenience extension trait to wrap get() with .ok_or(SliceReadError) 775 trait ReadBytesExt<'data> { 776 fn get_bytes(self, range: Range<usize>) -> Result<&'data [u8], elf::ParseError>; 777 } 778 impl<'data> ReadBytesExt<'data> for &'data [u8] { 779 fn get_bytes(self, range: Range<usize>) -> Result<&'data [u8], elf::ParseError> { 780 let start = range.start; 781 let end = range.end; 782 self.get(range) 783 .ok_or(elf::ParseError::SliceReadError((start, end))) 784 } 785 } 786