Skip to content
This repository was archived by the owner on Jul 30, 2022. It is now read-only.

Commit 043e662

Browse files
use syscall.syscallX so that calls switch to the g0 stack
1 parent 992aba7 commit 043e662

15 files changed

+60
-215
lines changed

dl.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package dl
33
import (
44
"errors"
55
"runtime"
6+
"unsafe"
67
)
78

89
var (
@@ -133,8 +134,7 @@ func Open(path string, mode int) (*Dylib, error) {
133134

134135
runtime.LockOSThread()
135136
defer runtime.UnlockOSThread()
136-
137-
handle := dlopen(p, mode)
137+
handle, _, _ := syscall_syscallX(dlopenABI0, uintptr(unsafe.Pointer(p)), uintptr(mode), 0)
138138
if handle <= 0 {
139139
return nil, lastError()
140140
}
@@ -159,7 +159,8 @@ func (d *Dylib) Lookup(name string) (uintptr, error) {
159159
runtime.LockOSThread()
160160
defer runtime.UnlockOSThread()
161161

162-
ret := dlsym(uintptr(d.Handle), p)
162+
ret, _, _ := syscall_syscallX(dlsymABI0, uintptr(d.Handle), uintptr(unsafe.Pointer(p)), 0)
163+
runtime.KeepAlive(p)
163164
// We must check dlerrgl.go
164165
//gl.sor because symbol could be NULL.
165166
if err = lastError(); err != nil {
@@ -188,7 +189,7 @@ func (d *Dylib) Close() (err error) {
188189
runtime.LockOSThread()
189190
defer runtime.UnlockOSThread()
190191

191-
ret := dlclose(uintptr(d.Handle))
192+
ret, _, _ := syscall_syscallX(dlcloseABI0, uintptr(d.Handle), 0, 0)
192193
if ret != 0 {
193194
err = lastError()
194195
}
@@ -206,7 +207,7 @@ func (d *Dylib) Close() (err error) {
206207
// See dlerror(3).
207208
//
208209
func lastError() error {
209-
ret := dlerror()
210+
ret, _, _ := syscall_syscallX(dlerrorABI0, 0, 0, 0)
210211
if ret != 0 {
211212
s := gostring(ret)
212213
// TODO export error vars by known string suffixes.

dl_darwin_amd64_test.s

Lines changed: 0 additions & 10 deletions
This file was deleted.

dl_darwin_arm64_test.s

Lines changed: 0 additions & 10 deletions
This file was deleted.

dl_test.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"fmt"
55
"runtime"
66
"testing"
7-
"unsafe"
87
)
98

109
func getLibc() string {
@@ -18,17 +17,14 @@ func getLibc() string {
1817
}
1918
}
2019

21-
var _malloc uintptr // pointer to malloc function
22-
23-
func libc_malloc(uintptr) unsafe.Pointer
24-
2520
func TestDL(t *testing.T) {
2621
var err error
2722
libc, err := Open(getLibc(), ScopeGlobal)
2823
if err != nil {
2924
t.Fatal(err)
3025
return
3126
}
27+
var _malloc uintptr
3228
_malloc, err = libc.Lookup("malloc")
3329
if err != nil {
3430
t.Fatal(err)
@@ -38,7 +34,7 @@ func TestDL(t *testing.T) {
3834
t.Failed()
3935
}
4036
const MallocSize = 5
41-
if libc_malloc(MallocSize) == nil {
37+
if ret, _, _ := syscall_syscallX(_malloc, MallocSize, 0, 0); ret == 0 {
4238
t.Failed()
4339
}
4440
_, err = libc.Lookup("UnknownFunctionName")

libc.go

Lines changed: 0 additions & 18 deletions
This file was deleted.
File renamed without changes.

libc_darwin_amd64.s

Lines changed: 0 additions & 40 deletions
This file was deleted.

libc_darwin_arm64.s

Lines changed: 0 additions & 40 deletions
This file was deleted.
File renamed without changes.

libc_ios_arm64.s

Lines changed: 0 additions & 40 deletions
This file was deleted.

libc_linux_amd64.s

Lines changed: 0 additions & 40 deletions
This file was deleted.

libc_so_ios_arm64.go

Lines changed: 0 additions & 6 deletions
This file was deleted.

runtime.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package dl
22

33
import (
4+
"reflect"
45
"strings"
56
"syscall"
67
"unsafe"
78
)
89

10+
//go:linkname syscall_syscallX syscall.syscallX
11+
func syscall_syscallX(fn, a1, a2, a3 uintptr) (r1, r2, err uintptr) // from runtime/sys_darwin_64.go
12+
913
func cstring(s string) (*byte, error) {
1014
if strings.IndexByte(s, 0) != -1 {
1115
return nil, syscall.EINVAL
@@ -23,3 +27,7 @@ func gostring(p uintptr) (ret string) {
2327
}
2428
return
2529
}
30+
31+
func funcPC(f interface{}) uintptr {
32+
return reflect.ValueOf(f).Pointer()
33+
}

stubs.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package dl
2+
3+
var dlopenABI0 uintptr
4+
var dlsymABI0 uintptr
5+
var dlerrorABI0 uintptr
6+
var dlcloseABI0 uintptr
7+
8+
func dlopen(path *byte, mode int) (ret uintptr)
9+
10+
func dlerror() (ret uintptr)
11+
12+
func dlclose(handle uintptr) (ret int)
13+
14+
func dlsym(handle uintptr, symbol *byte) (ret uintptr)

stubs.s

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include "textflag.h"
2+
3+
//func dlopen(path *byte, mode int) (ret uintptr)
4+
GLOBL ·dlopenABI0(SB), NOPTR|RODATA, $8
5+
DATA ·dlopenABI0(SB)/8, $·dlopen(SB)
6+
TEXT ·dlopen(SB), NOSPLIT, $0-0
7+
JMP _dlopen(SB)
8+
RET
9+
10+
//func dlerror() (ret uintptr)
11+
GLOBL ·dlerrorABI0(SB), NOPTR|RODATA, $8
12+
DATA ·dlerrorABI0(SB)/8, $·dlerror(SB)
13+
TEXT ·dlerror(SB), NOSPLIT, $0-0
14+
JMP _dlerror(SB)
15+
RET
16+
17+
//func dlclose(handle uintptr) (ret int)
18+
GLOBL ·dlcloseABI0(SB), NOPTR|RODATA, $8
19+
DATA ·dlcloseABI0(SB)/8, $·dlclose(SB)
20+
TEXT ·dlclose(SB), NOSPLIT, $0-0
21+
JMP _dlclose(SB)
22+
RET
23+
24+
//func dlsym(handle uintptr, symbol *byte) (ret uintptr)
25+
GLOBL ·dlsymABI0(SB), NOPTR|RODATA, $8
26+
DATA ·dlsymABI0(SB)/8, $·dlsym(SB)
27+
TEXT ·dlsym(SB), NOSPLIT, $0-0
28+
JMP _dlsym(SB)
29+
RET
30+

0 commit comments

Comments
 (0)