Text file misc/cgo/testcshared/testdata/main1.c

     1  // Copyright 2015 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  #include <stdint.h>
     6  #include <stdio.h>
     7  #include <dlfcn.h>
     8  
     9  int check_int8(void* handle, const char* fname, int8_t want) {
    10    int8_t (*fn)();
    11    fn = (int8_t (*)())dlsym(handle, fname);
    12    if (!fn) {
    13      fprintf(stderr, "ERROR: missing %s: %s\n", fname, dlerror());
    14      return 1;
    15    }
    16    signed char ret = fn();
    17    if (ret != want) {
    18      fprintf(stderr, "ERROR: %s=%d, want %d\n", fname, ret, want);
    19      return 1;
    20    }
    21    return 0;
    22  }
    23  
    24  int check_int32(void* handle, const char* fname, int32_t want) {
    25    int32_t (*fn)();
    26    fn = (int32_t (*)())dlsym(handle, fname);
    27    if (!fn) {
    28      fprintf(stderr, "ERROR: missing %s: %s\n", fname, dlerror());
    29      return 1;
    30    }
    31    int32_t ret = fn();
    32    if (ret != want) {
    33      fprintf(stderr, "ERROR: %s=%d, want %d\n", fname, ret, want);
    34      return 1;
    35    }
    36    return 0;
    37  }
    38  
    39  // Tests libgo.so to export the following functions.
    40  //   int8_t DidInitRun() // returns true
    41  //   int8_t DidMainRun() // returns true
    42  //   int32_t FromPkg() // returns 1024
    43  int main(int argc, char** argv) {
    44    void* handle = dlopen(argv[1], RTLD_LAZY | RTLD_GLOBAL);
    45    if (!handle) {
    46      fprintf(stderr, "ERROR: failed to open the shared library: %s\n",
    47  		    dlerror());
    48      return 2;
    49    }
    50  
    51    int ret = 0;
    52    ret = check_int8(handle, "DidInitRun", 1);
    53    if (ret != 0) {
    54      return ret;
    55    }
    56  
    57    ret = check_int8(handle, "DidMainRun", 0);
    58    if (ret != 0) {
    59      return ret;
    60    }
    61  
    62    ret = check_int32(handle, "FromPkg", 1024);
    63    if (ret != 0) {
    64     return ret;
    65    }
    66    // test.bash looks for "PASS" to ensure this program has reached the end.
    67    printf("PASS\n");
    68    return 0;
    69  }
    70  

View as plain text