detached 11c42be

reland: Fix issue 189 (#197)

Use the standard Cgo import mechanism when CGO_ENABLED=1. This fixes an issue where sometimes the external linker would fail to add the dlfcn symbols causing a linker error.

Closes #189
commit
11c42be97b82c25f97b7c45c230f2a670c2fc91f
author
TotallyGamerJet · · 2024-01-17 15:43 UTC
committer
Hajime Hoshi · · 2024-01-18 03:34 UTC
parent
603bc82
signature
unsigned
13 files · +71 -26
M cgo.go +5 -1
@@ -12,4 +12,8 @@ package purego
1212 // which will import this package automatically. Normally this isn't an issue since it
1313 // usually isn't possible to call into C without using that import. However, with purego
1414 // it is since we don't use `import "C"`!
15-import _ "runtime/cgo"
15+import (
16+ _ "runtime/cgo"
17+
18+ _ "github.com/ebitengine/purego/internal/cgo"
19+)
@@ -12,4 +12,8 @@ package purego
12 // which will import this package automatically. Normally this isn't an issue since it12 // which will import this package automatically. Normally this isn't an issue since it
13 // usually isn't possible to call into C without using that import. However, with purego13 // usually isn't possible to call into C without using that import. However, with purego
14 // it is since we don't use `import "C"`!14 // it is since we don't use `import "C"`!
15-import _ "runtime/cgo"15+import (
16+ _ "runtime/cgo"
17+
18+ _ "github.com/ebitengine/purego/internal/cgo"
19+)
M dlfcn_darwin.go +0 -5
@@ -12,8 +12,3 @@ const (
1212 RTLD_LOCAL = 0x4 // All symbols are not made available for relocation processing by other modules.
1313 RTLD_GLOBAL = 0x8 // All symbols are available for relocation processing of other modules.
1414 )
15-
16-//go:cgo_import_dynamic purego_dlopen dlopen "/usr/lib/libSystem.B.dylib"
17-//go:cgo_import_dynamic purego_dlsym dlsym "/usr/lib/libSystem.B.dylib"
18-//go:cgo_import_dynamic purego_dlerror dlerror "/usr/lib/libSystem.B.dylib"
19-//go:cgo_import_dynamic purego_dlclose dlclose "/usr/lib/libSystem.B.dylib"
@@ -12,8 +12,3 @@ const (
12 RTLD_LOCAL = 0x4 // All symbols are not made available for relocation processing by other modules.12 RTLD_LOCAL = 0x4 // All symbols are not made available for relocation processing by other modules.
13 RTLD_GLOBAL = 0x8 // All symbols are available for relocation processing of other modules.13 RTLD_GLOBAL = 0x8 // All symbols are available for relocation processing of other modules.
14 )14 )
15-
16-//go:cgo_import_dynamic purego_dlopen dlopen "/usr/lib/libSystem.B.dylib"
17-//go:cgo_import_dynamic purego_dlsym dlsym "/usr/lib/libSystem.B.dylib"
18-//go:cgo_import_dynamic purego_dlerror dlerror "/usr/lib/libSystem.B.dylib"
19-//go:cgo_import_dynamic purego_dlclose dlclose "/usr/lib/libSystem.B.dylib"
M dlfcn_freebsd.go +0 -5
@@ -11,8 +11,3 @@ const (
1111 RTLD_LOCAL = 0x00000 // All symbols are not made available for relocation processing by other modules.
1212 RTLD_GLOBAL = 0x00100 // All symbols are available for relocation processing of other modules.
1313 )
14-
15-//go:cgo_import_dynamic purego_dlopen dlopen "libc.so.7"
16-//go:cgo_import_dynamic purego_dlsym dlsym "libc.so.7"
17-//go:cgo_import_dynamic purego_dlerror dlerror "libc.so.7"
18-//go:cgo_import_dynamic purego_dlclose dlclose "libc.so.7"
@@ -11,8 +11,3 @@ const (
11 RTLD_LOCAL = 0x00000 // All symbols are not made available for relocation processing by other modules.11 RTLD_LOCAL = 0x00000 // All symbols are not made available for relocation processing by other modules.
12 RTLD_GLOBAL = 0x00100 // All symbols are available for relocation processing of other modules.12 RTLD_GLOBAL = 0x00100 // All symbols are available for relocation processing of other modules.
13 )13 )
14-
15-//go:cgo_import_dynamic purego_dlopen dlopen "libc.so.7"
16-//go:cgo_import_dynamic purego_dlsym dlsym "libc.so.7"
17-//go:cgo_import_dynamic purego_dlerror dlerror "libc.so.7"
18-//go:cgo_import_dynamic purego_dlclose dlclose "libc.so.7"
A dlfcn_nocgo_darwin.go +11 -0
@@ -0,0 +1,11 @@
1+// SPDX-License-Identifier: Apache-2.0
2+// SPDX-FileCopyrightText: 2024 The Ebitengine Authors
3+
4+//go:build !cgo
5+
6+package purego
7+
8+//go:cgo_import_dynamic purego_dlopen dlopen "/usr/lib/libSystem.B.dylib"
9+//go:cgo_import_dynamic purego_dlsym dlsym "/usr/lib/libSystem.B.dylib"
10+//go:cgo_import_dynamic purego_dlerror dlerror "/usr/lib/libSystem.B.dylib"
11+//go:cgo_import_dynamic purego_dlclose dlclose "/usr/lib/libSystem.B.dylib"
@@ -0,0 +1,11 @@
1+// SPDX-License-Identifier: Apache-2.0
2+// SPDX-FileCopyrightText: 2024 The Ebitengine Authors
3+
4+//go:build !cgo
5+
6+package purego
7+
8+//go:cgo_import_dynamic purego_dlopen dlopen "/usr/lib/libSystem.B.dylib"
9+//go:cgo_import_dynamic purego_dlsym dlsym "/usr/lib/libSystem.B.dylib"
10+//go:cgo_import_dynamic purego_dlerror dlerror "/usr/lib/libSystem.B.dylib"
11+//go:cgo_import_dynamic purego_dlclose dlclose "/usr/lib/libSystem.B.dylib"
A dlfcn_nocgo_freebsd.go +11 -0
@@ -0,0 +1,11 @@
1+// SPDX-License-Identifier: Apache-2.0
2+// SPDX-FileCopyrightText: 2022 The Ebitengine Authors
3+
4+//go:build !cgo
5+
6+package purego
7+
8+//go:cgo_import_dynamic purego_dlopen dlopen "libc.so.7"
9+//go:cgo_import_dynamic purego_dlsym dlsym "libc.so.7"
10+//go:cgo_import_dynamic purego_dlerror dlerror "libc.so.7"
11+//go:cgo_import_dynamic purego_dlclose dlclose "libc.so.7"
@@ -0,0 +1,11 @@
1+// SPDX-License-Identifier: Apache-2.0
2+// SPDX-FileCopyrightText: 2022 The Ebitengine Authors
3+
4+//go:build !cgo
5+
6+package purego
7+
8+//go:cgo_import_dynamic purego_dlopen dlopen "libc.so.7"
9+//go:cgo_import_dynamic purego_dlsym dlsym "libc.so.7"
10+//go:cgo_import_dynamic purego_dlerror dlerror "libc.so.7"
11+//go:cgo_import_dynamic purego_dlclose dlclose "libc.so.7"
M dlfcn_stubs.s +1 -1
@@ -1,7 +1,7 @@
11 // SPDX-License-Identifier: Apache-2.0
22 // SPDX-FileCopyrightText: 2022 The Ebitengine Authors
33
4-//go:build darwin || freebsd || (linux && !cgo)
4+//go:build !cgo && (darwin || freebsd || linux)
55
66 #include "textflag.h"
77
@@ -1,7 +1,7 @@
1 // SPDX-License-Identifier: Apache-2.01 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: 2022 The Ebitengine Authors2 // SPDX-FileCopyrightText: 2022 The Ebitengine Authors
3 3
4-//go:build darwin || freebsd || (linux && !cgo)4+//go:build !cgo && (darwin || freebsd || linux)
5 5
6 #include "textflag.h"6 #include "textflag.h"
7 7
A internal/buildtest/main.go +16 -0
@@ -0,0 +1,16 @@
1+// SPDX-License-Identifier: Apache-2.0
2+// SPDX-FileCopyrightText: 2024 The Ebitengine Authors
3+
4+//go:build !windows
5+
6+package main
7+
8+import (
9+ _ "github.com/ebitengine/purego"
10+)
11+
12+import "C"
13+
14+// This file tests that build Cgo and purego at the same time succeeds to build (#189).
15+func main() {
16+}
@@ -0,0 +1,16 @@
1+// SPDX-License-Identifier: Apache-2.0
2+// SPDX-FileCopyrightText: 2024 The Ebitengine Authors
3+
4+//go:build !windows
5+
6+package main
7+
8+import (
9+ _ "github.com/ebitengine/purego"
10+)
11+
12+import "C"
13+
14+// This file tests that build Cgo and purego at the same time succeeds to build (#189).
15+func main() {
16+}
A internal/cgo/dlfcn_cgo_unix.go +22 -0
@@ -0,0 +1,22 @@
1+// SPDX-License-Identifier: Apache-2.0
2+// SPDX-FileCopyrightText: 2024 The Ebitengine Authors
3+
4+//go:build darwin || freebsd || linux
5+
6+package cgo
7+
8+/*
9+ #cgo LDFLAGS: -ldl
10+
11+#include <dlfcn.h>
12+*/
13+import "C"
14+
15+// all that is needed is to assign each dl function because then its
16+// symbol will then be made available to the linker and linked to inside dlfcn.go
17+var (
18+ _ = C.dlopen
19+ _ = C.dlsym
20+ _ = C.dlerror
21+ _ = C.dlclose
22+)
@@ -0,0 +1,22 @@
1+// SPDX-License-Identifier: Apache-2.0
2+// SPDX-FileCopyrightText: 2024 The Ebitengine Authors
3+
4+//go:build darwin || freebsd || linux
5+
6+package cgo
7+
8+/*
9+ #cgo LDFLAGS: -ldl
10+
11+#include <dlfcn.h>
12+*/
13+import "C"
14+
15+// all that is needed is to assign each dl function because then its
16+// symbol will then be made available to the linker and linked to inside dlfcn.go
17+var (
18+ _ = C.dlopen
19+ _ = C.dlsym
20+ _ = C.dlerror
21+ _ = C.dlclose
22+)
M internal/cgo/syscall_cgo_unix.go +1 -10
@@ -1,7 +1,7 @@
11 // SPDX-License-Identifier: Apache-2.0
22 // SPDX-FileCopyrightText: 2022 The Ebitengine Authors
33
4-//go:build freebsd || linux
4+//go:build freebsd || (linux && !(arm64 || amd64))
55
66 package cgo
77
@@ -39,15 +39,6 @@ import "unsafe"
3939 // assign purego.syscall9XABI0 to the C version of this function.
4040 var Syscall9XABI0 = unsafe.Pointer(C.syscall9)
4141
42-// all that is needed is to assign each dl function because then its
43-// symbol will then be made available to the linker and linked to inside dlfcn.go
44-var (
45- _ = C.dlopen
46- _ = C.dlsym
47- _ = C.dlerror
48- _ = C.dlclose
49-)
50-
5142 //go:nosplit
5243 func Syscall9X(fn, a1, a2, a3, a4, a5, a6, a7, a8, a9 uintptr) (r1, r2, err uintptr) {
5344 args := C.syscall9Args{C.uintptr_t(fn), C.uintptr_t(a1), C.uintptr_t(a2), C.uintptr_t(a3),
@@ -1,7 +1,7 @@
1 // SPDX-License-Identifier: Apache-2.01 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: 2022 The Ebitengine Authors2 // SPDX-FileCopyrightText: 2022 The Ebitengine Authors
3 3
4-//go:build freebsd || linux4+//go:build freebsd || (linux && !(arm64 || amd64))
5 5
6 package cgo6 package cgo
7 7
@@ -39,15 +39,6 @@ import "unsafe"
39 // assign purego.syscall9XABI0 to the C version of this function.39 // assign purego.syscall9XABI0 to the C version of this function.
40 var Syscall9XABI0 = unsafe.Pointer(C.syscall9)40 var Syscall9XABI0 = unsafe.Pointer(C.syscall9)
41 41
42-// all that is needed is to assign each dl function because then its
43-// symbol will then be made available to the linker and linked to inside dlfcn.go
44-var (
45- _ = C.dlopen
46- _ = C.dlsym
47- _ = C.dlerror
48- _ = C.dlclose
49-)
50-
51 //go:nosplit42 //go:nosplit
52 func Syscall9X(fn, a1, a2, a3, a4, a5, a6, a7, a8, a9 uintptr) (r1, r2, err uintptr) {43 func Syscall9X(fn, a1, a2, a3, a4, a5, a6, a7, a8, a9 uintptr) (r1, r2, err uintptr) {
53 args := C.syscall9Args{C.uintptr_t(fn), C.uintptr_t(a1), C.uintptr_t(a2), C.uintptr_t(a3),44 args := C.syscall9Args{C.uintptr_t(fn), C.uintptr_t(a1), C.uintptr_t(a2), C.uintptr_t(a3),
M sys_amd64.s +1 -1
@@ -1,7 +1,7 @@
11 // SPDX-License-Identifier: Apache-2.0
22 // SPDX-FileCopyrightText: 2022 The Ebitengine Authors
33
4-//go:build darwin || freebsd || (!cgo && linux)
4+//go:build darwin || freebsd || linux
55
66 #include "textflag.h"
77 #include "abi_amd64.h"
@@ -1,7 +1,7 @@
1 // SPDX-License-Identifier: Apache-2.01 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: 2022 The Ebitengine Authors2 // SPDX-FileCopyrightText: 2022 The Ebitengine Authors
3 3
4-//go:build darwin || freebsd || (!cgo && linux)4+//go:build darwin || freebsd || linux
5 5
6 #include "textflag.h"6 #include "textflag.h"
7 #include "abi_amd64.h"7 #include "abi_amd64.h"
M sys_arm64.s +1 -1
@@ -1,7 +1,7 @@
11 // SPDX-License-Identifier: Apache-2.0
22 // SPDX-FileCopyrightText: 2022 The Ebitengine Authors
33
4-//go:build darwin || freebsd || (!cgo && linux) || windows
4+//go:build darwin || freebsd || linux || windows
55
66 #include "textflag.h"
77 #include "go_asm.h"
@@ -1,7 +1,7 @@
1 // SPDX-License-Identifier: Apache-2.01 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: 2022 The Ebitengine Authors2 // SPDX-FileCopyrightText: 2022 The Ebitengine Authors
3 3
4-//go:build darwin || freebsd || (!cgo && linux) || windows4+//go:build darwin || freebsd || linux || windows
5 5
6 #include "textflag.h"6 #include "textflag.h"
7 #include "go_asm.h"7 #include "go_asm.h"
M syscall_cgo_linux.go +1 -1
@@ -1,7 +1,7 @@
11 // SPDX-License-Identifier: Apache-2.0
22 // SPDX-FileCopyrightText: 2022 The Ebitengine Authors
33
4-//go:build cgo
4+//go:build cgo && !(amd64 || arm64)
55
66 package purego
77
@@ -1,7 +1,7 @@
1 // SPDX-License-Identifier: Apache-2.01 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: 2022 The Ebitengine Authors2 // SPDX-FileCopyrightText: 2022 The Ebitengine Authors
3 3
4-//go:build cgo4+//go:build cgo && !(amd64 || arm64)
5 5
6 package purego6 package purego
7 7
M syscall_sysv.go +1 -1
@@ -1,7 +1,7 @@
11 // SPDX-License-Identifier: Apache-2.0
22 // SPDX-FileCopyrightText: 2022 The Ebitengine Authors
33
4-//go:build darwin || freebsd || (!cgo && linux && (amd64 || arm64))
4+//go:build darwin || freebsd || (linux && (amd64 || arm64))
55
66 package purego
77
@@ -1,7 +1,7 @@
1 // SPDX-License-Identifier: Apache-2.01 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: 2022 The Ebitengine Authors2 // SPDX-FileCopyrightText: 2022 The Ebitengine Authors
3 3
4-//go:build darwin || freebsd || (!cgo && linux && (amd64 || arm64))4+//go:build darwin || freebsd || (linux && (amd64 || arm64))
5 5
6 package purego6 package purego
7 7

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 1

Tags 102

↑↓ moveenter openesc close