dyld-46.16.tar.gz
[apple/dyld.git] / unit-tests / test-cases / bundle-multi-load / 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 /// The point of this test is to load the same bundle file multiple times and
30 /// verify each time it is linked is a new instantiations (new globals, etc)
31 ///
32
33 int main()
34 {
35 NSObjectFileImage ofi;
36 if ( NSCreateObjectFileImageFromFile("test.bundle", &ofi) != NSObjectFileImageSuccess ) {
37 FAIL("NSCreateObjectFileImageFromFile failed");
38 return 0;
39 }
40
41 NSModule mod = NSLinkModule(ofi, "test.bundle", NSLINKMODULE_OPTION_NONE);
42 if ( mod == NULL ) {
43 FAIL("NSLinkModule failed");
44 return 0;
45 }
46
47 NSSymbol sym = NSLookupSymbolInModule(mod, "_foo");
48 if ( sym == NULL ) {
49 FAIL("NSLookupSymbolInModule failed");
50 return 0;
51 }
52
53 void* func = NSAddressOfSymbol(sym);
54 fprintf(stderr, "1st address of foo() = %p in module %p in OFI %p\n", func, mod, ofi);
55
56
57 NSObjectFileImage ofi2;
58 if ( NSCreateObjectFileImageFromFile("test.bundle", &ofi2) != NSObjectFileImageSuccess ) {
59 FAIL("2nd NSCreateObjectFileImageFromFile failed");
60 return 0;
61 }
62
63 NSModule mod2 = NSLinkModule(ofi2, "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\n");
70 return 0;
71 }
72
73 NSSymbol sym2 = NSLookupSymbolInModule(mod2, "_foo");
74 if ( sym2 == NULL ) {
75 FAIL("2nd NSLookupSymbolInModule failed\n");
76 return 0;
77 }
78
79 void* func2 = NSAddressOfSymbol(sym2);
80 fprintf(stderr, "2nd address of foo() = %p in module %p in OFI %p\n", func2, mod2, ofi2);
81 if ( func == func2 ) {
82 FAIL("2nd NSAddressOfSymbol return same function address as 1st\n");
83 return 0;
84 }
85
86
87 NSObjectFileImage ofi3;
88 if ( NSCreateObjectFileImageFromFile("test.bundle", &ofi3) != NSObjectFileImageSuccess ) {
89 FAIL("3rd NSCreateObjectFileImageFromFile failed");
90 return 0;
91 }
92 NSModule mod3 = NSLinkModule(ofi3, "test3.bundle", NSLINKMODULE_OPTION_NONE);
93 if ( mod3 == NULL ) {
94 FAIL("3rd NSLinkModule failed\n");
95 return 0;
96 }
97 if ( mod3 == mod ) {
98 FAIL("3rd NSLinkModule return same function address as 1st\n");
99 return 0;
100 }
101 if ( mod3 == mod2 ) {
102 FAIL("3rd NSLinkModule return same function address as 2nd\n");
103 return 0;
104 }
105
106 NSSymbol sym3 = NSLookupSymbolInModule(mod3, "_foo");
107 if ( sym3 == NULL ) {
108 FAIL("3rd NSLookupSymbolInModule failed\n");
109 return 0;
110 }
111 void* func3 = NSAddressOfSymbol(sym3);
112 fprintf(stderr, "3rd address of foo() = %p in module %p in OFI %p\n", func3, mod3, ofi3);
113 if ( func3 == func ) {
114 FAIL("3rd NSAddressOfSymbol return same function address as 1st\n");
115 return 0;
116 }
117 if ( func3 == func2 ) {
118 FAIL("3rd NSAddressOfSymbol return same function address as 2nd\n");
119 return 0;
120 }
121
122 if ( !NSUnLinkModule(mod, NSUNLINKMODULE_OPTION_NONE) ) {
123 FAIL("NSUnLinkModule failed");
124 return 0;
125 }
126
127 if ( !NSUnLinkModule(mod3, NSUNLINKMODULE_OPTION_NONE) ) {
128 FAIL("3rd NSUnLinkModule failed");
129 return 0;
130 }
131
132 // note, we are calling NSDestroyObjectFileImage() before NSUnLinkModule()
133 if ( !NSDestroyObjectFileImage(ofi2) ) {
134 FAIL("2nd NSDestroyObjectFileImage failed");
135 return 0;
136 }
137 if ( !NSUnLinkModule(mod2, NSUNLINKMODULE_OPTION_NONE) ) {
138 FAIL("2nd NSUnLinkModule failed");
139 return 0;
140 }
141
142 if ( !NSDestroyObjectFileImage(ofi) ) {
143 FAIL("1st NSDestroyObjectFileImage failed");
144 return 0;
145 }
146 if ( !NSDestroyObjectFileImage(ofi3) ) {
147 FAIL("3rd NSDestroyObjectFileImage failed");
148 return 0;
149 }
150
151 PASS("bundle-multi-load");
152 return 0;
153 }
154