github.com/pkujhd/goloader@v0.0.0-20240411034752-1a28096bd7bd/README.md (about)

     1  
     2  # Goloader
     3  
     4  ![Build Status](https://github.com/pkujhd/goloader/workflows/goloader%20Testing/badge.svg)
     5  
     6  Goloader can load and run Golang code at runtime.
     7  
     8  Forked from **https://github.com/dearplain/goloader**, Take over maintenance because the original author is not in maintenance
     9  
    10  ## How does it work?
    11  
    12  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.
    13  
    14  Goloader provides some information to the runtime and gc of Go, which allows it to work correctly with them.
    15  
    16  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.
    17  
    18  ## Comparison with plugin
    19  
    20  Goloader reuses the Go runtime, which makes it much smaller. And code loaded by Goloader is unloadable.
    21  
    22  Goloader supports pprof tool(Yes, you can see code loaded by Goloader in pprof). 
    23  
    24  Goloader don't support obj file with cgo.
    25  
    26  ## Build
    27  
    28  **Make sure you're using go >= 1.8.**
    29  
    30  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.
    31  ```
    32  cp -r $GOROOT/src/cmd/internal $GOROOT/src/cmd/objfile
    33  ```
    34  
    35  ## Examples
    36  
    37  If use go module or go version >= 1.20
    38  ```
    39  export GO111MODULE=on
    40  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
    41  go tool compile -importcfg importcfg $GOPATH/src/github.com/pkujhd/goloader/examples/schedule/schedule.go
    42  ```
    43  If use go path and go version < 1.20
    44  ```
    45  export GO111MODULE=auto
    46  go build github.com/pkujhd/goloader/examples/loader
    47  
    48  go tool compile $GOPATH/src/github.com/pkujhd/goloader/examples/schedule/schedule.go
    49  ./loader -o schedule.o -run main.main -times 10
    50  
    51  go tool compile $GOPATH/src/github.com/pkujhd/goloader/examples/base/base.go
    52  ./loader -o base.o -run main.main
    53  
    54  go tool compile $GOPATH/src/github.com/pkujhd/goloader/examples/http/http.go
    55  ./loader -o http.o -run main.main
    56  
    57  go install github.com/pkujhd/goloader/examples/basecontext
    58  go tool compile -I $GOPATH/pkg/`go env GOOS`_`go env GOARCH`/ $GOPATH/src/github.com/pkujhd/goloader/examples/inter/inter.go
    59  ./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
    60  
    61  #build multiple go files
    62  go tool compile -I $GOPATH/pkg/`go env GOOS`_`go env GOARCH`/ -o test.o test1.go test2.go
    63  ./loader -o test.o -run main.main
    64  ```
    65  
    66  ## Warning
    67  
    68  Don't use "-s -w" compile argument, It strips symbol table.
    69  
    70  This has currently only been tested and developed on:
    71  
    72  Golang 1.8-1.22 (x64/x86, darwin, linux, windows)
    73  
    74  Golang 1.10-1.22 (arm, linux, android)
    75  
    76  Golang 1.8-1.22 (arm64, linux, android)
    77  
    78  Golang 1.16-1.22 (arm64, darwin)