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