Source file 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  // +build !windows,!static
     6  // +build !darwin !internal_pie,!arm64
     7  
     8  // Excluded in darwin internal linking PIE mode, as dynamic export is not
     9  // supported.
    10  // Excluded in internal linking mode on darwin/arm64, as it is always PIE.
    11  
    12  package cgotest
    13  
    14  /*
    15  #include <stdint.h>
    16  #include <dlfcn.h>
    17  #cgo linux LDFLAGS: -ldl
    18  
    19  extern uintptr_t dlopen4029(char*, int);
    20  extern uintptr_t dlsym4029(uintptr_t, char*);
    21  extern int dlclose4029(uintptr_t);
    22  
    23  extern void call4029(uintptr_t arg);
    24  */
    25  import "C"
    26  
    27  import (
    28  	"testing"
    29  )
    30  
    31  var callbacks int
    32  
    33  //export IMPIsOpaque
    34  func IMPIsOpaque() {
    35  	callbacks++
    36  }
    37  
    38  //export IMPInitWithFrame
    39  func IMPInitWithFrame() {
    40  	callbacks++
    41  }
    42  
    43  //export IMPDrawRect
    44  func IMPDrawRect() {
    45  	callbacks++
    46  }
    47  
    48  //export IMPWindowResize
    49  func IMPWindowResize() {
    50  	callbacks++
    51  }
    52  
    53  func test4029(t *testing.T) {
    54  	loadThySelf(t, "IMPWindowResize")
    55  	loadThySelf(t, "IMPDrawRect")
    56  	loadThySelf(t, "IMPInitWithFrame")
    57  	loadThySelf(t, "IMPIsOpaque")
    58  	if callbacks != 4 {
    59  		t.Errorf("got %d callbacks, expected 4", callbacks)
    60  	}
    61  }
    62  
    63  func loadThySelf(t *testing.T, symbol string) {
    64  	this_process := C.dlopen4029(nil, C.RTLD_NOW)
    65  	if this_process == 0 {
    66  		t.Error("dlopen:", C.GoString(C.dlerror()))
    67  		return
    68  	}
    69  	defer C.dlclose4029(this_process)
    70  
    71  	symbol_address := C.dlsym4029(this_process, C.CString(symbol))
    72  	if symbol_address == 0 {
    73  		t.Error("dlsym:", C.GoString(C.dlerror()))
    74  		return
    75  	}
    76  	t.Log(symbol, symbol_address)
    77  	C.call4029(symbol_address)
    78  }
    79  

View as plain text