]> git.saurik.com Git - apple/dyld.git/blob - unit-tests/test-cases/dladdr/main.c
dyld-132.13.tar.gz
[apple/dyld.git] / unit-tests / test-cases / dladdr / main.c
1 /*
2 * Copyright (c) 2005-2008 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 #include <stdio.h> // fprintf(), NULL
24 #include <stdlib.h> // exit(), EXIT_SUCCESS
25 #include <string.h>
26 #include <dlfcn.h>
27 #include <mach-o/dyld.h>
28
29 #include "test.h" // PASS(), FAIL(), XPASS(), XFAIL()
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 exit(0);
55 }
56 if ( strcmp(info.dli_sname, "bar") != 0 ) {
57 if ( strcmp(info.dli_sname, "_bar") == 0 ) {
58 XFAIL("dladdr()->dli_sname is \"%s\" instead of \"bar\"", info.dli_sname);
59 }
60 else {
61 FAIL("dladdr()->dli_sname is \"%s\" instead of \"bar\"", info.dli_sname);
62 exit(0);
63 }
64 }
65 if ( info.dli_saddr != &bar) {
66 FAIL("dladdr()->dli_saddr is not &bar");
67 exit(0);
68 }
69 if ( info.dli_fbase != _dyld_get_image_header_containing_address(&bar) ) {
70 FAIL("dladdr()->dli_fbase is not image that contains &bar");
71 exit(0);
72 }
73 }
74
75 // checks local symbol
76 static void verifyfoo()
77 {
78 Dl_info info;
79 if ( dladdr(&foo, &info) == 0 ) {
80 FAIL("dladdr(&foo, xx) failed");
81 exit(0);
82 }
83 if ( strcmp(info.dli_sname, "foo") != 0 ) {
84 FAIL("dladdr()->dli_sname is \"%s\" instead of \"foo\"", info.dli_sname);
85 exit(0);
86 }
87 if ( info.dli_saddr != &foo) {
88 FAIL("dladdr()->dli_saddr is not &foo");
89 exit(0);
90 }
91 if ( info.dli_fbase != _dyld_get_image_header_containing_address(&foo) ) {
92 FAIL("dladdr()->dli_fbase is not image that contains &foo");
93 exit(0);
94 }
95 }
96
97 // checks hidden symbol
98 static void verifyhide()
99 {
100 Dl_info info;
101 if ( dladdr(&hide, &info) == 0 ) {
102 FAIL("dladdr(&hide, xx) failed");
103 exit(0);
104 }
105 if ( strcmp(info.dli_sname, "hide") != 0 ) {
106 FAIL("dladdr()->dli_sname is \"%s\" instead of \"hide\"", info.dli_sname);
107 exit(0);
108 }
109 if ( info.dli_saddr != &hide) {
110 FAIL("dladdr()->dli_saddr is not &hide");
111 exit(0);
112 }
113 if ( info.dli_fbase != _dyld_get_image_header_containing_address(&hide) ) {
114 FAIL("dladdr()->dli_fbase is not image that contains &hide");
115 exit(0);
116 }
117 }
118
119
120 int main()
121 {
122 verifybar();
123 verifyhide();
124 verifyfoo();
125
126
127 PASS("dladdr");
128 return EXIT_SUCCESS;
129 }