github.com/eh-steve/goloader@v0.0.0-20240111193454-90ff3cfdae39/libc/lookup_windows.go (about)

     1  //go:build cgo && windows
     2  // +build cgo,windows
     3  
     4  package libc
     5  
     6  /*
     7  // Purely to bake in _cgo_topofstack _cgo_get_context_function etc
     8  
     9  */
    10  import "C"
    11  import (
    12  	"fmt"
    13  	"strings"
    14  	"syscall"
    15  )
    16  
    17  var modkernel32, err = syscall.LoadDLL("kernel32.dll")
    18  
    19  func init() {
    20  	if err != nil {
    21  		panic("could not open kernel32.dll")
    22  	}
    23  }
    24  
    25  func LookupDynamicSymbol(symName string) (uintptr, error) {
    26  	// Windows doesn't have a libdl, so we can't find self-loaded C symbols by passing a null string to dlopen,
    27  	// so instead have to lookup in kernel32.dll.
    28  	// TODO - should we also attempt to look in some others?
    29  	if strings.HasPrefix(symName, "_cgo") {
    30  		return 0, fmt.Errorf("could not find CGo symbol %s (should have been included in host binary symtab)", symName)
    31  	}
    32  	proc, err := modkernel32.FindProc(symName)
    33  	if err != nil {
    34  		return 0, fmt.Errorf("failed to lookup symbol %s: %w", symName, err)
    35  	}
    36  	return proc.Addr(), nil
    37  }