-
Notifications
You must be signed in to change notification settings - Fork 48
/
dl.go
174 lines (165 loc) · 4.29 KB
/
dl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package dl
// #include <dlfcn.h>
// #include <stdlib.h>
// #cgo LDFLAGS: -ldl
import "C"
import (
"errors"
"fmt"
"path/filepath"
"reflect"
"runtime"
"sync"
"unsafe"
)
const (
// dlopen() flags. See man dlopen.
RTLD_LAZY = int(C.RTLD_LAZY)
RTLD_NOW = int(C.RTLD_NOW)
RTLD_GLOBAL = int(C.RTLD_GLOBAL)
RTLD_LOCAL = int(C.RTLD_LOCAL)
RTLD_NODELETE = int(C.RTLD_NODELETE)
RTLD_NOLOAD = int(C.RTLD_NOLOAD)
)
var (
mu sync.Mutex
)
// DL represents an opened dynamic library. Use Open
// to initialize a DL and use DL.Close when you're finished
// with it. Note that when the DL is closed all its loaded
// symbols become invalid.
type DL struct {
mu sync.Mutex
handle unsafe.Pointer
}
// Open opens the shared library identified by the given name
// with the given flags. See man dlopen for the available flags
// and its meaning. Note that the only difference with dlopen is that
// if nor RTLD_LAZY nor RTLD_NOW are specified, Open defaults to
// RTLD_NOW rather than returning an error. If the name argument
// passed to name does not have extension, the default for the
// platform will be appended to it (e.g. .so, .dylib, etc...).
func Open(name string, flag int) (*DL, error) {
if flag&RTLD_LAZY == 0 && flag&RTLD_NOW == 0 {
flag |= RTLD_NOW
}
if name != "" && filepath.Ext(name) == "" {
name = name + LibExt
}
s := C.CString(name)
defer C.free(unsafe.Pointer(s))
mu.Lock()
handle := C.dlopen(s, C.int(flag))
var err error
if handle == nil {
err = dlerror()
}
mu.Unlock()
if err != nil {
if runtime.GOOS == "linux" && name == "libc.so" {
// In most distros libc.so is now a text file
// and in order to dlopen() it the name libc.so.6
// must be used.
return Open(name+".6", flag)
}
return nil, err
}
return &DL{
handle: handle,
}, nil
}
// Sym loads the symbol identified by the given name into
// the out parameter. Note that out must always be a pointer.
// See the package documentation to learn how types are mapped
// between Go and C.
func (d *DL) Sym(symbol string, out interface{}) error {
s := C.CString(symbol)
defer C.free(unsafe.Pointer(s))
mu.Lock()
handle := C.dlsym(d.handle, s)
if handle == nil {
err := dlerror()
mu.Unlock()
return err
}
mu.Unlock()
val := reflect.ValueOf(out)
if !val.IsValid() || val.Kind() != reflect.Ptr {
return fmt.Errorf("out must be a pointer, not %T", out)
}
if val.IsNil() {
return errors.New("out can't be nil")
}
elem := val.Elem()
switch elem.Kind() {
case reflect.Int:
// We treat Go's int as long, since it
// varies depending on the platform bit size
elem.SetInt(int64(*(*int)(handle)))
case reflect.Int8:
elem.SetInt(int64(*(*int8)(handle)))
case reflect.Int16:
elem.SetInt(int64(*(*int16)(handle)))
case reflect.Int32:
elem.SetInt(int64(*(*int32)(handle)))
case reflect.Int64:
elem.SetInt(int64(*(*int64)(handle)))
case reflect.Uint:
// We treat Go's uint as unsigned long, since it
// varies depending on the platform bit size
elem.SetUint(uint64(*(*uint)(handle)))
case reflect.Uint8:
elem.SetUint(uint64(*(*uint8)(handle)))
case reflect.Uint16:
elem.SetUint(uint64(*(*uint16)(handle)))
case reflect.Uint32:
elem.SetUint(uint64(*(*uint32)(handle)))
case reflect.Uint64:
elem.SetUint(uint64(*(*uint64)(handle)))
case reflect.Uintptr:
elem.SetUint(uint64(*(*uintptr)(handle)))
case reflect.Float32:
elem.SetFloat(float64(*(*float32)(handle)))
case reflect.Float64:
elem.SetFloat(float64(*(*float64)(handle)))
case reflect.Func:
typ := elem.Type()
tr, err := makeTrampoline(typ, handle)
if err != nil {
return err
}
v := reflect.MakeFunc(typ, tr)
elem.Set(v)
case reflect.Ptr:
v := reflect.NewAt(elem.Type().Elem(), handle)
elem.Set(v)
case reflect.String:
elem.SetString(C.GoString(*(**C.char)(handle)))
case reflect.UnsafePointer:
elem.SetPointer(handle)
default:
return fmt.Errorf("invalid out type %T", out)
}
return nil
}
// Close closes the shared library handle. All symbols
// loaded from the library will become invalid.
func (d *DL) Close() error {
if d.handle != nil {
d.mu.Lock()
defer d.mu.Unlock()
if d.handle != nil {
mu.Lock()
defer mu.Unlock()
if C.dlclose(d.handle) != 0 {
return dlerror()
}
d.handle = nil
}
}
return nil
}
func dlerror() error {
s := C.dlerror()
return errors.New(C.GoString(s))
}