]> git.saurik.com Git - apple/dyld.git/blob - testing/test-cases/dladdr-dylib.dtest/foo.c
dyld-851.27.tar.gz
[apple/dyld.git] / testing / test-cases / dladdr-dylib.dtest / foo.c
1
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <dlfcn.h>
7 #include <mach-o/dyld_priv.h>
8 #if __has_feature(ptrauth_calls)
9 #include <ptrauth.h>
10 #endif
11
12 #include "test_support.h"
13
14 extern void* __dso_handle;
15
16
17 static const void* stripPointer(const void* ptr)
18 {
19 #if __has_feature(ptrauth_calls)
20 return __builtin_ptrauth_strip(ptr, ptrauth_key_asia);
21 #else
22 return ptr;
23 #endif
24 }
25
26
27 int dylib_bar()
28 {
29 return 2;
30 }
31
32 static int dylib_foo()
33 {
34 return 3;
35 }
36
37 __attribute__((visibility("hidden"))) int dylib_hide()
38 {
39 return 4;
40 }
41
42 // checks global symbol
43 static void verifybar()
44 {
45 Dl_info info;
46 if ( dladdr(&dylib_bar, &info) == 0 ) {
47 FAIL("dladdr(&dylib_bar, xx) failed");
48 }
49 if ( strcmp(info.dli_sname, "dylib_bar") != 0 ) {
50 FAIL("dladdr()->dli_sname is \"%s\" instead of \"dylib_bar\"", info.dli_sname);
51 }
52 if ( info.dli_saddr != stripPointer(&dylib_bar) ) {
53 FAIL("dladdr()->dli_saddr is not &dylib_bar");
54 }
55 if ( info.dli_fbase != &__dso_handle ) {
56 FAIL("dladdr()->dli_fbase is not image that contains &dylib_bar");
57 }
58 }
59
60 // checks local symbol
61 static void verifyfoo()
62 {
63 Dl_info info;
64 if ( dladdr(&dylib_foo, &info) == 0 ) {
65 FAIL("dladdr(&dylib_foo, xx) failed");
66 }
67 if ( strcmp(info.dli_sname, "dylib_foo") != 0 ) {
68 FAIL("dladdr()->dli_sname is \"%s\" instead of \"dylib_foo\"", info.dli_sname);
69 }
70 if ( info.dli_saddr != stripPointer(&dylib_foo) ) {
71 FAIL("dladdr()->dli_saddr is not &dylib_foo");
72 }
73 if ( info.dli_fbase != &__dso_handle ) {
74 FAIL("dladdr()->dli_fbase is not image that contains &dylib_foo");
75 }
76 }
77
78 // checks hidden symbol
79 static void verifyhide()
80 {
81 Dl_info info;
82 if ( dladdr(&dylib_hide, &info) == 0 ) {
83 FAIL("dladdr(&dylib_hide, xx) failed");
84 }
85 if ( strcmp(info.dli_sname, "dylib_hide") != 0 ) {
86 FAIL("dladdr()->dli_sname is \"%s\" instead of \"dylib_hide\"", info.dli_sname);
87 }
88 if ( info.dli_saddr != stripPointer(&dylib_hide) ) {
89 FAIL("dladdr()->dli_saddr is not &dylib_hide");
90 }
91 if ( info.dli_fbase != &__dso_handle ) {
92 FAIL("dladdr()->dli_fbase is not image that contains &dylib_hide");
93 }
94 }
95
96 // checks DSO handle
97 static void verifyDSOHandle()
98 {
99 Dl_info info;
100 if ( dladdr(&__dso_handle, &info) == 0 ) {
101 FAIL("dladdr(&__dso_handle, xx) failed");
102 }
103 if ( strcmp(info.dli_sname, "__dso_handle") != 0 ) {
104 FAIL("dladdr()->dli_sname is \"%s\" instead of \"__dso_handle\"", info.dli_sname);
105 }
106 if ( info.dli_saddr != stripPointer(&__dso_handle) ) {
107 FAIL("dladdr()->dli_saddr is not &__dso_handle");
108 }
109 if ( info.dli_fbase != &__dso_handle ) {
110 FAIL("dladdr()->dli_fbase is not image that contains &__dso_handle");
111 }
112 }
113
114
115 void verifyDylib()
116 {
117 verifybar();
118 verifyfoo();
119 verifyhide();
120 verifyDSOHandle();
121 }
122