CGO

  • 避免 CGO
    • notti/nocgo - dlopen without cgo
      • dlopen, dlclose, dlerror, dlsym
      • ffi 汇编, 支持 386 和 amd64
    • rainycape/dl - dlopen / dlsym
  • 参考

dlopen#

# 查看 so symbol
# 没有地址的 symbol 是动态的
nm -gDC /usr/lib/libsqlite3.so
objdump -TC libz.so
readelf -Ws libz.so
# 只看 symbol
readelf -Ws /usr/lib/libsqlite3.so | awk '{print $8}';
// +build !cgo
package dlopen
// we have to use the 3 argument format here :( - 2 argument format is only allowed from inside cgo
//go:cgo_import_dynamic libc_dlopen_x dlopen "libdl.so.2"
//go:cgo_import_dynamic libc_dlclose_x dlclose "libdl.so.2"
//go:cgo_import_dynamic libc_dlsym_x dlsym "libdl.so.2"
//go:cgo_import_dynamic libc_dlerror_x dlerror "libdl.so.2"
// on amd64 we don't need the following line - on 386 we do...
// anyway - with those lines the output is better (but doesn't matter) - without it on amd64 we get multiple DT_NEEDED with "libc.so.6" etc
//go:cgo_import_dynamic _ _ "libdl.so.2"

sqlite#