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 PARTIALLY DONE (#67) : program-header vectors and boot-stack argv pointers now use try_reserve_exact-based fallible collection and fail the load with an error instead of aborting. Full rollback of already-mapped segments still requires a fallible GlobalAlloc.

  • Fix TODO : find_free_vma_range : fallback hardcoded 0x1000_0000 DONE : the fallback was removed; a failed search now fails the load with โ€œNo virtual range for ET_DYN imageโ€.

Security:

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

Macrosยง

elf_trace ๐Ÿ”’

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 ๐Ÿ”’
MAX_INTERP_PATH_LEN ๐Ÿ”’
Maximum PT_INTERP path length we accept.
PF_R ๐Ÿ”’
PF_W ๐Ÿ”’
PF_X ๐Ÿ”’
PT_DYNAMIC ๐Ÿ”’
PT_GNU_RELRO ๐Ÿ”’
PT_GNU_STACK ๐Ÿ”’
PT_INTERP ๐Ÿ”’
PT_LOAD ๐Ÿ”’
PT_TLS ๐Ÿ”’
R_X86_64_64 ๐Ÿ”’
R_X86_64_COPY ๐Ÿ”’
R_X86_64_DTPMOD64 ๐Ÿ”’
R_X86_64_DTPOFF64 ๐Ÿ”’
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_RFLAGS ๐Ÿ”’
Standard user-mode RFLAGS: IF=1, reserved bit 1 set.
USER_STACK_MAX_PAGES
Upper bound on a per-process user stack (2048 pages = 8 MiB), to keep a buggy or hostile caller from exhausting the user address space / frames.
USER_STACK_MIN_PAGES
Lower bound on a per-process user stack (4 pages = 16 KiB): the boot stack layout alone (argv/envp/auxv + guard margins) needs at least one page, and tiny stacks would fault immediately.
USER_STACK_PAGES
Number of 4 KiB pages for the user stack (16 pages = 64 KiB).

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_and_run_elf_with_stack
Load an ELF64 binary with an explicit per-process user stack size.
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.
pie_base ๐Ÿ”’
Get the randomized PIE base address for ELF loading.
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.
try_collect_exact ๐Ÿ”’
Collects an exact-size-hint iterator into a Vec, failing cleanly on allocation failure instead of aborting (issue #67).
user_stack_base ๐Ÿ”’
Get the randomized user stack base address.
user_stack_guard ๐Ÿ”’
Get the guard page address below the user stack.
user_stack_top ๐Ÿ”’
Get the randomized user stack top address.
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.