dyld-625.13.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 #if __has_feature(ptrauth_calls)
13 #include <ptrauth.h>
14 #endif
15
16
17 int bar()
18 {
19 return 2;
20 }
21
22 static int foo()
23 {
24 return 3;
25 }
26
27 __attribute__((visibility("hidden"))) int hide()
28 {
29 return 4;
30 }
31
32 static const void *stripPointer(const void *ptr) {
33 #if __has_feature(ptrauth_calls)
34 return __builtin_ptrauth_strip(ptr, ptrauth_key_asia);
35 #else
36 return ptr;
37 #endif
38 }
39
40 // checks global symbol
41 static void verifybar()
42 {
43 Dl_info info;
44 if ( dladdr(&bar, &info) == 0 ) {
45 printf("[FAIL] dladdr(&bar, xx) failed\n");
46 exit(0);
47 }
48 if ( strcmp(info.dli_sname, "bar") != 0 ) {
49 printf("[FAIL] dladdr()->dli_sname is \"%s\" instead of \"bar\"\n", info.dli_sname);
50 exit(0);
51 }
52 if ( info.dli_saddr != stripPointer(&bar) ) {
53 printf("[FAIL] dladdr()->dli_saddr is not &bar\n");
54 exit(0);
55 }
56 if ( info.dli_fbase != dyld_image_header_containing_address(&bar) ) {
57 printf("[FAIL] dladdr()->dli_fbase is not image that contains &bar\n");
58 exit(0);
59 }
60 }
61
62 // checks local symbol
63 static void verifyfoo()
64 {
65 Dl_info info;
66 if ( dladdr(&foo, &info) == 0 ) {
67 printf("[FAIL] dladdr(&foo, xx) failed\n");
68 exit(0);
69 }
70 if ( strcmp(info.dli_sname, "foo") != 0 ) {
71 printf("[FAIL] dladdr()->dli_sname is \"%s\" instead of \"foo\"\n", info.dli_sname);
72 exit(0);
73 }
74 if ( info.dli_saddr != stripPointer(&foo) ) {
75 printf("[FAIL] dladdr()->dli_saddr is not &foo\n");
76 exit(0);
77 }
78 if ( info.dli_fbase != dyld_image_header_containing_address(&foo) ) {
79 printf("[FAIL] dladdr()->dli_fbase is not image that contains &foo\n");
80 exit(0);
81 }
82 }
83
84 // checks hidden symbol
85 static void verifyhide()
86 {
87 Dl_info info;
88 if ( dladdr(&hide, &info) == 0 ) {
89 printf("[FAIL] dladdr(&hide, xx) failed\n");
90 exit(0);
91 }
92 if ( strcmp(info.dli_sname, "hide") != 0 ) {
93 printf("[FAIL] dladdr()->dli_sname is \"%s\" instead of \"hide\"\n", info.dli_sname);
94 exit(0);
95 }
96 if ( info.dli_saddr != stripPointer(&hide) ) {
97 printf("[FAIL] dladdr()->dli_saddr is not &hide\n");
98 exit(0);
99 }
100 if ( info.dli_fbase != dyld_image_header_containing_address(&hide) ) {
101 printf("[FAIL] dladdr()->dli_fbase is not image that contains &hide\n");
102 exit(0);
103 }
104 }
105
106 // checks dylib symbol
107 static void verifymalloc()
108 {
109 Dl_info info;
110 if ( dladdr(&malloc, &info) == 0 ) {
111 printf("[FAIL] dladdr(&malloc, xx) failed\n");
112 exit(0);
113 }
114 if ( strcmp(info.dli_sname, "malloc") != 0 ) {
115 printf("[FAIL] dladdr()->dli_sname is \"%s\" instead of \"malloc\"\n", info.dli_sname);
116 exit(0);
117 }
118 if ( info.dli_saddr != stripPointer(&malloc) ) {
119 printf("[FAIL] dladdr()->dli_saddr is not &malloc\n");
120 exit(0);
121 }
122 if ( info.dli_fbase != dyld_image_header_containing_address(&malloc) ) {
123 printf("[FAIL] dladdr()->dli_fbase is not image that contains &malloc\n");
124 exit(0);
125 }
126 }
127
128 // checks passing NULL for info parameter gracefully fails
129 static void verifyNULL()
130 {
131 Dl_info info;
132 if ( dladdr(&malloc, NULL) != 0 ) {
133 printf("[FAIL] dladdr(&malloc, NULL) did not fail\n");
134 exit(0);
135 }
136 if ( dladdr(NULL, NULL) != 0 ) {
137 printf("[FAIL] dladdr(NULL, NULL) did not fail\n");
138 exit(0);
139 }
140 }
141
142 int main()
143 {
144 printf("[BEGIN] dladdr-basic\n");
145 verifybar();
146 verifyhide();
147 verifyfoo();
148 verifymalloc();
149 verifyNULL();
150
151 printf("[PASS] dladdr-basic\n");
152 return 0;
153 }
154