dyld-43.tar.gz
[apple/dyld.git] / unit-tests / test-cases / dladdr / main.c
1 /*
2 * Copyright (c) 2005 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 int bar2()
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 (should resolve to previoius global symbol bar)
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, "bar") != 0 ) {
84 if ( strcmp(info.dli_sname, "_bar") == 0 ) {
85 XFAIL("dladdr()->dli_sname is \"%s\" instead of \"bar\"", info.dli_sname);
86 }
87 else {
88 FAIL("dladdr()->dli_sname is \"%s\" instead of \"bar\"", info.dli_sname);
89 exit(0);
90 }
91 }
92 if ( info.dli_saddr != &bar) {
93 FAIL("dladdr()->dli_saddr is not &bar");
94 exit(0);
95 }
96 if ( info.dli_fbase != _dyld_get_image_header_containing_address(&bar) ) {
97 FAIL("dladdr()->dli_fbase is not image that contains &bar");
98 exit(0);
99 }
100 }
101
102
103 int main()
104 {
105 verifybar();
106 verifyfoo();
107
108
109 PASS("dladdr");
110 return EXIT_SUCCESS;
111 }