1/* 2 * Copyright (c) 2012 Apple 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> // fprintf(), NULL 24#include <stdlib.h> // exit(), EXIT_SUCCESS 25#include <dlfcn.h> 26 27#include "test.h" // PASS(), FAIL(), XPASS(), XFAIL() 28 29 30 31 32int main() 33{ 34 Dl_info info; 35 36 // load bar 37 void* handleBar = dlopen("libbar.dylib", RTLD_LAZY); 38 if ( handleBar == NULL ) { 39 FAIL("dlclose-dylib-ref-count: dlopen(\"libbar.dylib\", RTLD_LAZY) failed with dlerror()=%s", dlerror()); 40 exit(0); 41 } 42 43 // load foo 44 void* handleFoo = dlopen("libfoo.dylib", RTLD_LAZY); 45 if ( handleFoo == NULL ) { 46 FAIL("dlclose-dylib-ref-count: dlopen(\"libfoo.dylib\", RTLD_LAZY) failed with dlerror()=%s", dlerror()); 47 exit(0); 48 } 49 50 void* sym_base = dlsym(handleBar, "base"); 51 if ( sym_base == NULL ) { 52 FAIL("dlclose-dylib-ref-count: dlsym(handleBar, \"base\") failed"); 53 exit(0); 54 } 55 56 void* sym_foo = dlsym(handleFoo, "foo"); 57 if ( sym_foo == NULL ) { 58 FAIL("dlclose-dylib-ref-count: dlsym(handleBar, \"base\") failed"); 59 exit(0); 60 } 61 62 void* sym_bar = dlsym(handleBar, "bar"); 63 if ( sym_bar == NULL ) { 64 FAIL("dlclose-dylib-ref-count: dlsym(handleBar, \"base\") failed"); 65 exit(0); 66 } 67 68 if ( dlclose(handleBar) != 0 ) { 69 FAIL("dlclose-dylib-ref-count: dlclose(handleBar) != 0, dlerrr()=%s", dlerror()); 70 exit(0); 71 } 72 73 // sym_base should still be accessible via dladdr() because of external reference from libfoo.dylib 74 if ( dladdr(sym_base, &info) == 0 ) { 75 FAIL("dlclose-dylib-ref-count: dladdr(sym_base) == 0, but should have succeeded"); 76 exit(0); 77 } 78 79 // sym_foo should still be accessible via dladdr() because libfoo was dlopen'ed 80 if ( dladdr(sym_foo, &info) == 0 ) { 81 FAIL("dlclose-dylib-ref-count: dladdr(sym_foo) == 0, but should have succeeded"); 82 exit(0); 83 } 84 85 // sym_bar should not be accessible via dladdr() because libbar was dlclose'ed 86 if ( dladdr(sym_bar, &info) != 0 ) { 87 FAIL("dlclose-dylib-ref-count: dladdr(sym_bar) != 0, but should have failed"); 88 exit(0); 89 } 90 91 if ( dlclose(handleFoo) != 0 ) { 92 FAIL("dlclose-dylib-ref-count: dlclose(handleBar) != 0, dlerrr()=%s", dlerror()); 93 exit(0); 94 } 95 96 // sym_base should no longer be accessible via dladdr() because libfoo and libbar both closed 97 if ( dladdr(sym_base, &info) != 0 ) { 98 FAIL("dlclose-dylib-ref-count: dladdr(base) != 0, but should have failed"); 99 exit(0); 100 } 101 102 // sym_foo should still be accessible via dladdr() because libfoo was dlclose'ed 103 if ( dladdr(sym_foo, &info) != 0 ) { 104 FAIL("dlclose-dylib-ref-count: dladdr(sym_foo) != 0, but should have failed"); 105 exit(0); 106 } 107 108 PASS("dlclose-dylib-ref-count"); 109 return EXIT_SUCCESS; 110} 111