The goloader/jit
package can compile and load Go code from text, file, folder or remote package (including code with
package imports).
It automatically resolves package dependencies recursively, and provides a type safe way of interacting with the built functions.
Forked from dearplain and pkujhd.
Make sure you're using go >= 1.18.
First, execute the following command. 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
To allow the loader to know the types of exported functions, this package will attempt to patch the Go compiler (gc) to emit these if not already patched.
The effect of the patch can be found in jit/gc.patch
.
go install github.com/eh-steve/goloader/jit/patchgc@latest
# You may need to run patchgc as sudo if your $GOROOT is owned by root
# (alternatively `chown -R $USER:$USER $GOROOT`)
patchgc
package main
import (
"fmt"
"github.com/eh-steve/goloader/jit"
)
func main() {
conf := jit.BuildConfig{
KeepTempFiles: false, // Files are copied/written to a temp dir to ensure it is writable. This retains the temporary copies
ExtraBuildFlags: []string{"-x"}, // Flags passed to go build command
BuildEnv: nil, // Env vars to set for go build toolchain
TmpDir: "", // To control where temporary files are copied
DebugLog: true, //
}
loadable, err := jit.BuildGoFiles(conf, "./path/to/file1.go", "/path/to/file2.go")
if err != nil {
panic(err)
}
// or
loadable, err = jit.BuildGoPackage(conf, "./path/to/package")
if err != nil {
panic(err)
}
// or
loadable, err = jit.BuildGoPackageRemote(conf, "github.com/some/package/v4", "latest")
if err != nil {
panic(err)
}
// or
loadable, err = jit.BuildGoText(conf, `
package mypackage
import "encoding/json"
func MyFunc(input []byte) (interface{}, error) {
var output interface{}
err := json.Unmarshal(input, &output)
return output, err
}
`)
if err != nil {
panic(err)
}
module, err := loadable.Load()
// module.SymbolsByPkg is a map[string]map[string]interface{} of all packages and their exported functions and global vars
symbols := module.SymbolsByPkg[loadable.ImportPath]
if err != nil {
panic(err)
}
defer func() {
err = module.Unload()
if err != nil {
panic(err)
}
}()
switch f := symbols["MyFunc"].(type) {
case func([]byte) (interface{}, error):
result, err := f([]byte(`{"k":"v"}`))
if err != nil {
panic(err)
}
fmt.Println(result)
default:
panic("Function signature was not what was expected")
}
}
Goloader works like a linker, it relocates the addresses of symbols in an object file, generates runnable code, and then reuses the runtime functions and the type pointers of the loader where available.
Goloader provides some information to the runtime and garbage collector of Go, which allows it to work correctly with them.
Please note that Goloader is not a scripting engine. It reads the archives emitted from the Go compiler and makes them runnable. All features of Go are supported, and run just as fast and lightweight as native Go code.
plugin
Plugin:
plugin was built with a different version of package
) - this makes them basically unusable in most large
projectslibdl
/CGo (and doesn't work on Windows)Goloader:
libdl
/CgoGoloader supports pprof tool (yes, you can see code loaded by Goloader in pprof), but does not (yet) support debugging
with delve
.
JIT compiler tested/passing on:
OS/Arch | amd64/+CGo | arm64/+CGo | amd64/-CGo | arm64/-CGo |
---|---|---|---|---|
Linux/go-1.20.3 | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
Darwin/go-1.20.3 | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
Windows/go-1.20.3 | :heavy_check_mark: | :interrobang: | :heavy_check_mark: | :interrobang: |
Linux/go-1.19.4 | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
Darwin/go-1.19.4 | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
Windows/go-1.19.4 | :heavy_check_mark: | :interrobang: | :heavy_check_mark: | :interrobang: |
Linux/go-1.18.8 | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
Darwin/go-1.18.8 | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
Windows/go-1.18.8 | :x: | :interrobang: | :heavy_check_mark: | :interrobang: |
Don't use "-s -w" compile argument, It strips symbol table.