dyld-43.tar.gz
[apple/dyld.git] / unit-tests / test-cases / bundle-multi-link / 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>
24 #include <mach-o/dyld.h>
25
26 #include "test.h" // PASS(), FAIL()
27
28
29 ///
30 /// The point of this test case is to "load" a bundle once, but link it multiple times.
31 /// Each link creats a new instantiation of the bundle (e.g. new base address and new globals).
32 ///
33 ///
34
35 typedef void (*setter)(int);
36 typedef int (*getter)(void);
37
38 int main()
39 {
40 NSObjectFileImage ofi;
41 if ( NSCreateObjectFileImageFromFile("test.bundle", &ofi) != NSObjectFileImageSuccess ) {
42 FAIL("NSCreateObjectFileImageFromFile failed");
43 return 0;
44 }
45
46 NSModule mod = NSLinkModule(ofi, "test.bundle", NSLINKMODULE_OPTION_NONE);
47 if ( mod == NULL ) {
48 FAIL("NSLinkModule failed");
49 return 0;
50 }
51
52 NSSymbol sym = NSLookupSymbolInModule(mod, "_setValue");
53 if ( sym == NULL ) {
54 FAIL("NSLookupSymbolInModule failed");
55 return 0;
56 }
57
58 setter func = NSAddressOfSymbol(sym);
59 (*func)(1);
60 //fprintf(stderr, "address of foo() = %p in bundle first load %p\n", func, mod);
61
62
63 NSModule mod2 = NSLinkModule(ofi, "test2.bundle", NSLINKMODULE_OPTION_NONE);
64 if ( mod2 == NULL ) {
65 FAIL("2nd NSLookupSymbolInModule failed");
66 return 0;
67 }
68 if ( mod == mod2 ) {
69 FAIL("2nd NSLinkModule return same function address as first");
70 return 0;
71 }
72
73 NSSymbol sym2getter = NSLookupSymbolInModule(mod2, "_getValue");
74 if ( sym2getter == NULL ) {
75 FAIL("2nd NSLookupSymbolInModule failed");
76 return 0;
77 }
78 getter func2getter = NSAddressOfSymbol(sym2getter);
79 if ( (*func2getter)() != 0 ) {
80 FAIL("_getValue() on second link returned non-zero");
81 return 0;
82 }
83
84 NSSymbol sym2 = NSLookupSymbolInModule(mod2, "_setValue");
85 if ( sym2 == NULL ) {
86 FAIL("2nd NSLookupSymbolInModule failed");
87 return 0;
88 }
89 setter func2 = NSAddressOfSymbol(sym2);
90 (*func2)(2);
91
92 //fprintf(stderr, "address of foo() = %p in bundle second load %p\n", func2, mod2);
93 if ( func == func2 ) {
94 FAIL("2nd NSAddressOfSymbol return same function address as 1st");
95 return 0;
96 }
97
98
99 NSModule mod3 = NSLinkModule(ofi, "test3.bundle", NSLINKMODULE_OPTION_NONE);
100 if ( mod3 == NULL ) {
101 FAIL("3rd NSLinkModule failed");
102 return 0;
103 }
104 if ( mod3 == mod ) {
105 FAIL("3rd NSLinkModule return same function address as 1st");
106 return 0;
107 }
108 if ( mod3 == mod2 ) {
109 FAIL("3rd NSLinkModule return same function address as 2nd");
110 return 0;
111 }
112
113 NSSymbol sym3 = NSLookupSymbolInModule(mod3, "_setValue");
114 if ( sym3 == NULL ) {
115 FAIL("3rd NSLookupSymbolInModule failed");
116 return 0;
117 }
118 setter func3 = NSAddressOfSymbol(sym3);
119 (*func3)(3);
120 //fprintf(stderr, "address of foo() = %p in bundle third load %p\n", func3, mod3);
121 if ( func3 == func ) {
122 FAIL("3rd NSAddressOfSymbol return same function address as 1st");
123 return 0;
124 }
125 if ( func3 == func2 ) {
126 FAIL("3rd NSAddressOfSymbol return same function address as 2nd");
127 return 0;
128 }
129
130 if ( !NSUnLinkModule(mod, NSUNLINKMODULE_OPTION_NONE) ) {
131 FAIL("NSUnLinkModule failed");
132 return 0;
133 }
134
135 if ( !NSUnLinkModule(mod3, NSUNLINKMODULE_OPTION_NONE) ) {
136 FAIL("3rd NSUnLinkModule failed");
137 return 0;
138 }
139
140 if ( !NSUnLinkModule(mod2, NSUNLINKMODULE_OPTION_NONE) ) {
141 FAIL("2nd NSUnLinkModule failed");
142 return 0;
143 }
144
145 // now link again after unlinking everything
146 NSModule mod4 = NSLinkModule(ofi, "test4.bundle", NSLINKMODULE_OPTION_NONE);
147 if ( mod4 == NULL ) {
148 FAIL("4th NSLinkModule failed");
149 return 0;
150 }
151
152 // check that this is really a new copy by verifying the getValue() returns zero
153 NSSymbol sym4getter = NSLookupSymbolInModule(mod2, "_getValue");
154 if ( sym4getter == NULL ) {
155 FAIL("2nd NSLookupSymbolInModule failed");
156 return 0;
157 }
158 getter func4getter = NSAddressOfSymbol(sym4getter);
159 if ( (*func4getter)() != 0 ) {
160 FAIL("_getValue() on fourth link returned non-zero");
161 return 0;
162 }
163
164 if ( !NSUnLinkModule(mod4, NSUNLINKMODULE_OPTION_NONE) ) {
165 FAIL("4th NSUnLinkModule failed");
166 return 0;
167 }
168
169
170 if ( !NSDestroyObjectFileImage(ofi) ) {
171 FAIL("NSDestroyObjectFileImage failed");
172 return 0;
173 }
174
175 PASS("bundle-multi-link");
176 return 0;
177 }
178