Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/pkujhd/goloader
load and run golang code at runtime.
https://github.com/pkujhd/goloader
dynamic go golang plugin
Last synced: 3 days ago
JSON representation
load and run golang code at runtime.
- Host: GitHub
- URL: https://github.com/pkujhd/goloader
- Owner: pkujhd
- License: apache-2.0
- Created: 2019-07-23T05:32:38.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2024-11-19T04:11:10.000Z (23 days ago)
- Last Synced: 2024-12-02T03:02:01.752Z (10 days ago)
- Topics: dynamic, go, golang, plugin
- Language: Go
- Homepage:
- Size: 506 KB
- Stars: 508
- Watchers: 18
- Forks: 60
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-hacking-lists - pkujhd/goloader - load and run golang code at runtime. (Go)
README
# Goloader
![Build Status](https://github.com/pkujhd/goloader/workflows/goloader%20Testing/badge.svg)
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).
Goloader don't support obj file with cgo.
## 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:
If use go version >= 1.23
```
go build --ldflags="-checklinkname=0" github.com/pkujhd/goloader/examples/loader
```
If use go version <= 1.22
```
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 -f '{{if .Export}}packagefile {{.ImportPath}}={{.Export}}{{end}}' std `go list -f {{.Imports}} $GOPATH/src/github.com/pkujhd/goloader/examples/schedule/schedule.go | awk '{sub(/^\[/, ""); print }' | awk '{sub(/\]$/, ""); print }'` > 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 tool compile $GOPATH/src/github.com/pkujhd/goloader/examples/base/base.go
./loader -o base.o -run main.main
go tool compile $GOPATH/src/github.com/pkujhd/goloader/examples/http/http.go
./loader -o http.o -run main.main
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
```#Build multiple go files
```
go tool compile -I $GOPATH/pkg/`go env GOOS`_`go env GOARCH`/ -o test.o test1.go test2.go
./loader -o test.o -run main.main
```## Warning
Don't use "-s -w" compile argument, It strips symbol table.
This has currently only been tested and developed on:
Golang 1.8-1.23 (x64/x86, darwin, linux, windows)
Golang 1.10-1.23 (arm, linux, android)
Golang 1.8-1.23 (arm64, linux, android)
Golang 1.16-1.23 (arm64, darwin)