]> git.saurik.com Git - apple/dyld.git/blob - testing/test-cases/dladdr-dylib.dtest/main.c
dyld-519.2.2.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-basic.exe
4
5 // RUN: ./dladdr-basic.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
13 extern void* __dso_handle;
14
15 extern void verifyDylib();
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 // checks global symbol
33 static void verifybar()
34 {
35 Dl_info info;
36 if ( dladdr(&bar, &info) == 0 ) {
37 printf("[FAIL] dladdr(&bar, xx) failed\n");
38 exit(0);
39 }
40 if ( strcmp(info.dli_sname, "bar") != 0 ) {
41 printf("[FAIL] dladdr()->dli_sname is \"%s\" instead of \"bar\"\n", info.dli_sname);
42 exit(0);
43 }
44 if ( info.dli_saddr != &bar) {
45 printf("[FAIL] dladdr()->dli_saddr is not &bar\n");
46 exit(0);
47 }
48 if ( info.dli_fbase != &__dso_handle ) {
49 printf("[FAIL] dladdr()->dli_fbase is not image that contains &bar\n");
50 exit(0);
51 }
52 }
53
54 // checks local symbol
55 static void verifyfoo()
56 {
57 Dl_info info;
58 if ( dladdr(&foo, &info) == 0 ) {
59 printf("[FAIL] dladdr(&foo, xx) failed\n");
60 exit(0);
61 }
62 if ( strcmp(info.dli_sname, "foo") != 0 ) {
63 printf("[FAIL] dladdr()->dli_sname is \"%s\" instead of \"foo\"\n", info.dli_sname);
64 exit(0);
65 }
66 if ( info.dli_saddr != &foo) {
67 printf("[FAIL] dladdr()->dli_saddr is not &foo\n");
68 exit(0);
69 }
70 if ( info.dli_fbase != &__dso_handle ) {
71 printf("[FAIL] dladdr()->dli_fbase is not image that contains &foo\n");
72 exit(0);
73 }
74 }
75
76 // checks hidden symbol
77 static void verifyhide()
78 {
79 Dl_info info;
80 if ( dladdr(&hide, &info) == 0 ) {
81 printf("[FAIL] dladdr(&hide, xx) failed\n");
82 exit(0);
83 }
84 if ( strcmp(info.dli_sname, "hide") != 0 ) {
85 printf("[FAIL] dladdr()->dli_sname is \"%s\" instead of \"hide\"\n", info.dli_sname);
86 exit(0);
87 }
88 if ( info.dli_saddr != &hide) {
89 printf("[FAIL] dladdr()->dli_saddr is not &hide\n");
90 exit(0);
91 }
92 if ( info.dli_fbase != &__dso_handle ) {
93 printf("[FAIL] dladdr()->dli_fbase is not image that contains &hide\n");
94 exit(0);
95 }
96 }
97
98 // checks dylib symbol
99 static void verifymalloc()
100 {
101 Dl_info info;
102 if ( dladdr(&malloc, &info) == 0 ) {
103 printf("[FAIL] dladdr(&malloc, xx) failed\n");
104 exit(0);
105 }
106 if ( strcmp(info.dli_sname, "malloc") != 0 ) {
107 printf("[FAIL] dladdr()->dli_sname is \"%s\" instead of \"malloc\"\n", info.dli_sname);
108 exit(0);
109 }
110 if ( info.dli_saddr != &malloc) {
111 printf("[FAIL] dladdr()->dli_saddr is not &malloc\n");
112 exit(0);
113 }
114 if ( info.dli_fbase != dyld_image_header_containing_address(&malloc) ) {
115 printf("[FAIL] dladdr()->dli_fbase is not image that contains &malloc\n");
116 exit(0);
117 }
118 }
119
120
121 int main()
122 {
123 printf("[BEGIN] dladdr-basic\n");
124 verifybar();
125 verifyhide();
126 verifyfoo();
127 verifymalloc();
128
129 verifyDylib();
130
131 printf("[PASS] dladdr-basic\n");
132 return 0;
133 }
134