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