github.com/pkujhd/goloader@v0.0.0-20240411034752-1a28096bd7bd/examples/soloader/elf.txt (about) 1 #include <stdio.h> 2 #include <dlfcn.h> 3 #include <string.h> 4 #include "soloader.h" 5 6 GoString buildGoString(const char* p, size_t n){ 7 //typedef struct { const char *p; ptrdiff_t n; } _GoString_; 8 //typedef _GoString_ GoString; 9 return {p, static_cast<ptrdiff_t>(n)}; 10 } 11 12 typedef void (*loaderFunc)(GoString, GoString, GoString); 13 14 int main(int argc, char **argv) { 15 void *handle; 16 loaderFunc func; 17 char *error; 18 const char * objfile = "./soloadertest.o"; 19 const char * run = "main.main"; 20 const char * loaderso = "./soloader.so"; 21 22 handle = dlopen (loaderso, RTLD_LAZY); 23 if (!handle) { 24 fprintf (stderr, "%s \n", dlerror()); 25 return 0; 26 } 27 28 func = (loaderFunc)dlsym(handle, "loader"); 29 if ((error = dlerror()) != NULL) { 30 fprintf (stderr, "%s \n", error); 31 return 0; 32 } 33 34 GoString objfileGoStr = buildGoString(objfile, strlen(objfile)); 35 GoString runGoStr = buildGoString(run, strlen(run)); 36 GoString loadersoGoStr = buildGoString(loaderso, strlen(loaderso)); 37 (*func)(objfileGoStr, runGoStr, loadersoGoStr); 38 dlclose(handle); 39 return 0; 40 }