Source

go-sunos / src / pkg / syscall / so_solaris.go

The port-sunos branch has multiple heads

// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package syscall

import (
	"sync"
	"sync/atomic"
	"unsafe"
)

// SOError describes reasons for shared libraries load failures.
type SOError struct {
	Err     error
	ObjName string
	Msg     string
}

func (e *SOError) Error() string { return e.Msg }

// Implemented in ../runtime/syscall_solaris.goc.
func RawSysvicall6(trap, nargs, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno)
func Sysvicall6(trap, nargs, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno)
func Sysvicall6Reader(trap, nargs, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno)
func Sysvicall6Writer(trap, nargs, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno)
func dlclose(handle uintptr) (err Errno)
func dlopen(name *uint8, mode uintptr) (handle uintptr, err Errno)
func dlsym(handle uintptr, name *uint8) (proc uintptr, err Errno)

// A SO implements access to a single shared library object.
type SO struct {
	Name   string
	Handle uintptr
}

// LoadSO loads shared library file into memory.
func LoadSO(name string) (so *SO, err error) {
	namep, err := BytePtrFromString(name)
	if err != nil {
		return nil, err
	}
	h, e := dlopen(namep, 1) // RTLD_LAZY
	if e != 0 {
		return nil, &SOError{
			Err:     e,
			ObjName: name,
			Msg:     "Failed to load " + name + ": " + e.Error(),
		}
	}
	d := &SO{
		Name:   name,
		Handle: uintptr(h),
	}
	return d, nil
}

// MustLoadSO is like LoadSO but panics if load operation fails.
func MustLoadSO(name string) *SO {
	d, e := LoadSO(name)
	if e != nil {
		panic(e)
	}
	return d
}

// FindProc searches shared library d for procedure named name and returns
// *Proc if found. It returns an error if the search fails.
func (d *SO) FindProc(name string) (proc *Proc, err error) {
	namep, err := BytePtrFromString(name)
	if err != nil {
		return nil, err
	}
	a, _ := dlsym(uintptr(d.Handle), namep)
	if a == 0 {
		return nil, &SOError{
			Err:     ENOSYS,
			ObjName: name,
			Msg:     "Failed to find " + name + " procedure in " + d.Name,
		}
	}
	p := &Proc{
		SO:   d,
		Name: name,
		addr: a,
	}
	return p, nil
}

// MustFindProc is like FindProc but panics if search fails.
func (d *SO) MustFindProc(name string) *Proc {
	p, e := d.FindProc(name)
	if e != nil {
		panic(e)
	}
	return p
}

// Release unloads shared library d from memory.
func (d *SO) Release() (err error) {
	return dlclose(d.Handle)
}

// A Proc implements access to a procedure inside a shared library.
type Proc struct {
	SO   *SO
	Name string
	addr uintptr
}

// Addr returns the address of the procedure represented by p.
// The return value can be passed to Syscall to run the procedure.
func (p *Proc) Addr() uintptr {
	return p.addr
}

// Call executes procedure p with arguments a. It will panic, if more then
// 6 arguments are supplied.
//
// The returned error is always non-nil, constructed from the result of
// GetLastError.  Callers must inspect the primary return value to decide
// whether an error occurred (according to the semantics of the specific
// function being called) before consulting the error. The error will be
// guaranteed to contain syscall.Errno.
func (p *Proc) Call(a ...uintptr) (r1, r2 uintptr, lastErr error) {
	switch len(a) {
	case 0:
		return Sysvicall6(p.Addr(), uintptr(len(a)), 0, 0, 0, 0, 0, 0)
	case 1:
		return Sysvicall6(p.Addr(), uintptr(len(a)), a[0], 0, 0, 0, 0, 0)
	case 2:
		return Sysvicall6(p.Addr(), uintptr(len(a)), a[0], a[1], 0, 0, 0, 0)
	case 3:
		return Sysvicall6(p.Addr(), uintptr(len(a)), a[0], a[1], a[2], 0, 0, 0)
	case 4:
		return Sysvicall6(p.Addr(), uintptr(len(a)), a[0], a[1], a[2], a[3], 0, 0)
	case 5:
		return Sysvicall6(p.Addr(), uintptr(len(a)), a[0], a[1], a[2], a[3], a[4], 0)
	case 6:
		return Sysvicall6(p.Addr(), uintptr(len(a)), a[0], a[1], a[2], a[3], a[4], a[5])
	default:
		panic("Call " + p.Name + " with too many arguments " + itoa(len(a)) + ".")
	}
	return
}

// A LazySO implements access to a single shared library.  It will delay
// the load of the shared library until the first call to its Handle method
// or to one of its LazyProc's Addr method.
type LazySO struct {
	mu   sync.Mutex
	so   *SO // non nil once SO is loaded
	Name string
}

// Load loads single shared file d.Name into memory. It returns an error if
// fails.  Load will not try to load SO, if it is already loaded into memory.
func (d *LazySO) Load() error {
	// Non-racy version of:
	// if d.so == nil {
	if atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&d.so))) == nil {
		d.mu.Lock()
		defer d.mu.Unlock()
		if d.so == nil {
			so, e := LoadSO(d.Name)
			if e != nil {
				return e
			}
			// Non-racy version of:
			// d.so = so
			atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(&d.so)), unsafe.Pointer(so))
		}
	}
	return nil
}

// mustLoad is like Load but panics if search fails.
func (d *LazySO) mustLoad() {
	e := d.Load()
	if e != nil {
		panic(e)
	}
}

// Handle returns d's module handle.
func (d *LazySO) Handle() uintptr {
	d.mustLoad()
	return uintptr(d.so.Handle)
}

// NewProc returns a LazyProc for accessing the named procedure in the SO d.
func (d *LazySO) NewProc(name string) *LazyProc {
	return &LazyProc{l: d, Name: name}
}

// NewLazySO creates new LazySO associated with SO file.
func NewLazySO(name string) *LazySO {
	return &LazySO{Name: name}
}

// A LazyProc implements access to a procedure inside a LazySO.
// It delays the lookup until the Addr method is called.
type LazyProc struct {
	mu   sync.Mutex
	Name string
	l    *LazySO
	proc *Proc
}

// Find searches the shared library for procedure named p.Name. It returns an
// error if search fails. Find will not search procedure, if it is already
// found and loaded into memory.
func (p *LazyProc) Find() error {
	// Non-racy version of:
	// if p.proc == nil {
	if atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&p.proc))) == nil {
		p.mu.Lock()
		defer p.mu.Unlock()
		if p.proc == nil {
			e := p.l.Load()
			if e != nil {
				return e
			}
			proc, e := p.l.so.FindProc(p.Name)
			if e != nil {
				return e
			}
			// Non-racy version of:
			// p.proc = proc
			atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(&p.proc)), unsafe.Pointer(proc))
		}
	}
	return nil
}

// mustFind is like Find but panics if search fails.
func (p *LazyProc) mustFind() {
	e := p.Find()
	if e != nil {
		panic(e)
	}
}

// Addr returns the address of the procedure represented by p.
// The return value can be passed to Syscall to run the procedure.
func (p *LazyProc) Addr() uintptr {
	p.mustFind()
	return p.proc.Addr()
}

// Call executes procedure p with arguments a. It will panic, if more then
// 6 arguments are supplied.
//
// The returned error is always non-nil, constructed from the result of
// GetLastError.  Callers must inspect the primary return value to decide
// whether an error occurred (according to the semantics of the specific
// function being called) before consulting the error. The error will be
// guaranteed to contain syscall.Errno.
func (p *LazyProc) Call(a ...uintptr) (r1, r2 uintptr, lastErr error) {
	p.mustFind()
	return p.proc.Call(a...)
}