JFLib

plat/linux_elfload.rs (Linux)#

Source: lib/plat/linux_elfload.rs

linux_elfload -- plugin libraries on Linux: a small ELF loader without libc
                                       [src/plat/linux/elfload.s; docs/modules/PLUGIN_LOADER.md]

  lib_open(path) -> Lib / Err(LIBE_*)
  lib_sym(h, name) -> address | 0       .dynsym lookup (count from DT_HASH nchain or the
                                        DT_GNU_HASH chains)
  (crate) lib_unload(h)                 munmap (lib_close / drop)

Accepted: ELFCLASS64, little endian, ET_DYN, EM_X86_64 (ARM64: EM_AARCH64), program headers of 56 bytes
(1..64), at most LH_SEGMAX PT_LOAD segments in ascending, page-disjoint order, never
writable AND executable, one PT_DYNAMIC; relocations R_X86_64_NONE / RELATIVE and
R_X86_64_64 / GLOB_DAT / JUMP_SLOT against symbols the object defines itself (RELA only; ARM64 at the end).
Rejected: DT_NEEDED or undefined symbols (LIBE_DEPS), PT_INTERP / PT_TLS / DT_INIT /
DT_INIT_ARRAY / DT_PREINIT_ARRAY (LIBE_INIT: nothing of the object runs while loading),
DT_REL and other relocation types (LIBE_RELOC), everything malformed (LIBE_FORMAT).
The file is untrusted input: every header value, offset, size and address is checked in
64-bit arithmetic against the file size or the mapped span (LIB_SPAN_MAX) before use.
Mapping: one anonymous read-write reservation of the whole span, the segments copied in,
relocations applied, then mprotect per segment (gaps PROT_NONE, PT_GNU_RELRO read-only).
While loading, the mapping is accessed as a slice (every access bounds-checked on top of
the assembly's checks); lib_sym reads the protected image like the assembly.
  • fn lib_open(path: &str) -> Result<Lib, u32> open a plugin ELF object (see the file header) [plat_lib_open]

  • fn lib_sym(h: &Lib, name: &str) -> usize the address of the defined dynamic symbol name, or 0 [plat_lib_sym]

Source: docs/API.md in the JFLib repository.