[ELF] Add BPSectionOrderer options (#125559)
[llvm-project.git] / compiler-rt / test / profile / Inputs / instrprof-dlopen-dlclose-main.c
blob416b90384c7d26ee60cfbd91b668bdc11cb6f230
1 #include <dlfcn.h>
2 #include <stdio.h>
3 #include <stdlib.h>
5 int 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;
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;
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;
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;
31 func2();
33 #ifdef USE_LIB3
34 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;
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;
45 func3();
46 #endif
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;
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;
62 if (gcov_reset1 == gcov_reset2) {
63 fprintf(stderr, "Same __gcov_reset found in func.shared and func2.shared\n");
64 return EXIT_FAILURE;
67 dlerror();
68 if (dlclose(f2_handle) != 0) {
69 fprintf(stderr, "unable to close 'func2.shared': %s\n", dlerror());
70 return EXIT_FAILURE;
73 func();
75 int g1 = 0;
76 int g2 = 0;
77 int n = 10;
79 if (n % 5 == 0)
80 g1++;
81 else
82 g2++;
84 return EXIT_SUCCESS;