dyld-732.8.tar.gz
[apple/dyld.git] / testing / test-cases / dladdr-basic.dtest / main.c
1
2 // BUILD: $CC main.c -o $BUILD_DIR/dladdr-basic.exe
3
4 // RUN: ./dladdr-basic.exe
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <dlfcn.h>
10 #include <mach-o/dyld_priv.h>
11
12 extern char** environ;
13
14 #if __has_feature(ptrauth_calls)
15 #include <ptrauth.h>
16 #endif
17
18 int mydata = 5;
19
20 int bar()
21 {
22 return 2;
23 }
24
25 static int foo()
26 {
27 return 3;
28 }
29
30 __attribute__((visibility("hidden"))) int hide()
31 {
32 return 4;
33 }
34
35 static const void *stripPointer(const void *ptr) {
36 #if __has_feature(ptrauth_calls)
37 return __builtin_ptrauth_strip(ptr, ptrauth_key_asia);
38 #else
39 return ptr;
40 #endif
41 }
42
43 // checks global symbol
44 static void verifybar()
45 {
46 Dl_info info;
47 if ( dladdr(&bar, &info) == 0 ) {
48 printf("[FAIL] dladdr(&bar, xx) failed\n");
49 exit(0);
50 }
51 if ( strcmp(info.dli_sname, "bar") != 0 ) {
52 printf("[FAIL] dladdr()->dli_sname is \"%s\" instead of \"bar\"\n", info.dli_sname);
53 exit(0);
54 }
55 if ( info.dli_saddr != stripPointer(&bar) ) {
56 printf("[FAIL] dladdr()->dli_saddr is not &bar\n");
57 exit(0);
58 }
59 if ( info.dli_fbase != dyld_image_header_containing_address(&bar) ) {
60 printf("[FAIL] dladdr()->dli_fbase is not image that contains &bar\n");
61 exit(0);
62 }
63 }
64
65 // checks local symbol
66 static void verifyfoo()
67 {
68 Dl_info info;
69 if ( dladdr(&foo, &info) == 0 ) {
70 printf("[FAIL] dladdr(&foo, xx) failed\n");
71 exit(0);
72 }
73 if ( strcmp(info.dli_sname, "foo") != 0 ) {
74 printf("[FAIL] dladdr()->dli_sname is \"%s\" instead of \"foo\"\n", info.dli_sname);
75 exit(0);
76 }
77 if ( info.dli_saddr != stripPointer(&foo) ) {
78 printf("[FAIL] dladdr()->dli_saddr is not &foo\n");
79 exit(0);
80 }
81 if ( info.dli_fbase != dyld_image_header_containing_address(&foo) ) {
82 printf("[FAIL] dladdr()->dli_fbase is not image that contains &foo\n");
83 exit(0);
84 }
85 }
86
87 // checks hidden symbol
88 static void verifyhide()
89 {
90 Dl_info info;
91 if ( dladdr(&hide, &info) == 0 ) {
92 printf("[FAIL] dladdr(&hide, xx) failed\n");
93 exit(0);
94 }
95 if ( strcmp(info.dli_sname, "hide") != 0 ) {
96 printf("[FAIL] dladdr()->dli_sname is \"%s\" instead of \"hide\"\n", info.dli_sname);
97 exit(0);
98 }
99 if ( info.dli_saddr != stripPointer(&hide) ) {
100 printf("[FAIL] dladdr()->dli_saddr is not &hide\n");
101 exit(0);
102 }
103 if ( info.dli_fbase != dyld_image_header_containing_address(&hide) ) {
104 printf("[FAIL] dladdr()->dli_fbase is not image that contains &hide\n");
105 exit(0);
106 }
107 }
108
109 // checks dylib symbol
110 static void verifymalloc()
111 {
112 Dl_info info;
113 if ( dladdr(&malloc, &info) == 0 ) {
114 printf("[FAIL] dladdr(&malloc, xx) failed\n");
115 exit(0);
116 }
117 if ( strcmp(info.dli_sname, "malloc") != 0 ) {
118 printf("[FAIL] dladdr()->dli_sname is \"%s\" instead of \"malloc\"\n", info.dli_sname);
119 exit(0);
120 }
121 if ( info.dli_saddr != stripPointer(&malloc) ) {
122 printf("[FAIL] dladdr()->dli_saddr is not &malloc\n");
123 exit(0);
124 }
125 if ( info.dli_fbase != dyld_image_header_containing_address(&malloc) ) {
126 printf("[FAIL] dladdr()->dli_fbase is not image that contains &malloc\n");
127 exit(0);
128 }
129 }
130
131 // checks dylib data symbol
132 static void verifyenviron()
133 {
134 Dl_info info;
135 if ( dladdr(&environ, &info) == 0 ) {
136 printf("[FAIL] dladdr(&environ, xx) failed\n");
137 exit(0);
138 }
139 if ( strcmp(info.dli_sname, "environ") != 0 ) {
140 printf("[FAIL] dladdr()->dli_sname is \"%s\" instead of \"environ\"\n", info.dli_sname);
141 exit(0);
142 }
143 if ( info.dli_saddr != &environ ) {
144 printf("[FAIL] dladdr()->dli_saddr is not &environ\n");
145 exit(0);
146 }
147 if ( info.dli_fbase != dyld_image_header_containing_address(&environ) ) {
148 printf("[FAIL] dladdr()->dli_fbase is not image that contains &environ\n");
149 exit(0);
150 }
151 }
152
153
154 // checks data symbol in main executable
155 static void verifymydata()
156 {
157 Dl_info info;
158 if ( dladdr(&mydata, &info) == 0 ) {
159 printf("[FAIL] dladdr(&mydata, xx) failed\n");
160 exit(0);
161 }
162 if ( strcmp(info.dli_sname, "mydata") != 0 ) {
163 printf("[FAIL] dladdr()->dli_sname is \"%s\" instead of \"mydata\"\n", info.dli_sname);
164 exit(0);
165 }
166 if ( info.dli_saddr != &mydata ) {
167 printf("[FAIL] dladdr()->dli_saddr is not &mydata\n");
168 exit(0);
169 }
170 if ( info.dli_fbase != dyld_image_header_containing_address(&mydata) ) {
171 printf("[FAIL] dladdr()->dli_fbase is not image that contains &mydata\n");
172 exit(0);
173 }
174 }
175
176
177 // checks passing NULL for info parameter gracefully fails
178 static void verifyNULL()
179 {
180 Dl_info info;
181 if ( dladdr(&malloc, NULL) != 0 ) {
182 printf("[FAIL] dladdr(&malloc, NULL) did not fail\n");
183 exit(0);
184 }
185 if ( dladdr(NULL, NULL) != 0 ) {
186 printf("[FAIL] dladdr(NULL, NULL) did not fail\n");
187 exit(0);
188 }
189 }
190
191 int main()
192 {
193 printf("[BEGIN] dladdr-basic\n");
194 verifybar();
195 verifyhide();
196 verifyfoo();
197 verifymalloc();
198 verifyenviron();
199 verifymydata();
200 verifyNULL();
201
202 printf("[PASS] dladdr-basic\n");
203 return 0;
204 }
205