goloader

package module
v0.0.25 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2026 License: Apache-2.0 Imports: 3 Imported by: 6 modules, 8 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Goloader

Build Status

Goloader can load and run Golang code at runtime.

Forked from https://github.com/dearplain/goloader, Take over maintenance because the original author is not in maintenance

How does it work?

Goloader works like a linker: it relocates the address of symbols in an object file, generates runnable code, and then reuses the runtime function and the type pointer of the loader.

Goloader provides some information to the runtime and gc of Go, which allows it to work correctly with them.

Please note that Goloader is not a scripting engine. It reads the output of Go compiler and makes them runnable. All features of Go are supported, and run just as fast and lightweight as native Go code.

Comparison with plugin

Goloader reuses the Go runtime, which makes it much smaller. And code loaded by Goloader is unloadable.

Goloader supports pprof tool(Yes, you can see code loaded by Goloader in pprof).

Build

Make sure you're using go >= 1.8.

First, execute the following command, then do build and test. This is because Goloader relies on the internal package, which is forbidden by the Go compiler.

  cp -r $GOROOT/src/cmd/internal $GOROOT/src/cmd/objfile

Examples

Build Loader:
  go build github.com/pkujhd/goloader/examples/loader
Compile Object File and Run:

If use go module or go version >= 1.20

  export GO111MODULE=on
  go list -export -deps -f '{{if .Export}}packagefile {{.ImportPath}}={{.Export}}{{end}}' $GOPATH/src/github.com/pkujhd/goloader/examples/schedule/schedule.go > importcfg
  go tool compile -importcfg importcfg $GOPATH/src/github.com/pkujhd/goloader/examples/schedule/schedule.go
  ./loader -o schedule.o -run main.main -times 10

If use go path and go version < 1.20

  export GO111MODULE=auto
  go tool compile $GOPATH/src/github.com/pkujhd/goloader/examples/schedule/schedule.go
  ./loader -o schedule.o -run main.main -times 10
  
  go install github.com/pkujhd/goloader/examples/basecontext
  go tool compile -I $GOPATH/pkg/`go env GOOS`_`go env GOARCH`/ $GOPATH/src/github.com/pkujhd/goloader/examples/inter/inter.go
  ./loader -o $GOPATH/pkg/`go env GOOS`_`go env GOARCH`/github.com/pkujhd/goloader/examples/basecontext.a:github.com/pkujhd/goloader/examples/basecontext -o inter.o

compile with goloaderbuilder

compile only package archive
./builder -f $GOPATH/src/github.com/pkujhd/goloader/examples/inter/inter.go -p inter -b
./loader -o ./target/github.com/pkujhd/goloader/examples/inter/inter.a -run inter.main -times 10
compile with dependence packages
./builder -e ../runner/runner -f $GOPATH/src/github.com/pkujhd/goloader/examples/inter/inter.go -p inter
../runner/runner -f target/inter.goloader -r inter.main

Warning

Don't use "-s -w" compile argument, It strips symbol table.

Don't use "go run" and "go test" command, "-s -w" compile argument is default.

This has currently only been tested and developed on:

Golang 1.8-1.27 (x64/x86, darwin, linux, windows)

Golang 1.10-1.27 (arm, linux, android)

Golang 1.8-1.27 (arm64, linux, android)

Golang 1.16-1.27 (arm64, darwin)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Mmap

func Mmap(size int) ([]byte, error)

func Munmap

func Munmap(b []byte) (err error)

func Parse added in v0.0.12

func Parse(file, pkgPath string) ([]string, error)

func ReadDependPackages added in v0.0.22

func ReadDependPackages(linker *Linker, files, pkgPaths []string, symbolNames []string, symPtr map[string]uintptr) error

func RegSymbol

func RegSymbol(symPtr map[string]uintptr) error

func RegSymbolWithPath added in v0.0.19

func RegSymbolWithPath(symPtr map[string]uintptr, path string) error

func RegTypes

func RegTypes(symPtr map[string]uintptr, interfaces ...interface{})

func Serialize added in v0.0.19

func Serialize(linker *Linker, writer io.Writer) error

func UnresolvedSymbols added in v0.0.19

func UnresolvedSymbols(linker *Linker, symPtr map[string]uintptr) []string

Types

type CodeModule

type CodeModule link.CodeModule

func Load

func Load(linker *Linker, symPtr map[string]uintptr) (codeModule *CodeModule, err error)

func (*CodeModule) Unload

func (codeModule *CodeModule) Unload()

type Linker added in v0.0.12

type Linker link.Linker

func ReadObj

func ReadObj(file, pkgPath string) (*Linker, error)

func ReadObjs

func ReadObjs(files []string, pkgPaths []string) (*Linker, error)

func UnSerialize added in v0.0.19

func UnSerialize(reader io.Reader) (*Linker, error)

Directories

Path Synopsis
examples
loader command
basecontext module
inter module
objabi
tls

Jump to

Keyboard shortcuts

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