A bounded reader for the parts of an ELF file that decide whether a
target build runs on another release of the same Nerves system: the
file's class, machine and type, its DT_NEEDED libraries and DT_SONAME,
the symbol versions it needs from each library (.gnu.version_r) and the
ones it defines (.gnu.version_d). A precompiled NIF built against one
release can be installed on another when every library it needs is there
and defines every version it asks for.
The files are untrusted (built package code), so the reader is bounded
and never raises. read/1 checks every slice against the binary's size,
refuses a section header table or section that runs past the end of the
file, a string table over 1 MiB, a string with no NUL within 4096 bytes,
more than 256 DT_NEEDED entries and more than 4096 version entries
(needs and definitions, aux entries included), and follows the version
chains no further than their counts. Anything it cannot read is
{:error, :bounds}. Only little-endian files are read.
A dynamic object (ET_DYN, ET_EXEC with a PT_DYNAMIC program header)
whose section headers are missing or name no dynamic section is
{:error, :no_dynamic}: its needs cannot be read, so it cannot be judged.
A static executable reads with no needs.
satisfied?/2 is membership: the dynamic linker looks a version up by
name and a library keeps every old version node, so a needed version is
met only by a definition of that exact name. Names parse_version/1 does
not read (GLIBC_PRIVATE, V1) are skipped.
Summary
Functions
Splits a symbol version such as GLIBC_2.17 into its prefix and numbers,
{"GLIBC", [2, 17]}. A name without a dotted number suffix is nil.
Whether a file of this type can be loaded as built: :dyn and :exec.
Reads an ELF image. Never raises.
Reads the ELF file at path, refusing one over 64 MiB.
:ok when every needed version parse_version/1 reads is among the
defined ones, else the first that is not.
The major.minor series of a major.minor.patch version, else nil.
Types
@type error() :: :bounds | :big_endian | :truncated | :bad_class | :no_dynamic
@type info() :: %{ class: 32 | 64, machine: :aarch64 | :arm | :x86_64 | :riscv | :x86 | non_neg_integer(), type: :rel | :exec | :dyn | :core | {:other, non_neg_integer()}, soname: String.t() | nil, needed: [String.t()], needs: %{required(String.t()) => [String.t()]}, defines: [String.t()] }
Functions
@spec parse_version(String.t()) :: {String.t(), [non_neg_integer()]} | nil
Splits a symbol version such as GLIBC_2.17 into its prefix and numbers,
{"GLIBC", [2, 17]}. A name without a dotted number suffix is nil.
Whether a file of this type can be loaded as built: :dyn and :exec.
Reads an ELF image. Never raises.
@spec read_file(Path.t()) :: {:ok, info()} | :not_elf | {:error, error() | :too_large | File.posix()}
Reads the ELF file at path, refusing one over 64 MiB.
:ok when every needed version parse_version/1 reads is among the
defined ones, else the first that is not.
The major.minor series of a major.minor.patch version, else nil.