]>
git.saurik.com Git - apple/dyld.git/blob - unit-tests/test-cases/dlclose-dylib-unload/main.c
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@
23 #include <stdio.h> // fprintf(), NULL
24 #include <stdlib.h> // exit(), EXIT_SUCCESS
27 #include "test.h" // PASS(), FAIL(), XPASS(), XFAIL()
35 // open same dylib three times
36 void* handle1
= dlopen("libfoo.dylib", RTLD_LAZY
);
37 if ( handle1
== NULL
) {
38 FAIL("dlclose-dylib-unload: dlopen(\"libfoo.dylib\", RTLD_LAZY) failed with dlerror()=%s", dlerror());
42 void* handle2
= dlopen("libfoo.dylib", RTLD_LAZY
);
43 if ( handle2
== NULL
) {
44 FAIL("dlclose-dylib-unload: dlopen(\"libfoo.dylib\", RTLD_LAZY) failed with dlerror()=%s", dlerror());
48 void* handle3
= dlopen("libfoo.dylib", RTLD_LAZY
);
49 if ( handle3
== NULL
) {
50 FAIL("dlclose-dylib-unload: dlopen(\"libfoo.dylib\", RTLD_LAZY) failed with dlerror()=%s", dlerror());
55 void* sym
= dlsym(handle1
, "foo");
57 FAIL("dlclose-dylib-unload: dlsym(handle1, \"foo\") failed");
61 // close same bundle three times
62 if ( dlclose(handle3
) != 0 ) {
63 FAIL("dlclose-dylib-unload: dlclose(handle3) != 0, dlerrr()=%s", dlerror());
67 if ( dlclose(handle2
) != 0 ) {
68 FAIL("dlclose-dylib-unload: dlclose(handle2) != 0, dlerrr()=%s", dlerror());
72 if ( dlclose(handle1
) != 0 ) {
73 FAIL("dlclose-dylib-unload: dlclose(handle1) != 0, dlerrr()=%s", dlerror());
77 // symbol foo should no longer be accessible via dladdr()
79 if ( dladdr(sym
, &info
) != 0 ) {
80 FAIL("dlclose-dylib-unload: dladdr(foo_sym) != 0, but should have failed");
84 // extra close should fail
85 if ( dlclose(handle1
) == 0 ) {
86 FAIL("dlclose-dylib-unload: dlclose(foo_handle4) == 0, but should have failed");
96 // open same dylib three times
97 void* handle1
= dlopen("libbar.dylib", RTLD_LAZY
);
98 if ( handle1
== NULL
) {
99 FAIL("dlclose-dylib-unload: dlopen(\"libbar.dylib\", RTLD_LAZY) failed with dlerror()=%s", dlerror());
103 void* handle2
= dlopen("libbar.dylib", RTLD_LAZY
);
104 if ( handle2
== NULL
) {
105 FAIL("dlclose-dylib-unload: dlopen(\"libbar.dylib\", RTLD_LAZY) failed with dlerror()=%s", dlerror());
109 void* handle3
= dlopen("libbar.dylib", RTLD_LAZY
);
110 if ( handle3
== NULL
) {
111 FAIL("dlclose-dylib-unload: dlopen(\"libbar.dylib\", RTLD_LAZY) failed with dlerror()=%s", dlerror());
116 void* sym
= dlsym(handle1
, "bar");
118 FAIL("dlclose-dylib-unload: dlsym(handle1, \"bar\") failed");
122 // close same bundle three times
123 if ( dlclose(handle3
) != 0 ) {
124 FAIL("dlclose-dylib-unload: dlclose(handle3) != 0, dlerrr()=%s", dlerror());
128 if ( dlclose(handle2
) != 0 ) {
129 FAIL("dlclose-dylib-unload: dlclose(handle2) != 0, dlerrr()=%s", dlerror());
133 if ( dlclose(handle1
) != 0 ) {
134 FAIL("dlclose-dylib-unload: dlclose(handle1) != 0, dlerrr()=%s", dlerror());
138 // symbol bar should still longer be accessible via dladdr() because of external reference to libbar.dylib
140 if ( dladdr(sym
, &info
) == 0 ) {
141 FAIL("dlclose-dylib-unload: dladdr(bar_sym) == 0, but should have succeeded");
145 // extra close should fail
146 if ( dlclose(handle1
) == 0 ) {
147 FAIL("dlclose-dylib-unload: dlclose(bar_handle4) == 0, but should have failed");
153 // verify libbar.dylib can be loaded and unloaded
154 // verify libbar.dylib can be loaded, but cannot be unloaded (because main executable links against it)
160 PASS("dlclose-dylib-unload");