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