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