2 * Copyright (c) 2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
24 #include <mach-o/dyld.h>
25 #include <Availability.h>
27 #include "test.h" // PASS(), FAIL()
30 /// The point of this test is to load the same bundle file multiple times and
31 /// verify each time it is linked is a new instantiations (new globals, etc)
36 // NSCreateObjectFileImageFromMemory is only available on Mac OS X - not iPhone OS
37 #if __MAC_OS_X_VERSION_MIN_REQUIRED
38 NSObjectFileImage ofi
;
39 if ( NSCreateObjectFileImageFromFile("test.bundle", &ofi
) != NSObjectFileImageSuccess
) {
40 FAIL("NSCreateObjectFileImageFromFile failed");
44 NSModule mod
= NSLinkModule(ofi
, "test.bundle", NSLINKMODULE_OPTION_NONE
);
46 FAIL("NSLinkModule failed");
50 NSSymbol sym
= NSLookupSymbolInModule(mod
, "_foo");
52 FAIL("NSLookupSymbolInModule failed");
56 void* func
= NSAddressOfSymbol(sym
);
57 //fprintf(stderr, "1st address of foo() = %p in module %p in OFI %p\n", func, mod, ofi);
60 NSObjectFileImage ofi2
;
61 if ( NSCreateObjectFileImageFromFile("test.bundle", &ofi2
) != NSObjectFileImageSuccess
) {
62 FAIL("2nd NSCreateObjectFileImageFromFile failed");
66 NSModule mod2
= NSLinkModule(ofi2
, "test2.bundle", NSLINKMODULE_OPTION_NONE
);
68 FAIL("2nd NSLookupSymbolInModule failed");
72 FAIL("2nd NSLinkModule return same function address as first\n");
76 NSSymbol sym2
= NSLookupSymbolInModule(mod2
, "_foo");
78 FAIL("2nd NSLookupSymbolInModule failed\n");
82 void* func2
= NSAddressOfSymbol(sym2
);
83 //fprintf(stderr, "2nd address of foo() = %p in module %p in OFI %p\n", func2, mod2, ofi2);
84 if ( func
== func2
) {
85 FAIL("2nd NSAddressOfSymbol return same function address as 1st\n");
90 NSObjectFileImage ofi3
;
91 if ( NSCreateObjectFileImageFromFile("test.bundle", &ofi3
) != NSObjectFileImageSuccess
) {
92 FAIL("3rd NSCreateObjectFileImageFromFile failed");
95 NSModule mod3
= NSLinkModule(ofi3
, "test3.bundle", NSLINKMODULE_OPTION_NONE
);
97 FAIL("3rd NSLinkModule failed\n");
101 FAIL("3rd NSLinkModule return same function address as 1st\n");
104 if ( mod3
== mod2
) {
105 FAIL("3rd NSLinkModule return same function address as 2nd\n");
109 NSSymbol sym3
= NSLookupSymbolInModule(mod3
, "_foo");
110 if ( sym3
== NULL
) {
111 FAIL("3rd NSLookupSymbolInModule failed\n");
114 void* func3
= NSAddressOfSymbol(sym3
);
115 //fprintf(stderr, "3rd address of foo() = %p in module %p in OFI %p\n", func3, mod3, ofi3);
116 if ( func3
== func
) {
117 FAIL("3rd NSAddressOfSymbol return same function address as 1st\n");
120 if ( func3
== func2
) {
121 FAIL("3rd NSAddressOfSymbol return same function address as 2nd\n");
125 if ( !NSUnLinkModule(mod
, NSUNLINKMODULE_OPTION_NONE
) ) {
126 FAIL("NSUnLinkModule failed");
130 if ( !NSUnLinkModule(mod3
, NSUNLINKMODULE_OPTION_NONE
) ) {
131 FAIL("3rd NSUnLinkModule failed");
135 // note, we are calling NSDestroyObjectFileImage() before NSUnLinkModule()
136 if ( !NSDestroyObjectFileImage(ofi2
) ) {
137 FAIL("2nd NSDestroyObjectFileImage failed");
140 if ( !NSUnLinkModule(mod2
, NSUNLINKMODULE_OPTION_NONE
) ) {
141 FAIL("2nd NSUnLinkModule failed");
145 if ( !NSDestroyObjectFileImage(ofi
) ) {
146 FAIL("1st NSDestroyObjectFileImage failed");
149 if ( !NSDestroyObjectFileImage(ofi3
) ) {
150 FAIL("3rd NSDestroyObjectFileImage failed");
154 PASS("bundle-multi-load");