Precompiled.Elf (Precompiled v0.7.3)

Copy Markdown View Source

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

error()

@type error() :: :bounds | :big_endian | :truncated | :bad_class | :no_dynamic

info()

@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

parse_version(name)

@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.

portable_type?(type)

@spec portable_type?(atom()) :: boolean()

Whether a file of this type can be loaded as built: :dyn and :exec.

read(bin)

@spec read(binary()) :: {:ok, info()} | :not_elf | {:error, error()}

Reads an ELF image. Never raises.

read_file(path)

@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.

satisfied?(needed, defined)

@spec satisfied?([String.t()], [String.t()]) :: :ok | {:missing, String.t()}

:ok when every needed version parse_version/1 reads is among the defined ones, else the first that is not.

series_from_version(version)

@spec series_from_version(String.t()) :: String.t() | nil

The major.minor series of a major.minor.patch version, else nil.