Closed nrjatyunshan closed 1 year ago
Export functions to make it easier for other languages to load as libraries.
go build -buildmode=c-shared -o libGoReSym.so
then, use it in c
#include <stdio.h> #include <dlfcn.h> #include <assert.h> #include <stddef.h> typedef signed char GoInt8; typedef unsigned char GoUint8; typedef short GoInt16; typedef unsigned short GoUint16; typedef int GoInt32; typedef unsigned int GoUint32; typedef long long GoInt64; typedef unsigned long long GoUint64; typedef GoInt64 GoInt; typedef GoUint64 GoUint; typedef size_t GoUintptr; typedef float GoFloat32; typedef double GoFloat64; typedef struct { const char *p; ptrdiff_t n; } GoString; typedef void *GoMap; typedef void *GoChan; typedef struct { void *t; void *v; } GoInterface; typedef struct { void *data; GoInt len; GoInt cap; } GoSlice; struct Func { GoUintptr addr; GoInt size; }; int main() { void *dlfile = dlopen("./libGoReSym.so", RTLD_LAZY); assert(dlfile); struct Func (*GetFuncBySym)(GoString, GoString) = dlsym(dlfile, "GetFuncBySym"); assert(GetFuncBySym); GoString filename; GoString symname; filename.p = "GoReSym"; filename.n = sizeof("GoReSym") - 1; symname.p = "runtime.casgstatus"; symname.n = sizeof("runtime.casgstatus") - 1; struct Func func = GetFuncBySym(filename, symname); printf("addr=[%p]\n", func.addr); printf("size=[%d]\n", func.size); dlclose(dlfile); return 0; }
output
addr=[0x43ae00] size=[384]
Export functions to make it easier for other languages to load as libraries.
then, use it in c
output