Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | /* * Copyright (c) 2005 Apple Computer, Inc. All rights reserved. * * @APPLE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this * file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_LICENSE_HEADER_END@ */ #include <stdio.h> #include <mach-o/dyld.h> #include "test.h" // PASS(), FAIL() /// /// The point of this test case is to "load" a bundle once, but link it multiple times. /// Each link creats a new instantiation of the bundle (e.g. new base address and new globals). /// /// typedef void (*setter)(int); typedef int (*getter)(void); int main() { NSObjectFileImage ofi; if ( NSCreateObjectFileImageFromFile("test.bundle", &ofi) != NSObjectFileImageSuccess ) { FAIL("NSCreateObjectFileImageFromFile failed"); return 0; } NSModule mod = NSLinkModule(ofi, "test.bundle", NSLINKMODULE_OPTION_NONE); if ( mod == NULL ) { FAIL("NSLinkModule failed"); return 0; } NSSymbol sym = NSLookupSymbolInModule(mod, "_setValue"); if ( sym == NULL ) { FAIL("NSLookupSymbolInModule failed"); return 0; } setter func = NSAddressOfSymbol(sym); (*func)(1); //fprintf(stderr, "address of foo() = %p in bundle first load %p\n", func, mod); NSModule mod2 = NSLinkModule(ofi, "test2.bundle", NSLINKMODULE_OPTION_NONE); if ( mod2 == NULL ) { NSLinkEditErrors c; int errorNumber; const char* fileName; const char* errorString; NSLinkEditError(&c, &errorNumber, &fileName, &errorString); FAIL("2nd NSLinkModule failed: %s", errorString); return 0; } if ( mod == mod2 ) { FAIL("2nd NSLinkModule return same function address as first"); return 0; } NSSymbol sym2getter = NSLookupSymbolInModule(mod2, "_getValue"); if ( sym2getter == NULL ) { FAIL("2nd NSLookupSymbolInModule failed"); return 0; } getter func2getter = NSAddressOfSymbol(sym2getter); if ( (*func2getter)() != 0 ) { FAIL("_getValue() on second link returned non-zero"); return 0; } NSSymbol sym2 = NSLookupSymbolInModule(mod2, "_setValue"); if ( sym2 == NULL ) { FAIL("2nd NSLookupSymbolInModule failed"); return 0; } setter func2 = NSAddressOfSymbol(sym2); (*func2)(2); //fprintf(stderr, "address of foo() = %p in bundle second load %p\n", func2, mod2); if ( func == func2 ) { FAIL("2nd NSAddressOfSymbol return same function address as 1st"); return 0; } NSModule mod3 = NSLinkModule(ofi, "test3.bundle", NSLINKMODULE_OPTION_NONE); if ( mod3 == NULL ) { FAIL("3rd NSLinkModule failed"); return 0; } if ( mod3 == mod ) { FAIL("3rd NSLinkModule return same function address as 1st"); return 0; } if ( mod3 == mod2 ) { FAIL("3rd NSLinkModule return same function address as 2nd"); return 0; } NSSymbol sym3 = NSLookupSymbolInModule(mod3, "_setValue"); if ( sym3 == NULL ) { FAIL("3rd NSLookupSymbolInModule failed"); return 0; } setter func3 = NSAddressOfSymbol(sym3); (*func3)(3); //fprintf(stderr, "address of foo() = %p in bundle third load %p\n", func3, mod3); if ( func3 == func ) { FAIL("3rd NSAddressOfSymbol return same function address as 1st"); return 0; } if ( func3 == func2 ) { FAIL("3rd NSAddressOfSymbol return same function address as 2nd"); return 0; } if ( !NSUnLinkModule(mod, NSUNLINKMODULE_OPTION_NONE) ) { FAIL("NSUnLinkModule failed"); return 0; } if ( !NSUnLinkModule(mod3, NSUNLINKMODULE_OPTION_NONE) ) { FAIL("3rd NSUnLinkModule failed"); return 0; } if ( !NSUnLinkModule(mod2, NSUNLINKMODULE_OPTION_NONE) ) { FAIL("2nd NSUnLinkModule failed"); return 0; } // now link again after unlinking everything NSModule mod4 = NSLinkModule(ofi, "test4.bundle", NSLINKMODULE_OPTION_NONE); if ( mod4 == NULL ) { FAIL("4th NSLinkModule failed"); return 0; } // check that this is really a new copy by verifying the getValue() returns zero NSSymbol sym4getter = NSLookupSymbolInModule(mod4, "_getValue"); if ( sym4getter == NULL ) { FAIL("4th NSLookupSymbolInModule failed"); return 0; } getter func4getter = NSAddressOfSymbol(sym4getter); if ( (*func4getter)() != 0 ) { FAIL("_getValue() on fourth link returned non-zero"); return 0; } if ( !NSUnLinkModule(mod4, NSUNLINKMODULE_OPTION_NONE) ) { FAIL("4th NSUnLinkModule failed"); return 0; } if ( !NSDestroyObjectFileImage(ofi) ) { FAIL("NSDestroyObjectFileImage failed"); return 0; } PASS("bundle-multi-link"); return 0; } |