Commit fe5ef5c9 authored by Aram Hăvărneanu's avatar Aram Hăvărneanu

runtime, syscall: link Solaris binaries directly instead of using dlopen/dlsym

Before CL 8214 (use .plt instead of .got on Solaris) Solaris used a
dynamic linking scheme that didn't permit lazy binding. To speed program
startup, Go binaries only used it for a small number of symbols required
by the runtime. Other symbols were resolved on demand on first use, and
were cached for subsequent use. This required some moderately complex
code in the syscall package.

CL 8214 changed the way dynamic linking is implemented, and now lazy
binding is supported. As now all symbols are resolved lazily by the
dynamic loader, there is no need for the complex code in the syscall
package that did the same. This CL makes Go programs link directly
with the necessary shared libraries and deletes the lazy-loading code
implemented in Go.

Change-Id: Ifd7275db72de61b70647242e7056dd303b1aee9e
Reviewed-on: https://go-review.googlesource.com/9184Reviewed-by: 's avatarMinux Ma <minux@golang.org>
Reviewed-by: 's avatarIan Lance Taylor <iant@golang.org>
parent 2b90c3e8
......@@ -9,9 +9,6 @@ import "unsafe"
var (
libc_chdir,
libc_chroot,
libc_dlopen,
libc_dlclose,
libc_dlsym,
libc_execve,
libc_fcntl,
libc_forkx,
......@@ -85,48 +82,6 @@ func syscall_close(fd int32) int32 {
return int32(sysvicall1(&libc_close, uintptr(fd)))
}
func syscall_dlopen(name *byte, mode uintptr) (handle uintptr, err uintptr) {
call := libcall{
fn: uintptr(unsafe.Pointer(&libc_dlopen)),
n: 2,
args: uintptr(unsafe.Pointer(&name)),
}
entersyscallblock(0)
asmcgocall(unsafe.Pointer(&asmsysvicall6), unsafe.Pointer(&call))
exitsyscall(0)
if call.r1 == 0 {
return call.r1, call.err
}
return call.r1, 0
}
func syscall_dlclose(handle uintptr) (err uintptr) {
call := libcall{
fn: uintptr(unsafe.Pointer(&libc_dlclose)),
n: 1,
args: uintptr(unsafe.Pointer(&handle)),
}
entersyscallblock(0)
asmcgocall(unsafe.Pointer(&asmsysvicall6), unsafe.Pointer(&call))
exitsyscall(0)
return call.r1
}
func syscall_dlsym(handle uintptr, name *byte) (proc uintptr, err uintptr) {
call := libcall{
fn: uintptr(unsafe.Pointer(&libc_dlsym)),
n: 2,
args: uintptr(unsafe.Pointer(&handle)),
}
entersyscallblock(0)
asmcgocall(unsafe.Pointer(&asmsysvicall6), unsafe.Pointer(&call))
exitsyscall(0)
if call.r1 == 0 {
return call.r1, call.err
}
return call.r1, 0
}
//go:nosplit
func syscall_execve(path, argv, envp uintptr) (err uintptr) {
call := libcall{
......
......@@ -6,13 +6,21 @@
package syscall
import "unsafe"
//go:cgo_import_dynamic libc_Getpgid getpgid "libc.so"
//go:cgo_import_dynamic libc_Getpgrp getpgrp "libc.so"
//go:linkname libc_Getpgid libc_Getpgid
//go:linkname libc_Getpgrp libc_Getpgrp
var (
procGetpgid = modlibc.NewProc("getpgid")
procGetpgrp = modlibc.NewProc("getpgrp")
libc_Getpgid,
libc_Getpgrp libcFunc
)
func Getpgid(pid int) (pgid int, err error) {
r0, _, e1 := sysvicall6(procGetpgid.Addr(), 1, uintptr(pid), 0, 0, 0, 0, 0)
r0, _, e1 := sysvicall6(uintptr(unsafe.Pointer(&libc_Getpgid)), 1, uintptr(pid), 0, 0, 0, 0, 0)
pgid = int(r0)
if e1 != 0 {
err = e1
......@@ -21,7 +29,7 @@ func Getpgid(pid int) (pgid int, err error) {
}
func Getpgrp() (pgrp int) {
r0, _, _ := sysvicall6(procGetpgrp.Addr(), 0, 0, 0, 0, 0, 0, 0)
r0, _, _ := sysvicall6(uintptr(unsafe.Pointer(&libc_Getpgrp)), 0, 0, 0, 0, 0, 0, 0)
pgrp = int(r0)
return
}
......
......@@ -61,8 +61,8 @@ sub parseparam($) {
my $package = "";
my $text = "";
my $vars = "";
my $mods = "";
my $modnames = "";
my $dynimports = "";
my $linknames = "";
while(<>) {
chomp;
s/\s+/ /g;
......@@ -93,11 +93,6 @@ while(<>) {
if($modname eq "") {
$modname = "libc";
}
my $modvname = "mod$modname";
if($modnames !~ /$modname/) {
$modnames .= ".$modname";
$mods .= "\t$modvname = ${syscalldot}newLazySO(\"$modname.so\")\n";
}
# System call name.
if($sysname eq "") {
......@@ -105,14 +100,20 @@ while(<>) {
}
# System call pointer variable name.
my $sysvarname = "proc$sysname";
my $sysvarname = "libc_$sysname";
my $strconvfunc = "BytePtrFromString";
my $strconvtype = "*byte";
# Library proc address variable.
$sysname =~ y/A-Z/a-z/; # All libc functions are lowercase.
$vars .= "\t$sysvarname = $modvname.NewProc(\"$sysname\")\n";
if($vars eq "") {
$vars .= "\t$sysvarname";
} else {
$vars .= ",\n\t$sysvarname";
}
$dynimports .= "//go:cgo_import_dynamic $sysvarname $sysname \"$modname.so\"\n";
$linknames .= "//go:linkname $sysvarname $sysvarname\n";
# Go function header.
$out = join(', ', @out);
......@@ -196,7 +197,7 @@ while(<>) {
# Actual call.
my $args = join(', ', @args);
my $call = "$asm($sysvarname.Addr(), $nargs, $args)";
my $call = "$asm(uintptr(unsafe.Pointer(&$sysvarname)), $nargs, $args)";
# Assign return values.
my $body = "";
......@@ -272,9 +273,12 @@ print "import \"syscall\"\n" if $package ne "syscall";
print <<EOF;
$dynimports
$linknames
type libcFunc uintptr
var (
$mods
$vars
$vars libcFunc
)
$text
......
// 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 library load failures.
type soError struct {
Err error
ObjName string
Msg string
}
func (e *soError) Error() string { return e.Msg }
// Implemented in asm_solaris_amd64.s.
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 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, error) {
namep, err := BytePtrFromString(name)
if err != nil {
return nil, err
}
h, e := dlopen(namep, 1) // RTLD_LAZY
use(unsafe.Pointer(namep))
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, error) {
namep, err := BytePtrFromString(name)
if err != nil {
return nil, err
}
a, _ := dlsym(uintptr(d.Handle), namep)
use(unsafe.Pointer(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...)
}
......@@ -14,6 +14,10 @@ package syscall
import "unsafe"
// Implemented in asm_solaris_amd64.s.
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)
type SockaddrDatalink struct {
Family uint16
Index uint16
......@@ -283,7 +287,7 @@ func UtimesNano(path string, ts []Timespec) (err error) {
// FcntlFlock performs a fcntl syscall for the F_GETLK, F_SETLK or F_SETLKW command.
func FcntlFlock(fd uintptr, cmd int, lk *Flock_t) error {
_, _, e1 := sysvicall6(procfcntl.Addr(), 3, uintptr(fd), uintptr(cmd), uintptr(unsafe.Pointer(lk)), 0, 0, 0)
_, _, e1 := sysvicall6(uintptr(unsafe.Pointer(&libc_fcntl)), 3, uintptr(fd), uintptr(cmd), uintptr(unsafe.Pointer(lk)), 0, 0, 0)
if e1 != 0 {
return e1
}
......@@ -505,7 +509,7 @@ func SendmsgN(fd int, p, oob []byte, to Sockaddr, flags int) (n int, err error)
//sys recvmsg(s int, msg *Msghdr, flags int) (n int, err error) = libsocket.recvmsg
func readlen(fd int, buf *byte, nbuf int) (n int, err error) {
r0, _, e1 := sysvicall6(procread.Addr(), 3, uintptr(fd), uintptr(unsafe.Pointer(buf)), uintptr(nbuf), 0, 0, 0)
r0, _, e1 := sysvicall6(uintptr(unsafe.Pointer(&libc_read)), 3, uintptr(fd), uintptr(unsafe.Pointer(buf)), uintptr(nbuf), 0, 0, 0)
n = int(r0)
if e1 != 0 {
err = e1
......@@ -514,7 +518,7 @@ func readlen(fd int, buf *byte, nbuf int) (n int, err error) {
}
func writelen(fd int, buf *byte, nbuf int) (n int, err error) {
r0, _, e1 := sysvicall6(procwrite.Addr(), 3, uintptr(fd), uintptr(unsafe.Pointer(buf)), uintptr(nbuf), 0, 0, 0)
r0, _, e1 := sysvicall6(uintptr(unsafe.Pointer(&libc_write)), 3, uintptr(fd), uintptr(unsafe.Pointer(buf)), uintptr(nbuf), 0, 0, 0)
n = int(r0)
if e1 != 0 {
err = e1
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment