140fe15e0SLoGin use core::{ 240fe15e0SLoGin cmp::min, 340fe15e0SLoGin intrinsics::{likely, unlikely}, 440fe15e0SLoGin ops::Range, 540fe15e0SLoGin }; 640fe15e0SLoGin 740fe15e0SLoGin use alloc::vec::Vec; 840fe15e0SLoGin use elf::{endian::AnyEndian, file::FileHeader, segment::ProgramHeader}; 940fe15e0SLoGin 1040fe15e0SLoGin use crate::{ 1140fe15e0SLoGin arch::MMArch, 1240fe15e0SLoGin current_pcb, 1340fe15e0SLoGin io::SeekFrom, 1440fe15e0SLoGin kerror, 1540fe15e0SLoGin libs::align::page_align_up, 1640fe15e0SLoGin mm::{ 1740fe15e0SLoGin allocator::page_frame::{PageFrameCount, VirtPageFrame}, 1840fe15e0SLoGin syscall::{MapFlags, ProtFlags}, 1940fe15e0SLoGin ucontext::InnerAddressSpace, 2040fe15e0SLoGin MemoryManagementArch, VirtAddr, 2140fe15e0SLoGin }, 2240fe15e0SLoGin process::{ 2340fe15e0SLoGin abi::AtType, 2440fe15e0SLoGin exec::{BinaryLoader, BinaryLoaderResult, ExecError, ExecLoadMode, ExecParam}, 2540fe15e0SLoGin }, 2640fe15e0SLoGin syscall::{ 2740fe15e0SLoGin user_access::{clear_user, copy_to_user}, 2840fe15e0SLoGin SystemError, 2940fe15e0SLoGin }, 3040fe15e0SLoGin }; 3140fe15e0SLoGin 3240fe15e0SLoGin use super::rwlock::RwLockWriteGuard; 3340fe15e0SLoGin 3440fe15e0SLoGin #[derive(Debug)] 3540fe15e0SLoGin pub struct ElfLoader; 3640fe15e0SLoGin 3740fe15e0SLoGin pub const ELF_LOADER: ElfLoader = ElfLoader::new(); 3840fe15e0SLoGin 3940fe15e0SLoGin impl ElfLoader { 4040fe15e0SLoGin #[cfg(target_arch = "x86_64")] 4140fe15e0SLoGin pub const ELF_PAGE_SIZE: usize = MMArch::PAGE_SIZE; 4240fe15e0SLoGin 4340fe15e0SLoGin /// 读取文件的缓冲区大小 4440fe15e0SLoGin pub const FILE_READ_BUF_SIZE: usize = 512 * 1024; 4540fe15e0SLoGin 4640fe15e0SLoGin pub const fn new() -> Self { 4740fe15e0SLoGin Self 4840fe15e0SLoGin } 4940fe15e0SLoGin 5040fe15e0SLoGin #[cfg(target_arch = "x86_64")] 5140fe15e0SLoGin pub fn probe_x86_64( 5240fe15e0SLoGin &self, 5340fe15e0SLoGin param: &ExecParam, 5440fe15e0SLoGin ehdr: &FileHeader<AnyEndian>, 5540fe15e0SLoGin ) -> Result<(), ExecError> { 5640fe15e0SLoGin // 只支持 64 位的 ELF 文件 5740fe15e0SLoGin if ehdr.class != elf::file::Class::ELF64 { 5840fe15e0SLoGin return Err(ExecError::WrongArchitecture); 5940fe15e0SLoGin } 6040fe15e0SLoGin 6140fe15e0SLoGin // 判断架构是否匹配 6240fe15e0SLoGin if ElfMachine::from(ehdr.e_machine) != ElfMachine::X86_64 { 6340fe15e0SLoGin return Err(ExecError::WrongArchitecture); 6440fe15e0SLoGin } 6540fe15e0SLoGin 6640fe15e0SLoGin // 判断是否以可执行文件的形式加载 6740fe15e0SLoGin if param.load_mode() == ExecLoadMode::Exec { 6840fe15e0SLoGin // 检查文件类型是否为可执行文件 6940fe15e0SLoGin if ElfType::from(ehdr.e_type) != ElfType::Executable { 7040fe15e0SLoGin return Err(ExecError::NotExecutable); 7140fe15e0SLoGin } 7240fe15e0SLoGin } else { 7340fe15e0SLoGin return Err(ExecError::NotSupported); 7440fe15e0SLoGin } 7540fe15e0SLoGin 7640fe15e0SLoGin return Ok(()); 7740fe15e0SLoGin } 7840fe15e0SLoGin 7940fe15e0SLoGin /// 设置用户堆空间,映射[start, end)区间的虚拟地址,并把brk指针指向end 8040fe15e0SLoGin /// 8140fe15e0SLoGin /// ## 参数 8240fe15e0SLoGin /// 8340fe15e0SLoGin /// - `user_vm_guard` - 用户虚拟地址空间 8440fe15e0SLoGin /// - `start` - 本次映射的起始地址 8540fe15e0SLoGin /// - `end` - 本次映射的结束地址(不包含) 8640fe15e0SLoGin /// - `prot_flags` - 本次映射的权限 8740fe15e0SLoGin fn set_elf_brk( 8840fe15e0SLoGin &self, 8940fe15e0SLoGin user_vm_guard: &mut RwLockWriteGuard<'_, InnerAddressSpace>, 9040fe15e0SLoGin start: VirtAddr, 9140fe15e0SLoGin end: VirtAddr, 9240fe15e0SLoGin prot_flags: ProtFlags, 9340fe15e0SLoGin ) -> Result<(), ExecError> { 9440fe15e0SLoGin let start = self.elf_page_start(start); 9540fe15e0SLoGin let end = self.elf_page_align_up(end); 9640fe15e0SLoGin if end > start { 9740fe15e0SLoGin let r = user_vm_guard.map_anonymous( 9840fe15e0SLoGin start, 9940fe15e0SLoGin end - start, 10040fe15e0SLoGin prot_flags, 10140fe15e0SLoGin MapFlags::MAP_ANONYMOUS | MapFlags::MAP_FIXED_NOREPLACE, 10240fe15e0SLoGin false, 10340fe15e0SLoGin ); 10440fe15e0SLoGin if r.is_err() { 10540fe15e0SLoGin kerror!("set_elf_brk: map_anonymous failed, err={:?}", r); 10640fe15e0SLoGin return Err(ExecError::OutOfMemory); 10740fe15e0SLoGin } 10840fe15e0SLoGin } 10940fe15e0SLoGin user_vm_guard.elf_brk_start = end; 11040fe15e0SLoGin user_vm_guard.elf_brk = end; 11140fe15e0SLoGin return Ok(()); 11240fe15e0SLoGin } 11340fe15e0SLoGin 11440fe15e0SLoGin /// 计算addr在ELF PAGE内的偏移 11540fe15e0SLoGin fn elf_page_offset(&self, addr: VirtAddr) -> usize { 11640fe15e0SLoGin addr.data() & (Self::ELF_PAGE_SIZE - 1) 11740fe15e0SLoGin } 11840fe15e0SLoGin 11940fe15e0SLoGin fn elf_page_start(&self, addr: VirtAddr) -> VirtAddr { 12040fe15e0SLoGin VirtAddr::new(addr.data() & (!(Self::ELF_PAGE_SIZE - 1))) 12140fe15e0SLoGin } 12240fe15e0SLoGin 12340fe15e0SLoGin fn elf_page_align_up(&self, addr: VirtAddr) -> VirtAddr { 12440fe15e0SLoGin VirtAddr::new((addr.data() + Self::ELF_PAGE_SIZE - 1) & (!(Self::ELF_PAGE_SIZE - 1))) 12540fe15e0SLoGin } 12640fe15e0SLoGin 12740fe15e0SLoGin /// 根据ELF的p_flags生成对应的ProtFlags 12840fe15e0SLoGin fn make_prot(&self, p_flags: u32, _has_interpreter: bool, _is_interpreter: bool) -> ProtFlags { 12940fe15e0SLoGin let mut prot = ProtFlags::empty(); 13040fe15e0SLoGin if p_flags & elf::abi::PF_R != 0 { 13140fe15e0SLoGin prot |= ProtFlags::PROT_READ; 13240fe15e0SLoGin } 13340fe15e0SLoGin if p_flags & elf::abi::PF_W != 0 { 13440fe15e0SLoGin prot |= ProtFlags::PROT_WRITE; 13540fe15e0SLoGin } 13640fe15e0SLoGin if p_flags & elf::abi::PF_X != 0 { 13740fe15e0SLoGin prot |= ProtFlags::PROT_EXEC; 13840fe15e0SLoGin } 13940fe15e0SLoGin 14040fe15e0SLoGin // todo: 增加与架构相关的处理 14140fe15e0SLoGin // ref: https://opengrok.ringotek.cn/xref/linux-5.19.10/fs/binfmt_elf.c?r=&mo=22652&fi=824#572 14240fe15e0SLoGin 14340fe15e0SLoGin return prot; 14440fe15e0SLoGin } 14540fe15e0SLoGin 14640fe15e0SLoGin /// 加载ELF文件到用户空间 14740fe15e0SLoGin /// 14840fe15e0SLoGin /// 参考Linux的elf_map函数 14940fe15e0SLoGin /// https://opengrok.ringotek.cn/xref/linux-5.19.10/fs/binfmt_elf.c?r=&mo=22652&fi=824#365 15040fe15e0SLoGin /// ## 参数 15140fe15e0SLoGin /// 15240fe15e0SLoGin /// - `user_vm_guard`:用户空间地址空间 15340fe15e0SLoGin /// - `param`:执行参数 15440fe15e0SLoGin /// - `phent`:ELF文件的ProgramHeader 15540fe15e0SLoGin /// - `addr_to_map`:当前段应该被加载到的内存地址 15640fe15e0SLoGin /// - `prot`:保护标志 15740fe15e0SLoGin /// - `map_flags`:映射标志 15840fe15e0SLoGin /// - `total_size`:ELF文件的总大小 15940fe15e0SLoGin /// 16040fe15e0SLoGin /// ## 返回值 16140fe15e0SLoGin /// 16240fe15e0SLoGin /// - `Ok((VirtAddr, bool))`:如果成功加载,则bool值为true,否则为false. VirtAddr为加载的地址 16340fe15e0SLoGin fn load_elf_segment( 16440fe15e0SLoGin &self, 16540fe15e0SLoGin user_vm_guard: &mut RwLockWriteGuard<'_, InnerAddressSpace>, 16640fe15e0SLoGin param: &mut ExecParam, 16740fe15e0SLoGin phent: &ProgramHeader, 16840fe15e0SLoGin mut addr_to_map: VirtAddr, 16940fe15e0SLoGin prot: &ProtFlags, 17040fe15e0SLoGin map_flags: &MapFlags, 17140fe15e0SLoGin total_size: usize, 17240fe15e0SLoGin ) -> Result<(VirtAddr, bool), SystemError> { 17340fe15e0SLoGin // kdebug!("load_elf_segment: addr_to_map={:?}", addr_to_map); 17440fe15e0SLoGin 17540fe15e0SLoGin // 映射位置的偏移量(页内偏移) 17640fe15e0SLoGin let beginning_page_offset = self.elf_page_offset(addr_to_map); 17740fe15e0SLoGin addr_to_map = self.elf_page_start(addr_to_map); 17840fe15e0SLoGin // 计算要映射的内存的大小 179*6d81180bSLoGin let map_size = phent.p_filesz as usize + beginning_page_offset; 18040fe15e0SLoGin let map_size = self.elf_page_align_up(VirtAddr::new(map_size)).data(); 18140fe15e0SLoGin // 当前段在文件中的大小 18240fe15e0SLoGin let seg_in_file_size = phent.p_filesz as usize; 18340fe15e0SLoGin // 当前段在文件中的偏移量 18440fe15e0SLoGin let file_offset = phent.p_offset as usize; 18540fe15e0SLoGin 18640fe15e0SLoGin // 如果当前段的大小为0,则直接返回. 18740fe15e0SLoGin // 段在文件中的大小为0,是合法的,但是段在内存中的大小不能为0 18840fe15e0SLoGin if map_size == 0 { 18940fe15e0SLoGin return Ok((addr_to_map, true)); 19040fe15e0SLoGin } 19140fe15e0SLoGin 19240fe15e0SLoGin let map_err_handler = |err: SystemError| { 19340fe15e0SLoGin if err == SystemError::EEXIST { 19440fe15e0SLoGin kerror!( 19540fe15e0SLoGin "Pid: {}, elf segment at {:p} overlaps with existing mapping", 19640fe15e0SLoGin current_pcb().pid, 19740fe15e0SLoGin addr_to_map.as_ptr::<u8>() 19840fe15e0SLoGin ); 19940fe15e0SLoGin } 20040fe15e0SLoGin err 20140fe15e0SLoGin }; 20240fe15e0SLoGin // 由于后面需要把ELF文件的内容加载到内存,因此暂时把当前段的权限设置为可写 20340fe15e0SLoGin let tmp_prot = if !prot.contains(ProtFlags::PROT_WRITE) { 20440fe15e0SLoGin *prot | ProtFlags::PROT_WRITE 20540fe15e0SLoGin } else { 20640fe15e0SLoGin *prot 20740fe15e0SLoGin }; 20840fe15e0SLoGin 20940fe15e0SLoGin // 映射到的虚拟地址。请注意,这个虚拟地址是user_vm_guard这个地址空间的虚拟地址。不一定是当前进程地址空间的 21040fe15e0SLoGin let map_addr: VirtAddr; 21140fe15e0SLoGin 21240fe15e0SLoGin // total_size is the size of the ELF (interpreter) image. 21340fe15e0SLoGin // The _first_ mmap needs to know the full size, otherwise 21440fe15e0SLoGin // randomization might put this image into an overlapping 21540fe15e0SLoGin // position with the ELF binary image. (since size < total_size) 21640fe15e0SLoGin // So we first map the 'big' image - and unmap the remainder at 21740fe15e0SLoGin // the end. (which unmap is needed for ELF images with holes.) 21840fe15e0SLoGin if total_size != 0 { 21940fe15e0SLoGin let total_size = self.elf_page_align_up(VirtAddr::new(total_size)).data(); 22040fe15e0SLoGin 22140fe15e0SLoGin // kdebug!("total_size={}", total_size); 22240fe15e0SLoGin 22340fe15e0SLoGin map_addr = user_vm_guard 22440fe15e0SLoGin .map_anonymous(addr_to_map, total_size, tmp_prot, *map_flags, false) 22540fe15e0SLoGin .map_err(map_err_handler)? 22640fe15e0SLoGin .virt_address(); 22740fe15e0SLoGin // kdebug!("map ok: addr_to_map={:?}", addr_to_map); 22840fe15e0SLoGin 22940fe15e0SLoGin let to_unmap = map_addr + map_size; 23040fe15e0SLoGin let to_unmap_size = total_size - map_size; 23140fe15e0SLoGin 23240fe15e0SLoGin // kdebug!("to_unmap={:?}, to_unmap_size={}", to_unmap, to_unmap_size); 23340fe15e0SLoGin user_vm_guard.munmap( 23440fe15e0SLoGin VirtPageFrame::new(to_unmap), 23540fe15e0SLoGin PageFrameCount::from_bytes(to_unmap_size).unwrap(), 23640fe15e0SLoGin )?; 23740fe15e0SLoGin 23840fe15e0SLoGin // 加载文件到内存 23940fe15e0SLoGin self.do_load_file( 24040fe15e0SLoGin map_addr + beginning_page_offset, 24140fe15e0SLoGin seg_in_file_size, 24240fe15e0SLoGin file_offset, 24340fe15e0SLoGin param, 24440fe15e0SLoGin )?; 24540fe15e0SLoGin if tmp_prot != *prot { 24640fe15e0SLoGin user_vm_guard.mprotect( 24740fe15e0SLoGin VirtPageFrame::new(map_addr), 24840fe15e0SLoGin PageFrameCount::from_bytes(page_align_up(map_size)).unwrap(), 24940fe15e0SLoGin *prot, 25040fe15e0SLoGin )?; 25140fe15e0SLoGin } 25240fe15e0SLoGin } else { 25340fe15e0SLoGin // kdebug!("total size = 0"); 25440fe15e0SLoGin 25540fe15e0SLoGin map_addr = user_vm_guard 25640fe15e0SLoGin .map_anonymous(addr_to_map, map_size, tmp_prot, *map_flags, false)? 25740fe15e0SLoGin .virt_address(); 25840fe15e0SLoGin // kdebug!( 25940fe15e0SLoGin // "map ok: addr_to_map={:?}, map_addr={map_addr:?},beginning_page_offset={beginning_page_offset:?}", 26040fe15e0SLoGin // addr_to_map 26140fe15e0SLoGin // ); 26240fe15e0SLoGin 26340fe15e0SLoGin // 加载文件到内存 26440fe15e0SLoGin self.do_load_file( 26540fe15e0SLoGin map_addr + beginning_page_offset, 26640fe15e0SLoGin seg_in_file_size, 26740fe15e0SLoGin file_offset, 26840fe15e0SLoGin param, 26940fe15e0SLoGin )?; 27040fe15e0SLoGin 27140fe15e0SLoGin if tmp_prot != *prot { 27240fe15e0SLoGin user_vm_guard.mprotect( 27340fe15e0SLoGin VirtPageFrame::new(map_addr), 27440fe15e0SLoGin PageFrameCount::from_bytes(page_align_up(map_size)).unwrap(), 27540fe15e0SLoGin *prot, 27640fe15e0SLoGin )?; 27740fe15e0SLoGin } 27840fe15e0SLoGin } 27940fe15e0SLoGin // kdebug!("load_elf_segment OK: map_addr={:?}", map_addr); 28040fe15e0SLoGin return Ok((map_addr, true)); 28140fe15e0SLoGin } 28240fe15e0SLoGin 28340fe15e0SLoGin /// 加载ELF文件到用户空间 28440fe15e0SLoGin /// 28540fe15e0SLoGin /// ## 参数 28640fe15e0SLoGin /// 28740fe15e0SLoGin /// - `vaddr`:要加载到的虚拟地址 28840fe15e0SLoGin /// - `size`:要加载的大小 28940fe15e0SLoGin /// - `offset_in_file`:在文件内的偏移量 29040fe15e0SLoGin /// - `param`:执行参数 29140fe15e0SLoGin fn do_load_file( 29240fe15e0SLoGin &self, 29340fe15e0SLoGin mut vaddr: VirtAddr, 29440fe15e0SLoGin size: usize, 29540fe15e0SLoGin offset_in_file: usize, 29640fe15e0SLoGin param: &mut ExecParam, 29740fe15e0SLoGin ) -> Result<(), SystemError> { 29840fe15e0SLoGin let file = param.file_mut(); 29940fe15e0SLoGin if (file.metadata()?.size as usize) < offset_in_file + size { 30040fe15e0SLoGin return Err(SystemError::ENOEXEC); 30140fe15e0SLoGin } 30240fe15e0SLoGin let buf_size = min(size, Self::FILE_READ_BUF_SIZE); 30340fe15e0SLoGin let mut buf = vec![0u8; buf_size]; 30440fe15e0SLoGin 30540fe15e0SLoGin let mut remain = size; 30640fe15e0SLoGin 30740fe15e0SLoGin file.lseek(SeekFrom::SeekSet(offset_in_file as i64))?; 30840fe15e0SLoGin 30940fe15e0SLoGin while remain > 0 { 31040fe15e0SLoGin let read_size = min(remain, buf_size); 31140fe15e0SLoGin file.read(read_size, &mut buf[..read_size])?; 31240fe15e0SLoGin // kdebug!("copy_to_user: vaddr={:?}, read_size = {read_size}", vaddr); 31340fe15e0SLoGin unsafe { 31440fe15e0SLoGin copy_to_user(vaddr, &buf[..read_size]).map_err(|_| SystemError::EFAULT)?; 31540fe15e0SLoGin } 31640fe15e0SLoGin 31740fe15e0SLoGin vaddr += read_size; 31840fe15e0SLoGin remain -= read_size; 31940fe15e0SLoGin } 32040fe15e0SLoGin return Ok(()); 32140fe15e0SLoGin } 32240fe15e0SLoGin 32340fe15e0SLoGin /// 我们需要显式的把数据段之后剩余的内存页都清零。 32440fe15e0SLoGin fn pad_zero(&self, elf_bss: VirtAddr) -> Result<(), SystemError> { 32540fe15e0SLoGin let nbyte = self.elf_page_offset(elf_bss); 32640fe15e0SLoGin if nbyte > 0 { 32740fe15e0SLoGin let nbyte = Self::ELF_PAGE_SIZE - nbyte; 32840fe15e0SLoGin unsafe { clear_user(elf_bss, nbyte).map_err(|_| SystemError::EFAULT) }?; 32940fe15e0SLoGin } 33040fe15e0SLoGin return Ok(()); 33140fe15e0SLoGin } 33240fe15e0SLoGin 33340fe15e0SLoGin /// 创建auxv 33440fe15e0SLoGin /// 33540fe15e0SLoGin /// ## 参数 33640fe15e0SLoGin /// 33740fe15e0SLoGin /// - `param`:执行参数 33840fe15e0SLoGin /// - `entrypoint_vaddr`:程序入口地址 33940fe15e0SLoGin /// - `phdr_vaddr`:程序头表地址 34040fe15e0SLoGin /// - `elf_header`:ELF文件头 34140fe15e0SLoGin fn create_auxv( 34240fe15e0SLoGin &self, 34340fe15e0SLoGin param: &mut ExecParam, 34440fe15e0SLoGin entrypoint_vaddr: VirtAddr, 34540fe15e0SLoGin phdr_vaddr: Option<VirtAddr>, 34640fe15e0SLoGin ehdr: &elf::file::FileHeader<AnyEndian>, 34740fe15e0SLoGin ) -> Result<(), ExecError> { 34840fe15e0SLoGin let phdr_vaddr = phdr_vaddr.unwrap_or(VirtAddr::new(0)); 34940fe15e0SLoGin 35040fe15e0SLoGin let init_info = param.init_info_mut(); 35140fe15e0SLoGin init_info 35240fe15e0SLoGin .auxv 35340fe15e0SLoGin .insert(AtType::PhEnt as u8, ehdr.e_phentsize as usize); 35440fe15e0SLoGin init_info 35540fe15e0SLoGin .auxv 35640fe15e0SLoGin .insert(AtType::PageSize as u8, MMArch::PAGE_SIZE); 35740fe15e0SLoGin init_info.auxv.insert(AtType::Phdr as u8, phdr_vaddr.data()); 35840fe15e0SLoGin init_info 35940fe15e0SLoGin .auxv 36040fe15e0SLoGin .insert(AtType::PhNum as u8, ehdr.e_phnum as usize); 36140fe15e0SLoGin init_info 36240fe15e0SLoGin .auxv 36340fe15e0SLoGin .insert(AtType::Entry as u8, entrypoint_vaddr.data()); 36440fe15e0SLoGin 36540fe15e0SLoGin return Ok(()); 36640fe15e0SLoGin } 36740fe15e0SLoGin 36840fe15e0SLoGin /// 解析文件的ehdr 36940fe15e0SLoGin fn parse_ehdr(data: &[u8]) -> Result<FileHeader<AnyEndian>, elf::ParseError> { 37040fe15e0SLoGin let ident_buf = data.get_bytes(0..elf::abi::EI_NIDENT)?; 37140fe15e0SLoGin let ident = elf::file::parse_ident::<AnyEndian>(ident_buf)?; 37240fe15e0SLoGin 37340fe15e0SLoGin let tail_start = elf::abi::EI_NIDENT; 37440fe15e0SLoGin let tail_end = match ident.1 { 37540fe15e0SLoGin elf::file::Class::ELF32 => tail_start + elf::file::ELF32_EHDR_TAILSIZE, 37640fe15e0SLoGin elf::file::Class::ELF64 => tail_start + elf::file::ELF64_EHDR_TAILSIZE, 37740fe15e0SLoGin }; 37840fe15e0SLoGin let tail_buf = data.get_bytes(tail_start..tail_end)?; 37940fe15e0SLoGin 38040fe15e0SLoGin let ehdr: FileHeader<_> = FileHeader::parse_tail(ident, tail_buf)?; 38140fe15e0SLoGin return Ok(ehdr); 38240fe15e0SLoGin } 38340fe15e0SLoGin 38440fe15e0SLoGin /// 解析文件的program header table 38540fe15e0SLoGin /// 38640fe15e0SLoGin /// ## 参数 38740fe15e0SLoGin /// 38840fe15e0SLoGin /// - `param`:执行参数 38940fe15e0SLoGin /// - `ehdr`:文件头 39040fe15e0SLoGin /// - `data_buf`:用于缓存SegmentTable的Vec。 39140fe15e0SLoGin /// 这是因为SegmentTable的生命周期与data_buf一致。初始化这个Vec的大小为0即可。 39240fe15e0SLoGin /// 39340fe15e0SLoGin /// ## 说明 39440fe15e0SLoGin /// 39540fe15e0SLoGin /// 这个函数由elf库的`elf::elf_bytes::find_phdrs`修改而来。 39640fe15e0SLoGin fn parse_segments<'a>( 39740fe15e0SLoGin param: &mut ExecParam, 39840fe15e0SLoGin ehdr: &FileHeader<AnyEndian>, 39940fe15e0SLoGin data_buf: &'a mut Vec<u8>, 40040fe15e0SLoGin ) -> Result<Option<elf::segment::SegmentTable<'a, AnyEndian>>, elf::ParseError> { 40140fe15e0SLoGin // It's Ok to have no program headers 40240fe15e0SLoGin if ehdr.e_phoff == 0 { 40340fe15e0SLoGin return Ok(None); 40440fe15e0SLoGin } 40540fe15e0SLoGin let file = param.file_mut(); 40640fe15e0SLoGin // If the number of segments is greater than or equal to PN_XNUM (0xffff), 40740fe15e0SLoGin // e_phnum is set to PN_XNUM, and the actual number of program header table 40840fe15e0SLoGin // entries is contained in the sh_info field of the section header at index 0. 40940fe15e0SLoGin let mut phnum = ehdr.e_phnum as usize; 41040fe15e0SLoGin if phnum == elf::abi::PN_XNUM as usize { 41140fe15e0SLoGin let shoff: usize = ehdr.e_shoff.try_into()?; 41240fe15e0SLoGin 41340fe15e0SLoGin // 从磁盘读取shdr的前2个entry 41440fe15e0SLoGin file.lseek(SeekFrom::SeekSet(shoff as i64)) 41540fe15e0SLoGin .map_err(|_| elf::ParseError::BadOffset(shoff as u64))?; 41640fe15e0SLoGin let shdr_buf_size = ehdr.e_shentsize * 2; 41740fe15e0SLoGin let mut shdr_buf = vec![0u8; shdr_buf_size as usize]; 41840fe15e0SLoGin file.read(shdr_buf_size as usize, &mut shdr_buf) 41940fe15e0SLoGin .map_err(|_| elf::ParseError::BadOffset(shoff as u64))?; 42040fe15e0SLoGin 42140fe15e0SLoGin let mut offset = 0; 42240fe15e0SLoGin let shdr0 = <elf::section::SectionHeader as elf::parse::ParseAt>::parse_at( 42340fe15e0SLoGin ehdr.endianness, 42440fe15e0SLoGin ehdr.class, 42540fe15e0SLoGin &mut offset, 42640fe15e0SLoGin &shdr_buf, 42740fe15e0SLoGin )?; 42840fe15e0SLoGin phnum = shdr0.sh_info.try_into()?; 42940fe15e0SLoGin } 43040fe15e0SLoGin 43140fe15e0SLoGin // Validate phentsize before trying to read the table so that we can error early for corrupted files 43240fe15e0SLoGin let entsize = <ProgramHeader as elf::parse::ParseAt>::validate_entsize( 43340fe15e0SLoGin ehdr.class, 43440fe15e0SLoGin ehdr.e_phentsize as usize, 43540fe15e0SLoGin )?; 43640fe15e0SLoGin let phoff: usize = ehdr.e_phoff.try_into()?; 43740fe15e0SLoGin let size = entsize 43840fe15e0SLoGin .checked_mul(phnum) 43940fe15e0SLoGin .ok_or(elf::ParseError::IntegerOverflow)?; 44040fe15e0SLoGin phoff 44140fe15e0SLoGin .checked_add(size) 44240fe15e0SLoGin .ok_or(elf::ParseError::IntegerOverflow)?; 44340fe15e0SLoGin 44440fe15e0SLoGin // 读取program header table 44540fe15e0SLoGin 44640fe15e0SLoGin file.lseek(SeekFrom::SeekSet(phoff as i64)) 44740fe15e0SLoGin .map_err(|_| elf::ParseError::BadOffset(phoff as u64))?; 44840fe15e0SLoGin data_buf.clear(); 44940fe15e0SLoGin data_buf.resize(size, 0); 45040fe15e0SLoGin 45140fe15e0SLoGin file.read(size, data_buf) 45240fe15e0SLoGin .expect("read program header table failed"); 45340fe15e0SLoGin let buf = data_buf.get_bytes(0..size)?; 45440fe15e0SLoGin 45540fe15e0SLoGin return Ok(Some(elf::segment::SegmentTable::new( 45640fe15e0SLoGin ehdr.endianness, 45740fe15e0SLoGin ehdr.class, 45840fe15e0SLoGin buf, 45940fe15e0SLoGin ))); 46040fe15e0SLoGin } 46140fe15e0SLoGin } 46240fe15e0SLoGin 46340fe15e0SLoGin impl BinaryLoader for ElfLoader { 46440fe15e0SLoGin fn probe(self: &'static Self, param: &ExecParam, buf: &[u8]) -> Result<(), ExecError> { 46540fe15e0SLoGin // let elf_bytes = 46640fe15e0SLoGin // ElfBytes::<AnyEndian>::minimal_parse(buf).map_err(|_| ExecError::NotExecutable)?; 46740fe15e0SLoGin 46840fe15e0SLoGin let ehdr = Self::parse_ehdr(buf).map_err(|_| ExecError::NotExecutable)?; 46940fe15e0SLoGin 47040fe15e0SLoGin #[cfg(target_arch = "x86_64")] 47140fe15e0SLoGin return self.probe_x86_64(param, &ehdr); 47240fe15e0SLoGin 47340fe15e0SLoGin #[cfg(not(target_arch = "x86_64"))] 47440fe15e0SLoGin unimplemented!("Unsupported architecture"); 47540fe15e0SLoGin } 47640fe15e0SLoGin 47740fe15e0SLoGin fn load( 47840fe15e0SLoGin self: &'static Self, 47940fe15e0SLoGin param: &mut ExecParam, 48040fe15e0SLoGin head_buf: &[u8], 48140fe15e0SLoGin ) -> Result<BinaryLoaderResult, ExecError> { 48240fe15e0SLoGin // 解析elf文件头 48340fe15e0SLoGin let ehdr = Self::parse_ehdr(head_buf).map_err(|_| ExecError::NotExecutable)?; 48440fe15e0SLoGin 48540fe15e0SLoGin // 参考linux-5.19的load_elf_binary函数 48640fe15e0SLoGin // https://opengrok.ringotek.cn/xref/linux-5.19.10/fs/binfmt_elf.c?r=&mo=22652&fi=824#1034 48740fe15e0SLoGin 48840fe15e0SLoGin let elf_type = ElfType::from(ehdr.e_type); 48940fe15e0SLoGin // kdebug!("ehdr = {:?}", ehdr); 49040fe15e0SLoGin 49140fe15e0SLoGin let binding = param.vm().clone(); 49240fe15e0SLoGin let mut user_vm = binding.write(); 49340fe15e0SLoGin 49440fe15e0SLoGin // todo: 增加对user stack上的内存是否具有可执行权限的处理(方法:寻找phdr里面的PT_GNU_STACK段) 49540fe15e0SLoGin 49640fe15e0SLoGin // todo: 增加对动态链接的处理 49740fe15e0SLoGin 49840fe15e0SLoGin // kdebug!("to parse segments"); 49940fe15e0SLoGin // 加载ELF文件并映射到用户空间 50040fe15e0SLoGin let mut phdr_buf = Vec::new(); 50140fe15e0SLoGin let loadable_sections = Self::parse_segments(param, &ehdr, &mut phdr_buf) 50240fe15e0SLoGin .map_err(|_| ExecError::ParseError)? 50340fe15e0SLoGin .ok_or(ExecError::ParseError)? 50440fe15e0SLoGin .iter() 50540fe15e0SLoGin .filter(|seg| seg.p_type == elf::abi::PT_LOAD); 50640fe15e0SLoGin 50740fe15e0SLoGin // kdebug!("loadable_sections = {:?}", loadable_sections); 50840fe15e0SLoGin 50940fe15e0SLoGin let mut elf_brk = VirtAddr::new(0); 51040fe15e0SLoGin let mut elf_bss = VirtAddr::new(0); 51140fe15e0SLoGin let mut start_code: Option<VirtAddr> = None; 51240fe15e0SLoGin let mut end_code: Option<VirtAddr> = None; 51340fe15e0SLoGin let mut start_data: Option<VirtAddr> = None; 51440fe15e0SLoGin let mut end_data: Option<VirtAddr> = None; 51540fe15e0SLoGin 51640fe15e0SLoGin // 加载的时候的偏移量(这个偏移量在加载动态链接段的时候产生,由于还没有动态链接,因此暂时不可变。) 51740fe15e0SLoGin // 请不要删除load_bias! 以免到时候写动态链接的时候忘记了。 51840fe15e0SLoGin let load_bias = 0usize; 51940fe15e0SLoGin let mut bss_prot_flags = ProtFlags::empty(); 52040fe15e0SLoGin // 是否是第一个加载的段 52140fe15e0SLoGin let mut first_pt_load = true; 52240fe15e0SLoGin // program header的虚拟地址 52340fe15e0SLoGin let mut phdr_vaddr: Option<VirtAddr> = None; 52440fe15e0SLoGin for seg_to_load in loadable_sections { 52540fe15e0SLoGin // kdebug!("seg_to_load = {:?}", seg_to_load); 52640fe15e0SLoGin if unlikely(elf_brk > elf_bss) { 52740fe15e0SLoGin // kdebug!( 52840fe15e0SLoGin // "to set brk, elf_brk = {:?}, elf_bss = {:?}", 52940fe15e0SLoGin // elf_brk, 53040fe15e0SLoGin // elf_bss 53140fe15e0SLoGin // ); 53240fe15e0SLoGin self.set_elf_brk( 53340fe15e0SLoGin &mut user_vm, 53440fe15e0SLoGin elf_bss + load_bias, 53540fe15e0SLoGin elf_brk + load_bias, 53640fe15e0SLoGin bss_prot_flags, 53740fe15e0SLoGin )?; 53840fe15e0SLoGin let nbyte = self.elf_page_offset(elf_bss); 53940fe15e0SLoGin if nbyte > 0 { 54040fe15e0SLoGin let nbyte = min(Self::ELF_PAGE_SIZE - nbyte, elf_brk - elf_bss); 54140fe15e0SLoGin unsafe { 54240fe15e0SLoGin // This bss-zeroing can fail if the ELF file specifies odd protections. 54340fe15e0SLoGin // So we don't check the return value. 54440fe15e0SLoGin clear_user(elf_bss + load_bias, nbyte).ok(); 54540fe15e0SLoGin } 54640fe15e0SLoGin } 54740fe15e0SLoGin } 54840fe15e0SLoGin 54940fe15e0SLoGin // 生成ProtFlags. 55040fe15e0SLoGin // TODO: 当有了动态链接之后,需要根据情况设置这里的has_interpreter 55140fe15e0SLoGin let elf_prot_flags = self.make_prot(seg_to_load.p_flags, false, false); 55240fe15e0SLoGin 55340fe15e0SLoGin let mut elf_map_flags = MapFlags::MAP_PRIVATE; 55440fe15e0SLoGin 55540fe15e0SLoGin let vaddr = VirtAddr::new(seg_to_load.p_vaddr as usize); 55640fe15e0SLoGin 55740fe15e0SLoGin if !first_pt_load { 55840fe15e0SLoGin elf_map_flags.insert(MapFlags::MAP_FIXED_NOREPLACE); 55940fe15e0SLoGin } else if elf_type == ElfType::Executable { 56040fe15e0SLoGin /* 56140fe15e0SLoGin * This logic is run once for the first LOAD Program 56240fe15e0SLoGin * Header for ET_EXEC binaries. No special handling 56340fe15e0SLoGin * is needed. 56440fe15e0SLoGin */ 56540fe15e0SLoGin elf_map_flags.insert(MapFlags::MAP_FIXED_NOREPLACE); 56640fe15e0SLoGin } else if elf_type == ElfType::DSO { 56740fe15e0SLoGin // TODO: 支持动态链接 56840fe15e0SLoGin unimplemented!("DragonOS currently does not support dynamic linking!"); 56940fe15e0SLoGin } 57040fe15e0SLoGin 57140fe15e0SLoGin // 加载这个段到用户空间 57240fe15e0SLoGin // todo: 引入动态链接后,这里的total_size要按照实际的填写,而不一定是0 57340fe15e0SLoGin 57440fe15e0SLoGin let e = self 57540fe15e0SLoGin .load_elf_segment( 57640fe15e0SLoGin &mut user_vm, 57740fe15e0SLoGin param, 57840fe15e0SLoGin &seg_to_load, 57940fe15e0SLoGin vaddr + load_bias, 58040fe15e0SLoGin &elf_prot_flags, 58140fe15e0SLoGin &elf_map_flags, 58240fe15e0SLoGin 0, 58340fe15e0SLoGin ) 58440fe15e0SLoGin .map_err(|e| match e { 58540fe15e0SLoGin SystemError::EFAULT => ExecError::BadAddress(None), 58640fe15e0SLoGin SystemError::ENOMEM => ExecError::OutOfMemory, 58740fe15e0SLoGin _ => ExecError::Other(format!("load_elf_segment failed: {:?}", e)), 58840fe15e0SLoGin })?; 58940fe15e0SLoGin 59040fe15e0SLoGin // 如果地址不对,那么就报错 59140fe15e0SLoGin if !e.1 { 59240fe15e0SLoGin return Err(ExecError::BadAddress(Some(e.0))); 59340fe15e0SLoGin } 59440fe15e0SLoGin 59540fe15e0SLoGin if first_pt_load { 59640fe15e0SLoGin first_pt_load = false; 59740fe15e0SLoGin if elf_type == ElfType::DSO { 59840fe15e0SLoGin // todo: 在这里增加对load_bias和reloc_func_desc的更新代码 59940fe15e0SLoGin todo!() 60040fe15e0SLoGin } 60140fe15e0SLoGin } 60240fe15e0SLoGin 60340fe15e0SLoGin // kdebug!("seg_to_load.p_offset={}", seg_to_load.p_offset); 60440fe15e0SLoGin // kdebug!("e_phoff={}", ehdr.e_phoff); 60540fe15e0SLoGin // kdebug!("seg_to_load.p_filesz={}", seg_to_load.p_filesz); 60640fe15e0SLoGin // Figure out which segment in the file contains the Program Header Table, 60740fe15e0SLoGin // and map to the associated virtual address. 60840fe15e0SLoGin if (seg_to_load.p_offset <= ehdr.e_phoff) 60940fe15e0SLoGin && (ehdr.e_phoff < (seg_to_load.p_offset + seg_to_load.p_filesz)) 61040fe15e0SLoGin { 61140fe15e0SLoGin phdr_vaddr = Some(VirtAddr::new( 61240fe15e0SLoGin (ehdr.e_phoff - seg_to_load.p_offset + seg_to_load.p_vaddr) as usize, 61340fe15e0SLoGin )); 61440fe15e0SLoGin } 61540fe15e0SLoGin 61640fe15e0SLoGin let p_vaddr = VirtAddr::new(seg_to_load.p_vaddr as usize); 61740fe15e0SLoGin if (seg_to_load.p_flags & elf::abi::PF_X) != 0 { 61840fe15e0SLoGin if start_code.is_none() || start_code.as_ref().unwrap() > &p_vaddr { 61940fe15e0SLoGin start_code = Some(p_vaddr); 62040fe15e0SLoGin } 62140fe15e0SLoGin } 62240fe15e0SLoGin 62340fe15e0SLoGin if start_data.is_none() 62440fe15e0SLoGin || (start_data.is_some() && start_data.as_ref().unwrap() > &p_vaddr) 62540fe15e0SLoGin { 62640fe15e0SLoGin start_data = Some(p_vaddr); 62740fe15e0SLoGin } 62840fe15e0SLoGin 62940fe15e0SLoGin // 如果程序段要加载的目标地址不在用户空间内,或者是其他不合法的情况,那么就报错 63040fe15e0SLoGin if !p_vaddr.check_user() 63140fe15e0SLoGin || seg_to_load.p_filesz > seg_to_load.p_memsz 63240fe15e0SLoGin || seg_to_load.p_memsz > MMArch::USER_END_VADDR.data() as u64 63340fe15e0SLoGin { 63440fe15e0SLoGin // kdebug!("ERR: p_vaddr={p_vaddr:?}"); 63540fe15e0SLoGin return Err(ExecError::InvalidParemeter); 63640fe15e0SLoGin } 63740fe15e0SLoGin 63840fe15e0SLoGin drop(p_vaddr); 63940fe15e0SLoGin 64040fe15e0SLoGin // end vaddr of this segment(code+data+bss) 64140fe15e0SLoGin let seg_end_vaddr_f = self.elf_page_align_up(VirtAddr::new( 64240fe15e0SLoGin (seg_to_load.p_vaddr + seg_to_load.p_filesz) as usize, 64340fe15e0SLoGin )); 64440fe15e0SLoGin 64540fe15e0SLoGin if seg_end_vaddr_f > elf_bss { 64640fe15e0SLoGin elf_bss = seg_end_vaddr_f; 64740fe15e0SLoGin } 64840fe15e0SLoGin 64940fe15e0SLoGin if ((seg_to_load.p_flags & elf::abi::PF_X) != 0) 65040fe15e0SLoGin && (end_code.is_none() 65140fe15e0SLoGin || (end_code.is_some() && end_code.as_ref().unwrap() < &seg_end_vaddr_f)) 65240fe15e0SLoGin { 65340fe15e0SLoGin end_code = Some(seg_end_vaddr_f); 65440fe15e0SLoGin } 65540fe15e0SLoGin 65640fe15e0SLoGin if end_data.is_none() 65740fe15e0SLoGin || (end_data.is_some() && end_data.as_ref().unwrap() < &seg_end_vaddr_f) 65840fe15e0SLoGin { 65940fe15e0SLoGin end_data = Some(seg_end_vaddr_f); 66040fe15e0SLoGin } 66140fe15e0SLoGin 66240fe15e0SLoGin drop(seg_end_vaddr_f); 66340fe15e0SLoGin 66440fe15e0SLoGin let seg_end_vaddr = VirtAddr::new((seg_to_load.p_vaddr + seg_to_load.p_memsz) as usize); 66540fe15e0SLoGin 66640fe15e0SLoGin if seg_end_vaddr > elf_brk { 66740fe15e0SLoGin bss_prot_flags = elf_prot_flags; 66840fe15e0SLoGin elf_brk = seg_end_vaddr; 66940fe15e0SLoGin } 67040fe15e0SLoGin } 67140fe15e0SLoGin // kdebug!("elf load: phdr_vaddr={phdr_vaddr:?}"); 67240fe15e0SLoGin let program_entrypoint = VirtAddr::new(ehdr.e_entry as usize + load_bias); 67340fe15e0SLoGin let phdr_vaddr = if phdr_vaddr.is_some() { 67440fe15e0SLoGin Some(phdr_vaddr.unwrap() + load_bias) 67540fe15e0SLoGin } else { 67640fe15e0SLoGin None 67740fe15e0SLoGin }; 67840fe15e0SLoGin 67940fe15e0SLoGin elf_bss += load_bias; 68040fe15e0SLoGin elf_brk += load_bias; 68140fe15e0SLoGin start_code = start_code.map(|v| v + load_bias); 68240fe15e0SLoGin end_code = end_code.map(|v| v + load_bias); 68340fe15e0SLoGin start_data = start_data.map(|v| v + load_bias); 68440fe15e0SLoGin end_data = end_data.map(|v| v + load_bias); 68540fe15e0SLoGin 68640fe15e0SLoGin // kdebug!( 68740fe15e0SLoGin // "to set brk: elf_bss: {:?}, elf_brk: {:?}, bss_prot_flags: {:?}", 68840fe15e0SLoGin // elf_bss, 68940fe15e0SLoGin // elf_brk, 69040fe15e0SLoGin // bss_prot_flags 69140fe15e0SLoGin // ); 69240fe15e0SLoGin self.set_elf_brk(&mut user_vm, elf_bss, elf_brk, bss_prot_flags)?; 69340fe15e0SLoGin 69440fe15e0SLoGin if likely(elf_bss != elf_brk) && unlikely(self.pad_zero(elf_bss).is_err()) { 69540fe15e0SLoGin // kdebug!("elf_bss = {elf_bss:?}, elf_brk = {elf_brk:?}"); 69640fe15e0SLoGin return Err(ExecError::BadAddress(Some(elf_bss))); 69740fe15e0SLoGin } 69840fe15e0SLoGin // todo: 动态链接:增加加载interpreter的代码 69940fe15e0SLoGin // kdebug!("to create auxv"); 70040fe15e0SLoGin 70140fe15e0SLoGin self.create_auxv(param, program_entrypoint, phdr_vaddr, &ehdr)?; 70240fe15e0SLoGin 70340fe15e0SLoGin // kdebug!("auxv create ok"); 70440fe15e0SLoGin user_vm.start_code = start_code.unwrap_or(VirtAddr::new(0)); 70540fe15e0SLoGin user_vm.end_code = end_code.unwrap_or(VirtAddr::new(0)); 70640fe15e0SLoGin user_vm.start_data = start_data.unwrap_or(VirtAddr::new(0)); 70740fe15e0SLoGin user_vm.end_data = end_data.unwrap_or(VirtAddr::new(0)); 70840fe15e0SLoGin 70940fe15e0SLoGin let result = BinaryLoaderResult::new(program_entrypoint); 71040fe15e0SLoGin // kdebug!("elf load OK!!!"); 71140fe15e0SLoGin return Ok(result); 71240fe15e0SLoGin } 71340fe15e0SLoGin } 71440fe15e0SLoGin 71540fe15e0SLoGin /// Elf机器架构,对应于e_machine字段。在ABI中,以EM_开头的常量是e_machine字段的值。 71640fe15e0SLoGin #[derive(Debug, Eq, PartialEq)] 71740fe15e0SLoGin pub enum ElfMachine { 71840fe15e0SLoGin I386, 71940fe15e0SLoGin AArch32, 72040fe15e0SLoGin AArch64, 72140fe15e0SLoGin X86_64, 72240fe15e0SLoGin RiscV, 72340fe15e0SLoGin /// 龙芯架构 72440fe15e0SLoGin LoongArch, 72540fe15e0SLoGin /// 未知架构 72640fe15e0SLoGin Unknown, 72740fe15e0SLoGin } 72840fe15e0SLoGin 72940fe15e0SLoGin impl From<u16> for ElfMachine { 73040fe15e0SLoGin fn from(machine: u16) -> Self { 73140fe15e0SLoGin match machine { 73240fe15e0SLoGin 0x03 => Self::I386, 73340fe15e0SLoGin 0x28 => Self::AArch32, 73440fe15e0SLoGin 0xb7 => Self::AArch64, 73540fe15e0SLoGin 0x3e => Self::X86_64, 73640fe15e0SLoGin 0xf3 => Self::RiscV, 73740fe15e0SLoGin 0x102 => Self::LoongArch, 73840fe15e0SLoGin // 未知架构 73940fe15e0SLoGin _ => Self::Unknown, 74040fe15e0SLoGin } 74140fe15e0SLoGin } 74240fe15e0SLoGin } 74340fe15e0SLoGin 74440fe15e0SLoGin /// Elf文件类型,对应于e_type字段。在ABI中,以ET_开头的常量是e_type字段的值。 74540fe15e0SLoGin #[derive(Debug, Eq, PartialEq)] 74640fe15e0SLoGin pub enum ElfType { 74740fe15e0SLoGin /// 可重定位文件 74840fe15e0SLoGin Relocatable, 74940fe15e0SLoGin /// 可执行文件 75040fe15e0SLoGin Executable, 75140fe15e0SLoGin /// 动态链接库 75240fe15e0SLoGin DSO, 75340fe15e0SLoGin /// 核心转储文件 75440fe15e0SLoGin Core, 75540fe15e0SLoGin /// 未知类型 75640fe15e0SLoGin Unknown, 75740fe15e0SLoGin } 75840fe15e0SLoGin 75940fe15e0SLoGin impl From<u16> for ElfType { 76040fe15e0SLoGin fn from(elf_type: u16) -> Self { 76140fe15e0SLoGin match elf_type { 76240fe15e0SLoGin 0x01 => Self::Relocatable, 76340fe15e0SLoGin 0x02 => Self::Executable, 76440fe15e0SLoGin 0x03 => Self::DSO, 76540fe15e0SLoGin 0x04 => Self::Core, 76640fe15e0SLoGin _ => Self::Unknown, 76740fe15e0SLoGin } 76840fe15e0SLoGin } 76940fe15e0SLoGin } 77040fe15e0SLoGin 77140fe15e0SLoGin // Simple convenience extension trait to wrap get() with .ok_or(SliceReadError) 77240fe15e0SLoGin trait ReadBytesExt<'data> { 77340fe15e0SLoGin fn get_bytes(self, range: Range<usize>) -> Result<&'data [u8], elf::ParseError>; 77440fe15e0SLoGin } 77540fe15e0SLoGin impl<'data> ReadBytesExt<'data> for &'data [u8] { 77640fe15e0SLoGin fn get_bytes(self, range: Range<usize>) -> Result<&'data [u8], elf::ParseError> { 77740fe15e0SLoGin let start = range.start; 77840fe15e0SLoGin let end = range.end; 77940fe15e0SLoGin self.get(range) 78040fe15e0SLoGin .ok_or(elf::ParseError::SliceReadError((start, end))) 78140fe15e0SLoGin } 78240fe15e0SLoGin } 783