xref: /llvm-project/compiler-rt/test/orc/TestCases/Darwin/arm64/trivial-dlsym.c (revision 93509b4462a74c3f96eb576f1bbaaa26328e63b2)
1 // Test that __orc_rt_macho_jit_dlsym works as expected.
2 //
3 // RUN: %clang -c -o %t.sym.o %p/Inputs/ret_self.S
4 // RUN: %clang -c -o %t.test.o %s
5 // RUN: %llvm_jitlink \
6 // RUN:   -alias Platform:_dlopen=___orc_rt_macho_jit_dlopen \
7 // RUN:   -alias Platform:_dlsym=___orc_rt_macho_jit_dlsym \
8 // RUN:   -alias Platform:_dlclose=___orc_rt_macho_jit_dlclose \
9 // RUN:   %t.test.o -lextra_sym -jd extra_sym %t.sym.o | FileCheck %s
10 
11 // CHECK: entering main
12 // CHECK-NEXT: found "ret_self" at
13 // CHECK-NEXT: address of "ret_self" is consistent
14 // CHECK-NEXT: leaving main
15 
16 int printf(const char *restrict format, ...);
17 void *dlopen(const char *path, int mode);
18 void *dlsym(void *handle, const char *symbol);
19 int dlclose(void *handle);
20 
main(int argc,char * argv[])21 int main(int argc, char *argv[]) {
22   printf("entering main\n");
23   void *H = dlopen("extra_sym", 0);
24   if (!H) {
25     printf("failed\n");
26     return -1;
27   }
28 
29   void *(*ret_self)(void) = (void *(*)(void))dlsym(H, "ret_self");
30   if (ret_self)
31     printf("found \"ret_self\" at %p\n", ret_self);
32   else
33     printf("failed to find \"ret_self\" via dlsym\n");
34 
35   printf("address of \"ret_self\" is %s\n",
36          ret_self() == ret_self ? "consistent" : "inconsistent");
37 
38   if (dlclose(H) == -1) {
39     printf("failed\n");
40     return -1;
41   }
42   printf("leaving main\n");
43   return 0;
44 }
45