]>
git.saurik.com Git - apple/dyld.git/blob - unit-tests/test-cases/dlclose-dylib-dynamic-ref/main.c
2 * Copyright (c) 2013 Apple 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@
23 #include <stdio.h> // fprintf(), NULL
24 #include <stdlib.h> // exit(), EXIT_SUCCESS
27 #include "test.h" // PASS(), FAIL(), XPASS(), XFAIL()
37 void* handleFoo
= dlopen("libfoo.dylib", RTLD_LAZY
);
38 if ( handleFoo
== NULL
) {
39 FAIL("dlclose-dylib-dynamic-ref: dlopen(\"libfoo.dylib\", RTLD_LAZY) failed with dlerror()=%s", dlerror());
44 void* handleBar
= dlopen("libbar.dylib", RTLD_LAZY
);
45 if ( handleBar
== NULL
) {
46 FAIL("dlclose-dylib-dynamic-ref: dlopen(\"libbar.dylib\", RTLD_LAZY) failed with dlerror()=%s", dlerror());
51 void* handleBaz
= dlopen("libbaz.dylib", RTLD_LAZY
);
52 if ( handleBaz
== NULL
) {
53 FAIL("dlclose-dylib-dynamic-ref: dlopen(\"libbaz.dylib\", RTLD_LAZY) failed with dlerror()=%s", dlerror());
58 void* sym_foo
= dlsym(handleFoo
, "foo");
59 if ( sym_foo
== NULL
) {
60 FAIL("dlclose-dylib-dynamic-ref: dlsym(handleFoo, \"foo\") failed");
64 void* sym_bar
= dlsym(handleBar
, "bar");
65 if ( sym_bar
== NULL
) {
66 FAIL("dlclose-dylib-dynamic-ref: dlsym(handleBar, \"bar\") failed");
70 void* sym_baz
= dlsym(handleBaz
, "baz");
71 if ( sym_baz
== NULL
) {
72 FAIL("dlclose-dylib-dynamic-ref: dlsym(handleBaz, \"baz\") failed");
76 // since foo loaded first, bar and baz should have a dynamic reference to foo because all
77 // the weak mydata symbols were coaleseced to the one in foo.
80 if ( dlclose(handleFoo
) != 0 ) {
81 FAIL("dlclose-dylib-dynamic-ref: dlclose(handleBar) != 0, dlerrr()=%s", dlerror());
85 // sym_foo should still be accessible via dladdr() because libbar and libbaz
86 // have dynamic references to libfoo.
87 if ( dladdr(sym_foo
, &info
) == 0 ) {
88 FAIL("dlclose-dylib-dynamic-ref: dladdr(sym_base) == 0, but should have succeeded");
93 if ( dlclose(handleBar
) != 0 ) {
94 FAIL("dlclose-dylib-dynamic-ref: dlclose(handleBar) != 0, dlerrr()=%s", dlerror());
99 // sym_bar should not be accessible via dladdr() because libbar was dlclose'ed
100 if ( dladdr(sym_bar
, &info
) != 0 ) {
101 FAIL("dlclose-dylib-dynamic-ref: dladdr(sym_bar) != 0, but should have failed");
105 // sym_foo should still be accessible via dladdr() because of external libbaz
106 // has a dynamic references to libfoo.
107 if ( dladdr(sym_foo
, &info
) == 0 ) {
108 FAIL("dlclose-dylib-dynamic-ref: dladdr(sym_base) == 0, but should have succeeded");
113 if ( dlclose(handleBaz
) != 0 ) {
114 FAIL("dlclose-dylib-dynamic-ref: dlclose(handleBar) != 0, dlerrr()=%s", dlerror());
118 // sym_baz should not be accessible via dladdr() because libbar was dlclose'ed
119 if ( dladdr(sym_baz
, &info
) != 0 ) {
120 FAIL("dlclose-dylib-dynamic-ref: dladdr(sym_baz) != 0, but should have failed");
125 // sym_foo should finally be inaccessible via dladdr() because all dynamic references to libfoo are gone
126 if ( dladdr(sym_foo
, &info
) != 0 ) {
127 FAIL("dlclose-dylib-dynamic-ref: dladdr(sym_foo) == 0, but should have succeeded");
132 PASS("dlclose-dylib-dynamic-ref");