goloader

package module
v0.0.13 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2021 License: Apache-2.0 Imports: 15 Imported by: 7

README

Goloader

Build Status

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 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

export GO111MODULE=auto
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

Don't use "-s -w" compile argument, It strips symbol table.

This has currently only been tested and developed on:

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

Golang 1.10-1.17 (arm, linux, android)

Golang 1.8-1.17 (arm64(LE) linux, android)

Documentation

Index

Constants

View Source
const (
	PtrSize    = 4 << (^uintptr(0) >> 63)
	Uint32Size = int(unsafe.Sizeof(uint32(0)))
	IntSize    = int(unsafe.Sizeof(int(0)))
	UInt64Size = int(unsafe.Sizeof(uint64(0)))

	ItabSize           = int(unsafe.Sizeof(itab{}))
	FindFuncBucketSize = int(unsafe.Sizeof(findfuncbucket{}))
	InlinedCallSize    = int(unsafe.Sizeof(inlinedCall{}))
	InvalidHandleValue = ^uintptr(0)
	InvalidOffset      = int(-1)
	InvalidIndex       = uint32(0xFFFFFFFF)
	PageSize           = 1 << 12 //4096
)

size

View Source
const (
	EmptyString    = ""
	DefaultPkgPath = "main"
	EmptyPkgPath   = `""`
	ZeroByte       = byte(0x00)
)
View Source
const (
	FileSymPrefix        = "gofile.."
	MainPkgPrefix        = "main."
	TypeImportPathPrefix = "type..importpath."
	TypeDoubleDotPrefix  = "type.."
	TypePrefix           = "type."
	ItabPrefix           = "go.itab."
	StkobjSuffix         = ".stkobj"
	InlineTreeSuffix     = ".inlinetree"
	OsStdout             = "os.Stdout"
)

string match prefix/suffix

View Source
const (
	KindGCProg = 1 << 6
)
View Source
const (
	RuntimeDeferReturn = "runtime.deferreturn"
)

runtime symbol

View Source
const (
	TLSNAME = "(TLS)"
)

Variables

This section is empty.

Functions

func Mmap

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

func Munmap

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

func Parse added in v0.0.12

func Parse(f *os.File, pkgpath *string) ([]string, error)

func RegSymbol

func RegSymbol(symPtr map[string]uintptr) error

func RegSymbolWithSo added in v0.0.13

func RegSymbolWithSo(symPtr map[string]uintptr, path string) error

func RegTypes

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

Types

type CodeModule

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

func Load

func Load(linker *Linker, symPtr map[string]uintptr) (codeModule *CodeModule, err error)

func (*CodeModule) Unload

func (cm *CodeModule) Unload()

type Func added in v0.0.7

type Func struct {
	PCData   []uint32
	FuncData []uintptr
}

type FuncInfo added in v0.0.12

type FuncInfo struct {
	Args     uint32
	Locals   uint32
	FuncID   uint8
	PCSP     []byte
	PCFile   []byte
	PCLine   []byte
	PCInline []byte
	PCData   [][]byte
	File     []string
	FuncData []string
	InlTree  []InlTreeNode
}

type InlTreeNode added in v0.0.12

type InlTreeNode struct {
	Parent   int64
	File     string
	Line     int64
	Func     string
	ParentPC int64
}

type Linker added in v0.0.12

type Linker struct {
	Arch *sys.Arch
	// contains filtered or unexported fields
}

func ReadObj

func ReadObj(f *os.File, pkgpath *string) (*Linker, error)

func ReadObjs

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

type ObjSymbol added in v0.0.12

type ObjSymbol struct {
	Name  string
	Kind  int    // kind of symbol
	DupOK bool   // are duplicate definitions okay?
	Size  int64  // size of corresponding data
	Data  []byte // memory image of symbol
	Type  string
	Reloc []Reloc
	Func  *FuncInfo // additional data for functions
}

type Pkg added in v0.0.12

type Pkg struct {
	Syms    map[string]*ObjSymbol
	Arch    string
	PkgPath string
	// contains filtered or unexported fields
}

type Reloc

type Reloc struct {
	Offset int
	Sym    *Sym
	Size   int
	Type   int
	Add    int
}

copy from $GOROOT/src/cmd/internal/goobj/read.go type Reloc struct

type Sym added in v0.0.5

type Sym struct {
	Name   string
	Kind   int
	Offset int
	Func   *Func
	Reloc  []Reloc
}

copy from $GOROOT/src/cmd/internal/goobj/read.go type Sym struct

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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