Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

any document for load compied binary? #29

Open
blue-troy opened this issue Jan 17, 2024 · 2 comments
Open

any document for load compied binary? #29

blue-troy opened this issue Jan 17, 2024 · 2 comments

Comments

@blue-troy
Copy link

I try to use this project with load compied binary like https://github.com/pkujhd/goloader's usage type like:

linker, err := goloader.ReadObjs....
err = goloader.RegSymbol(symPtr....
codeModule, err := goloader.Load...

but faild, can you add any document?

@blue-troy
Copy link
Author

this is my demo:

func TestGoLoader(t *testing.T) {
	linker, err := goloader.ReadObjs([]string{"custom_pro.o"}, []string{"custom"}, nil, goloader.WithForceTestRelocationEpilogues())
	if err != nil {
		t.Error(err)
		return
	}
	run := "custom.Run"

	symPtr := make(map[string]uintptr)
	pkgSet := make(map[string]struct{})
	err = goloader.RegSymbol(symPtr, pkgSet)
	if err != nil {
		t.Fatal(err)
	}

	codeModule, err := goloader.Load(linker, symPtr)
	if err != nil {
		t.Fatal(err)
	}
	runFuncPtr, ok := codeModule.Syms[run]
	if !ok || runFuncPtr == 0 {
		t.Fatalf("Load error! not find function: %s", run)
	}
	funcPtrContainer := (uintptr)(unsafe.Pointer(&runFuncPtr))
	runFunc := *(*func(event *beat.Event) (*beat.Event, error))(unsafe.Pointer(&funcPtrContainer))
	event := getEvent()
	res, err := runFunc(event)
	fmt.Println(res)
	os.Stdout.Sync()
	codeModule.Unload()
}

which print:

2024/01/17 16:28:25 Got a TLS symbol [runtime.tls_g] emitted in the main binary (value 0x0 or 0x1024267c0), but not sure what to do with it
=== RUN   TestGoLoader
2024/01/17 16:28:31 Got a TLS symbol [runtime.tls_g] emitted in the main binary (value 0x0 or 0x1024267c0), but not sure what to do with it
--- FAIL: TestGoLoader (27.57s)
panic: runtime error: index out of range [0] with length 0 [recovered]
	panic: runtime error: index out of range [0] with length 0

goroutine 7 [running]:
testing.tRunner.func1.2({0x101995be0, 0x140024a2030})
	/opt/homebrew/opt/go/libexec/src/testing/testing.go:1545 +0x3c0
testing.tRunner.func1()
	/opt/homebrew/opt/go/libexec/src/testing/testing.go:1548 +0x5d0
panic({0x101995be0?, 0x140024a2030?})
	/opt/homebrew/opt/go/libexec/src/runtime/panic.go:920 +0x254
github.com/eh-steve/goloader.(*Linker).buildModule(0x140007fa240, 0x14000350000, 0x14000362270)
	/Users/bluetroy/go/pkg/mod/github.com/eh-steve/[email protected]/ld.go:888 +0xbf4
github.com/eh-steve/goloader.Load(0x140007fa240, 0x140026e9ce8)
	/Users/bluetroy/go/pkg/mod/github.com/eh-steve/[email protected]/ld.go:1279 +0x9fc
github.com/elastic/beats/v7/libbeat/processors/actions.TestGoLoader(0x1400055b380)
	/Users/bluetroy/IdeaProjects/github.com/elastic/beats/libbeat/processors/actions/bench_test.go:210 +0x4bc
testing.tRunner(0x1400055b380, 0x101a25188)
	/opt/homebrew/opt/go/libexec/src/testing/testing.go:1595 +0x1b4
created by testing.(*T).Run in goroutine 1
	/opt/homebrew/opt/go/libexec/src/testing/testing.go:1648 +0x660

@eh-steve
Copy link
Owner

The intended usage is as per the README. If you want to load precompiled packages, you can either set KeepTempFiles to true, and use the resulting package archives in the same way that the jit package does, or wait for me to get around to implementing #18

In this fork, you should use SymbolsByPkg not Syms and you don't need any unsafe function pointer casts.

If you share some basic reproducing source code for custom_pro.o and your imports for github.com/elastic/beats/libbeat/beat, I can see if I can reproduce on my side

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants