dyld-732.8.tar.gz
[apple/dyld.git] / testing / test-cases / dladdr-dylib.dtest / foo.c
1
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <dlfcn.h>
7 #include <mach-o/dyld_priv.h>
8 #if __has_feature(ptrauth_calls)
9 #include <ptrauth.h>
10 #endif
11
12 extern void* __dso_handle;
13
14
15 static const void* stripPointer(const void* ptr)
16 {
17 #if __has_feature(ptrauth_calls)
18 return __builtin_ptrauth_strip(ptr, ptrauth_key_asia);
19 #else
20 return ptr;
21 #endif
22 }
23
24
25 int dylib_bar()
26 {
27 return 2;
28 }
29
30 static int dylib_foo()
31 {
32 return 3;
33 }
34
35 __attribute__((visibility("hidden"))) int dylib_hide()
36 {
37 return 4;
38 }
39
40 // checks global symbol
41 static void verifybar()
42 {
43 Dl_info info;
44 if ( dladdr(&dylib_bar, &info) == 0 ) {
45 printf("[FAIL] dladdr(&dylib_bar, xx) failed\n");
46 exit(0);
47 }
48 if ( strcmp(info.dli_sname, "dylib_bar") != 0 ) {
49 printf("[FAIL] dladdr()->dli_sname is \"%s\" instead of \"dylib_bar\"\n", info.dli_sname);
50 exit(0);
51 }
52 if ( info.dli_saddr != stripPointer(&dylib_bar) ) {
53 printf("[FAIL] dladdr()->dli_saddr is not &dylib_bar\n");
54 exit(0);
55 }
56 if ( info.dli_fbase != &__dso_handle ) {
57 printf("[FAIL] dladdr()->dli_fbase is not image that contains &dylib_bar\n");
58 exit(0);
59 }
60 }
61
62 // checks local symbol
63 static void verifyfoo()
64 {
65 Dl_info info;
66 if ( dladdr(&dylib_foo, &info) == 0 ) {
67 printf("[FAIL] dladdr(&dylib_foo, xx) failed\n");
68 exit(0);
69 }
70 if ( strcmp(info.dli_sname, "dylib_foo") != 0 ) {
71 printf("[FAIL] dladdr()->dli_sname is \"%s\" instead of \"dylib_foo\"\n", info.dli_sname);
72 exit(0);
73 }
74 if ( info.dli_saddr != stripPointer(&dylib_foo) ) {
75 printf("[FAIL] dladdr()->dli_saddr is not &dylib_foo\n");
76 exit(0);
77 }
78 if ( info.dli_fbase != &__dso_handle ) {
79 printf("[FAIL] dladdr()->dli_fbase is not image that contains &dylib_foo\n");
80 exit(0);
81 }
82 }
83
84 // checks hidden symbol
85 static void verifyhide()
86 {
87 Dl_info info;
88 if ( dladdr(&dylib_hide, &info) == 0 ) {
89 printf("[FAIL] dladdr(&dylib_hide, xx) failed\n");
90 exit(0);
91 }
92 if ( strcmp(info.dli_sname, "dylib_hide") != 0 ) {
93 printf("[FAIL] dladdr()->dli_sname is \"%s\" instead of \"dylib_hide\"\n", info.dli_sname);
94 exit(0);
95 }
96 if ( info.dli_saddr != stripPointer(&dylib_hide) ) {
97 printf("[FAIL] dladdr()->dli_saddr is not &dylib_hide\n");
98 exit(0);
99 }
100 if ( info.dli_fbase != &__dso_handle ) {
101 printf("[FAIL] dladdr()->dli_fbase is not image that contains &dylib_hide\n");
102 exit(0);
103 }
104 }
105
106 // checks DSO handle
107 static void verifyDSOHandle()
108 {
109 Dl_info info;
110 if ( dladdr(&__dso_handle, &info) == 0 ) {
111 printf("[FAIL] dladdr(&__dso_handle, xx) failed\n");
112 exit(0);
113 }
114 if ( strcmp(info.dli_sname, "__dso_handle") != 0 ) {
115 printf("[FAIL] dladdr()->dli_sname is \"%s\" instead of \"__dso_handle\"\n", info.dli_sname);
116 exit(0);
117 }
118 if ( info.dli_saddr != stripPointer(&__dso_handle) ) {
119 printf("[FAIL] dladdr()->dli_saddr is not &__dso_handle\n");
120 exit(0);
121 }
122 if ( info.dli_fbase != &__dso_handle ) {
123 printf("[FAIL] dladdr()->dli_fbase is not image that contains &__dso_handle\n");
124 exit(0);
125 }
126 }
127
128
129 void verifyDylib()
130 {
131 verifybar();
132 verifyfoo();
133 verifyhide();
134 verifyDSOHandle();
135 }
136