]> git.saurik.com Git - apple/dyld.git/blob - testing/test-cases/dladdr-dylib.dtest/main.c
dyld-750.5.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 #include "test_support.h"
17
18 extern void* __dso_handle;
19
20 extern void verifyDylib();
21
22
23 static const void* stripPointer(const void* ptr)
24 {
25 #if __has_feature(ptrauth_calls)
26 return __builtin_ptrauth_strip(ptr, ptrauth_key_asia);
27 #else
28 return ptr;
29 #endif
30 }
31
32
33 int bar()
34 {
35 return 2;
36 }
37
38 static int foo()
39 {
40 return 3;
41 }
42
43 __attribute__((visibility("hidden"))) int hide()
44 {
45 return 4;
46 }
47
48 // checks global symbol
49 static void verifybar()
50 {
51 Dl_info info;
52 if ( dladdr(&bar, &info) == 0 ) {
53 FAIL("dladdr(&bar, xx) failed");
54 }
55 if ( strcmp(info.dli_sname, "bar") != 0 ) {
56 FAIL("dladdr()->dli_sname is \"%s\" instead of \"bar\"", info.dli_sname);
57 }
58 if ( info.dli_saddr != stripPointer(&bar) ) {
59 FAIL("dladdr()->dli_saddr is not &bar");
60 }
61 if ( info.dli_fbase != &__dso_handle ) {
62 FAIL("dladdr()->dli_fbase is not image that contains &bar");
63 }
64 }
65
66 // checks local symbol
67 static void verifyfoo()
68 {
69 Dl_info info;
70 if ( dladdr(&foo, &info) == 0 ) {
71 FAIL("dladdr(&foo, xx) failed");
72 }
73 if ( strcmp(info.dli_sname, "foo") != 0 ) {
74 FAIL("dladdr()->dli_sname is \"%s\" instead of \"foo\"", info.dli_sname);
75 }
76 if ( info.dli_saddr != stripPointer(&foo) ) {
77 FAIL("dladdr()->dli_saddr is not &foo");
78 }
79 if ( info.dli_fbase != &__dso_handle ) {
80 FAIL("dladdr()->dli_fbase is not image that contains &foo");
81 }
82 }
83
84 // checks hidden symbol
85 static void verifyhide()
86 {
87 Dl_info info;
88 if ( dladdr(&hide, &info) == 0 ) {
89 FAIL("dladdr(&hide, xx) failed");
90 }
91 if ( strcmp(info.dli_sname, "hide") != 0 ) {
92 FAIL("dladdr()->dli_sname is \"%s\" instead of \"hide\"", info.dli_sname);
93 }
94 if ( info.dli_saddr != stripPointer(&hide) ) {
95 FAIL("dladdr()->dli_saddr is not &hide");
96 }
97 if ( info.dli_fbase != &__dso_handle ) {
98 FAIL("dladdr()->dli_fbase is not image that contains &hide");
99 }
100 }
101
102 // checks dylib symbol
103 static void verifymalloc()
104 {
105 Dl_info info;
106 if ( dladdr(&malloc, &info) == 0 ) {
107 FAIL("dladdr(&malloc, xx) failed");
108 }
109 if ( strcmp(info.dli_sname, "malloc") != 0 ) {
110 FAIL("dladdr()->dli_sname is \"%s\" instead of \"malloc\"", info.dli_sname);
111 }
112 if ( info.dli_saddr != stripPointer(&malloc) ) {
113 FAIL("dladdr()->dli_saddr is not &malloc");
114 }
115 if ( info.dli_fbase != dyld_image_header_containing_address(&malloc) ) {
116 FAIL("dladdr()->dli_fbase is not image that contains &malloc");
117 }
118 }
119
120
121 int main(int argc, const char* argv[], const char* envp[], const char* apple[]) {
122 verifybar();
123 verifyhide();
124 verifyfoo();
125 verifymalloc();
126
127 verifyDylib();
128
129 PASS("Success");
130 }
131