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