func (insns Instructions) Marshal(w io.Writer, bo binary.ByteOrder) error {
	for i, ins := range insns {
		_, err := ins.Marshal(w, bo)
		if err != nil {
			return fmt.Errorf("instruction %d: %w", i, err)
		}
	}
	return nil
}
Marshal encodes a BPF program into the kernel format.
package asm
import (
	"crypto/sha1"
	"encoding/binary"
	"encoding/hex"
	"errors"
	"fmt"
	"io"
	"math"
	"strings"
	"github.com/cilium/ebpf/internal/unix"
)
const InstructionSize = 8
type RawInstructionOffset uint64
func (rio RawInstructionOffset) Bytes() uint64 {
	return uint64(rio) * InstructionSize
}
type Instruction struct {
	OpCode    OpCode
	Dst       Register
	Src       Register
	Offset    int16
	Constant  int64
	Reference string
	Symbol    string
}
func (ins Instruction) Sym(name string) Instruction {
	ins.Symbol = name
	return ins
}
func (ins *Instruction) Unmarshal(r io.Reader, bo binary.ByteOrder) (uint64, error) {
	var bi bpfInstruction
	err := binary.Read(r, bo, &bi)
	if err != nil {
		return 0, err
	}
	ins.OpCode = bi.OpCode
	ins.Offset = bi.Offset
	ins.Constant = int64(bi.Constant)
	ins.Dst, ins.Src, err = bi.Registers.Unmarshal(bo)
	if err != nil {
		return 0, fmt.Errorf("can't unmarshal registers: %s", err)
	}
	if !bi.OpCode.IsDWordLoad() {
		return InstructionSize, nil
	}
	var bi2 bpfInstruction
	if err := binary.Read(r, bo, &bi2); err != nil {
		return 0, errors.New("64bit immediate is missing second half")
	}
	if bi2.OpCode != 0 || bi2.Offset != 0 || bi2.Registers != 0 {
		return 0, errors.New("64bit immediate has non-zero fields")
	}
	ins.Constant = int64(uint64(uint32(bi2.Constant))<<32 | uint64(uint32(bi.Constant)))
	return 2 * InstructionSize, nil
}
func (ins Instruction) Marshal(w io.Writer, bo binary.ByteOrder) (uint64, error) {
	if ins.OpCode == InvalidOpCode {
		return 0, errors.New("invalid opcode")
	}
	isDWordLoad := ins.OpCode.IsDWordLoad()
	cons := int32(ins.Constant)
	if isDWordLoad {
		cons = int32(uint32(ins.Constant))
	}
	regs, err := newBPFRegisters(ins.Dst, ins.Src, bo)
	if err != nil {
		return 0, fmt.Errorf("can't marshal registers: %s", err)
	}
	bpfi := bpfInstruction{
		ins.OpCode,
		regs,
		ins.Offset,
		cons,
	}
	if err := binary.Write(w, bo, &bpfi); err != nil {
		return 0, err
	}
	if !isDWordLoad {
		return InstructionSize, nil
	}
	bpfi = bpfInstruction{
		Constant: int32(ins.Constant >> 32),
	}
	if err := binary.Write(w, bo, &bpfi); err != nil {
		return 0, err
	}
	return 2 * InstructionSize, nil
}
func (ins *Instruction) RewriteMapPtr(fd int) error {
	if !ins.OpCode.IsDWordLoad() {
		return fmt.Errorf("%s is not a 64 bit load", ins.OpCode)
	}
	if ins.Src != PseudoMapFD && ins.Src != PseudoMapValue {
		return errors.New("not a load from a map")
	}
	offset := uint64(ins.Constant) & (math.MaxUint32 << 32)
	rawFd := uint64(uint32(fd))
	ins.Constant = int64(offset | rawFd)
	return nil
}
func (ins *Instruction) MapPtr() int {
	return int(int32(uint64(ins.Constant) & math.MaxUint32))
}
func (ins *Instruction) RewriteMapOffset(offset uint32) error {
	if !ins.OpCode.IsDWordLoad() {
		return fmt.Errorf("%s is not a 64 bit load", ins.OpCode)
	}
	if ins.Src != PseudoMapValue {
		return errors.New("not a direct load from a map")
	}
	fd := uint64(ins.Constant) & math.MaxUint32
	ins.Constant = int64(uint64(offset)<<32 | fd)
	return nil
}
func (ins *Instruction) mapOffset() uint32 {
	return uint32(uint64(ins.Constant) >> 32)
}
func (ins *Instruction) IsLoadFromMap() bool {
	return ins.OpCode == LoadImmOp(DWord) && (ins.Src == PseudoMapFD || ins.Src == PseudoMapValue)
}
func (ins *Instruction) IsFunctionCall() bool {
	return ins.OpCode.JumpOp() == Call && ins.Src == PseudoCall
}
func (ins *Instruction) IsConstantLoad(size Size) bool {
	return ins.OpCode == LoadImmOp(size) && ins.Src == R0 && ins.Offset == 0
}
func (ins Instruction) Format(f fmt.State, c rune) {
	if c != 'v' {
		fmt.Fprintf(f, "{UNRECOGNIZED: %c}", c)
		return
	}
	op := ins.OpCode
	if op == InvalidOpCode {
		fmt.Fprint(f, "INVALID")
		return
	}
	if op.JumpOp() == Exit {
		fmt.Fprint(f, op)
		return
	}
	if ins.IsLoadFromMap() {
		fd := ins.MapPtr()
		switch ins.Src {
		case PseudoMapFD:
			fmt.Fprintf(f, "LoadMapPtr dst: %s fd: %d", ins.Dst, fd)
		case PseudoMapValue:
			fmt.Fprintf(f, "LoadMapValue dst: %s, fd: %d off: %d", ins.Dst, fd, ins.mapOffset())
		}
		goto ref
	}
	fmt.Fprintf(f, "%v ", op)
	switch cls := op.Class(); cls {
	case LdClass, LdXClass, StClass, StXClass:
		switch op.Mode() {
		case ImmMode:
			fmt.Fprintf(f, "dst: %s imm: %d", ins.Dst, ins.Constant)
		case AbsMode:
			fmt.Fprintf(f, "imm: %d", ins.Constant)
		case IndMode:
			fmt.Fprintf(f, "dst: %s src: %s imm: %d", ins.Dst, ins.Src, ins.Constant)
		case MemMode:
			fmt.Fprintf(f, "dst: %s src: %s off: %d imm: %d", ins.Dst, ins.Src, ins.Offset, ins.Constant)
		case XAddMode:
			fmt.Fprintf(f, "dst: %s src: %s", ins.Dst, ins.Src)
		}
	case ALU64Class, ALUClass:
		fmt.Fprintf(f, "dst: %s ", ins.Dst)
		if op.ALUOp() == Swap || op.Source() == ImmSource {
			fmt.Fprintf(f, "imm: %d", ins.Constant)
		} else {
			fmt.Fprintf(f, "src: %s", ins.Src)
		}
	case JumpClass:
		switch jop := op.JumpOp(); jop {
		case Call:
			if ins.Src == PseudoCall {
				fmt.Fprint(f, ins.Constant)
			} else {
				fmt.Fprint(f, BuiltinFunc(ins.Constant))
			}
		default:
			fmt.Fprintf(f, "dst: %s off: %d ", ins.Dst, ins.Offset)
			if op.Source() == ImmSource {
				fmt.Fprintf(f, "imm: %d", ins.Constant)
			} else {
				fmt.Fprintf(f, "src: %s", ins.Src)
			}
		}
	}
ref:
	if ins.Reference != "" {
		fmt.Fprintf(f, " <%s>", ins.Reference)
	}
}
type Instructions []Instruction
func (insns Instructions) String() string {
	return fmt.Sprint(insns)
}
func (insns Instructions) RewriteMapPtr(symbol string, fd int) error {
	if symbol == "" {
		return errors.New("empty symbol")
	}
	found := false
	for i := range insns {
		ins := &insns[i]
		if ins.Reference != symbol {
			continue
		}
		if err := ins.RewriteMapPtr(fd); err != nil {
			return err
		}
		found = true
	}
	if !found {
		return &unreferencedSymbolError{symbol}
	}
	return nil
}
func (insns Instructions) SymbolOffsets() (map[string]int, error) {
	offsets := make(map[string]int)
	for i, ins := range insns {
		if ins.Symbol == "" {
			continue
		}
		if _, ok := offsets[ins.Symbol]; ok {
			return nil, fmt.Errorf("duplicate symbol %s", ins.Symbol)
		}
		offsets[ins.Symbol] = i
	}
	return offsets, nil
}
func (insns Instructions) ReferenceOffsets() map[string][]int {
	offsets := make(map[string][]int)
	for i, ins := range insns {
		if ins.Reference == "" {
			continue
		}
		offsets[ins.Reference] = append(offsets[ins.Reference], i)
	}
	return offsets
}
func (insns Instructions) Format(f fmt.State, c rune) {
	if c != 's' && c != 'v' {
		fmt.Fprintf(f, "{UNKNOWN FORMAT '%c'}", c)
		return
	}
	padding, ok := f.Precision()
	if !ok {
		padding = 1
	}
	indent := strings.Repeat("\t", padding)
	if f.Flag(' ') {
		indent = strings.Repeat(" ", padding)
	}
	symPadding, ok := f.Width()
	if !ok {
		symPadding = padding - 1
	}
	if symPadding < 0 {
		symPadding = 0
	}
	symIndent := strings.Repeat("\t", symPadding)
	if f.Flag(' ') {
		symIndent = strings.Repeat(" ", symPadding)
	}
	highestOffset := len(insns) * 2
	offsetWidth := int(math.Ceil(math.Log10(float64(highestOffset))))
	iter := insns.Iterate()
	for iter.Next() {
		if iter.Ins.Symbol != "" {
			fmt.Fprintf(f, "%s%s:\n", symIndent, iter.Ins.Symbol)
		}
		fmt.Fprintf(f, "%s%*d: %v\n", indent, offsetWidth, iter.Offset, iter.Ins)
	}
}