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

Call function got crashed #4

Open
shallowclouds opened this issue Dec 25, 2021 · 0 comments
Open

Call function got crashed #4

shallowclouds opened this issue Dec 25, 2021 · 0 comments

Comments

@shallowclouds
Copy link

Examples are very simple functions, I wrote a simple function which use printf to print a message, but got crashed, is there anything else I should do?

The lib src:

#include "stdio.h"

void test_go() {
  printf("invoke test_go");
}

Compiled to lib:

gcc -shared testlib/biz.c -o testlib.so

Run with the following go src:

package main

import (
	"fmt"
	"io/ioutil"

	"github.com/Binject/universal"
)

func main() {
	var (
		libPath = "testlib.so"
		fName   = "test_go"
	)

	image, err := ioutil.ReadFile(libPath)
	if err != nil {
		panic(err)
	}

	loader, err := universal.NewLoader()
	if err != nil {
		panic(err)
	}

	lib, err := loader.LoadLibrary("main", &image)
	if err != nil {
		panic(err)
	}

	for k := range lib.Exports {
		fmt.Printf("exported function: %s\n", k)
	}

	addr, ok := lib.FindProc(fName)
	if ok {
		fmt.Printf("function address of %s: %x\n", fName, addr)
	} else {
		panic("function not found")
	}

	val, err := lib.Call(fName)
	if err != nil {
		panic(err)
	}

	fmt.Printf("returned value: %v\n", val)
}

But got a sad crash:

image

Go version: go version go1.17.1 linux/amd64

GCC version: gcc version 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04)

System: WSL1, and ubuntu20.04 also fails.

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

1 participant