github.com/notti/nocgo@v0.0.0-20190619201224-fc443047424c/internal/dlopen/dlopen.go (about)

     1  // +build !cgo
     2  
     3  package dlopen
     4  
     5  import (
     6  	"unsafe"
     7  
     8  	_ "github.com/notti/nocgo/internal/fakecgo" // get everything we need to fake cgo behaviour if we're not using cgo
     9  	"github.com/notti/nocgo/internal/ffi"
    10  )
    11  
    12  func mustSpec(fn *byte, fun interface{}) {
    13  	err := ffi.MakeSpec(uintptr(unsafe.Pointer(fn)), fun)
    14  	if err != nil {
    15  		panic(err)
    16  	}
    17  }
    18  
    19  // on 386 we need to do the dance of cgo_import_dynamic followed by two linknames,
    20  // definining a variable that gets the dynamic symbol, and then derefercing it.
    21  // Othwerwise we get an unknown relocation type error during linking
    22  
    23  //go:linkname libc_dlopen_x libc_dlopen_x
    24  var libc_dlopen_x byte
    25  var libc_dlopen = &libc_dlopen_x
    26  
    27  //go:linkname libc_dlclose_x libc_dlclose_x
    28  var libc_dlclose_x byte
    29  var libc_dlclose = &libc_dlclose_x
    30  
    31  //go:linkname libc_dlsym_x libc_dlsym_x
    32  var libc_dlsym_x byte
    33  var libc_dlsym = &libc_dlsym_x
    34  
    35  //go:linkname libc_dlerror_x libc_dlerror_x
    36  var libc_dlerror_x byte
    37  var libc_dlerror = &libc_dlerror_x
    38  
    39  var DLOpen func(filename []byte, flags int32) uintptr
    40  var DLClose func(handle uintptr) int32
    41  var DLSym func(handle uintptr, symbol []byte) uintptr
    42  var DLError func() uintptr
    43  
    44  func init() {
    45  	mustSpec(libc_dlopen, &DLOpen)
    46  	mustSpec(libc_dlclose, &DLClose)
    47  	mustSpec(libc_dlsym, &DLSym)
    48  	mustSpec(libc_dlerror, &DLError)
    49  }