#include <stdio.h> // fprintf(), NULL
#include <stdlib.h> // exit(), EXIT_SUCCESS
#include <dlfcn.h>
#include "test.h" // PASS(), FAIL(), XPASS(), XFAIL()
void verifyfoo()
{
void* handle1 = dlopen("libfoo.dylib", RTLD_LAZY);
if ( handle1 == NULL ) {
FAIL("dlclose-dylib-unload: dlopen(\"libfoo.dylib\", RTLD_LAZY) failed with dlerror()=%s", dlerror());
exit(0);
}
void* handle2 = dlopen("libfoo.dylib", RTLD_LAZY);
if ( handle2 == NULL ) {
FAIL("dlclose-dylib-unload: dlopen(\"libfoo.dylib\", RTLD_LAZY) failed with dlerror()=%s", dlerror());
exit(0);
}
void* handle3 = dlopen("libfoo.dylib", RTLD_LAZY);
if ( handle3 == NULL ) {
FAIL("dlclose-dylib-unload: dlopen(\"libfoo.dylib\", RTLD_LAZY) failed with dlerror()=%s", dlerror());
exit(0);
}
void* sym = dlsym(handle1, "foo");
if ( sym == NULL ) {
FAIL("dlclose-dylib-unload: dlsym(handle1, \"foo\") failed");
exit(0);
}
if ( dlclose(handle3) != 0 ) {
FAIL("dlclose-dylib-unload: dlclose(handle3) != 0, dlerrr()=%s", dlerror());
exit(0);
}
if ( dlclose(handle2) != 0 ) {
FAIL("dlclose-dylib-unload: dlclose(handle2) != 0, dlerrr()=%s", dlerror());
exit(0);
}
if ( dlclose(handle1) != 0 ) {
FAIL("dlclose-dylib-unload: dlclose(handle1) != 0, dlerrr()=%s", dlerror());
exit(0);
}
Dl_info info;
if ( dladdr(sym, &info) != 0 ) {
FAIL("dlclose-dylib-unload: dladdr(foo_sym) != 0, but should have failed");
}
if ( dlclose(handle1) == 0 ) {
FAIL("dlclose-dylib-unload: dlclose(foo_handle4) == 0, but should have failed");
}
}
void verifybar()
{
void* handle1 = dlopen("libbar.dylib", RTLD_LAZY);
if ( handle1 == NULL ) {
FAIL("dlclose-dylib-unload: dlopen(\"libbar.dylib\", RTLD_LAZY) failed with dlerror()=%s", dlerror());
exit(0);
}
void* handle2 = dlopen("libbar.dylib", RTLD_LAZY);
if ( handle2 == NULL ) {
FAIL("dlclose-dylib-unload: dlopen(\"libbar.dylib\", RTLD_LAZY) failed with dlerror()=%s", dlerror());
exit(0);
}
void* handle3 = dlopen("libbar.dylib", RTLD_LAZY);
if ( handle3 == NULL ) {
FAIL("dlclose-dylib-unload: dlopen(\"libbar.dylib\", RTLD_LAZY) failed with dlerror()=%s", dlerror());
exit(0);
}
void* sym = dlsym(handle1, "bar");
if ( sym == NULL ) {
FAIL("dlclose-dylib-unload: dlsym(handle1, \"bar\") failed");
exit(0);
}
if ( dlclose(handle3) != 0 ) {
FAIL("dlclose-dylib-unload: dlclose(handle3) != 0, dlerrr()=%s", dlerror());
exit(0);
}
if ( dlclose(handle2) != 0 ) {
FAIL("dlclose-dylib-unload: dlclose(handle2) != 0, dlerrr()=%s", dlerror());
exit(0);
}
if ( dlclose(handle1) != 0 ) {
FAIL("dlclose-dylib-unload: dlclose(handle1) != 0, dlerrr()=%s", dlerror());
exit(0);
}
Dl_info info;
if ( dladdr(sym, &info) == 0 ) {
FAIL("dlclose-dylib-unload: dladdr(bar_sym) == 0, but should have succeeded");
exit(0);
}
if ( dlclose(handle1) == 0 ) {
FAIL("dlclose-dylib-unload: dlclose(bar_handle4) == 0, but should have failed");
exit(0);
}
}
int main()
{
verifyfoo();
verifybar();
PASS("dlclose-dylib-unload");
return EXIT_SUCCESS;
}