github.com/pkujhd/goloader@v0.0.0-20240411034752-1a28096bd7bd/examples/soloader/soloader.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "net/http" 6 "os" 7 "runtime" 8 "sync" 9 "unsafe" 10 11 "github.com/pkujhd/goloader" 12 ) 13 14 import "C" 15 16 //export loader 17 func loader(name, run, selfpath string) { 18 symPtr := make(map[string]uintptr) 19 err := goloader.RegSymbolWithSo(symPtr, selfpath) 20 if err != nil { 21 fmt.Println(err) 22 return 23 } 24 25 // most of time you don't need to register function, but if loader complain about it, you have to. 26 w := sync.WaitGroup{} 27 goloader.RegTypes(symPtr, http.ListenAndServe, http.Dir("/"), 28 http.Handler(http.FileServer(http.Dir("/"))), http.FileServer, http.HandleFunc, 29 &http.Request{}, &http.Server{}) 30 goloader.RegTypes(symPtr, runtime.LockOSThread, &w, w.Wait) 31 goloader.RegTypes(symPtr, fmt.Sprint) 32 33 var linker *goloader.Linker 34 linker, err = goloader.ReadObj(name, "") 35 if err != nil { 36 fmt.Println(err) 37 return 38 } 39 40 var codeModule *goloader.CodeModule 41 codeModule, err = goloader.Load(linker, symPtr) 42 if err != nil { 43 fmt.Println("Load error:", err) 44 return 45 } 46 runFuncPtr := codeModule.Syms[run] 47 if runFuncPtr == 0 { 48 fmt.Println("Load error! not find function:", run) 49 fmt.Println(codeModule.Syms) 50 return 51 } 52 funcPtrContainer := (uintptr)(unsafe.Pointer(&runFuncPtr)) 53 runFunc := *(*func())(unsafe.Pointer(&funcPtrContainer)) 54 runFunc() 55 os.Stdout.Sync() 56 codeModule.Unload() 57 } 58 59 func main() { 60 61 }