summaryrefslogtreecommitdiff
path: root/src/dl/dlfcn.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/dl/dlfcn.go')
-rw-r--r--src/dl/dlfcn.go62
1 files changed, 51 insertions, 11 deletions
diff --git a/src/dl/dlfcn.go b/src/dl/dlfcn.go
index d5467f3..3ab5abb 100644
--- a/src/dl/dlfcn.go
+++ b/src/dl/dlfcn.go
@@ -1,3 +1,25 @@
+// Copyright 2015 Luke Shumaker <lukeshu@sbcglobal.net>.
+//
+// This is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License as
+// published by the Free Software Foundation; either version 2 of
+// the License, or (at your option) any later version.
+//
+// The GNU General Public License's references to "object code" and
+// "executables" are to be interpreted to also include the output of
+// any document formatting or typesetting system, including
+// intermediate and printed output.
+//
+// This software is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public
+// License along with this manual; if not, see
+// <http://www.gnu.org/licenses/>.
+
+// Package dl provides an interface to the POSIX runtime linker.
package dl
import (
@@ -12,26 +34,39 @@ import "C"
type Flag int
+// POSIX specifies these four flags to Open().
const (
- RTLD_LAZY Flag = C.RTLD_LAZY // Relocations are performed at an
- // implementation-defined time.
- RTLD_NOW Flag = C.RTLD_NOW // Relocations are performed when the
- // object is loaded.
- RTLD_GLOBAL Flag = C.RTLD_GLOBAL // All symbols are available for
- // relocation processing of other
- // modules.
- RTLD_LOCAL Flag = C.RTLD_LOCAL // All symbols are not made available
- // for relocation processing by other
- // modules.
+ // Relocations are performed at an implementation-defined
+ // time.
+ RTLD_LAZY Flag = C.RTLD_LAZY
+
+ // Relocations are performed when the object is loaded.
+ RTLD_NOW Flag = C.RTLD_NOW
+
+ // All symbols are available for relocation processing of
+ // other modules.
+ RTLD_GLOBAL Flag = C.RTLD_GLOBAL
+
+ // All symbols are not made available for relocation
+ // processing by other modules.
+ RTLD_LOCAL Flag = C.RTLD_LOCAL
)
type Handle struct {
c unsafe.Pointer
}
+// Open a shared object file, returning a Handle to it, or an error.
+// If name is an empty string, then the returned handle is the global
+// symbol table for the current process; if the name contains a slash,
+// then it is interpretted as a pathname; otherwise, it is
+// interpretted in an implementation-defined manner.
func Open(name string, flags Flag) (Handle, error) {
nameC := C.CString(name)
defer C.free(unsafe.Pointer(nameC))
+ if name == "" {
+ nameC = nil
+ }
dlerror()
ptr := C.dlopen(nameC, C.int(flags))
@@ -41,8 +76,10 @@ func Open(name string, flags Flag) (Handle, error) {
return Handle{ptr}, nil
}
+// Look up a symbol, and return a pointer to it.
+//
// This returns uintptr instead of unsafe.Pointer so that code using
-// reflect cannot obtain unsafe.Pointers without importing the unsafe
+// dl cannot obtain unsafe.Pointers without importing the unsafe
// package explicitly.
func (h Handle) Sym(symbol string) (uintptr, error) {
symbolC := C.CString(symbol)
@@ -56,6 +93,9 @@ func (h Handle) Sym(symbol string) (uintptr, error) {
return uintptr(ptr), nil
}
+// Close this handle on a shared object; decrementint the reference
+// count; if the reference count drops below 0, then the object is
+// unloaded.
func (h Handle) Close() error {
dlerror()
r := C.dlclose(h.c)