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>
26 #include "test.h" // PASS(), FAIL()
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)
35 NSObjectFileImage ofi
;
36 if ( NSCreateObjectFileImageFromFile("test.bundle", &ofi
) != NSObjectFileImageSuccess
) {
37 FAIL("NSCreateObjectFileImageFromFile failed");
41 NSModule mod
= NSLinkModule(ofi
, "test.bundle", NSLINKMODULE_OPTION_NONE
);
43 FAIL("NSLinkModule failed");
47 NSSymbol sym
= NSLookupSymbolInModule(mod
, "_foo");
49 FAIL("NSLookupSymbolInModule failed");
53 void* func
= NSAddressOfSymbol(sym
);
54 //fprintf(stderr, "1st address of foo() = %p in module %p in OFI %p\n", func, mod, ofi);
57 NSObjectFileImage ofi2
;
58 if ( NSCreateObjectFileImageFromFile("test.bundle", &ofi2
) != NSObjectFileImageSuccess
) {
59 FAIL("2nd NSCreateObjectFileImageFromFile failed");
63 NSModule mod2
= NSLinkModule(ofi2
, "test2.bundle", NSLINKMODULE_OPTION_NONE
);
65 FAIL("2nd NSLookupSymbolInModule failed");
69 FAIL("2nd NSLinkModule return same function address as first\n");
73 NSSymbol sym2
= NSLookupSymbolInModule(mod2
, "_foo");
75 FAIL("2nd NSLookupSymbolInModule failed\n");
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");
87 NSObjectFileImage ofi3
;
88 if ( NSCreateObjectFileImageFromFile("test.bundle", &ofi3
) != NSObjectFileImageSuccess
) {
89 FAIL("3rd NSCreateObjectFileImageFromFile failed");
92 NSModule mod3
= NSLinkModule(ofi3
, "test3.bundle", NSLINKMODULE_OPTION_NONE
);
94 FAIL("3rd NSLinkModule failed\n");
98 FAIL("3rd NSLinkModule return same function address as 1st\n");
101 if ( mod3
== mod2
) {
102 FAIL("3rd NSLinkModule return same function address as 2nd\n");
106 NSSymbol sym3
= NSLookupSymbolInModule(mod3
, "_foo");
107 if ( sym3
== NULL
) {
108 FAIL("3rd NSLookupSymbolInModule failed\n");
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");
117 if ( func3
== func2
) {
118 FAIL("3rd NSAddressOfSymbol return same function address as 2nd\n");
122 if ( !NSUnLinkModule(mod
, NSUNLINKMODULE_OPTION_NONE
) ) {
123 FAIL("NSUnLinkModule failed");
127 if ( !NSUnLinkModule(mod3
, NSUNLINKMODULE_OPTION_NONE
) ) {
128 FAIL("3rd NSUnLinkModule failed");
132 // note, we are calling NSDestroyObjectFileImage() before NSUnLinkModule()
133 if ( !NSDestroyObjectFileImage(ofi2
) ) {
134 FAIL("2nd NSDestroyObjectFileImage failed");
137 if ( !NSUnLinkModule(mod2
, NSUNLINKMODULE_OPTION_NONE
) ) {
138 FAIL("2nd NSUnLinkModule failed");
142 if ( !NSDestroyObjectFileImage(ofi
) ) {
143 FAIL("1st NSDestroyObjectFileImage failed");
146 if ( !NSDestroyObjectFileImage(ofi3
) ) {
147 FAIL("3rd NSDestroyObjectFileImage failed");
151 PASS("bundle-multi-load");