dlfcn.go RawHistoryFolder
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: 2022 The Ebitengine Authors
3
4//go:build darwin || linux
5
6package purego
7
8import (
9 "unsafe"
10)
11
12// Unix Specification for dlfcn.h: https://pubs.opengroup.org/onlinepubs/7908799/xsh/dlfcn.h.html
13
14var (
15 fnDlopen func(path string, mode int) uintptr
16 fnDlsym func(handle uintptr, name string) uintptr
17 fnDlerror func() string
18 fnDlclose func(handle uintptr) bool
19)
20
21func init() {
22 RegisterFunc(&fnDlopen, dlopenABI0)
23 RegisterFunc(&fnDlsym, dlsymABI0)
24 RegisterFunc(&fnDlerror, dlerrorABI0)
25 RegisterFunc(&fnDlclose, dlcloseABI0)
26}
27
28// Dlopen examines the dynamic library or bundle file specified by path. If the file is compatible
29// with the current process and has not already been loaded into the
30// current process, it is loaded and linked. After being linked, if it contains
31// any initializer functions, they are called, before Dlopen
32// returns. It returns a handle that can be used with Dlsym and Dlclose.
33// A second call to Dlopen with the same path will return the same handle, but the internal
34// reference count for the handle will be incremented. Therefore, all
35// Dlopen calls should be balanced with a Dlclose call.
36func Dlopen(path string, mode int) uintptr {
37 return fnDlopen(path, mode)
38}
39
40// Dlsym takes a "handle" of a dynamic library returned by Dlopen and the symbol name.
41// It returns the address where that symbol is loaded into memory. If the symbol is not found,
42// in the specified library or any of the libraries that were automatically loaded by Dlopen
43// when that library was loaded, Dlsym returns zero.
44func Dlsym(handle uintptr, name string) uintptr {
45 return fnDlsym(handle, name)
46}
47
48// Dlerror returns a human-readable string describing the most recent error that
49// occurred from Dlopen, Dlsym or Dlclose since the last call to Dlerror. It
50// returns an empty string if no errors have occurred since initialization or
51// since it was last called.
52func Dlerror() string {
53 // msg is only valid until the next call to Dlerror
54 // which is why it gets copied into a Go string
55 return fnDlerror()
56}
57
58// Dlclose decrements the reference count on the dynamic library handle.
59// If the reference count drops to zero and no other loaded libraries
60// use symbols in it, then the dynamic library is unloaded.
61// Dlclose returns false on success, and true on error.
62func Dlclose(handle uintptr) bool {
63 return fnDlclose(handle)
64}
65
66// these functions exist in dlfcn_stubs.s and are calling C functions linked to in dlfcn_GOOS.go
67// the indirection is necessary because a function is actually a pointer to the pointer to the code.
68// sadly, I do not know of anyway to remove the assembly stubs entirely because //go:linkname doesn't
69// appear to work if you link directly to the C function on darwin arm64.
70
71//go:linkname dlopen dlopen
72var dlopen uintptr
73var dlopenABI0 = uintptr(unsafe.Pointer(&dlopen))
74
75//go:linkname dlsym dlsym
76var dlsym uintptr
77var dlsymABI0 = uintptr(unsafe.Pointer(&dlsym))
78
79//go:linkname dlclose dlclose
80var dlclose uintptr
81var dlcloseABI0 = uintptr(unsafe.Pointer(&dlclose))
82
83//go:linkname dlerror dlerror
84var dlerror uintptr
85var dlerrorABI0 = uintptr(unsafe.Pointer(&dlerror))