main1.c 1.7 KB

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