Skip to content

Call function got crashed #4

Description

@shallowclouds

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions