native

package
v3.0.0-alpha.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 5, 2026 License: BSD-2-Clause Imports: 14 Imported by: 0

Documentation

Overview

Package native tells native files - ELF and Mach-O binaries - by their content, and reports what they need to run: the platforms they run on and, for an ELF program, the loader and the shared libraries it asks for.

tt package pack labels an archive with the platform its native files pin it to, and tt package build checks that what a component built into .rocks/bin/ is a program of the platform it built for, static when the manifest asks for that. Both read a file the same way, so the detector lives here rather than in either of them.

Index

Constants

This section is empty.

Variables

View Source
var ErrNotNative = errors.New("not an ELF or Mach-O file")

ErrNotNative is wrapped by InspectProgram's error for a file that is neither ELF nor Mach-O.

Functions

func Describe

func Describe(platforms []Platform) string

Describe renders the platforms of a native file for a message: "linux-arm64", or "darwin-amd64 and darwin-arm64" for a universal one.

Types

type File

type File struct {
	// Name is the file's path relative to the tree, slash-separated.
	Name string
	// Platforms lists the platforms the file runs on: one for a thin binary,
	// one per slice for a universal Mach-O.
	Platforms []Platform
}

File is a native file of a tree: an ELF or Mach-O file, whatever its name.

func Files

func Files(root string) ([]File, error)

Files lists every native file under root, in lexical order, with the platforms each runs on. It does not judge them: the caller compares them with the platform it packs for.

The whole tree is looked at. For an archive that is the [package].include payload, .rocks/ and _runtime/ alike, and the bundled runtime is no exception: the runtime cache has no platform in its layout, so a build of another platform placed there would otherwise travel under the host's label.

func (File) RunsOn

func (f File) RunsOn(target Platform) bool

RunsOn reports whether the file runs on target. A universal Mach-O runs wherever one of its slices does.

type Platform

type Platform struct {
	OS   string
	Arch string
}

Platform is an operating system and CPU architecture pair, spelled the way Go spells GOOS and GOARCH. For the four supported pairs its String form is the archive's platform token: linux-amd64, linux-arm64, darwin-amd64, darwin-arm64.

func FilePlatforms

func FilePlatforms(path string) ([]Platform, error)

FilePlatforms reports the platforms the file at path runs on, reading only its header. A file that is neither ELF nor Mach-O - Lua, a shebang script, text - is not native and yields nil, and so does one that only starts like a binary and is too short or malformed to be one.

func Host

func Host() Platform

Host returns the platform of the machine tt runs on, the only one tt builds and packs for until it learns to build for another.

func (Platform) String

func (p Platform) String() string

String renders the pair as a token, "<os>-<arch>".

type Program

type Program struct {
	// Platforms lists the platforms the program runs on, as FilePlatforms
	// reports them.
	Platforms []Platform
	// Executable reports that a process can be started from the file: an ELF
	// executable or position-independent executable, or a Mach-O executable
	// in every slice. A shared library, an object file or a core dump is
	// native, but not a program.
	Executable bool
	// Interpreter is the dynamic loader an ELF program names (PT_INTERP);
	// empty when it names none.
	Interpreter string
	// Needed lists the shared libraries the program asks the loader for
	// (DT_NEEDED of an ELF program, the dylibs of a Mach-O one).
	Needed []string
}

Program describes a native program: the platforms it runs on, whether a process can be started from it, and, for an ELF one, what the system has to supply when it is loaded.

Unlike Files and FilePlatforms, which only read the header, InspectProgram reads the program headers and the dynamic segment too. That is fine for the few files a build declares its output, not for every file of an archive.

func InspectProgram

func InspectProgram(path string) (Program, error)

InspectProgram reads what the native file at path is as a program. A file that is neither ELF nor Mach-O is an error wrapping ErrNotNative.

func (Program) Static

func (p Program) Static() bool

Static reports whether the program needs no loader or shared libraries beyond the base libraries of every platform it runs on.

A static-pie program, which Rust builds for a musl target, has a dynamic segment of its own - it relocates itself - so the dynamic segment is not what tells a dynamic program; the loader and the libraries are.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL