87 lines · c
1#include <dlfcn.h>2#include <stdio.h>3#include <stdlib.h>4 5int main(int argc, char *argv[]) {6 dlerror();7 void *f1_handle = dlopen("func.shared", RTLD_LAZY | RTLD_GLOBAL);8 if (f1_handle == NULL) {9 fprintf(stderr, "unable to open 'func.shared': %s\n", dlerror());10 return EXIT_FAILURE;11 }12 13 void (*func)(void) = (void (*)(void))dlsym(f1_handle, "func");14 if (func == NULL) {15 fprintf(stderr, "unable to lookup symbol 'func': %s\n", dlerror());16 return EXIT_FAILURE;17 }18 19 dlerror();20 void *f2_handle = dlopen("func2.shared", RTLD_LAZY | RTLD_GLOBAL);21 if (f2_handle == NULL) {22 fprintf(stderr, "unable to open 'func2.shared': %s\n", dlerror());23 return EXIT_FAILURE;24 }25 26 void (*func2)(void) = (void (*)(void))dlsym(f2_handle, "func2");27 if (func2 == NULL) {28 fprintf(stderr, "unable to lookup symbol 'func2': %s\n", dlerror());29 return EXIT_FAILURE;30 }31 func2();32 33#ifdef USE_LIB334 void *f3_handle = dlopen("func3.shared", RTLD_LAZY | RTLD_GLOBAL);35 if (f3_handle == NULL) {36 fprintf(stderr, "unable to open 'func3.shared': %s\n", dlerror());37 return EXIT_FAILURE;38 }39 40 void (*func3)(void) = (void (*)(void))dlsym(f3_handle, "func3");41 if (func3 == NULL) {42 fprintf(stderr, "unable to lookup symbol 'func3': %s\n", dlerror());43 return EXIT_FAILURE;44 }45 func3();46#endif47 48 dlerror();49 void (*gcov_reset1)() = (void (*)())dlsym(f1_handle, "__gcov_reset");50 if (gcov_reset1 == NULL) {51 fprintf(stderr, "unable to find __gcov_reset in func.shared': %s\n", dlerror());52 return EXIT_FAILURE;53 }54 55 dlerror();56 void (*gcov_reset2)() = (void (*)())dlsym(f2_handle, "__gcov_reset");57 if (gcov_reset2 == NULL) {58 fprintf(stderr, "unable to find __gcov_reset in func2.shared': %s\n", dlerror());59 return EXIT_FAILURE;60 }61 62 if (gcov_reset1 == gcov_reset2) {63 fprintf(stderr, "Same __gcov_reset found in func.shared and func2.shared\n");64 return EXIT_FAILURE;65 }66 67 dlerror();68 if (dlclose(f2_handle) != 0) {69 fprintf(stderr, "unable to close 'func2.shared': %s\n", dlerror());70 return EXIT_FAILURE;71 }72 73 func();74 75 int g1 = 0;76 int g2 = 0;77 int n = 10;78 79 if (n % 5 == 0)80 g1++;81 else82 g2++;83 84 return EXIT_SUCCESS;85}86 87