Skip to main content

Module elf

Module elf 

Source
Expand description

ELF64 loader for Strat9-OS.

Parses ELF64 headers and loads PT_LOAD segments into a user address space, then creates a kernel task that trampolines into Ring 3 via IRETQ.

Supports :

  • ET_EXEC
  • ET_DYN (PIE/static-PIE)
  • ELF64 little-endian x86_64 binaries.

Does not support (or need future fix) :

  • FIx TODO : allocation heap during ELF loading program_headers(elf_data, &header).collect() β†’ Vec, Vec::new() for interp_phdrs, etc. The kernel uses alloc so it’s normal, but allocation errors are not handled (no try_collect, no fallible GlobalAlloc).

  • Fix TODO : find_free_vma_range : fallback hardcoded 0x1000_0000 If PIE_BASE_ADDR (0x1_0000_0000) fails, fallback to 0x1000_0000. This value is arbitrary and could overlap existing mappings if many libraries are loaded.

Security:

  • User stack has a guard page (USER_STACK_BASE - 4096) that is intentionally left unmapped. Stack underflows hit it and page-fault.

StructsΒ§

Elf64Dyn πŸ”’
Elf64Header πŸ”’
Parsed ELF64 file header (copy-friendly, no borrows).
Elf64Phdr πŸ”’
Parsed ELF64 program header (copy-friendly, packed for raw byte reading).
Elf64Rela πŸ”’
Elf64Sym πŸ”’
LoadedElfInfo
Result of loading an ELF image into an address space.

ConstantsΒ§

AT_BASE πŸ”’
AT_ENTRY πŸ”’
AT_PAGESZ πŸ”’
AT_PHDR πŸ”’
AT_PHENT πŸ”’
AT_PHNUM πŸ”’
AT_RANDOM πŸ”’
DT_JMPREL πŸ”’
DT_NULL πŸ”’
DT_PLTREL πŸ”’
DT_PLTRELSZ πŸ”’
DT_RELA πŸ”’
DT_RELACOUNT πŸ”’
DT_RELAENT πŸ”’
DT_RELASZ πŸ”’
DT_RELR πŸ”’
DT_RELRENT πŸ”’
DT_RELRSZ πŸ”’
DT_STRTAB πŸ”’
DT_SYMENT πŸ”’
DT_SYMTAB πŸ”’
ET_DYN πŸ”’
ET_EXEC πŸ”’
PF_R πŸ”’
PF_W πŸ”’
PF_X πŸ”’
PIE_BASE_ADDR πŸ”’
Preferred base when placing ET_DYN (PIE) images.
PT_DYNAMIC πŸ”’
PT_INTERP πŸ”’
PT_LOAD πŸ”’
PT_TLS πŸ”’
R_X86_64_64 πŸ”’
R_X86_64_COPY πŸ”’
R_X86_64_GLOB_DAT πŸ”’
R_X86_64_IRELATIVE πŸ”’
R_X86_64_JUMP_SLOT πŸ”’
R_X86_64_RELATIVE πŸ”’
R_X86_64_TPOFF64 πŸ”’
USER_ADDR_MAX
Maximum virtual address we accept for user-space mappings.
USER_STACK_BASE
User stack location (below the non-canonical gap).
USER_STACK_GUARD
Guard page below the user stack : unmapped, catches stack underflows.
USER_STACK_PAGES
Number of 4 KiB pages for the user stack (16 pages = 64 KiB).
USER_STACK_TOP
Top of the user stack (stack grows down).

FunctionsΒ§

apply_dynamic_relocations πŸ”’
Performs the apply dynamic relocations operation.
apply_relr_relocations πŸ”’
Performs the apply relr relocations operation.
apply_segment_permissions πŸ”’
Performs the apply segment permissions operation.
call_ifunc_resolver πŸ”’
Calls a user-space IFUNC resolver function and returns its result.
compute_load_bias_and_entry πŸ”’
Compute load bias and relocated entry for ET_EXEC / ET_DYN.
compute_load_bounds πŸ”’
Compute total mapped bounds for all PT_LOAD segments.
elf_flags_to_vma πŸ”’
Convert ELF p_flags to VmaFlags.
elf_ring3_trampoline πŸ”’
Parameters for the Ring 3 trampoline, stored in a static so the Trampoline that switches to user address space and does IRETQ to Ring 3.
find_relocated_phdr_vaddr πŸ”’
Performs the find relocated phdr vaddr operation.
generate_aux_random_seed πŸ”’
load_and_run_elf
Load an ELF64 binary and schedule it as a Ring 3 user task.
load_and_run_elf_with_args
Load an ELF64 binary with command-line arguments and schedule it as a Ring 3 task.
load_and_run_elf_with_caps
Performs the load and run elf with caps operation.
load_elf_image
Load an ELF binary into the provided address space. Returns the entry point address.
load_elf_task_inner πŸ”’
Internal ELF task builder used by all public loading APIs. extra_args are written to the user stack as argv[1..] after the program name.
load_elf_task_with_caps
Thin public wrapper that keeps the existing API stable.
load_segment πŸ”’
Load a single PT_LOAD segment into the given address space.
parse_header πŸ”’
Parse and validate the ELF64 file header from raw bytes.
parse_interp_path πŸ”’
Parses interp path.
program_headers πŸ”’
Iterate over program headers in the ELF.
push_auxv πŸ”’
Performs the push auxv operation.
read_elf_from_vfs πŸ”’
Reads elf from vfs.
read_user_mapped_bytes πŸ”’
Reads user mapped bytes.
read_user_mapped_bytes_pub
Reads user mapped bytes pub.
read_user_u64 πŸ”’
Reads user u64.
setup_boot_user_stack πŸ”’
Performs the setup boot user stack operation. Sets up the initial user-space stack for a freshly loaded ELF task.
write_user_mapped_bytes πŸ”’
Writes user mapped bytes.
write_user_mapped_bytes_pub
Writes user mapped bytes pub.
write_user_u64 πŸ”’
Writes user u64.
write_user_u64_pub
Writes user u64 pub.