dlstatic.c 784 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #include <fcntl.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <dlfcn.h>
  5. #include <stdint.h>
  6. #define LIBNAME "libstatic.so"
  7. int load_and_test(void)
  8. {
  9. void *handle;
  10. int (*mystatic)(void);
  11. handle = dlopen(LIBNAME, RTLD_LAZY);
  12. if (!handle) {
  13. fprintf(stderr, "Could not open ./%s: %s\n", LIBNAME, dlerror());
  14. return 1;
  15. }
  16. mystatic = dlsym(handle, "static_test");
  17. if (mystatic == NULL) {
  18. fprintf(stderr, "Could not locate symbol 'static_test': %s\n", dlerror());
  19. return 1;
  20. }
  21. if (!mystatic()) {
  22. fprintf(stderr, "mystatic() failed: static vars were not setup properly\n");
  23. return 1;
  24. }
  25. dlclose(handle);
  26. return 0;
  27. }
  28. int main(int argc, char **argv)
  29. {
  30. int count = 5;
  31. while (count-- > 0)
  32. if (load_and_test())
  33. return EXIT_FAILURE;
  34. return EXIT_SUCCESS;
  35. }