github.com/dearplain/goloader@v0.0.0-20190107071432-2b1e47d74273/README.md (about)

     1  
     2  # Goloader
     3  
     4  [![Build Status](https://travis-ci.org/dearplain/goloader.svg?branch=master)](https://travis-ci.org/dearplain/goloader)
     5  
     6  Goloader can load and run Golang code at runtime.
     7  
     8  ## How does it work?
     9  
    10  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.
    11  
    12  Goloader provides some information to the runtime and gc of Go, which allows it to work correctly with them.
    13  
    14  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.
    15  
    16  ## Comparison with plugin
    17  
    18  Goloader reuses the Go runtime, which makes it much smaller. And code loaded by Goloader is unloadable.
    19  
    20  Goloader is debuggable, and supports pprof tool(Yes, you can see code loaded by Goloader in pprof).
    21  
    22  ## Build
    23  
    24  **Make sure you're using go >= 1.8.**
    25  
    26  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.
    27  ```
    28  cp -r $GOROOT/src/cmd/internal $GOROOT/src/cmd/objfile
    29  ```
    30  
    31  ## Examples
    32  
    33  ```
    34  go build github.com/dearplain/goloader/examples/loader
    35  
    36  go tool compile $GOPATH/src/github.com/dearplain/goloader/examples/schedule/schedule.go
    37  ./loader -o schedule.o -run main.main -times 10
    38  
    39  go tool compile $GOPATH/src/github.com/dearplain/goloader/examples/base/base.go
    40  ./loader -o base.o -run main.main
    41  
    42  go tool compile $GOPATH/src/github.com/dearplain/goloader/examples/http/http.go
    43  ./loader -o http.o -run main.main
    44  
    45  #build multiple go files
    46  go tool compile -I $GOPATH/pkg/darwin_amd64 1.go 2.go
    47  
    48  #build for arm32
    49  GOOS=linux GOARCH=arm GOARM=5 go install fmt runtime sync net/http time
    50  GOOS=linux GOARCH=arm GOARM=5 go build github.com/dearplain/goloader/examples/loader
    51  GOOS=linux GOARCH=arm GOARM=5 go tool compile $GOPATH/src/github.com/dearplain/goloader/examples/base/base.go
    52  #test on arm32 linux
    53  ./loader -o base.o -run main.main
    54  ```
    55  
    56  ## Warning
    57  
    58  This has currently only been tested and developed on Golang 1.8, 1.9, and 1.10 (x64/x86, darwin, linux, windows).
    59  Be aware of Golang's internal structs change, especially the moduledata struct.