xref: /llvm-project/compiler-rt/test/profile/Inputs/instrprof-dlopen-dlclose-main.c (revision 5809a32e7c2d79a9a463eb9c15cde994b42e3002)
18dfdfa65SMarco Castelluccio #include <dlfcn.h>
28dfdfa65SMarco Castelluccio #include <stdio.h>
38dfdfa65SMarco Castelluccio #include <stdlib.h>
48dfdfa65SMarco Castelluccio 
main(int argc,char * argv[])58dfdfa65SMarco Castelluccio int main(int argc, char *argv[]) {
68dfdfa65SMarco Castelluccio   dlerror();
78dfdfa65SMarco Castelluccio   void *f1_handle = dlopen("func.shared", RTLD_LAZY | RTLD_GLOBAL);
88dfdfa65SMarco Castelluccio   if (f1_handle == NULL) {
98dfdfa65SMarco Castelluccio     fprintf(stderr, "unable to open 'func.shared': %s\n", dlerror());
108dfdfa65SMarco Castelluccio     return EXIT_FAILURE;
118dfdfa65SMarco Castelluccio   }
128dfdfa65SMarco Castelluccio 
132827420aSMarco Castelluccio   void (*func)(void) = (void (*)(void))dlsym(f1_handle, "func");
142827420aSMarco Castelluccio   if (func == NULL) {
152827420aSMarco Castelluccio     fprintf(stderr, "unable to lookup symbol 'func': %s\n", dlerror());
162827420aSMarco Castelluccio     return EXIT_FAILURE;
172827420aSMarco Castelluccio   }
182827420aSMarco Castelluccio 
197222e8e3SChih-Hung Hsieh   dlerror();
208dfdfa65SMarco Castelluccio   void *f2_handle = dlopen("func2.shared", RTLD_LAZY | RTLD_GLOBAL);
218dfdfa65SMarco Castelluccio   if (f2_handle == NULL) {
228dfdfa65SMarco Castelluccio     fprintf(stderr, "unable to open 'func2.shared': %s\n", dlerror());
238dfdfa65SMarco Castelluccio     return EXIT_FAILURE;
248dfdfa65SMarco Castelluccio   }
258dfdfa65SMarco Castelluccio 
262827420aSMarco Castelluccio   void (*func2)(void) = (void (*)(void))dlsym(f2_handle, "func2");
272827420aSMarco Castelluccio   if (func2 == NULL) {
282827420aSMarco Castelluccio     fprintf(stderr, "unable to lookup symbol 'func2': %s\n", dlerror());
292827420aSMarco Castelluccio     return EXIT_FAILURE;
302827420aSMarco Castelluccio   }
312827420aSMarco Castelluccio   func2();
322827420aSMarco Castelluccio 
332827420aSMarco Castelluccio #ifdef USE_LIB3
342827420aSMarco Castelluccio   void *f3_handle = dlopen("func3.shared", RTLD_LAZY | RTLD_GLOBAL);
352827420aSMarco Castelluccio   if (f3_handle == NULL) {
362827420aSMarco Castelluccio     fprintf(stderr, "unable to open 'func3.shared': %s\n", dlerror());
372827420aSMarco Castelluccio     return EXIT_FAILURE;
382827420aSMarco Castelluccio   }
392827420aSMarco Castelluccio 
402827420aSMarco Castelluccio   void (*func3)(void) = (void (*)(void))dlsym(f3_handle, "func3");
412827420aSMarco Castelluccio   if (func3 == NULL) {
422827420aSMarco Castelluccio     fprintf(stderr, "unable to lookup symbol 'func3': %s\n", dlerror());
432827420aSMarco Castelluccio     return EXIT_FAILURE;
442827420aSMarco Castelluccio   }
452827420aSMarco Castelluccio   func3();
462827420aSMarco Castelluccio #endif
472827420aSMarco Castelluccio 
487222e8e3SChih-Hung Hsieh   dlerror();
49*5809a32eSFangrui Song   void (*gcov_reset1)() = (void (*)())dlsym(f1_handle, "__gcov_reset");
50*5809a32eSFangrui Song   if (gcov_reset1 == NULL) {
51*5809a32eSFangrui Song     fprintf(stderr, "unable to find __gcov_reset in func.shared': %s\n", dlerror());
527222e8e3SChih-Hung Hsieh     return EXIT_FAILURE;
537222e8e3SChih-Hung Hsieh   }
547222e8e3SChih-Hung Hsieh 
557222e8e3SChih-Hung Hsieh   dlerror();
56*5809a32eSFangrui Song   void (*gcov_reset2)() = (void (*)())dlsym(f2_handle, "__gcov_reset");
57*5809a32eSFangrui Song   if (gcov_reset2 == NULL) {
58*5809a32eSFangrui Song     fprintf(stderr, "unable to find __gcov_reset in func2.shared': %s\n", dlerror());
597222e8e3SChih-Hung Hsieh     return EXIT_FAILURE;
607222e8e3SChih-Hung Hsieh   }
617222e8e3SChih-Hung Hsieh 
62*5809a32eSFangrui Song   if (gcov_reset1 == gcov_reset2) {
63*5809a32eSFangrui Song     fprintf(stderr, "Same __gcov_reset found in func.shared and func2.shared\n");
647222e8e3SChih-Hung Hsieh     return EXIT_FAILURE;
657222e8e3SChih-Hung Hsieh   }
667222e8e3SChih-Hung Hsieh 
677222e8e3SChih-Hung Hsieh   dlerror();
688dfdfa65SMarco Castelluccio   if (dlclose(f2_handle) != 0) {
698dfdfa65SMarco Castelluccio     fprintf(stderr, "unable to close 'func2.shared': %s\n", dlerror());
708dfdfa65SMarco Castelluccio     return EXIT_FAILURE;
718dfdfa65SMarco Castelluccio   }
728dfdfa65SMarco Castelluccio 
732827420aSMarco Castelluccio   func();
742827420aSMarco Castelluccio 
752827420aSMarco Castelluccio   int g1 = 0;
762827420aSMarco Castelluccio   int g2 = 0;
772827420aSMarco Castelluccio   int n = 10;
782827420aSMarco Castelluccio 
792827420aSMarco Castelluccio   if (n % 5 == 0)
802827420aSMarco Castelluccio     g1++;
812827420aSMarco Castelluccio   else
822827420aSMarco Castelluccio     g2++;
832827420aSMarco Castelluccio 
848dfdfa65SMarco Castelluccio   return EXIT_SUCCESS;
858dfdfa65SMarco Castelluccio }
868dfdfa65SMarco Castelluccio 
87