]> git.saurik.com Git - apple/dyld.git/blob - unit-tests/test-cases/bundle-multi-link/main.c
dyld-132.13.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 NSLinkEditErrors c; int errorNumber; const char* fileName; const char* errorString;
66 NSLinkEditError(&c, &errorNumber, &fileName, &errorString);
67 FAIL("2nd NSLinkModule failed: %s", errorString);
68 return 0;
69 }
70 if ( mod == mod2 ) {
71 FAIL("2nd NSLinkModule return same function address as first");
72 return 0;
73 }
74
75 NSSymbol sym2getter = NSLookupSymbolInModule(mod2, "_getValue");
76 if ( sym2getter == NULL ) {
77 FAIL("2nd NSLookupSymbolInModule failed");
78 return 0;
79 }
80 getter func2getter = NSAddressOfSymbol(sym2getter);
81 if ( (*func2getter)() != 0 ) {
82 FAIL("_getValue() on second link returned non-zero");
83 return 0;
84 }
85
86 NSSymbol sym2 = NSLookupSymbolInModule(mod2, "_setValue");
87 if ( sym2 == NULL ) {
88 FAIL("2nd NSLookupSymbolInModule failed");
89 return 0;
90 }
91 setter func2 = NSAddressOfSymbol(sym2);
92 (*func2)(2);
93
94 //fprintf(stderr, "address of foo() = %p in bundle second load %p\n", func2, mod2);
95 if ( func == func2 ) {
96 FAIL("2nd NSAddressOfSymbol return same function address as 1st");
97 return 0;
98 }
99
100
101 NSModule mod3 = NSLinkModule(ofi, "test3.bundle", NSLINKMODULE_OPTION_NONE);
102 if ( mod3 == NULL ) {
103 FAIL("3rd NSLinkModule failed");
104 return 0;
105 }
106 if ( mod3 == mod ) {
107 FAIL("3rd NSLinkModule return same function address as 1st");
108 return 0;
109 }
110 if ( mod3 == mod2 ) {
111 FAIL("3rd NSLinkModule return same function address as 2nd");
112 return 0;
113 }
114
115 NSSymbol sym3 = NSLookupSymbolInModule(mod3, "_setValue");
116 if ( sym3 == NULL ) {
117 FAIL("3rd NSLookupSymbolInModule failed");
118 return 0;
119 }
120 setter func3 = NSAddressOfSymbol(sym3);
121 (*func3)(3);
122 //fprintf(stderr, "address of foo() = %p in bundle third load %p\n", func3, mod3);
123 if ( func3 == func ) {
124 FAIL("3rd NSAddressOfSymbol return same function address as 1st");
125 return 0;
126 }
127 if ( func3 == func2 ) {
128 FAIL("3rd NSAddressOfSymbol return same function address as 2nd");
129 return 0;
130 }
131
132 if ( !NSUnLinkModule(mod, NSUNLINKMODULE_OPTION_NONE) ) {
133 FAIL("NSUnLinkModule failed");
134 return 0;
135 }
136
137 if ( !NSUnLinkModule(mod3, NSUNLINKMODULE_OPTION_NONE) ) {
138 FAIL("3rd NSUnLinkModule failed");
139 return 0;
140 }
141
142 if ( !NSUnLinkModule(mod2, NSUNLINKMODULE_OPTION_NONE) ) {
143 FAIL("2nd NSUnLinkModule failed");
144 return 0;
145 }
146
147 // now link again after unlinking everything
148 NSModule mod4 = NSLinkModule(ofi, "test4.bundle", NSLINKMODULE_OPTION_NONE);
149 if ( mod4 == NULL ) {
150 FAIL("4th NSLinkModule failed");
151 return 0;
152 }
153
154 // check that this is really a new copy by verifying the getValue() returns zero
155 NSSymbol sym4getter = NSLookupSymbolInModule(mod4, "_getValue");
156 if ( sym4getter == NULL ) {
157 FAIL("4th NSLookupSymbolInModule failed");
158 return 0;
159 }
160 getter func4getter = NSAddressOfSymbol(sym4getter);
161 if ( (*func4getter)() != 0 ) {
162 FAIL("_getValue() on fourth link returned non-zero");
163 return 0;
164 }
165
166 if ( !NSUnLinkModule(mod4, NSUNLINKMODULE_OPTION_NONE) ) {
167 FAIL("4th NSUnLinkModule failed");
168 return 0;
169 }
170
171
172 if ( !NSDestroyObjectFileImage(ofi) ) {
173 FAIL("NSDestroyObjectFileImage failed");
174 return 0;
175 }
176
177 PASS("bundle-multi-link");
178 return 0;
179 }
180