linker

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2025 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package linker provides a general-purpose in-memory linker, that is used to assemble the output buffer within the compiler.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func PushTable

func PushTable[K swiss.Key, V comparable](s *Sym, entries ...swiss.Entry[K, V])

PushTable pushes a swiss.Table onto a symbol.

func Symbols

func Symbols[Name any](l *Linker) iter.Seq2[Name, int]

Symbols returns an iterator over all symbols in l with the given name type.

Returns the names of the symbols and, if this is called after Linker.Link, their final offsets.

Types

type Kind

type Kind byte

Kind is a relocation kind.

const (
	Unknown Kind = iota
	Address      // Target-specific pointer in the final output buffer.
	Abs32        // Absolute (relative to the program base) 32-bit offset.
)

type Linker

type Linker struct {
	// contains filtered or unexported fields
}

Linker implements a primitive linker, for writing symbols to an output buffer and resolving relocations at the very end.

A zero value is ready to use.

func (l *Linker) Link(alloc func(size, align int) []byte) ([]byte, error)

Link executes the final link and returns the result.

alloc is used to obtain a suitable buffer for the size of the linked program.

func (*Linker) NewSymbol

func (l *Linker) NewSymbol(name any) *Sym

NewSymbol allocates a new symbol. Symbols are laid out in the order in which they are added to the linker.

type Rel

type Rel struct {
	Symbol any     // The name of the symbol this relocation references.
	Offset uintptr // Offset of the relocation within [Sym.data].

	Kind Kind
}

Rel is a relocation within a [Symbol].

type Sym

type Sym struct {
	// contains filtered or unexported fields
}

Sym is all of the metadata associated with a symbol in Linker.

func (*Sym) At

func (s *Sym) At(i, j int) []byte

At returns a mutable reference to the data in the given range.

Pushing more data to this symbol may cause this to become invalidated.

func (*Sym) Push

func (s *Sym) Push(v any) int

Push appends a value to this symbol, ensuring that it is correctly-aligned.

Returns the offset of the pushed data.

func (*Sym) PushBytes

func (s *Sym) PushBytes(align int, data []byte) int

Push appends raw bytes to this symbol, ensuring that it is correctly-aligned.

Returns the offset of the pushed data.

func (*Sym) Rel

func (s *Sym) Rel(rels ...Rel)

Rel appends relocations to this symbol.

Relocations are all relative to the end of the data written to this symbol so far.

func (*Sym) Reserve

func (s *Sym) Reserve(size, align int) []byte

Reserve reserves a region of the given layout in this symbol and returns it.

Jump to

Keyboard shortcuts

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