goloader

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: May 25, 2020 License: Apache-2.0 Imports: 15 Imported by: 7

README

Goloader

Build Status

Goloader can load and run Golang code at runtime.

Forked from 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 is debuggable, and 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

go build github.com/pkujhd/goloader/examples/loader

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

This has currently only been tested and developed on:

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

Golang 1.10-1.14 (arm, linux)

golang 1.8-1.14 (arm64(LE) linux)

Documentation

Index

Constants

View Source
const (
	R_ADDR = 1
	// R_ADDRARM64 relocates an adrp, add pair to compute the address of the
	// referenced symbol.
	R_ADDRARM64 = 3
	// R_ADDROFF resolves to a 32-bit offset from the beginning of the section
	// holding the data being relocated to the referenced symbol.
	R_ADDROFF = 5
	// R_WEAKADDROFF resolves just like R_ADDROFF but is a weak relocation.
	// A weak relocation does not make the symbol it refers to reachable,
	// and is only honored by the linker if the symbol is in some other way
	// reachable.
	R_WEAKADDROFF = 6
	R_CALL        = 8
	R_CALLARM     = 9
	R_CALLARM64   = 10
	R_CALLIND     = 11
)

copy from $GOROOT/src/cmd/internal/objabi/reloctype.go

View Source
const (
	TLSNAME        = "(TLS)"
	R_CALLIND_NAME = "R_CALLIND"
)
View Source
const IntSize = int(unsafe.Sizeof(int(0)))
View Source
const PtrSize = 4 << (^uintptr(0) >> 63)
View Source
const Uint32Size = int(unsafe.Sizeof(uint32(0)))

Variables

This section is empty.

Functions

func GetFunctionName

func GetFunctionName(i interface{}) string

func Loadp added in v0.0.2

func Loadp(ptr unsafe.Pointer) unsafe.Pointer

func Mmap

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

func Munmap

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

func RegFunc

func RegFunc(symPtr map[string]uintptr, name string, f interface{})

func RegItab

func RegItab(symPtr map[string]uintptr, name string, addr uintptr)

func RegSymbol

func RegSymbol(symPtr map[string]uintptr)

func RegTLS

func RegTLS(symPtr map[string]uintptr, offset int)

func RegType

func RegType(symPtr map[string]uintptr, name string, typ interface{})

func RegTypes

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

Types

type CodeModule

type CodeModule struct {
	Syms     map[string]uintptr
	CodeByte []byte
	Module   interface{}
	// contains filtered or unexported fields
}

func Load

func Load(code *CodeReloc, symPtr map[string]uintptr) (*CodeModule, error)

func (*CodeModule) Unload

func (cm *CodeModule) Unload()

type CodeReloc

type CodeReloc struct {
	Code    []byte
	Data    []byte
	Mod     Module
	Syms    []SymData
	SymMap  map[string]int
	GCObjs  map[string]uintptr
	FileMap map[string]int
	Arch    string
}

CodeReloc dispatch and load CodeReloc struct via network is OK

func ReadObj

func ReadObj(f *os.File) (*CodeReloc, error)

func ReadObjs

func ReadObjs(files []string, pkgPath []string) (*CodeReloc, error)

type Module

type Module struct {
	// contains filtered or unexported fields
}

type Reloc

type Reloc struct {
	Offset   int
	SymOff   int
	Size     int
	Type     int
	Add      int
	DataSize int64
}

type SymData

type SymData struct {
	Name   string
	Kind   int
	Offset int
	Reloc  []Reloc
}

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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