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