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