]> Cypherpunks.ru repositories - gostls13.git/blob - misc/cgo/test/issue4029.go
misc/cgo: remove +build lines, add go:build where needed
[gostls13.git] / misc / cgo / test / issue4029.go
1 // Copyright 2012 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 //go:build !windows && !static && (!darwin || (!internal_pie && !arm64))
6
7 // Excluded in darwin internal linking PIE mode, as dynamic export is not
8 // supported.
9 // Excluded in internal linking mode on darwin/arm64, as it is always PIE.
10
11 package cgotest
12
13 /*
14 #include <stdint.h>
15 #include <dlfcn.h>
16 #cgo linux LDFLAGS: -ldl
17
18 extern uintptr_t dlopen4029(char*, int);
19 extern uintptr_t dlsym4029(uintptr_t, char*);
20 extern int dlclose4029(uintptr_t);
21
22 extern void call4029(uintptr_t arg);
23 */
24 import "C"
25
26 import (
27         "testing"
28 )
29
30 var callbacks int
31
32 //export IMPIsOpaque
33 func IMPIsOpaque() {
34         callbacks++
35 }
36
37 //export IMPInitWithFrame
38 func IMPInitWithFrame() {
39         callbacks++
40 }
41
42 //export IMPDrawRect
43 func IMPDrawRect() {
44         callbacks++
45 }
46
47 //export IMPWindowResize
48 func IMPWindowResize() {
49         callbacks++
50 }
51
52 func test4029(t *testing.T) {
53         loadThySelf(t, "IMPWindowResize")
54         loadThySelf(t, "IMPDrawRect")
55         loadThySelf(t, "IMPInitWithFrame")
56         loadThySelf(t, "IMPIsOpaque")
57         if callbacks != 4 {
58                 t.Errorf("got %d callbacks, expected 4", callbacks)
59         }
60 }
61
62 func loadThySelf(t *testing.T, symbol string) {
63         this_process := C.dlopen4029(nil, C.RTLD_NOW)
64         if this_process == 0 {
65                 t.Error("dlopen:", C.GoString(C.dlerror()))
66                 return
67         }
68         defer C.dlclose4029(this_process)
69
70         symbol_address := C.dlsym4029(this_process, C.CString(symbol))
71         if symbol_address == 0 {
72                 t.Error("dlsym:", C.GoString(C.dlerror()))
73                 return
74         }
75         t.Log(symbol, symbol_address)
76         C.call4029(symbol_address)
77 }